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 | |
| 12 | using ::std::string; |
| 13 | using ::testing::_; |
| 14 | |
| 15 | namespace shill { |
| 16 | |
| 17 | class 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 Wiley | d34a47c | 2012-08-01 15:42:45 -0700 | [diff] [blame] | 26 | ScopeLogger::GetInstance()->set_verbose_level(2); |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 27 | SLOG(Manager, 2) << message; |
Christopher Wiley | d34a47c | 2012-08-01 15:42:45 -0700 | [diff] [blame] | 28 | ScopeLogger::GetInstance()->EnableScopesByName("-manager"); |
| 29 | ScopeLogger::GetInstance()->set_verbose_level(0); |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 30 | } |
| 31 | }; |
| 32 | |
| 33 | TEST_F(MockLogTest, MatchMessageOnly) { |
| 34 | ScopedMockLog log; |
| 35 | const string kMessage("Something"); |
| 36 | EXPECT_CALL(log, Log(_, _, kMessage)); |
| 37 | LogSomething(kMessage); |
| 38 | } |
| 39 | |
| 40 | TEST_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 | |
| 47 | TEST_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 | |
| 54 | TEST_F(MockLogTest, MatchEmptyString) { |
| 55 | ScopedMockLog log; |
| 56 | const string kMessage(""); |
| 57 | EXPECT_CALL(log, Log(_, _, kMessage)); |
| 58 | LogSomething(kMessage); |
| 59 | } |
| 60 | |
| 61 | TEST_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 | |
| 68 | TEST_F(MockLogTest, MatchSlog) { |
| 69 | ScopedMockLog log; |
| 70 | const string kMessage("Something"); |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 71 | const string kMessageWithPrefix("memlog: Something"); |
| 72 | EXPECT_CALL(log, Log(_, _, kMessageWithPrefix)); |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 73 | SlogSomething(kMessage); |
| 74 | } |
| 75 | |
| 76 | TEST_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 |