blob: 2ea9e6fed9a3ae227678fd666439f85955f63bb1 [file] [log] [blame]
Gary Morain8a5726a2012-05-15 10:56:49 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "shill/mock_log.h"
6
7#include <gmock/gmock.h>
8#include <gtest/gtest.h>
9
Christopher Wileyb691efd2012-08-09 13:51:51 -070010#include "shill/logging.h"
Gary Morain8a5726a2012-05-15 10:56:49 -070011
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070012
Gary Morain8a5726a2012-05-15 10:56:49 -070013using ::std::string;
14using ::testing::_;
15
16namespace shill {
17
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070018namespace Logging {
19static auto kModuleLogScope = ScopeLogger::kManager;
Paul Stewart1e006c62015-06-16 12:29:06 -070020static string ObjectID(testing::Test* m) { return "(mock_log_test)"; }
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070021}
22
Gary Morain8a5726a2012-05-15 10:56:49 -070023class MockLogTest : public testing::Test {
24 protected:
25 MockLogTest() {}
26
Paul Stewart1e006c62015-06-16 12:29:06 -070027 void LogSomething(const string& message) const {
Gary Morain8a5726a2012-05-15 10:56:49 -070028 LOG(INFO) << message;
29 }
Paul Stewart1e006c62015-06-16 12:29:06 -070030 void SlogSomething(testing::Test* t, const string& message) const {
Gary Morain8a5726a2012-05-15 10:56:49 -070031 ScopeLogger::GetInstance()->EnableScopesByName("manager");
Christopher Wileyd34a47c2012-08-01 15:42:45 -070032 ScopeLogger::GetInstance()->set_verbose_level(2);
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070033 SLOG(t, 2) << message;
Christopher Wileyd34a47c2012-08-01 15:42:45 -070034 ScopeLogger::GetInstance()->EnableScopesByName("-manager");
35 ScopeLogger::GetInstance()->set_verbose_level(0);
Gary Morain8a5726a2012-05-15 10:56:49 -070036 }
37};
38
39TEST_F(MockLogTest, MatchMessageOnly) {
40 ScopedMockLog log;
41 const string kMessage("Something");
42 EXPECT_CALL(log, Log(_, _, kMessage));
43 LogSomething(kMessage);
44}
45
46TEST_F(MockLogTest, MatchSeverityAndMessage) {
47 ScopedMockLog log;
48 const string kMessage("Something");
49 EXPECT_CALL(log, Log(logging::LOG_INFO, _, kMessage));
50 LogSomething(kMessage);
51}
52
53TEST_F(MockLogTest, MatchSeverityAndFileAndMessage) {
54 ScopedMockLog log;
55 const string kMessage("Something");
Liam McLoughlinef342b42013-09-13 21:05:36 +010056 EXPECT_CALL(log, Log(logging::LOG_INFO,
57 ::testing::EndsWith("mock_log_unittest.cc"), kMessage));
Gary Morain8a5726a2012-05-15 10:56:49 -070058 LogSomething(kMessage);
59}
60
61TEST_F(MockLogTest, MatchEmptyString) {
62 ScopedMockLog log;
63 const string kMessage("");
64 EXPECT_CALL(log, Log(_, _, kMessage));
65 LogSomething(kMessage);
66}
67
68TEST_F(MockLogTest, MatchMessageContainsBracketAndNewline) {
69 ScopedMockLog log;
70 const string kMessage("blah [and more blah] \n yet more blah\n\n\n");
71 EXPECT_CALL(log, Log(_, _, kMessage));
72 LogSomething(kMessage);
73}
74
75TEST_F(MockLogTest, MatchSlog) {
76 ScopedMockLog log;
77 const string kMessage("Something");
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070078 const string kLogMessage("(anon) Something");
79 EXPECT_CALL(log, Log(_, _, kLogMessage));
80 SlogSomething(nullptr, kMessage);
81}
82
83TEST_F(MockLogTest, MatchSlogWithObject) {
84 ScopedMockLog log;
85 const string kMessage("Something");
86 const string kLogMessage("(mock_log_test) Something");
87 EXPECT_CALL(log, Log(_, _, kLogMessage));
88 SlogSomething(this, kMessage);
Gary Morain8a5726a2012-05-15 10:56:49 -070089}
90
91TEST_F(MockLogTest, MatchWithGmockMatchers) {
92 ScopedMockLog log;
93 const string kMessage("Something");
94 EXPECT_CALL(log, Log(::testing::Lt(::logging::LOG_ERROR),
95 ::testing::EndsWith(".cc"),
96 ::testing::StartsWith("Some")));
97 LogSomething(kMessage);
98}
99
100} // namespace shill