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 | d778352 | 2012-08-10 14:21:47 -0700 | [diff] [blame] | 71 | EXPECT_CALL(log, Log(_, _, kMessage)); |
Gary Morain | 8a5726a | 2012-05-15 10:56:49 -0700 | [diff] [blame] | 72 | SlogSomething(kMessage); |
| 73 | } |
| 74 | |
| 75 | TEST_F(MockLogTest, MatchWithGmockMatchers) { |
| 76 | ScopedMockLog log; |
| 77 | const string kMessage("Something"); |
| 78 | EXPECT_CALL(log, Log(::testing::Lt(::logging::LOG_ERROR), |
| 79 | ::testing::EndsWith(".cc"), |
| 80 | ::testing::StartsWith("Some"))); |
| 81 | LogSomething(kMessage); |
| 82 | } |
| 83 | |
| 84 | } // namespace shill |