OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/compiler_specific.h" | 5 #include "base/compiler_specific.h" |
6 #include "base/logging.h" | 6 #include "base/logging.h" |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 | 8 |
9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace logging { | 12 namespace logging { |
13 | 13 |
14 namespace { | 14 namespace { |
15 | 15 |
| 16 using ::testing::HasSubstr; |
16 using ::testing::Return; | 17 using ::testing::Return; |
| 18 using ::testing::_; |
17 | 19 |
18 // Needs to be global since log assert handlers can't maintain state. | 20 // Needs to be global since log assert handlers can't maintain state. |
19 int log_sink_call_count = 0; | 21 int log_sink_call_count = 0; |
20 | 22 |
21 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) | 23 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) |
22 void LogSink(const std::string& str) { | 24 void LogSink(const std::string& str) { |
23 ++log_sink_call_count; | 25 ++log_sink_call_count; |
24 } | 26 } |
25 #endif | 27 #endif |
26 | 28 |
(...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 if (false) | 321 if (false) |
320 CHECK_EQ(false, true); // Unreached. | 322 CHECK_EQ(false, true); // Unreached. |
321 else | 323 else |
322 CHECK_EQ(true, reached = true); // Reached, passed. | 324 CHECK_EQ(true, reached = true); // Reached, passed. |
323 ASSERT_TRUE(reached); | 325 ASSERT_TRUE(reached); |
324 | 326 |
325 if (false) | 327 if (false) |
326 CHECK_EQ(false, true); // Unreached. | 328 CHECK_EQ(false, true); // Unreached. |
327 } | 329 } |
328 | 330 |
| 331 class MockLogMessageListener : logging::LogMessageListener { |
| 332 public: |
| 333 MockLogMessageListener(std::string& buffer) : buffer_(buffer) {} |
| 334 void OnMessage(int severity, |
| 335 const char* file, |
| 336 int line, |
| 337 size_t message_start, |
| 338 const std::string& str) override { |
| 339 buffer_.append(str); |
| 340 } |
| 341 |
| 342 private: |
| 343 std::string& buffer_; |
| 344 }; |
| 345 |
| 346 TEST_F(LoggingTest, LogListenerBasic) { |
| 347 std::string buffer; |
| 348 MockLogMessageListener log(buffer); |
| 349 |
| 350 LOG(INFO) << "testing1"; |
| 351 LOG(WARNING) << "testing2"; |
| 352 |
| 353 EXPECT_NE(std::string::npos, buffer.find("testing1")); |
| 354 EXPECT_NE(std::string::npos, buffer.find("testing2")); |
| 355 } |
| 356 |
| 357 TEST_F(LoggingTest, LogListenerLifespan) { |
| 358 size_t count = LogMessageListenerCountForTesting(); |
| 359 std::string buf1, buf2; |
| 360 MockLogMessageListener* log1 = nullptr; |
| 361 { |
| 362 MockLogMessageListener log2(buf2); |
| 363 EXPECT_EQ(count + 1, LogMessageListenerCountForTesting()); |
| 364 |
| 365 LOG(WARNING) << "testing1"; |
| 366 log1 = new MockLogMessageListener(buf1); |
| 367 EXPECT_EQ(count + 2, LogMessageListenerCountForTesting()); |
| 368 LOG(WARNING) << "testing2"; |
| 369 } |
| 370 EXPECT_EQ(count + 1, LogMessageListenerCountForTesting()); |
| 371 LOG(WARNING) << "testing3"; |
| 372 delete log1; |
| 373 EXPECT_EQ(count, LogMessageListenerCountForTesting()); |
| 374 |
| 375 EXPECT_EQ(std::string::npos, buf1.find("testing1")); |
| 376 EXPECT_NE(std::string::npos, buf1.find("testing2")); |
| 377 EXPECT_NE(std::string::npos, buf1.find("testing3")); |
| 378 EXPECT_NE(std::string::npos, buf2.find("testing1")); |
| 379 EXPECT_NE(std::string::npos, buf2.find("testing2")); |
| 380 EXPECT_EQ(std::string::npos, buf2.find("testing3")); |
| 381 } |
| 382 |
| 383 class ChattyLogMessageListener : logging::LogMessageListener { |
| 384 public: |
| 385 ChattyLogMessageListener() : count_(0) {} |
| 386 void OnMessage(int severity, |
| 387 const char* file, |
| 388 int line, |
| 389 size_t message_start, |
| 390 const std::string& str) override { |
| 391 count_++; |
| 392 if (count_ == 1) { |
| 393 LOG(ERROR) << "message inside OnMessage()"; |
| 394 } |
| 395 } |
| 396 int Count() { return count_; } |
| 397 |
| 398 private: |
| 399 int count_; |
| 400 }; |
| 401 |
| 402 TEST_F(LoggingTest, LogListenerReentrant) { |
| 403 ChattyLogMessageListener log; |
| 404 |
| 405 LOG(INFO) << "testing1"; |
| 406 EXPECT_EQ(2, log.Count()); |
| 407 } |
| 408 |
329 // Test that defining an operator<< for a type in a namespace doesn't prevent | 409 // Test that defining an operator<< for a type in a namespace doesn't prevent |
330 // other code in that namespace from calling the operator<<(ostream, wstring) | 410 // other code in that namespace from calling the operator<<(ostream, wstring) |
331 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be | 411 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be |
332 // found by ADL, since defining another operator<< prevents name lookup from | 412 // found by ADL, since defining another operator<< prevents name lookup from |
333 // looking in the global namespace. | 413 // looking in the global namespace. |
334 namespace nested_test { | 414 namespace nested_test { |
335 class Streamable {}; | 415 class Streamable {}; |
336 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, | 416 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, |
337 const Streamable&) { | 417 const Streamable&) { |
338 return out << "Streamable"; | 418 return out << "Streamable"; |
339 } | 419 } |
340 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { | 420 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { |
341 std::wstring wstr = L"Hello World"; | 421 std::wstring wstr = L"Hello World"; |
342 std::ostringstream ostr; | 422 std::ostringstream ostr; |
343 ostr << wstr; | 423 ostr << wstr; |
344 EXPECT_EQ("Hello World", ostr.str()); | 424 EXPECT_EQ("Hello World", ostr.str()); |
345 } | 425 } |
346 } // namespace nested_test | 426 } // namespace nested_test |
347 | 427 |
348 } // namespace | 428 } // namespace |
349 | 429 |
350 } // namespace logging | 430 } // namespace logging |
OLD | NEW |