blob: e3a6d07a2fbe509b8a494c0309fbd39e445eccd2 [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
10#include "shill/scope_logger.h"
11
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");
26 SLOG(Manager, 2) << message;
27 }
28};
29
30TEST_F(MockLogTest, MatchMessageOnly) {
31 ScopedMockLog log;
32 const string kMessage("Something");
33 EXPECT_CALL(log, Log(_, _, kMessage));
34 LogSomething(kMessage);
35}
36
37TEST_F(MockLogTest, MatchSeverityAndMessage) {
38 ScopedMockLog log;
39 const string kMessage("Something");
40 EXPECT_CALL(log, Log(logging::LOG_INFO, _, kMessage));
41 LogSomething(kMessage);
42}
43
44TEST_F(MockLogTest, MatchSeverityAndFileAndMessage) {
45 ScopedMockLog log;
46 const string kMessage("Something");
47 EXPECT_CALL(log, Log(logging::LOG_INFO, "mock_log_unittest.cc", kMessage));
48 LogSomething(kMessage);
49}
50
51TEST_F(MockLogTest, MatchEmptyString) {
52 ScopedMockLog log;
53 const string kMessage("");
54 EXPECT_CALL(log, Log(_, _, kMessage));
55 LogSomething(kMessage);
56}
57
58TEST_F(MockLogTest, MatchMessageContainsBracketAndNewline) {
59 ScopedMockLog log;
60 const string kMessage("blah [and more blah] \n yet more blah\n\n\n");
61 EXPECT_CALL(log, Log(_, _, kMessage));
62 LogSomething(kMessage);
63}
64
65TEST_F(MockLogTest, MatchSlog) {
66 ScopedMockLog log;
67 const string kMessage("Something");
68 EXPECT_CALL(log, Log(_, _, kMessage));
69 SlogSomething(kMessage);
70}
71
72TEST_F(MockLogTest, MatchWithGmockMatchers) {
73 ScopedMockLog log;
74 const string kMessage("Something");
75 EXPECT_CALL(log, Log(::testing::Lt(::logging::LOG_ERROR),
76 ::testing::EndsWith(".cc"),
77 ::testing::StartsWith("Some")));
78 LogSomething(kMessage);
79}
80
81} // namespace shill