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

Side by Side Diff: base/logging_unittest.cc

Issue 4199005: Fixed subtle difference in behavior between DCHECK and DCHECK_EQ et al. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed wtc's comments Created 10 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « base/logging.h ('k') | no next file » | 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/logging.h" 6 #include "base/logging.h"
7 7
8 #include "testing/gmock/include/gmock/gmock.h" 8 #include "testing/gmock/include/gmock/gmock.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 namespace logging { 11 namespace logging {
12 12
13 namespace { 13 namespace {
14 14
15 using ::testing::Return; 15 using ::testing::Return;
16 16
17 // Needs to be global since log assert handlers can't maintain state.
18 int log_sink_call_count = 0;
19
20 void LogSink(const std::string& str) {
21 ++log_sink_call_count;
22 }
23
17 // Class to make sure any manipulations we do to the min log level are 24 // Class to make sure any manipulations we do to the min log level are
18 // contained (i.e., do not affect other unit tests). 25 // contained (i.e., do not affect other unit tests).
19 class MinLogLevelSaver { 26 class LogStateSaver {
20 public: 27 public:
21 MinLogLevelSaver() : old_min_log_level_(GetMinLogLevel()) {} 28 LogStateSaver() : old_min_log_level_(GetMinLogLevel()) {}
22 29
23 ~MinLogLevelSaver() { SetMinLogLevel(old_min_log_level_); } 30 ~LogStateSaver() {
31 SetMinLogLevel(old_min_log_level_);
32 SetLogAssertHandler(NULL);
33 SetLogReportHandler(NULL);
34 log_sink_call_count = 0;
35 }
24 36
25 private: 37 private:
26 int old_min_log_level_; 38 int old_min_log_level_;
27 39
28 DISALLOW_COPY_AND_ASSIGN(MinLogLevelSaver); 40 DISALLOW_COPY_AND_ASSIGN(LogStateSaver);
29 }; 41 };
30 42
31 class LoggingTest : public testing::Test { 43 class LoggingTest : public testing::Test {
32 private: 44 private:
33 MinLogLevelSaver min_log_level_saver_; 45 LogStateSaver log_state_saver_;
34 }; 46 };
35 47
36 class MockLogSource { 48 class MockLogSource {
37 public: 49 public:
38 MOCK_METHOD0(Log, const char*()); 50 MOCK_METHOD0(Log, const char*());
39 }; 51 };
40 52
41 TEST_F(LoggingTest, BasicLogging) { 53 TEST_F(LoggingTest, BasicLogging) {
42 MockLogSource mock_log_source; 54 MockLogSource mock_log_source;
43 const int kExpectedDebugOrReleaseCalls = 6; 55 const int kExpectedDebugOrReleaseCalls = 6;
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 SetMinLogLevel(LOG_FATAL + 1); 145 SetMinLogLevel(LOG_FATAL + 1);
134 EXPECT_FALSE(LOG_IS_ON(FATAL)); 146 EXPECT_FALSE(LOG_IS_ON(FATAL));
135 #endif // !defined(LOGGING_IS_OFFICIAL_BUILD) && defined(NDEBUG) 147 #endif // !defined(LOGGING_IS_OFFICIAL_BUILD) && defined(NDEBUG)
136 DCHECK(mock_log_source.Log()) << mock_log_source.Log(); 148 DCHECK(mock_log_source.Log()) << mock_log_source.Log();
137 DPCHECK(mock_log_source.Log()) << mock_log_source.Log(); 149 DPCHECK(mock_log_source.Log()) << mock_log_source.Log();
138 DCHECK_EQ(0, 0) << mock_log_source.Log(); 150 DCHECK_EQ(0, 0) << mock_log_source.Log();
139 DCHECK_EQ(mock_log_source.Log(), static_cast<const char*>(NULL)) 151 DCHECK_EQ(mock_log_source.Log(), static_cast<const char*>(NULL))
140 << mock_log_source.Log(); 152 << mock_log_source.Log();
141 } 153 }
142 154
155 TEST_F(LoggingTest, Dcheck) {
156 #if defined(LOGGING_IS_OFFICIAL_BUILD)
157 // Official build.
158 EXPECT_FALSE(DCHECK_IS_ON());
159 EXPECT_FALSE(DLOG_IS_ON(DCHECK));
160 #elif defined(NDEBUG)
161 // Unofficial release build.
162 logging::g_enable_dcheck = true;
163 logging::SetLogReportHandler(&LogSink);
164 EXPECT_TRUE(DCHECK_IS_ON());
165 EXPECT_FALSE(DLOG_IS_ON(DCHECK));
166 #else
167 // Unofficial debug build.
168 logging::SetLogAssertHandler(&LogSink);
169 EXPECT_TRUE(DCHECK_IS_ON());
170 EXPECT_TRUE(DLOG_IS_ON(DCHECK));
171 #endif // defined(LOGGING_IS_OFFICIAL_BUILD)
172
173 EXPECT_EQ(0, log_sink_call_count);
174 DCHECK(false);
175 EXPECT_EQ(DCHECK_IS_ON() ? 1 : 0, log_sink_call_count);
176 DPCHECK(false);
177 EXPECT_EQ(DCHECK_IS_ON() ? 2 : 0, log_sink_call_count);
178 DCHECK_EQ(0, 1);
179 EXPECT_EQ(DCHECK_IS_ON() ? 3 : 0, log_sink_call_count);
180 }
181
143 TEST_F(LoggingTest, DcheckReleaseBehavior) { 182 TEST_F(LoggingTest, DcheckReleaseBehavior) {
144 int some_variable = 1; 183 int some_variable = 1;
145 // These should still reference |some_variable| so we don't get 184 // These should still reference |some_variable| so we don't get
146 // unused variable warnings. 185 // unused variable warnings.
147 DCHECK(some_variable) << "test"; 186 DCHECK(some_variable) << "test";
148 DPCHECK(some_variable) << "test"; 187 DPCHECK(some_variable) << "test";
149 DCHECK_EQ(some_variable, 1) << "test"; 188 DCHECK_EQ(some_variable, 1) << "test";
150 } 189 }
151 190
152 } // namespace 191 } // namespace
153 192
154 } // namespace logging 193 } // namespace logging
OLDNEW
« no previous file with comments | « base/logging.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698