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

Side by Side Diff: base/logging_unittest.cc

Issue 2288473002: Implement Dump-on-DCHECK (via a new LogSeverity). (Closed)
Patch Set: Migrate some tests to EXPECT_DCHECK_DEATH Created 4 years 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
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/compiler_specific.h"
6 #include "base/debug/dump_without_crashing.h"
6 #include "base/logging.h" 7 #include "base/logging.h"
7 #include "base/macros.h" 8 #include "base/macros.h"
8 9
9 #include "testing/gmock/include/gmock/gmock.h" 10 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
11 12
12 namespace logging { 13 namespace logging {
13 14
14 namespace { 15 namespace {
15 16
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 PCHECK(!mock_log_source.Log()) << mock_log_source.Log(); 185 PCHECK(!mock_log_source.Log()) << mock_log_source.Log();
185 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log()) 186 CHECK_EQ(mock_log_source.Log(), mock_log_source.Log())
186 << uncalled_mock_log_source.Log(); 187 << uncalled_mock_log_source.Log();
187 CHECK_NE(mock_log_source.Log(), mock_log_source.Log()) 188 CHECK_NE(mock_log_source.Log(), mock_log_source.Log())
188 << mock_log_source.Log(); 189 << mock_log_source.Log();
189 } 190 }
190 191
191 #endif 192 #endif
192 193
193 TEST_F(LoggingTest, DebugLoggingReleaseBehavior) { 194 TEST_F(LoggingTest, DebugLoggingReleaseBehavior) {
194 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON) 195 #if DCHECK_IS_ON()
195 int debug_only_variable = 1; 196 int debug_only_variable = 1;
196 #endif 197 #endif
197 // These should avoid emitting references to |debug_only_variable| 198 // These should avoid emitting references to |debug_only_variable|
198 // in release mode. 199 // in release mode.
199 DLOG_IF(INFO, debug_only_variable) << "test"; 200 DLOG_IF(INFO, debug_only_variable) << "test";
200 DLOG_ASSERT(debug_only_variable) << "test"; 201 DLOG_ASSERT(debug_only_variable) << "test";
201 DPLOG_IF(INFO, debug_only_variable) << "test"; 202 DPLOG_IF(INFO, debug_only_variable) << "test";
202 DVLOG_IF(1, debug_only_variable) << "test"; 203 DVLOG_IF(1, debug_only_variable) << "test";
203 } 204 }
204 205
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
319 if (false) 320 if (false)
320 CHECK_EQ(false, true); // Unreached. 321 CHECK_EQ(false, true); // Unreached.
321 else 322 else
322 CHECK_EQ(true, reached = true); // Reached, passed. 323 CHECK_EQ(true, reached = true); // Reached, passed.
323 ASSERT_TRUE(reached); 324 ASSERT_TRUE(reached);
324 325
325 if (false) 326 if (false)
326 CHECK_EQ(false, true); // Unreached. 327 CHECK_EQ(false, true); // Unreached.
327 } 328 }
328 329
330 namespace {
331
332 int fake_dump_without_crashing_count = 0;
333 void FakeDumpWithoutCrashing() {
334 fake_dump_without_crashing_count++;
335 }
336
337 } // namespace
338
339 TEST_F(LoggingTest, LogMessageDump) {
340 // Replace the dump-without-crashing function, to test.
341 base::debug::SetDumpWithoutCrashingFunction(&FakeDumpWithoutCrashing);
342
343 // LOG_DUMP is an internal severity level for use by dump-on-DCHECK, so
344 // we must call LogMessage() directly, rather than using LOG(DUMP).
345 LogMessage(__FILE__, __LINE__, LOG_DUMP, new std::string);
346 EXPECT_EQ(1, fake_dump_without_crashing_count);
347 LogMessage(__FILE__, __LINE__, LOG_DUMP, new std::string);
348 EXPECT_EQ(1, fake_dump_without_crashing_count);
349
350 base::debug::SetDumpWithoutCrashingFunction(nullptr);
351 }
352
329 // Test that defining an operator<< for a type in a namespace doesn't prevent 353 // 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) 354 // 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 355 // 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 356 // found by ADL, since defining another operator<< prevents name lookup from
333 // looking in the global namespace. 357 // looking in the global namespace.
334 namespace nested_test { 358 namespace nested_test {
335 class Streamable {}; 359 class Streamable {};
336 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, 360 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out,
337 const Streamable&) { 361 const Streamable&) {
338 return out << "Streamable"; 362 return out << "Streamable";
339 } 363 }
340 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { 364 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) {
341 std::wstring wstr = L"Hello World"; 365 std::wstring wstr = L"Hello World";
342 std::ostringstream ostr; 366 std::ostringstream ostr;
343 ostr << wstr; 367 ostr << wstr;
344 EXPECT_EQ("Hello World", ostr.str()); 368 EXPECT_EQ("Hello World", ostr.str());
345 } 369 }
346 } // namespace nested_test 370 } // namespace nested_test
347 371
348 } // namespace 372 } // namespace
349 373
350 } // namespace logging 374 } // namespace logging
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698