blob: 87bcaef020468c427bdc97e083f0e9a30097e173 [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
12using ::std::string;
13using ::testing::_;
14
15namespace shill {
16
17class MockLogTest : public testing::Test {
18 protected:
19 MockLogTest() {}
20
21 void LogSomething(const string &message) const {
22 LOG(INFO) << message;
23 }
24 void SlogSomething(const string &message) const {
25 ScopeLogger::GetInstance()->EnableScopesByName("manager");
Christopher Wileyd34a47c2012-08-01 15:42:45 -070026 ScopeLogger::GetInstance()->set_verbose_level(2);
Gary Morain8a5726a2012-05-15 10:56:49 -070027 SLOG(Manager, 2) << message;
Christopher Wileyd34a47c2012-08-01 15:42:45 -070028 ScopeLogger::GetInstance()->EnableScopesByName("-manager");
29 ScopeLogger::GetInstance()->set_verbose_level(0);
Gary Morain8a5726a2012-05-15 10:56:49 -070030 }
31};
32
33TEST_F(MockLogTest, MatchMessageOnly) {
34 ScopedMockLog log;
35 const string kMessage("Something");
36 EXPECT_CALL(log, Log(_, _, kMessage));
37 LogSomething(kMessage);
38}
39
40TEST_F(MockLogTest, MatchSeverityAndMessage) {
41 ScopedMockLog log;
42 const string kMessage("Something");
43 EXPECT_CALL(log, Log(logging::LOG_INFO, _, kMessage));
44 LogSomething(kMessage);
45}
46
47TEST_F(MockLogTest, MatchSeverityAndFileAndMessage) {
48 ScopedMockLog log;
49 const string kMessage("Something");
Liam McLoughlinef342b42013-09-13 21:05:36 +010050 EXPECT_CALL(log, Log(logging::LOG_INFO,
51 ::testing::EndsWith("mock_log_unittest.cc"), kMessage));
Gary Morain8a5726a2012-05-15 10:56:49 -070052 LogSomething(kMessage);
53}
54
55TEST_F(MockLogTest, MatchEmptyString) {
56 ScopedMockLog log;
57 const string kMessage("");
58 EXPECT_CALL(log, Log(_, _, kMessage));
59 LogSomething(kMessage);
60}
61
62TEST_F(MockLogTest, MatchMessageContainsBracketAndNewline) {
63 ScopedMockLog log;
64 const string kMessage("blah [and more blah] \n yet more blah\n\n\n");
65 EXPECT_CALL(log, Log(_, _, kMessage));
66 LogSomething(kMessage);
67}
68
69TEST_F(MockLogTest, MatchSlog) {
70 ScopedMockLog log;
71 const string kMessage("Something");
Christopher Wileyd7783522012-08-10 14:21:47 -070072 EXPECT_CALL(log, Log(_, _, kMessage));
Gary Morain8a5726a2012-05-15 10:56:49 -070073 SlogSomething(kMessage);
74}
75
76TEST_F(MockLogTest, MatchWithGmockMatchers) {
77 ScopedMockLog log;
78 const string kMessage("Something");
79 EXPECT_CALL(log, Log(::testing::Lt(::logging::LOG_ERROR),
80 ::testing::EndsWith(".cc"),
81 ::testing::StartsWith("Some")));
82 LogSomething(kMessage);
83}
84
85} // namespace shill