blob: 4a6693df4395a6c3d5dad9517c70e3a655ea7e15 [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");
50 EXPECT_CALL(log, Log(logging::LOG_INFO, "mock_log_unittest.cc", kMessage));
51 LogSomething(kMessage);
52}
53
54TEST_F(MockLogTest, MatchEmptyString) {
55 ScopedMockLog log;
56 const string kMessage("");
57 EXPECT_CALL(log, Log(_, _, kMessage));
58 LogSomething(kMessage);
59}
60
61TEST_F(MockLogTest, MatchMessageContainsBracketAndNewline) {
62 ScopedMockLog log;
63 const string kMessage("blah [and more blah] \n yet more blah\n\n\n");
64 EXPECT_CALL(log, Log(_, _, kMessage));
65 LogSomething(kMessage);
66}
67
68TEST_F(MockLogTest, MatchSlog) {
69 ScopedMockLog log;
70 const string kMessage("Something");
Christopher Wileyb691efd2012-08-09 13:51:51 -070071 const string kMessageWithPrefix("memlog: Something");
72 EXPECT_CALL(log, Log(_, _, kMessageWithPrefix));
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