Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stdlib.h> | 5 #include <stdlib.h> |
| 6 | 6 |
| 7 #include <sstream> | 7 #include <sstream> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "mojo/public/cpp/environment/environment.h" | 10 #include "mojo/public/cpp/environment/environment.h" |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 | 194 |
| 195 ResetMockLogger(); | 195 ResetMockLogger(); |
| 196 | 196 |
| 197 MOJO_LAZY_LOG_STREAM(INFO, false) << "hello"; | 197 MOJO_LAZY_LOG_STREAM(INFO, false) << "hello"; |
| 198 EXPECT_FALSE(log_message_was_called()); | 198 EXPECT_FALSE(log_message_was_called()); |
| 199 | 199 |
| 200 ResetMockLogger(); | 200 ResetMockLogger(); |
| 201 | 201 |
| 202 MOJO_LAZY_LOG_STREAM(FATAL, false) << "hello"; | 202 MOJO_LAZY_LOG_STREAM(FATAL, false) << "hello"; |
| 203 EXPECT_FALSE(log_message_was_called()); | 203 EXPECT_FALSE(log_message_was_called()); |
| 204 | |
| 205 ResetMockLogger(); | |
| 206 | |
| 207 bool x = false; | |
| 208 // This probably fails to compile if we forget to parenthesize the condition | |
| 209 // in the macro (= has low precedence, and needs an lvalue on the LHS). | |
| 210 MOJO_LAZY_LOG_STREAM(ERROR, x = true) << "hello"; | |
| 211 EXPECT_TRUE(log_message_was_called()); | |
| 212 | |
| 213 ResetMockLogger(); | |
| 214 | |
| 215 MOJO_LAZY_LOG_STREAM(WARNING, x = false) << "hello"; | |
| 216 EXPECT_FALSE(log_message_was_called()); | |
| 217 } | 204 } |
| 218 | 205 |
| 219 TEST_F(LoggingTest, ShouldLog) { | 206 TEST_F(LoggingTest, ShouldLog) { |
| 220 // We start at |MOJO_LOG_LEVEL_INFO|. | 207 // We start at |MOJO_LOG_LEVEL_INFO|. |
| 221 EXPECT_FALSE(MOJO_SHOULD_LOG(VERBOSE)); | 208 EXPECT_FALSE(MOJO_SHOULD_LOG(VERBOSE)); |
| 222 EXPECT_TRUE(MOJO_SHOULD_LOG(INFO)); | 209 EXPECT_TRUE(MOJO_SHOULD_LOG(INFO)); |
| 223 EXPECT_TRUE(MOJO_SHOULD_LOG(WARNING)); | 210 EXPECT_TRUE(MOJO_SHOULD_LOG(WARNING)); |
| 224 EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR)); | 211 EXPECT_TRUE(MOJO_SHOULD_LOG(ERROR)); |
| 225 EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL)); | 212 EXPECT_TRUE(MOJO_SHOULD_LOG(FATAL)); |
| 226 | 213 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 MOJO_LOG_IF(VERBOSE, true) << "hello"; | 270 MOJO_LOG_IF(VERBOSE, true) << "hello"; |
| 284 EXPECT_FALSE(log_message_was_called()); | 271 EXPECT_FALSE(log_message_was_called()); |
| 285 | 272 |
| 286 ResetMockLogger(); | 273 ResetMockLogger(); |
| 287 | 274 |
| 288 MOJO_LOG_IF(VERBOSE, false) << "hello"; | 275 MOJO_LOG_IF(VERBOSE, false) << "hello"; |
| 289 EXPECT_FALSE(log_message_was_called()); | 276 EXPECT_FALSE(log_message_was_called()); |
| 290 | 277 |
| 291 ResetMockLogger(); | 278 ResetMockLogger(); |
| 292 | 279 |
| 293 bool x = false; | |
| 294 // Also try to make sure that we parenthesize the condition properly. | |
| 295 MOJO_LOG_IF(INFO, x = true) << "hello"; | |
| 296 EXPECT_TRUE(log_message_was_called()); | |
| 297 EXPECT_EQ(MOJO_LOG_LEVEL_INFO, last_log_level()); | |
| 298 EXPECT_EQ(ExpectedLogMessage(__LINE__-3, "hello"), last_message()); | |
| 299 | |
| 300 ResetMockLogger(); | |
| 301 | |
| 302 MOJO_LOG_IF(INFO, x = false) << "hello"; | |
| 303 EXPECT_FALSE(log_message_was_called()); | |
| 304 | |
| 305 ResetMockLogger(); | |
| 306 | |
| 307 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR); | 280 Environment::GetDefaultLogger()->SetMinimumLogLevel(MOJO_LOG_LEVEL_ERROR); |
| 308 | 281 |
| 309 ResetMockLogger(); | 282 ResetMockLogger(); |
| 310 | 283 |
| 311 MOJO_LOG_IF(INFO, 0 != 1) << "hello"; | 284 MOJO_LOG_IF(INFO, 0 != 1) << "hello"; |
| 312 EXPECT_FALSE(log_message_was_called()); | 285 EXPECT_FALSE(log_message_was_called()); |
| 313 | 286 |
| 314 ResetMockLogger(); | 287 ResetMockLogger(); |
| 315 | 288 |
| 316 MOJO_LOG_IF(WARNING, 1+1 == 2) << "hello"; | 289 MOJO_LOG_IF(WARNING, 1+1 == 2) << "hello"; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 335 MOJO_LOG_IF(INFO, NotCalled()) << "hello"; | 308 MOJO_LOG_IF(INFO, NotCalled()) << "hello"; |
| 336 EXPECT_FALSE(log_message_was_called()); | 309 EXPECT_FALSE(log_message_was_called()); |
| 337 } | 310 } |
| 338 | 311 |
| 339 TEST_F(LoggingTest, Check) { | 312 TEST_F(LoggingTest, Check) { |
| 340 MOJO_CHECK(true) << "hello"; | 313 MOJO_CHECK(true) << "hello"; |
| 341 EXPECT_FALSE(log_message_was_called()); | 314 EXPECT_FALSE(log_message_was_called()); |
| 342 | 315 |
| 343 ResetMockLogger(); | 316 ResetMockLogger(); |
| 344 | 317 |
| 345 bool x = true; | 318 MOJO_CHECK(false) << "hello"; |
| 346 // Also try to make sure that we parenthesize the condition properly. | |
|
viettrungluu
2014/07/08 15:19:28
Uhm, no. Way to defeat exactly what we're testing.
| |
| 347 MOJO_CHECK(x = false) << "hello"; | |
| 348 EXPECT_TRUE(log_message_was_called()); | 319 EXPECT_TRUE(log_message_was_called()); |
| 349 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level()); | 320 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level()); |
| 350 // Different compilers have different ideas about the line number of a split | 321 // Different compilers have different ideas about the line number of a split |
| 351 // line. | 322 // line. |
| 352 int line = __LINE__; | 323 int line = __LINE__; |
| 353 EXPECT_EQ(ExpectedLogMessage(line-5, "Check failed: x = false. hello"), | 324 EXPECT_EQ(ExpectedLogMessage(line-5, "Check failed: false. hello"), |
| 354 last_message()); | 325 last_message()); |
| 355 | 326 |
| 356 ResetMockLogger(); | 327 ResetMockLogger(); |
| 357 | 328 |
| 358 // Also test a "naked" |MOJO_CHECK()|s. | 329 // Also test a "naked" |MOJO_CHECK()|s. |
| 359 MOJO_CHECK(1+2 == 3); | 330 MOJO_CHECK(1+2 == 3); |
| 360 EXPECT_FALSE(log_message_was_called()); | 331 EXPECT_FALSE(log_message_was_called()); |
| 361 } | 332 } |
| 362 | 333 |
| 363 TEST_F(LoggingTest, Dlog) { | 334 TEST_F(LoggingTest, Dlog) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 423 | 394 |
| 424 MOJO_DCHECK(true) << "hello"; | 395 MOJO_DCHECK(true) << "hello"; |
| 425 EXPECT_FALSE(log_message_was_called()); | 396 EXPECT_FALSE(log_message_was_called()); |
| 426 | 397 |
| 427 ResetMockLogger(); | 398 ResetMockLogger(); |
| 428 | 399 |
| 429 // |MOJO_DCHECK()| should compile (but not evaluate) its condition even for | 400 // |MOJO_DCHECK()| should compile (but not evaluate) its condition even for |
| 430 // non-debug builds. (Hopefully, we'll get an unused variable error if it | 401 // non-debug builds. (Hopefully, we'll get an unused variable error if it |
| 431 // fails to compile the condition.) | 402 // fails to compile the condition.) |
| 432 bool x = true; | 403 bool x = true; |
| 433 MOJO_DCHECK(x = false) << "hello"; | 404 MOJO_DCHECK(x == false) << "hello"; |
| 434 #ifdef NDEBUG | 405 #ifdef NDEBUG |
| 435 EXPECT_FALSE(log_message_was_called()); | 406 EXPECT_FALSE(log_message_was_called()); |
| 436 #else | 407 #else |
| 437 EXPECT_TRUE(log_message_was_called()); | 408 EXPECT_TRUE(log_message_was_called()); |
| 438 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level()); | 409 EXPECT_EQ(MOJO_LOG_LEVEL_FATAL, last_log_level()); |
| 439 // Different compilers have different ideas about the line number of a split | 410 // Different compilers have different ideas about the line number of a split |
| 440 // line. | 411 // line. |
| 441 int line = __LINE__; | 412 int line = __LINE__; |
| 442 EXPECT_EQ(ExpectedLogMessage(line-8, "Check failed: x = false. hello"), | 413 EXPECT_EQ(ExpectedLogMessage(line-8, "Check failed: x == false. hello"), |
| 443 last_message()); | 414 last_message()); |
| 444 #endif | 415 #endif |
| 445 } | 416 } |
| 446 | 417 |
| 447 } // namespace | 418 } // namespace |
| 448 } // namespace mojo | 419 } // namespace mojo |
| OLD | NEW |