Chromium Code Reviews| 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/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
| 7 #include "base/logging.h" | 7 #include "base/logging.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" |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 35 SetLogAssertHandler(NULL); | 35 SetLogAssertHandler(NULL); |
| 36 log_sink_call_count = 0; | 36 log_sink_call_count = 0; |
| 37 } | 37 } |
| 38 | 38 |
| 39 private: | 39 private: |
| 40 int old_min_log_level_; | 40 int old_min_log_level_; |
| 41 | 41 |
| 42 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); | 42 DISALLOW_COPY_AND_ASSIGN(LogStateSaver); |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 // Validates that exactly N log messages are received during its lifetime. | |
| 46 template <int N> | |
| 47 class ExpectNLogs { | |
| 48 public: | |
| 49 ExpectNLogs() { | |
| 50 SetLogAssertHandler(&LogSink); | |
| 51 log_sink_call_count = 0; | |
| 52 } | |
| 53 | |
| 54 ~ExpectNLogs() { | |
| 55 SetLogAssertHandler(NULL); | |
| 56 EXPECT_EQ(N, log_sink_call_count); | |
| 57 } | |
| 58 | |
| 59 private: | |
| 60 LogStateSaver log_state_saver_; | |
| 61 | |
| 62 DISALLOW_COPY_AND_ASSIGN(ExpectNLogs); | |
| 63 }; | |
| 64 | |
| 65 using ExpectNoLogs = ExpectNLogs<0>; | |
| 66 using ExpectOneLog = ExpectNLogs<1>; | |
| 67 using ExpectOneLogIfDCheckIsOn = ExpectNLogs<DCHECK_IS_ON() ? 1 : 0>; | |
| 68 | |
| 69 // A convenience function to aid in test readability. | |
| 70 bool UnreachedBoolExpr() { | |
| 71 ADD_FAILURE() << "Unexpectedly evaluated."; | |
| 72 return false; | |
| 73 } | |
| 74 | |
| 75 // Validates that operator()() is invoked exactly N times. | |
| 76 template<int N> | |
| 77 class EvaluatedNTimes { | |
| 78 public: | |
| 79 EvaluatedNTimes() {} | |
| 80 ~EvaluatedNTimes() { | |
| 81 EXPECT_EQ(N, evaluations_) << "Expected to be evaluated exactly " << N | |
| 82 << " times."; | |
| 83 } | |
| 84 | |
| 85 bool operator()() { | |
| 86 EXPECT_GT(N, evaluations_) << "Evaluated more than the expected " << N | |
| 87 << " times."; | |
| 88 ++evaluations_; | |
| 89 return true; | |
| 90 }; | |
| 91 | |
| 92 private: | |
| 93 int evaluations_ = 0; | |
| 94 DISALLOW_COPY_AND_ASSIGN(EvaluatedNTimes); | |
| 95 }; | |
| 96 | |
| 97 using EvaluatedOnce = EvaluatedNTimes<1>; | |
| 98 using EvaluatedOnceIfDCheckIsOn = EvaluatedNTimes<DCHECK_IS_ON() ? 1 : 0>; | |
| 99 | |
| 45 class LoggingTest : public testing::Test { | 100 class LoggingTest : public testing::Test { |
| 46 private: | 101 private: |
| 47 LogStateSaver log_state_saver_; | 102 LogStateSaver log_state_saver_; |
| 48 }; | 103 }; |
| 49 | 104 |
| 50 class MockLogSource { | 105 class MockLogSource { |
| 51 public: | 106 public: |
| 52 MOCK_METHOD0(Log, const char*()); | 107 MOCK_METHOD0(Log, const char*()); |
| 53 }; | 108 }; |
| 54 | 109 |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 227 | 282 |
| 228 TEST_F(LoggingTest, DcheckReleaseBehavior) { | 283 TEST_F(LoggingTest, DcheckReleaseBehavior) { |
| 229 int some_variable = 1; | 284 int some_variable = 1; |
| 230 // These should still reference |some_variable| so we don't get | 285 // These should still reference |some_variable| so we don't get |
| 231 // unused variable warnings. | 286 // unused variable warnings. |
| 232 DCHECK(some_variable) << "test"; | 287 DCHECK(some_variable) << "test"; |
| 233 DPCHECK(some_variable) << "test"; | 288 DPCHECK(some_variable) << "test"; |
| 234 DCHECK_EQ(some_variable, 1) << "test"; | 289 DCHECK_EQ(some_variable, 1) << "test"; |
| 235 } | 290 } |
| 236 | 291 |
| 292 // Test that DCHECK acts as a single statement in variety of syntactic | |
| 293 // situations. | |
| 294 TEST_F(LoggingTest, DCheckStatements) { | |
| 295 // The next three blocks validate that the DCHECK is correctly bound to the | |
| 296 // 'if' statement. | |
| 297 { | |
| 298 ExpectNoLogs expect_no_logs; | |
| 299 | |
| 300 // Unreached DCHECK. | |
| 301 if (false) | |
| 302 DCHECK(UnreachedBoolExpr()); | |
| 303 } | |
| 304 | |
| 305 { | |
| 306 EvaluatedOnceIfDCheckIsOn bool_expr; | |
|
Sigurður Ásgeirsson
2015/07/16 10:51:28
nit: IMHO the use of this would be marginally more
| |
| 307 ExpectNoLogs expect_no_logs; | |
| 308 | |
| 309 // Reached, successful DCHECK. | |
| 310 if (true) | |
| 311 DCHECK(bool_expr()); | |
| 312 } | |
| 313 | |
| 314 { | |
| 315 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 316 ExpectOneLogIfDCheckIsOn expect_one_dlog; | |
| 317 | |
| 318 // Reached, failed DCHECK. | |
| 319 if (true) | |
| 320 DCHECK(!bool_expr()); | |
| 321 } | |
| 322 | |
| 323 // The following four cases validate that the DCHECK is correctly bound in an | |
| 324 // 'if/else' statement. | |
| 325 { | |
| 326 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 327 ExpectNoLogs expect_no_logs; | |
| 328 | |
| 329 // Unreached if, successful else. | |
| 330 if (false) | |
| 331 DCHECK(UnreachedBoolExpr()); | |
| 332 else | |
| 333 DCHECK(bool_expr()); | |
| 334 } | |
| 335 | |
| 336 { | |
| 337 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 338 ExpectNoLogs expect_no_logs; | |
| 339 | |
| 340 // Successful if, unreached else. | |
| 341 if (true) | |
| 342 DCHECK(bool_expr()); | |
| 343 else | |
| 344 DCHECK(UnreachedBoolExpr()); | |
| 345 } | |
| 346 | |
| 347 { | |
| 348 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 349 ExpectOneLogIfDCheckIsOn expect_one_dlog; | |
| 350 | |
| 351 // Unreached if, failed else. | |
| 352 if (false) | |
| 353 DCHECK(UnreachedBoolExpr()); | |
| 354 else | |
| 355 DCHECK(!bool_expr()); | |
| 356 } | |
| 357 | |
| 358 { | |
| 359 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 360 ExpectOneLogIfDCheckIsOn expect_one_dlog; | |
| 361 | |
| 362 // Failed if, unreached else. | |
| 363 if (true) | |
| 364 DCHECK(!bool_expr()); | |
| 365 else | |
| 366 DCHECK(UnreachedBoolExpr()); | |
| 367 } | |
| 368 | |
| 369 // These cases validate that a DCHECK may appear in a switch statement. | |
| 370 { | |
| 371 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 372 ExpectNoLogs expect_no_logs; | |
| 373 switch (2) { | |
| 374 case 1: | |
| 375 DCHECK(UnreachedBoolExpr()); | |
| 376 case 2: | |
| 377 DCHECK(bool_expr()); | |
| 378 default: | |
| 379 break; | |
| 380 } | |
| 381 } | |
| 382 | |
| 383 { | |
| 384 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 385 ExpectOneLogIfDCheckIsOn expect_one_dlog; | |
| 386 switch (2) { | |
| 387 case 1: | |
| 388 DCHECK(UnreachedBoolExpr()); | |
| 389 case 2: | |
| 390 DCHECK(!bool_expr()); | |
| 391 default: | |
| 392 break; | |
| 393 } | |
| 394 } | |
| 395 } | |
| 396 | |
| 397 // Test that CHECK acts as a single statement in variety of syntactic | |
| 398 // situations. | |
| 399 TEST_F(LoggingTest, CheckStatements) { | |
| 400 // The next three blocks validate that the CHECK is correctly bound to the | |
| 401 // 'if' statement. | |
| 402 { | |
| 403 ExpectNoLogs expect_no_logs; | |
| 404 | |
| 405 // Unreached CHECK. | |
| 406 if (false) | |
| 407 CHECK(UnreachedBoolExpr()); | |
| 408 } | |
| 409 | |
| 410 { | |
| 411 EvaluatedOnce bool_expr; | |
| 412 ExpectNoLogs expect_no_logs; | |
| 413 | |
| 414 // Reached, successful CHECK. | |
| 415 if (true) | |
| 416 CHECK(bool_expr()); | |
| 417 } | |
| 418 | |
| 419 { | |
| 420 EvaluatedOnce bool_expr; | |
| 421 ExpectOneLog expect_one_log; | |
| 422 | |
| 423 // Reached, failed CHECK. | |
| 424 if (true) | |
| 425 CHECK(!bool_expr()); | |
| 426 } | |
| 427 | |
| 428 // The following four cases validate that the CHECK is correctly bound in an | |
| 429 // 'if/else' statement. | |
| 430 { | |
| 431 EvaluatedOnce bool_expr; | |
| 432 ExpectNoLogs expect_no_logs; | |
| 433 | |
| 434 // Unreached if, successful else. | |
| 435 if (false) | |
| 436 CHECK(UnreachedBoolExpr()); | |
| 437 else | |
| 438 CHECK(bool_expr()); | |
| 439 } | |
| 440 | |
| 441 { | |
| 442 EvaluatedOnce bool_expr; | |
| 443 ExpectNoLogs expect_no_logs; | |
| 444 | |
| 445 // Successful if, unreached else. | |
| 446 if (true) | |
| 447 CHECK(bool_expr()); | |
| 448 else | |
| 449 CHECK(UnreachedBoolExpr()); | |
| 450 } | |
| 451 | |
| 452 { | |
| 453 EvaluatedOnce bool_expr; | |
| 454 ExpectOneLog expect_one_log; | |
| 455 | |
| 456 // Unreached if, failed else. | |
| 457 if (false) | |
| 458 CHECK(UnreachedBoolExpr()); | |
| 459 else | |
| 460 CHECK(!bool_expr()); | |
| 461 } | |
| 462 | |
| 463 { | |
| 464 EvaluatedOnce bool_expr; | |
| 465 ExpectOneLog expect_one_log; | |
| 466 | |
| 467 // Failed if, unreached else. | |
| 468 if (true) | |
| 469 CHECK(!bool_expr()); | |
| 470 else | |
| 471 CHECK(UnreachedBoolExpr()); | |
| 472 } | |
| 473 | |
| 474 // These cases validate that a CHECK may appear in a switch statement. | |
| 475 { | |
| 476 EvaluatedOnce bool_expr; | |
| 477 ExpectNoLogs expect_no_logs; | |
| 478 switch (2) { | |
| 479 case 1: | |
| 480 CHECK(UnreachedBoolExpr()); | |
| 481 case 2: | |
| 482 CHECK(bool_expr()); | |
| 483 default: | |
| 484 break; | |
| 485 } | |
| 486 } | |
| 487 | |
| 488 { | |
| 489 EvaluatedOnce bool_expr; | |
| 490 ExpectOneLog expect_one_log; | |
| 491 switch (2) { | |
| 492 case 1: | |
| 493 CHECK(UnreachedBoolExpr()); | |
| 494 case 2: | |
| 495 CHECK(!bool_expr()); | |
| 496 default: | |
| 497 break; | |
| 498 } | |
| 499 } | |
| 500 } | |
| 501 | |
| 502 // Test that DCHECK_EQ acts as a single statement in variety of syntactic | |
| 503 // situations. | |
| 504 TEST_F(LoggingTest, DCheckEqStatements) { | |
| 505 // The next three blocks validate that the DCHECK_EQ is correctly bound to the | |
| 506 // 'if' statement. | |
| 507 { | |
| 508 ExpectNoLogs expect_no_logs; | |
| 509 | |
| 510 // Unreached DCHECK_EQ. | |
| 511 if (false) | |
| 512 DCHECK_EQ(false, UnreachedBoolExpr()); | |
| 513 } | |
| 514 | |
| 515 { | |
| 516 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 517 ExpectNoLogs expect_no_logs; | |
| 518 | |
| 519 // Reached, successful DCHECK_EQ. | |
| 520 if (true) | |
| 521 DCHECK_EQ(true, bool_expr()); | |
| 522 } | |
| 523 | |
| 524 { | |
| 525 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 526 ExpectOneLogIfDCheckIsOn expect_one_dlog; | |
| 527 | |
| 528 // Reached, failed DCHECK_EQ. | |
| 529 if (true) | |
| 530 DCHECK_EQ(false, bool_expr()); | |
| 531 } | |
| 532 | |
| 533 // The following four cases validate that the DCHECK_EQ is correctly bound in | |
| 534 // an 'if/else' statement. | |
| 535 { | |
| 536 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 537 ExpectNoLogs expect_no_logs; | |
| 538 | |
| 539 // Unreached if, successful else. | |
| 540 if (false) | |
| 541 DCHECK_EQ(false, UnreachedBoolExpr()); | |
| 542 else | |
| 543 DCHECK_EQ(true, bool_expr()); | |
| 544 } | |
| 545 | |
| 546 { | |
| 547 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 548 ExpectNoLogs expect_no_logs; | |
| 549 | |
| 550 // Successful if, unreached else. | |
| 551 if (true) | |
| 552 DCHECK_EQ(true, bool_expr()); | |
| 553 else | |
| 554 DCHECK_EQ(false, UnreachedBoolExpr()); | |
| 555 } | |
| 556 | |
| 557 { | |
| 558 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 559 ExpectOneLogIfDCheckIsOn expect_one_dlog; | |
| 560 | |
| 561 // Unreached if, failed else. | |
| 562 if (false) | |
| 563 DCHECK_EQ(false, UnreachedBoolExpr()); | |
| 564 else | |
| 565 DCHECK_EQ(false, bool_expr()); | |
| 566 } | |
| 567 | |
| 568 { | |
| 569 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 570 ExpectOneLogIfDCheckIsOn expect_one_dlog; | |
| 571 | |
| 572 // Failed if, unreached else. | |
| 573 if (true) | |
| 574 DCHECK_EQ(false, bool_expr()); | |
| 575 else | |
| 576 DCHECK_EQ(false, UnreachedBoolExpr()); | |
| 577 } | |
| 578 | |
| 579 // These cases validate that a DCHECK_EQ may appear in a switch statement. | |
| 580 { | |
| 581 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 582 ExpectNoLogs expect_no_logs; | |
| 583 switch (2) { | |
| 584 case 1: | |
| 585 DCHECK_EQ(false, UnreachedBoolExpr()); | |
| 586 case 2: | |
| 587 DCHECK_EQ(true, bool_expr()); | |
| 588 default: | |
| 589 break; | |
| 590 } | |
| 591 } | |
| 592 | |
| 593 { | |
| 594 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 595 ExpectOneLogIfDCheckIsOn expect_one_dlog; | |
| 596 switch (2) { | |
| 597 case 1: | |
| 598 DCHECK_EQ(false, UnreachedBoolExpr()); | |
| 599 case 2: | |
| 600 DCHECK_EQ(false, bool_expr()); | |
| 601 default: | |
| 602 break; | |
| 603 } | |
| 604 } | |
| 605 } | |
| 606 | |
| 607 // Test that CHECK_EQ acts as a single statement in variety of syntactic | |
| 608 // situations. | |
| 609 TEST_F(LoggingTest, CheckEqStatements) { | |
| 610 // The next three blocks validate that the CHECK_EQ is correctly bound to the | |
| 611 // 'if' statement. | |
| 612 { | |
| 613 ExpectNoLogs expect_no_logs; | |
| 614 | |
| 615 // Unreached CHECK_EQ. | |
| 616 if (false) | |
| 617 CHECK_EQ(false, UnreachedBoolExpr()); | |
| 618 } | |
| 619 | |
| 620 { | |
| 621 EvaluatedOnce bool_expr; | |
| 622 ExpectNoLogs expect_no_logs; | |
| 623 | |
| 624 // Reached, successful CHECK_EQ. | |
| 625 if (true) | |
| 626 CHECK_EQ(true, bool_expr()); | |
| 627 } | |
| 628 | |
| 629 { | |
| 630 EvaluatedOnce bool_expr; | |
| 631 ExpectOneLog expect_one_log; | |
| 632 | |
| 633 // Reached, failed CHECK_EQ. | |
| 634 if (true) | |
| 635 CHECK_EQ(false, bool_expr()); | |
| 636 } | |
| 637 | |
| 638 // The following four cases validate that the CHECK_EQ is correctly bound in | |
| 639 // an 'if/else' statement. | |
| 640 { | |
| 641 EvaluatedOnce bool_expr; | |
| 642 ExpectNoLogs expect_no_logs; | |
| 643 | |
| 644 // Unreached if, successful else. | |
| 645 if (false) | |
| 646 CHECK_EQ(false, UnreachedBoolExpr()); | |
| 647 else | |
| 648 CHECK_EQ(true, bool_expr()); | |
| 649 } | |
| 650 | |
| 651 { | |
| 652 EvaluatedOnce bool_expr; | |
| 653 ExpectNoLogs expect_no_logs; | |
| 654 | |
| 655 // Successful if, unreached else. | |
| 656 if (true) | |
| 657 CHECK_EQ(true, bool_expr()); | |
| 658 else | |
| 659 CHECK_EQ(false, UnreachedBoolExpr()); | |
| 660 } | |
| 661 | |
| 662 { | |
| 663 EvaluatedOnce bool_expr; | |
| 664 ExpectOneLog expect_one_log; | |
| 665 | |
| 666 // Unreached if, failed else. | |
| 667 if (false) | |
| 668 CHECK_EQ(false, UnreachedBoolExpr()); | |
| 669 else | |
| 670 CHECK_EQ(false, bool_expr()); | |
| 671 } | |
| 672 | |
| 673 { | |
| 674 EvaluatedOnce bool_expr; | |
| 675 ExpectOneLog expect_one_log; | |
| 676 | |
| 677 // Failed if, unreached else. | |
| 678 if (true) | |
| 679 CHECK_EQ(false, bool_expr()); | |
| 680 else | |
| 681 CHECK_EQ(false, UnreachedBoolExpr()); | |
| 682 } | |
| 683 | |
| 684 // These cases validate that a CHECK_EQ may appear in a switch statement. | |
| 685 { | |
| 686 EvaluatedOnce bool_expr; | |
| 687 ExpectNoLogs expect_no_logs; | |
| 688 switch (2) { | |
| 689 case 1: | |
| 690 CHECK_EQ(false, UnreachedBoolExpr()); | |
| 691 case 2: | |
| 692 CHECK_EQ(true, bool_expr()); | |
| 693 default: | |
| 694 break; | |
| 695 } | |
| 696 } | |
| 697 | |
| 698 { | |
| 699 EvaluatedOnce bool_expr; | |
| 700 ExpectOneLog expect_one_log; | |
| 701 switch (2) { | |
| 702 case 1: | |
| 703 CHECK_EQ(false, UnreachedBoolExpr()); | |
| 704 case 2: | |
| 705 CHECK_EQ(false, bool_expr()); | |
| 706 default: | |
| 707 break; | |
| 708 } | |
| 709 } | |
| 710 } | |
| 711 | |
| 712 // Test that DLOG_IF acts as a single statement in variety of syntactic | |
| 713 // situations. | |
| 714 TEST_F(LoggingTest, DLogIfStatements) { | |
| 715 // The next three blocks validate that the DLOG_IF is correctly bound to the | |
| 716 // 'if' statement. | |
| 717 { | |
| 718 ExpectNoLogs expect_no_logs; | |
| 719 | |
| 720 // Unreached DLOG_IF. | |
| 721 if (false) | |
| 722 DLOG_IF(FATAL, UnreachedBoolExpr()) << "message"; | |
| 723 } | |
| 724 | |
| 725 { | |
| 726 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 727 ExpectNoLogs expect_no_logs; | |
| 728 | |
| 729 // Reached, negative DLOG_IF. | |
| 730 if (true) | |
| 731 DLOG_IF(FATAL, !bool_expr()) << "message"; | |
| 732 } | |
| 733 | |
| 734 { | |
| 735 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 736 ExpectOneLogIfDCheckIsOn expect_one_dlog; | |
| 737 | |
| 738 // Reached, positive DLOG_IF. | |
| 739 if (true) | |
| 740 DLOG_IF(FATAL, bool_expr()) << "message"; | |
| 741 } | |
| 742 | |
| 743 // The following four cases validate that the DLOG_IF is correctly bound in an | |
| 744 // 'if/else' statement. | |
| 745 { | |
| 746 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 747 ExpectNoLogs expect_no_logs; | |
| 748 | |
| 749 // Unreached if, negative else. | |
| 750 if (false) | |
| 751 DLOG_IF(FATAL, UnreachedBoolExpr()) << "message"; | |
| 752 else | |
| 753 DLOG_IF(FATAL, !bool_expr()) << "message"; | |
| 754 } | |
| 755 | |
| 756 { | |
| 757 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 758 ExpectNoLogs expect_no_logs; | |
| 759 | |
| 760 // Negative if, unreached else. | |
| 761 if (true) | |
| 762 DLOG_IF(FATAL, !bool_expr()) << "message"; | |
| 763 else | |
| 764 DLOG_IF(FATAL, UnreachedBoolExpr()) << "message"; | |
| 765 } | |
| 766 | |
| 767 { | |
| 768 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 769 ExpectOneLogIfDCheckIsOn expect_one_dlog; | |
| 770 | |
| 771 // Unreached if, positive else. | |
| 772 if (false) | |
| 773 DLOG_IF(FATAL, UnreachedBoolExpr()) << "message"; | |
| 774 else | |
| 775 DLOG_IF(FATAL, bool_expr()) << "message"; | |
| 776 } | |
| 777 | |
| 778 { | |
| 779 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 780 ExpectOneLogIfDCheckIsOn expect_one_dlog; | |
| 781 | |
| 782 // Positive if, unreached else. | |
| 783 if (true) | |
| 784 DLOG_IF(FATAL, bool_expr()) << "message"; | |
| 785 else | |
| 786 DLOG_IF(FATAL, UnreachedBoolExpr()) << "message"; | |
| 787 } | |
| 788 | |
| 789 // These cases validate that a DLOG_IF may appear in a switch statement. | |
| 790 { | |
| 791 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 792 ExpectNoLogs expect_no_logs; | |
| 793 switch (2) { | |
| 794 case 1: | |
| 795 DLOG_IF(FATAL, UnreachedBoolExpr()) << "message"; | |
| 796 case 2: | |
| 797 DLOG_IF(FATAL, !bool_expr()) << "message"; | |
| 798 default: | |
| 799 break; | |
| 800 } | |
| 801 } | |
| 802 | |
| 803 { | |
| 804 EvaluatedOnceIfDCheckIsOn bool_expr; | |
| 805 ExpectOneLogIfDCheckIsOn expect_one_dlog; | |
| 806 switch (2) { | |
| 807 case 1: | |
| 808 DLOG_IF(FATAL, UnreachedBoolExpr()) << "message"; | |
| 809 case 2: | |
| 810 DLOG_IF(FATAL, bool_expr()) << "message"; | |
| 811 default: | |
| 812 break; | |
| 813 } | |
| 814 } | |
| 815 } | |
| 816 | |
| 817 // Test that LOG_IF acts as a single statement in variety of syntactic | |
| 818 // situations. | |
| 819 TEST_F(LoggingTest, LogIfStatements) { | |
| 820 // The next three blocks validate that the LOG_IF is correctly bound to the | |
| 821 // 'if' statement. | |
| 822 { | |
| 823 ExpectNoLogs expect_no_logs; | |
| 824 | |
| 825 // Unreached LOG_IF. | |
| 826 if (false) | |
| 827 LOG_IF(FATAL, UnreachedBoolExpr()) << "message"; | |
| 828 } | |
| 829 | |
| 830 { | |
| 831 EvaluatedOnce bool_expr; | |
| 832 ExpectNoLogs expect_no_logs; | |
| 833 | |
| 834 // Reached, negative LOG_IF. | |
| 835 if (true) | |
| 836 LOG_IF(FATAL, !bool_expr()) << "message"; | |
| 837 } | |
| 838 | |
| 839 { | |
| 840 EvaluatedOnce bool_expr; | |
| 841 ExpectOneLog expect_one_log; | |
| 842 | |
| 843 // Reached, positive LOG_IF. | |
| 844 if (true) | |
| 845 LOG_IF(FATAL, bool_expr()) << "message"; | |
| 846 } | |
| 847 | |
| 848 // The following four cases validate that the LOG_IF is correctly bound in an | |
| 849 // 'if/else' statement. | |
| 850 { | |
| 851 EvaluatedOnce bool_expr; | |
| 852 ExpectNoLogs expect_no_logs; | |
| 853 | |
| 854 // Unreached if, negative else. | |
| 855 if (false) | |
| 856 LOG_IF(FATAL, UnreachedBoolExpr()) << "message"; | |
| 857 else | |
| 858 LOG_IF(FATAL, !bool_expr()) << "message"; | |
| 859 } | |
| 860 | |
| 861 { | |
| 862 EvaluatedOnce bool_expr; | |
| 863 ExpectNoLogs expect_no_logs; | |
| 864 | |
| 865 // Negative if, unreached else. | |
| 866 if (true) | |
| 867 LOG_IF(FATAL, !bool_expr()) << "message"; | |
| 868 else | |
| 869 LOG_IF(FATAL, UnreachedBoolExpr()) << "message"; | |
| 870 } | |
| 871 | |
| 872 { | |
| 873 EvaluatedOnce bool_expr; | |
| 874 ExpectOneLog expect_one_log; | |
| 875 | |
| 876 // Unreached if, positive else. | |
| 877 if (false) | |
| 878 LOG_IF(FATAL, UnreachedBoolExpr()) << "message"; | |
| 879 else | |
| 880 LOG_IF(FATAL, bool_expr()) << "message"; | |
| 881 } | |
| 882 | |
| 883 { | |
| 884 EvaluatedOnce bool_expr; | |
| 885 ExpectOneLog expect_one_log; | |
| 886 | |
| 887 // Positive if, unreached else. | |
| 888 if (true) | |
| 889 LOG_IF(FATAL, bool_expr()) << "message"; | |
| 890 else | |
| 891 LOG_IF(FATAL, UnreachedBoolExpr()) << "message"; | |
| 892 } | |
| 893 | |
| 894 // These cases validate that a LOG_IF may appear in a switch statement. | |
| 895 { | |
| 896 EvaluatedOnce bool_expr; | |
| 897 ExpectNoLogs expect_no_logs; | |
| 898 switch (2) { | |
| 899 case 1: | |
| 900 LOG_IF(FATAL, UnreachedBoolExpr()) << "message"; | |
| 901 case 2: | |
| 902 LOG_IF(FATAL, !bool_expr()) << "message"; | |
| 903 default: | |
| 904 break; | |
| 905 } | |
| 906 } | |
| 907 | |
| 908 { | |
| 909 EvaluatedOnce bool_expr; | |
| 910 ExpectOneLog expect_one_log; | |
| 911 switch (2) { | |
| 912 case 1: | |
| 913 LOG_IF(FATAL, UnreachedBoolExpr()) << "message"; | |
| 914 case 2: | |
| 915 LOG_IF(FATAL, bool_expr()) << "message"; | |
| 916 default: | |
| 917 break; | |
| 918 } | |
| 919 } | |
| 920 } | |
| 921 | |
| 237 // Test that defining an operator<< for a type in a namespace doesn't prevent | 922 // Test that defining an operator<< for a type in a namespace doesn't prevent |
| 238 // other code in that namespace from calling the operator<<(ostream, wstring) | 923 // other code in that namespace from calling the operator<<(ostream, wstring) |
| 239 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be | 924 // defined by logging.h. This can fail if operator<<(ostream, wstring) can't be |
| 240 // found by ADL, since defining another operator<< prevents name lookup from | 925 // found by ADL, since defining another operator<< prevents name lookup from |
| 241 // looking in the global namespace. | 926 // looking in the global namespace. |
| 242 namespace nested_test { | 927 namespace nested_test { |
| 243 class Streamable {}; | 928 class Streamable {}; |
| 244 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, | 929 ALLOW_UNUSED_TYPE std::ostream& operator<<(std::ostream& out, |
| 245 const Streamable&) { | 930 const Streamable&) { |
| 246 return out << "Streamable"; | 931 return out << "Streamable"; |
| 247 } | 932 } |
| 248 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { | 933 TEST_F(LoggingTest, StreamingWstringFindsCorrectOperator) { |
| 249 std::wstring wstr = L"Hello World"; | 934 std::wstring wstr = L"Hello World"; |
| 250 std::ostringstream ostr; | 935 std::ostringstream ostr; |
| 251 ostr << wstr; | 936 ostr << wstr; |
| 252 EXPECT_EQ("Hello World", ostr.str()); | 937 EXPECT_EQ("Hello World", ostr.str()); |
| 253 } | 938 } |
| 254 } // namespace nested_test | 939 } // namespace nested_test |
| 255 | 940 |
| 256 } // namespace | 941 } // namespace |
| 257 | 942 |
| 258 } // namespace logging | 943 } // namespace logging |
| OLD | NEW |