Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 1 | // 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 Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 10 | #include "shill/logging.h" |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 11 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 12 | |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 13 | using ::std::string; |
| 14 | using ::testing::_; |
| 15 | |
| 16 | namespace shill { |
| 17 | |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 18 | namespace Logging { |
| 19 | static auto kModuleLogScope = ScopeLogger::kManager; |
| 20 | static string ObjectID(testing::Test *m) { return "(mock_log_test)"; } |
| 21 | } |
| 22 | |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 23 | class MockLogTest : public testing::Test { |
| 24 | protected: |
| 25 | MockLogTest() {} |
| 26 | |
| 27 | void LogSomething(const string &message) const { |
| 28 | LOG(INFO) << message; |
| 29 | } |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 30 | void SlogSomething(testing::Test *t, const string &message) const { |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 31 | ScopeLogger::GetInstance()->EnableScopesByName("manager"); |
Christopher Wiley | d34a47c | 2012-08-01 15:42:45 -0700 | [diff] [blame] | 32 | ScopeLogger::GetInstance()->set_verbose_level(2); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 33 | SLOG(t, 2) << message; |
Christopher Wiley | d34a47c | 2012-08-01 15:42:45 -0700 | [diff] [blame] | 34 | ScopeLogger::GetInstance()->EnableScopesByName("-manager"); |
| 35 | ScopeLogger::GetInstance()->set_verbose_level(0); |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 36 | } |
| 37 | }; |
| 38 | |
| 39 | TEST_F(MockLogTest, MatchMessageOnly) { |
| 40 | ScopedMockLog log; |
| 41 | const string kMessage("Something"); |
| 42 | EXPECT_CALL(log, Log(_, _, kMessage)); |
| 43 | LogSomething(kMessage); |
| 44 | } |
| 45 | |
| 46 | TEST_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 | |
| 53 | TEST_F(MockLogTest, MatchSeverityAndFileAndMessage) { |
| 54 | ScopedMockLog log; |
| 55 | const string kMessage("Something"); |
Liam McLoughlin | ef342b4 | 2013-09-13 21:05:36 +0100 | [diff] [blame] | 56 | EXPECT_CALL(log, Log(logging::LOG_INFO, |
| 57 | ::testing::EndsWith("mock_log_unittest.cc"), kMessage)); |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 58 | LogSomething(kMessage); |
| 59 | } |
| 60 | |
| 61 | TEST_F(MockLogTest, MatchEmptyString) { |
| 62 | ScopedMockLog log; |
| 63 | const string kMessage(""); |
| 64 | EXPECT_CALL(log, Log(_, _, kMessage)); |
| 65 | LogSomething(kMessage); |
| 66 | } |
| 67 | |
| 68 | TEST_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 | |
| 75 | TEST_F(MockLogTest, MatchSlog) { |
| 76 | ScopedMockLog log; |
| 77 | const string kMessage("Something"); |
Rebecca Silberstein | c9c31d8 | 2014-10-21 15:01:00 -0700 | [diff] [blame] | 78 | const string kLogMessage("(anon) Something"); |
| 79 | EXPECT_CALL(log, Log(_, _, kLogMessage)); |
| 80 | SlogSomething(nullptr, kMessage); |
| 81 | } |
| 82 | |
| 83 | TEST_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 Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | TEST_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 |