Home | History | Annotate | Download | only in shill
      1 //
      2 // Copyright (C) 2012 The Android Open Source Project
      3 //
      4 // Licensed under the Apache License, Version 2.0 (the "License");
      5 // you may not use this file except in compliance with the License.
      6 // You may obtain a copy of the License at
      7 //
      8 //      http://www.apache.org/licenses/LICENSE-2.0
      9 //
     10 // Unless required by applicable law or agreed to in writing, software
     11 // distributed under the License is distributed on an "AS IS" BASIS,
     12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13 // See the License for the specific language governing permissions and
     14 // limitations under the License.
     15 //
     16 
     17 #include "shill/mock_log.h"
     18 
     19 #include <gmock/gmock.h>
     20 #include <gtest/gtest.h>
     21 
     22 #include "shill/logging.h"
     23 
     24 
     25 using ::std::string;
     26 using ::testing::_;
     27 
     28 namespace shill {
     29 
     30 namespace Logging {
     31 static auto kModuleLogScope = ScopeLogger::kManager;
     32 static string ObjectID(testing::Test* m) { return "(mock_log_test)"; }
     33 }
     34 
     35 class MockLogTest : public testing::Test {
     36  protected:
     37   MockLogTest() {}
     38 
     39   void LogSomething(const string& message) const {
     40     LOG(INFO) << message;
     41   }
     42   void SlogSomething(testing::Test* t, const string& message) const {
     43     ScopeLogger::GetInstance()->EnableScopesByName("manager");
     44     ScopeLogger::GetInstance()->set_verbose_level(2);
     45     SLOG(t, 2) << message;
     46     ScopeLogger::GetInstance()->EnableScopesByName("-manager");
     47     ScopeLogger::GetInstance()->set_verbose_level(0);
     48   }
     49 };
     50 
     51 TEST_F(MockLogTest, MatchMessageOnly) {
     52   ScopedMockLog log;
     53   const string kMessage("Something");
     54   EXPECT_CALL(log, Log(_, _, kMessage));
     55   LogSomething(kMessage);
     56 }
     57 
     58 TEST_F(MockLogTest, MatchSeverityAndMessage) {
     59   ScopedMockLog log;
     60   const string kMessage("Something");
     61   EXPECT_CALL(log, Log(logging::LOG_INFO, _, kMessage));
     62   LogSomething(kMessage);
     63 }
     64 
     65 TEST_F(MockLogTest, MatchSeverityAndFileAndMessage) {
     66   ScopedMockLog log;
     67   const string kMessage("Something");
     68   EXPECT_CALL(log, Log(logging::LOG_INFO,
     69               ::testing::EndsWith("mock_log_unittest.cc"), kMessage));
     70   LogSomething(kMessage);
     71 }
     72 
     73 TEST_F(MockLogTest, MatchEmptyString) {
     74   ScopedMockLog log;
     75   const string kMessage("");
     76   EXPECT_CALL(log, Log(_, _, kMessage));
     77   LogSomething(kMessage);
     78 }
     79 
     80 TEST_F(MockLogTest, MatchMessageContainsBracketAndNewline) {
     81   ScopedMockLog log;
     82   const string kMessage("blah [and more blah] \n yet more blah\n\n\n");
     83   EXPECT_CALL(log, Log(_, _, kMessage));
     84   LogSomething(kMessage);
     85 }
     86 
     87 TEST_F(MockLogTest, MatchSlog) {
     88   ScopedMockLog log;
     89   const string kMessage("Something");
     90   const string kLogMessage("(anon) Something");
     91   EXPECT_CALL(log, Log(_, _, kLogMessage));
     92   SlogSomething(nullptr, kMessage);
     93 }
     94 
     95 TEST_F(MockLogTest, MatchSlogWithObject) {
     96   ScopedMockLog log;
     97   const string kMessage("Something");
     98   const string kLogMessage("(mock_log_test) Something");
     99   EXPECT_CALL(log, Log(_, _, kLogMessage));
    100   SlogSomething(this, kMessage);
    101 }
    102 
    103 TEST_F(MockLogTest, MatchWithGmockMatchers) {
    104   ScopedMockLog log;
    105   const string kMessage("Something");
    106   EXPECT_CALL(log, Log(::testing::Lt(::logging::LOG_ERROR),
    107                        ::testing::EndsWith(".cc"),
    108                        ::testing::StartsWith("Some")));
    109   LogSomething(kMessage);
    110 }
    111 
    112 }  // namespace shill
    113