• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2011 The Libphonenumber Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Author: Philippe Liard
16 
17 #include <string>
18 
19 #include <gtest/gtest.h>
20 
21 #include "phonenumbers/base/memory/scoped_ptr.h"
22 #include "phonenumbers/default_logger.h"
23 #include "phonenumbers/logger.h"
24 
25 namespace i18n {
26 namespace phonenumbers {
27 
28 // String logger implementation used for testing. Messages are output to a
29 // string for convenience.
30 class StringLogger : public Logger {
31  public:
~StringLogger()32   virtual ~StringLogger() {}
33 
message() const34   const string& message() const {
35     return msg_;
36   }
37 
WriteMessage(const string & msg)38   virtual void WriteMessage(const string& msg) {
39     msg_ += msg;
40   }
41 
42  private:
43   string msg_;
44 };
45 
46 class LoggerTest : public ::testing::Test {
47  protected:
SetUp()48   virtual void SetUp() {
49     test_logger_.reset(new StringLogger());
50     test_logger_->set_level(LOG_INFO);
51     // Save the current logger implementation and restore it when the test is
52     // done to avoid side-effects in other tests (including phonenumberutil
53     // tests) as the logger implementation is global.
54     old_logger_ = Logger::mutable_logger_impl();
55     Logger::set_logger_impl(test_logger_.get());
56   }
57 
TearDown()58   virtual void TearDown() {
59     // Restore the previous logger implementation to avoid side-effects in other
60     // tests as mentioned above.
61     Logger::set_logger_impl(old_logger_);
62   }
63 
64   scoped_ptr<StringLogger> test_logger_;
65   Logger* old_logger_;
66 };
67 
TEST_F(LoggerTest,LoggerIgnoresHigherVerbosity)68 TEST_F(LoggerTest, LoggerIgnoresHigherVerbosity) {
69   // The logger verbosity is set to LOG_INFO, therefore LOG_DEBUG messages
70   // should be ignored.
71   LOG(LOG_DEBUG) << "Hello";
72   EXPECT_EQ("", test_logger_->message());
73 }
74 
TEST_F(LoggerTest,LoggerOutputsNewline)75 TEST_F(LoggerTest, LoggerOutputsNewline) {
76   LOG(LOG_INFO) << "Hello";
77   EXPECT_EQ("Hello\n", test_logger_->message());
78 }
79 
TEST_F(LoggerTest,LoggerLogsEqualVerbosity)80 TEST_F(LoggerTest, LoggerLogsEqualVerbosity) {
81   LOG(LOG_INFO) << "Hello";
82   EXPECT_EQ("Hello\n", test_logger_->message());
83 }
84 
TEST_F(LoggerTest,LoggerLogsMoreSeriousMessages)85 TEST_F(LoggerTest, LoggerLogsMoreSeriousMessages) {
86   // The logger verbosity is set to LOG_INFO, therefore LOG_WARNING messages
87   // should still be printed.
88   LOG(LOG_WARNING) << "Hello";
89   EXPECT_EQ("Hello\n", test_logger_->message());
90 }
91 
TEST_F(LoggerTest,LoggerConcatenatesMessages)92 TEST_F(LoggerTest, LoggerConcatenatesMessages) {
93   LOG(LOG_INFO) << "Hello";
94   ASSERT_EQ("Hello\n", test_logger_->message());
95 
96   LOG(LOG_INFO) << " World";
97   EXPECT_EQ("Hello\n World\n", test_logger_->message());
98 }
99 
TEST_F(LoggerTest,LoggerHandlesDifferentTypes)100 TEST_F(LoggerTest, LoggerHandlesDifferentTypes) {
101   LOG(LOG_INFO) << "Hello " << 42;
102   EXPECT_EQ("Hello 42\n", test_logger_->message());
103 }
104 
TEST_F(LoggerTest,LoggerIgnoresVerboseLogs)105 TEST_F(LoggerTest, LoggerIgnoresVerboseLogs) {
106   // VLOG is always lower verbosity than LOG, so with LOG_INFO set as the
107   // verbosity level, no VLOG call should result in anything.
108   VLOG(1) << "Hello";
109   EXPECT_EQ("", test_logger_->message());
110 
111   // VLOG(0) is the same as LOG_DEBUG.
112   VLOG(0) << "Hello";
113   EXPECT_EQ("", test_logger_->message());
114 
115   // With LOG_DEBUG as the current verbosity level, VLOG(1) should still not
116   // result in anything.
117   test_logger_->set_level(LOG_DEBUG);
118 
119   VLOG(1) << "Hello";
120   EXPECT_EQ("", test_logger_->message());
121 
122   // However, VLOG(0) does.
123   VLOG(0) << "Hello";
124   EXPECT_EQ("Hello\n", test_logger_->message());
125 }
126 
TEST_F(LoggerTest,LoggerShowsDebugLogsAtDebugLevel)127 TEST_F(LoggerTest, LoggerShowsDebugLogsAtDebugLevel) {
128   test_logger_->set_level(LOG_DEBUG);
129   // Debug logs should still be seen.
130   LOG(LOG_DEBUG) << "Debug hello";
131   EXPECT_EQ("Debug hello\n", test_logger_->message());
132 }
133 
TEST_F(LoggerTest,LoggerOutputsDebugLogsWhenVerbositySet)134 TEST_F(LoggerTest, LoggerOutputsDebugLogsWhenVerbositySet) {
135   // This should now output LOG_DEBUG.
136   int verbose_log_level = 2;
137   test_logger_->set_verbosity_level(verbose_log_level);
138 
139   LOG(LOG_DEBUG) << "Debug hello";
140   EXPECT_EQ("Debug hello\n", test_logger_->message());
141 }
142 
TEST_F(LoggerTest,LoggerOutputsErrorLogsWhenVerbositySet)143 TEST_F(LoggerTest, LoggerOutputsErrorLogsWhenVerbositySet) {
144   // This should now output LOG_ERROR.
145   int verbose_log_level = 2;
146   test_logger_->set_verbosity_level(verbose_log_level);
147 
148   LOG(ERROR) << "Error hello";
149   EXPECT_EQ("Error hello\n", test_logger_->message());
150 }
151 
TEST_F(LoggerTest,LoggerOutputsLogsAccordingToVerbosity)152 TEST_F(LoggerTest, LoggerOutputsLogsAccordingToVerbosity) {
153   int verbose_log_level = 2;
154   test_logger_->set_verbosity_level(verbose_log_level);
155 
156   // More verbose than the current limit.
157   VLOG(verbose_log_level + 1) << "Hello 3";
158   EXPECT_EQ("", test_logger_->message());
159 
160   // Less verbose than the current limit.
161   VLOG(verbose_log_level - 1) << "Hello";
162   EXPECT_EQ("Hello\n", test_logger_->message());
163 
164   // At the current limit. This will be appended to the previous log output.
165   VLOG(verbose_log_level) << "Hello 2";
166   EXPECT_EQ("Hello\nHello 2\n", test_logger_->message());
167 }
168 
169 }  // namespace phonenumbers
170 }  // namespace i18n
171