OLD | NEW |
| (Empty) |
1 // Copyright (C) 2011 Google Inc. | |
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 "default_logger.h" | |
22 | |
23 namespace i18n { | |
24 namespace phonenumbers { | |
25 | |
26 using std::string; | |
27 | |
28 // String logger implementation used for testing. Messages are output to a | |
29 // string for convenience. | |
30 class StringLogger : public Logger { | |
31 public: | |
32 virtual ~StringLogger() {} | |
33 | |
34 const string& message() const { | |
35 return msg_; | |
36 } | |
37 | |
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: | |
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 | |
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 | |
68 TEST_F(LoggerTest, LoggerIgnoresHigherVerbosity) { | |
69 // The logger verbosity is set to LOG_INFO, therefore LOG_DEBUG messages | |
70 // should be ignored. | |
71 VLOG(LOG_DEBUG) << "Hello"; | |
72 EXPECT_EQ("", test_logger_->message()); | |
73 } | |
74 | |
75 TEST_F(LoggerTest, LoggerOutputsNewline) { | |
76 VLOG(LOG_INFO) << "Hello"; | |
77 EXPECT_EQ("Hello\n", test_logger_->message()); | |
78 } | |
79 | |
80 TEST_F(LoggerTest, LoggerLogsEqualVerbosity) { | |
81 VLOG(LOG_INFO) << "Hello"; | |
82 EXPECT_EQ("Hello\n", test_logger_->message()); | |
83 } | |
84 | |
85 TEST_F(LoggerTest, LoggerLogsLowerVerbosity) { | |
86 VLOG(LOG_WARNING) << "Hello"; | |
87 EXPECT_EQ("Hello\n", test_logger_->message()); | |
88 } | |
89 | |
90 TEST_F(LoggerTest, LoggerConcatenatesMessages) { | |
91 VLOG(LOG_INFO) << "Hello"; | |
92 ASSERT_EQ("Hello\n", test_logger_->message()); | |
93 | |
94 VLOG(LOG_INFO) << " World"; | |
95 EXPECT_EQ("Hello\n World\n", test_logger_->message()); | |
96 } | |
97 | |
98 TEST_F(LoggerTest, LoggerHandlesDifferentTypes) { | |
99 VLOG(LOG_INFO) << "Hello " << 42; | |
100 EXPECT_EQ("Hello 42\n", test_logger_->message()); | |
101 } | |
102 | |
103 } // namespace phonenumbers | |
104 } // namespace i18n | |
OLD | NEW |