Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(122)

Side by Side Diff: base/logging_unittest.cc

Issue 2828373002: Revert of Report CHECK/DCHECK to test launcher summary output. (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/logging.cc ('k') | base/metrics/statistics_recorder_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/logging.h" 6 #include "base/logging.h"
6 #include "base/bind.h"
7 #include "base/callback.h"
8 #include "base/compiler_specific.h"
9 #include "base/macros.h" 7 #include "base/macros.h"
10 #include "base/strings/string_piece.h"
11 8
12 #include "testing/gmock/include/gmock/gmock.h" 9 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
14 11
15 #if defined(OS_POSIX) 12 #if defined(OS_POSIX)
16 #include <signal.h> 13 #include <signal.h>
17 #include <unistd.h> 14 #include <unistd.h>
18 #include "base/posix/eintr_wrapper.h" 15 #include "base/posix/eintr_wrapper.h"
19 #endif // OS_POSIX 16 #endif // OS_POSIX
20 17
21 #if defined(OS_LINUX) || defined(OS_ANDROID) 18 #if defined(OS_LINUX) || defined(OS_ANDROID)
22 #include <ucontext.h> 19 #include <ucontext.h>
23 #endif 20 #endif
24 21
25 #if defined(OS_WIN) 22 #if defined(OS_WIN)
26 #include <excpt.h> 23 #include <excpt.h>
27 #include <windows.h> 24 #include <windows.h>
28 #endif // OS_WIN 25 #endif // OS_WIN
29 26
30 namespace logging { 27 namespace logging {
31 28
32 namespace { 29 namespace {
33 30
34 using ::testing::Return; 31 using ::testing::Return;
35 using ::testing::_;
36 32
37 // Needs to be global since log assert handlers can't maintain state. 33 // Needs to be global since log assert handlers can't maintain state.
38 int log_sink_call_count = 0; 34 int log_sink_call_count = 0;
39 35
40 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG) 36 #if !defined(OFFICIAL_BUILD) || defined(DCHECK_ALWAYS_ON) || !defined(NDEBUG)
41 void LogSink(const char* file, 37 void LogSink(const std::string& str) {
42 int line,
43 const base::StringPiece message,
44 const base::StringPiece stack_trace) {
45 ++log_sink_call_count; 38 ++log_sink_call_count;
46 } 39 }
47 #endif 40 #endif
48 41
49 // Class to make sure any manipulations we do to the min log level are 42 // Class to make sure any manipulations we do to the min log level are
50 // contained (i.e., do not affect other unit tests). 43 // contained (i.e., do not affect other unit tests).
51 class LogStateSaver { 44 class LogStateSaver {
52 public: 45 public:
53 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {} 46 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {}
54 47
55 ~LogStateSaver() { 48 ~LogStateSaver() {
56 SetMinLogLevel(old_min_log_level_); 49 SetMinLogLevel(old_min_log_level_);
50 SetLogAssertHandler(NULL);
57 log_sink_call_count = 0; 51 log_sink_call_count = 0;
58 } 52 }
59 53
60 private: 54 private:
61 int old_min_log_level_; 55 int old_min_log_level_;
62 56
63 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); 57 DISALLOW_COPY_AND_ASSIGN(LogStateSaver);
64 }; 58 };
65 59
66 class LoggingTest : public testing::Test { 60 class LoggingTest : public testing::Test {
67 private: 61 private:
68 LogStateSaver log_state_saver_; 62 LogStateSaver log_state_saver_;
69 }; 63 };
70 64
71 class MockLogSource { 65 class MockLogSource {
72 public: 66 public:
73 MOCK_METHOD0(Log, const char*()); 67 MOCK_METHOD0(Log, const char*());
74 }; 68 };
75 69
76 class MockLogAssertHandler {
77 public:
78 MOCK_METHOD4(
79 HandleLogAssert,
80 void(const char*, int, const base::StringPiece, const base::StringPiece));
81 };
82
83 TEST_F(LoggingTest, BasicLogging) { 70 TEST_F(LoggingTest, BasicLogging) {
84 MockLogSource mock_log_source; 71 MockLogSource mock_log_source;
85 EXPECT_CALL(mock_log_source, Log()) 72 EXPECT_CALL(mock_log_source, Log())
86 .Times(DCHECK_IS_ON() ? 16 : 8) 73 .Times(DCHECK_IS_ON() ? 16 : 8)
87 .WillRepeatedly(Return("log message")); 74 .WillRepeatedly(Return("log message"));
88 75
89 SetMinLogLevel(LOG_INFO); 76 SetMinLogLevel(LOG_INFO);
90 77
91 EXPECT_TRUE(LOG_IS_ON(INFO)); 78 EXPECT_TRUE(LOG_IS_ON(INFO));
92 EXPECT_TRUE((DCHECK_IS_ON() != 0) == DLOG_IS_ON(INFO)); 79 EXPECT_TRUE((DCHECK_IS_ON() != 0) == DLOG_IS_ON(INFO));
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 #define MAYBE_CheckStreamsAreLazy DISABLED_CheckStreamsAreLazy 190 #define MAYBE_CheckStreamsAreLazy DISABLED_CheckStreamsAreLazy
204 #else 191 #else
205 #define MAYBE_CheckStreamsAreLazy CheckStreamsAreLazy 192 #define MAYBE_CheckStreamsAreLazy CheckStreamsAreLazy
206 #endif 193 #endif
207 TEST_F(LoggingTest, MAYBE_CheckStreamsAreLazy) { 194 TEST_F(LoggingTest, MAYBE_CheckStreamsAreLazy) {
208 MockLogSource mock_log_source, uncalled_mock_log_source; 195 MockLogSource mock_log_source, uncalled_mock_log_source;
209 EXPECT_CALL(mock_log_source, Log()).Times(8). 196 EXPECT_CALL(mock_log_source, Log()).Times(8).
210 WillRepeatedly(Return("check message")); 197 WillRepeatedly(Return("check message"));
211 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0); 198 EXPECT_CALL(uncalled_mock_log_source, Log()).Times(0);
212 199
213 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); 200 SetLogAssertHandler(&LogSink);
214 201
215 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log(); 202 CHECK(mock_log_source.Log()) << uncalled_mock_log_source.Log();
216 PCHECK(!mock_log_source.Log()) << mock_log_source.Log(); 203 PCHECK(!mock_log_source.Log()) << mock_log_source.Log();
217 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log()) 204 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log())
218 << uncalled_mock_log_source.Log(); 205 << uncalled_mock_log_source.Log();
219 CHECK_NE(mock_log_source.Log(), mock_log_source.Log()) 206 CHECK_NE(mock_log_source.Log(), mock_log_source.Log())
220 << mock_log_source.Log(); 207 << mock_log_source.Log();
221 } 208 }
222 209
223 #endif 210 #endif
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 #else 398 #else
412 #define MAYBE_Dcheck Dcheck 399 #define MAYBE_Dcheck Dcheck
413 #endif 400 #endif
414 TEST_F(LoggingTest, MAYBE_Dcheck) { 401 TEST_F(LoggingTest, MAYBE_Dcheck) {
415 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON) 402 #if defined(NDEBUG) && !defined(DCHECK_ALWAYS_ON)
416 // Release build. 403 // Release build.
417 EXPECT_FALSE(DCHECK_IS_ON()); 404 EXPECT_FALSE(DCHECK_IS_ON());
418 EXPECT_FALSE(DLOG_IS_ON(DCHECK)); 405 EXPECT_FALSE(DLOG_IS_ON(DCHECK));
419 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON) 406 #elif defined(NDEBUG) && defined(DCHECK_ALWAYS_ON)
420 // Release build with real DCHECKS. 407 // Release build with real DCHECKS.
421 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); 408 SetLogAssertHandler(&LogSink);
422 EXPECT_TRUE(DCHECK_IS_ON()); 409 EXPECT_TRUE(DCHECK_IS_ON());
423 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); 410 EXPECT_TRUE(DLOG_IS_ON(DCHECK));
424 #else 411 #else
425 // Debug build. 412 // Debug build.
426 ScopedLogAssertHandler scoped_assert_handler(base::Bind(LogSink)); 413 SetLogAssertHandler(&LogSink);
427 EXPECT_TRUE(DCHECK_IS_ON()); 414 EXPECT_TRUE(DCHECK_IS_ON());
428 EXPECT_TRUE(DLOG_IS_ON(DCHECK)); 415 EXPECT_TRUE(DLOG_IS_ON(DCHECK));
429 #endif 416 #endif
430 417
431 EXPECT_EQ(0, log_sink_call_count); 418 EXPECT_EQ(0, log_sink_call_count);
432 DCHECK(false); 419 DCHECK(false);
433 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count); 420 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count);
434 DPCHECK(false); 421 DPCHECK(false);
435 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count); 422 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count);
436 DCHECK_EQ(0, 1); 423 DCHECK_EQ(0, 1);
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 if (false) 492 if (false)
506 CHECK_EQ(false, true); // Unreached. 493 CHECK_EQ(false, true); // Unreached.
507 else 494 else
508 CHECK_EQ(true, reached = true); // Reached, passed. 495 CHECK_EQ(true, reached = true); // Reached, passed.
509 ASSERT_TRUE(reached); 496 ASSERT_TRUE(reached);
510 497
511 if (false) 498 if (false)
512 CHECK_EQ(false, true); // Unreached. 499 CHECK_EQ(false, true); // Unreached.
513 } 500 }
514 501
515 TEST_F(LoggingTest, NestedLogAssertHandlers) {
516 ::testing::InSequence dummy;
517 ::testing::StrictMock<MockLogAssertHandler> handler_a, handler_b;
518
519 EXPECT_CALL(
520 handler_a,
521 HandleLogAssert(
522 _, _,
523 base::StringPiece(
524 "Check failed: false. First assert must be catched by handler_a"),
525 _));
526 EXPECT_CALL(
527 handler_b,
528 HandleLogAssert(_, _,
529 base::StringPiece("Check failed: false. Second assert "
530 "must be catched by handler_b"),
531 _));
532 EXPECT_CALL(
533 handler_a,
534 HandleLogAssert(_, _,
535 base::StringPiece("Check failed: false. Last assert "
536 "must be catched by handler_a again"),
537 _));
538
539 logging::ScopedLogAssertHandler scoped_handler_a(base::Bind(
540 &MockLogAssertHandler::HandleLogAssert, base::Unretained(&handler_a)));
541
542 CHECK(false) << "First assert must be catched by handler_a";
543
544 {
545 logging::ScopedLogAssertHandler scoped_handler_b(base::Bind(
546 &MockLogAssertHandler::HandleLogAssert, base::Unretained(&handler_b)));
547 CHECK(false) << "Second assert must be catched by handler_b";
548 }
549
550 CHECK(false) << "Last assert must be catched by handler_a again";
551 }
552
553 // Test that defining an operator<< for a type in a namespace doesn't prevent 502 // Test that defining an operator<< for a type in a namespace doesn't prevent
554 // other code in that namespace from calling the operator<<(ostream, wstring) 503 // other code in that namespace from calling the operator<<(ostream, wstring)
555 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be 504 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be
556 // found by ADL, since defining another operator<< prevents name lookup from 505 // found by ADL, since defining another operator<< prevents name lookup from
557 // looking in the global namespace. 506 // looking in the global namespace.
558 namespace nested_test { 507 namespace nested_test {
559 class Streamable {}; 508 class Streamable {};
560 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, 509 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out,
561 const Streamable&) { 510 const Streamable&) {
562 return out << "Streamable"; 511 return out << "Streamable";
563 } 512 }
564 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { 513 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) {
565 std::wstring wstr = L"Hello World"; 514 std::wstring wstr = L"Hello World";
566 std::ostringstream ostr; 515 std::ostringstream ostr;
567 ostr << wstr; 516 ostr << wstr;
568 EXPECT_EQ("Hello World", ostr.str()); 517 EXPECT_EQ("Hello World", ostr.str());
569 } 518 }
570 } // namespace nested_test 519 } // namespace nested_test
571 520
572 } // namespace 521 } // namespace
573 522
574 } // namespace logging 523 } // namespace logging
OLDNEW
« no previous file with comments | « base/logging.cc ('k') | base/metrics/statistics_recorder_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698