1 // Copyright 2005, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 8 // * Redistributions of source code must retain the above copyright 9 // notice, this list of conditions and the following disclaimer. 10 // * Redistributions in binary form must reproduce the above 11 // copyright notice, this list of conditions and the following disclaimer 12 // in the documentation and/or other materials provided with the 13 // distribution. 14 // * Neither the name of Google Inc. nor the names of its 15 // contributors may be used to endorse or promote products derived from 16 // this software without specific prior written permission. 17 // 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 // 30 // The purpose of this file is to generate Google Test output under 31 // various conditions. The output will then be verified by 32 // googletest-output-test.py to ensure that Google Test generates the 33 // desired messages. Therefore, most tests in this file are MEANT TO 34 // FAIL. 35 36 #include "gtest/gtest-spi.h" 37 #include "gtest/gtest.h" 38 #include "src/gtest-internal-inl.h" 39 40 #include <stdlib.h> 41 42 #if _MSC_VER 43 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */) 44 #endif // _MSC_VER 45 46 #if GTEST_IS_THREADSAFE 47 using testing::ScopedFakeTestPartResultReporter; 48 using testing::TestPartResultArray; 49 50 using testing::internal::Notification; 51 using testing::internal::ThreadWithParam; 52 #endif 53 54 namespace posix = ::testing::internal::posix; 55 56 // Tests catching fatal failures. 57 58 // A subroutine used by the following test. 59 void TestEq1(int x) { 60 ASSERT_EQ(1, x); 61 } 62 63 // This function calls a test subroutine, catches the fatal failure it 64 // generates, and then returns early. 65 void TryTestSubroutine() { 66 // Calls a subrountine that yields a fatal failure. 67 TestEq1(2); 68 69 // Catches the fatal failure and aborts the test. 70 // 71 // The testing::Test:: prefix is necessary when calling 72 // HasFatalFailure() outside of a TEST, TEST_F, or test fixture. 73 if (testing::Test::HasFatalFailure()) return; 74 75 // If we get here, something is wrong. 76 FAIL() << "This should never be reached."; 77 } 78 79 TEST(PassingTest, PassingTest1) { 80 } 81 82 TEST(PassingTest, PassingTest2) { 83 } 84 85 // Tests that parameters of failing parameterized tests are printed in the 86 // failing test summary. 87 class FailingParamTest : public testing::TestWithParam<int> {}; 88 89 TEST_P(FailingParamTest, Fails) { 90 EXPECT_EQ(1, GetParam()); 91 } 92 93 // This generates a test which will fail. Google Test is expected to print 94 // its parameter when it outputs the list of all failed tests. 95 INSTANTIATE_TEST_CASE_P(PrintingFailingParams, 96 FailingParamTest, 97 testing::Values(2)); 98 99 static const char kGoldenString[] = "\"Line\0 1\"\nLine 2"; 100 101 TEST(NonfatalFailureTest, EscapesStringOperands) { 102 std::string actual = "actual \"string\""; 103 EXPECT_EQ(kGoldenString, actual); 104 105 const char* golden = kGoldenString; 106 EXPECT_EQ(golden, actual); 107 } 108 109 TEST(NonfatalFailureTest, DiffForLongStrings) { 110 std::string golden_str(kGoldenString, sizeof(kGoldenString) - 1); 111 EXPECT_EQ(golden_str, "Line 2"); 112 } 113 114 // Tests catching a fatal failure in a subroutine. 115 TEST(FatalFailureTest, FatalFailureInSubroutine) { 116 printf("(expecting a failure that x should be 1)\n"); 117 118 TryTestSubroutine(); 119 } 120 121 // Tests catching a fatal failure in a nested subroutine. 122 TEST(FatalFailureTest, FatalFailureInNestedSubroutine) { 123 printf("(expecting a failure that x should be 1)\n"); 124 125 // Calls a subrountine that yields a fatal failure. 126 TryTestSubroutine(); 127 128 // Catches the fatal failure and aborts the test. 129 // 130 // When calling HasFatalFailure() inside a TEST, TEST_F, or test 131 // fixture, the testing::Test:: prefix is not needed. 132 if (HasFatalFailure()) return; 133 134 // If we get here, something is wrong. 135 FAIL() << "This should never be reached."; 136 } 137 138 // Tests HasFatalFailure() after a failed EXPECT check. 139 TEST(FatalFailureTest, NonfatalFailureInSubroutine) { 140 printf("(expecting a failure on false)\n"); 141 EXPECT_TRUE(false); // Generates a nonfatal failure 142 ASSERT_FALSE(HasFatalFailure()); // This should succeed. 143 } 144 145 // Tests interleaving user logging and Google Test assertions. 146 TEST(LoggingTest, InterleavingLoggingAndAssertions) { 147 static const int a[4] = { 148 3, 9, 2, 6 149 }; 150 151 printf("(expecting 2 failures on (3) >= (a[i]))\n"); 152 for (int i = 0; i < static_cast<int>(sizeof(a)/sizeof(*a)); i++) { 153 printf("i == %d\n", i); 154 EXPECT_GE(3, a[i]); 155 } 156 } 157 158 // Tests the SCOPED_TRACE macro. 159 160 // A helper function for testing SCOPED_TRACE. 161 void SubWithoutTrace(int n) { 162 EXPECT_EQ(1, n); 163 ASSERT_EQ(2, n); 164 } 165 166 // Another helper function for testing SCOPED_TRACE. 167 void SubWithTrace(int n) { 168 SCOPED_TRACE(testing::Message() << "n = " << n); 169 170 SubWithoutTrace(n); 171 } 172 173 TEST(SCOPED_TRACETest, AcceptedValues) { 174 SCOPED_TRACE("literal string"); 175 SCOPED_TRACE(std::string("std::string")); 176 SCOPED_TRACE(1337); // streamable type 177 const char* null_value = NULL; 178 SCOPED_TRACE(null_value); 179 180 ADD_FAILURE() << "Just checking that all these values work fine."; 181 } 182 183 // Tests that SCOPED_TRACE() obeys lexical scopes. 184 TEST(SCOPED_TRACETest, ObeysScopes) { 185 printf("(expected to fail)\n"); 186 187 // There should be no trace before SCOPED_TRACE() is invoked. 188 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace."; 189 190 { 191 SCOPED_TRACE("Expected trace"); 192 // After SCOPED_TRACE(), a failure in the current scope should contain 193 // the trace. 194 ADD_FAILURE() << "This failure is expected, and should have a trace."; 195 } 196 197 // Once the control leaves the scope of the SCOPED_TRACE(), there 198 // should be no trace again. 199 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace."; 200 } 201 202 // Tests that SCOPED_TRACE works inside a loop. 203 TEST(SCOPED_TRACETest, WorksInLoop) { 204 printf("(expected to fail)\n"); 205 206 for (int i = 1; i <= 2; i++) { 207 SCOPED_TRACE(testing::Message() << "i = " << i); 208 209 SubWithoutTrace(i); 210 } 211 } 212 213 // Tests that SCOPED_TRACE works in a subroutine. 214 TEST(SCOPED_TRACETest, WorksInSubroutine) { 215 printf("(expected to fail)\n"); 216 217 SubWithTrace(1); 218 SubWithTrace(2); 219 } 220 221 // Tests that SCOPED_TRACE can be nested. 222 TEST(SCOPED_TRACETest, CanBeNested) { 223 printf("(expected to fail)\n"); 224 225 SCOPED_TRACE(""); // A trace without a message. 226 227 SubWithTrace(2); 228 } 229 230 // Tests that multiple SCOPED_TRACEs can be used in the same scope. 231 TEST(SCOPED_TRACETest, CanBeRepeated) { 232 printf("(expected to fail)\n"); 233 234 SCOPED_TRACE("A"); 235 ADD_FAILURE() 236 << "This failure is expected, and should contain trace point A."; 237 238 SCOPED_TRACE("B"); 239 ADD_FAILURE() 240 << "This failure is expected, and should contain trace point A and B."; 241 242 { 243 SCOPED_TRACE("C"); 244 ADD_FAILURE() << "This failure is expected, and should " 245 << "contain trace point A, B, and C."; 246 } 247 248 SCOPED_TRACE("D"); 249 ADD_FAILURE() << "This failure is expected, and should " 250 << "contain trace point A, B, and D."; 251 } 252 253 #if GTEST_IS_THREADSAFE 254 // Tests that SCOPED_TRACE()s can be used concurrently from multiple 255 // threads. Namely, an assertion should be affected by 256 // SCOPED_TRACE()s in its own thread only. 257 258 // Here's the sequence of actions that happen in the test: 259 // 260 // Thread A (main) | Thread B (spawned) 261 // ===============================|================================ 262 // spawns thread B | 263 // -------------------------------+-------------------------------- 264 // waits for n1 | SCOPED_TRACE("Trace B"); 265 // | generates failure #1 266 // | notifies n1 267 // -------------------------------+-------------------------------- 268 // SCOPED_TRACE("Trace A"); | waits for n2 269 // generates failure #2 | 270 // notifies n2 | 271 // -------------------------------|-------------------------------- 272 // waits for n3 | generates failure #3 273 // | trace B dies 274 // | generates failure #4 275 // | notifies n3 276 // -------------------------------|-------------------------------- 277 // generates failure #5 | finishes 278 // trace A dies | 279 // generates failure #6 | 280 // -------------------------------|-------------------------------- 281 // waits for thread B to finish | 282 283 struct CheckPoints { 284 Notification n1; 285 Notification n2; 286 Notification n3; 287 }; 288 289 static void ThreadWithScopedTrace(CheckPoints* check_points) { 290 { 291 SCOPED_TRACE("Trace B"); 292 ADD_FAILURE() 293 << "Expected failure #1 (in thread B, only trace B alive)."; 294 check_points->n1.Notify(); 295 check_points->n2.WaitForNotification(); 296 297 ADD_FAILURE() 298 << "Expected failure #3 (in thread B, trace A & B both alive)."; 299 } // Trace B dies here. 300 ADD_FAILURE() 301 << "Expected failure #4 (in thread B, only trace A alive)."; 302 check_points->n3.Notify(); 303 } 304 305 TEST(SCOPED_TRACETest, WorksConcurrently) { 306 printf("(expecting 6 failures)\n"); 307 308 CheckPoints check_points; 309 ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace, 310 &check_points, 311 NULL); 312 check_points.n1.WaitForNotification(); 313 314 { 315 SCOPED_TRACE("Trace A"); 316 ADD_FAILURE() 317 << "Expected failure #2 (in thread A, trace A & B both alive)."; 318 check_points.n2.Notify(); 319 check_points.n3.WaitForNotification(); 320 321 ADD_FAILURE() 322 << "Expected failure #5 (in thread A, only trace A alive)."; 323 } // Trace A dies here. 324 ADD_FAILURE() 325 << "Expected failure #6 (in thread A, no trace alive)."; 326 thread.Join(); 327 } 328 #endif // GTEST_IS_THREADSAFE 329 330 // Tests basic functionality of the ScopedTrace utility (most of its features 331 // are already tested in SCOPED_TRACETest). 332 TEST(ScopedTraceTest, WithExplicitFileAndLine) { 333 testing::ScopedTrace trace("explicit_file.cc", 123, "expected trace message"); 334 ADD_FAILURE() << "Check that the trace is attached to a particular location."; 335 } 336 337 TEST(DisabledTestsWarningTest, 338 DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) { 339 // This test body is intentionally empty. Its sole purpose is for 340 // verifying that the --gtest_also_run_disabled_tests flag 341 // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of 342 // the test output. 343 } 344 345 // Tests using assertions outside of TEST and TEST_F. 346 // 347 // This function creates two failures intentionally. 348 void AdHocTest() { 349 printf("The non-test part of the code is expected to have 2 failures.\n\n"); 350 EXPECT_TRUE(false); 351 EXPECT_EQ(2, 3); 352 } 353 354 // Runs all TESTs, all TEST_Fs, and the ad hoc test. 355 int RunAllTests() { 356 AdHocTest(); 357 return RUN_ALL_TESTS(); 358 } 359 360 // Tests non-fatal failures in the fixture constructor. 361 class NonFatalFailureInFixtureConstructorTest : public testing::Test { 362 protected: 363 NonFatalFailureInFixtureConstructorTest() { 364 printf("(expecting 5 failures)\n"); 365 ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor."; 366 } 367 368 ~NonFatalFailureInFixtureConstructorTest() { 369 ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor."; 370 } 371 372 virtual void SetUp() { 373 ADD_FAILURE() << "Expected failure #2, in SetUp()."; 374 } 375 376 virtual void TearDown() { 377 ADD_FAILURE() << "Expected failure #4, in TearDown."; 378 } 379 }; 380 381 TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) { 382 ADD_FAILURE() << "Expected failure #3, in the test body."; 383 } 384 385 // Tests fatal failures in the fixture constructor. 386 class FatalFailureInFixtureConstructorTest : public testing::Test { 387 protected: 388 FatalFailureInFixtureConstructorTest() { 389 printf("(expecting 2 failures)\n"); 390 Init(); 391 } 392 393 ~FatalFailureInFixtureConstructorTest() { 394 ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor."; 395 } 396 397 virtual void SetUp() { 398 ADD_FAILURE() << "UNEXPECTED failure in SetUp(). " 399 << "We should never get here, as the test fixture c'tor " 400 << "had a fatal failure."; 401 } 402 403 virtual void TearDown() { 404 ADD_FAILURE() << "UNEXPECTED failure in TearDown(). " 405 << "We should never get here, as the test fixture c'tor " 406 << "had a fatal failure."; 407 } 408 409 private: 410 void Init() { 411 FAIL() << "Expected failure #1, in the test fixture c'tor."; 412 } 413 }; 414 415 TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) { 416 ADD_FAILURE() << "UNEXPECTED failure in the test body. " 417 << "We should never get here, as the test fixture c'tor " 418 << "had a fatal failure."; 419 } 420 421 // Tests non-fatal failures in SetUp(). 422 class NonFatalFailureInSetUpTest : public testing::Test { 423 protected: 424 virtual ~NonFatalFailureInSetUpTest() { 425 Deinit(); 426 } 427 428 virtual void SetUp() { 429 printf("(expecting 4 failures)\n"); 430 ADD_FAILURE() << "Expected failure #1, in SetUp()."; 431 } 432 433 virtual void TearDown() { 434 FAIL() << "Expected failure #3, in TearDown()."; 435 } 436 private: 437 void Deinit() { 438 FAIL() << "Expected failure #4, in the test fixture d'tor."; 439 } 440 }; 441 442 TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) { 443 FAIL() << "Expected failure #2, in the test function."; 444 } 445 446 // Tests fatal failures in SetUp(). 447 class FatalFailureInSetUpTest : public testing::Test { 448 protected: 449 virtual ~FatalFailureInSetUpTest() { 450 Deinit(); 451 } 452 453 virtual void SetUp() { 454 printf("(expecting 3 failures)\n"); 455 FAIL() << "Expected failure #1, in SetUp()."; 456 } 457 458 virtual void TearDown() { 459 FAIL() << "Expected failure #2, in TearDown()."; 460 } 461 private: 462 void Deinit() { 463 FAIL() << "Expected failure #3, in the test fixture d'tor."; 464 } 465 }; 466 467 TEST_F(FatalFailureInSetUpTest, FailureInSetUp) { 468 FAIL() << "UNEXPECTED failure in the test function. " 469 << "We should never get here, as SetUp() failed."; 470 } 471 472 TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) { 473 ADD_FAILURE_AT("foo.cc", 42) << "Expected failure in foo.cc"; 474 } 475 476 #if GTEST_IS_THREADSAFE 477 478 // A unary function that may die. 479 void DieIf(bool should_die) { 480 GTEST_CHECK_(!should_die) << " - death inside DieIf()."; 481 } 482 483 // Tests running death tests in a multi-threaded context. 484 485 // Used for coordination between the main and the spawn thread. 486 struct SpawnThreadNotifications { 487 SpawnThreadNotifications() {} 488 489 Notification spawn_thread_started; 490 Notification spawn_thread_ok_to_terminate; 491 492 private: 493 GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications); 494 }; 495 496 // The function to be executed in the thread spawn by the 497 // MultipleThreads test (below). 498 static void ThreadRoutine(SpawnThreadNotifications* notifications) { 499 // Signals the main thread that this thread has started. 500 notifications->spawn_thread_started.Notify(); 501 502 // Waits for permission to finish from the main thread. 503 notifications->spawn_thread_ok_to_terminate.WaitForNotification(); 504 } 505 506 // This is a death-test test, but it's not named with a DeathTest 507 // suffix. It starts threads which might interfere with later 508 // death tests, so it must run after all other death tests. 509 class DeathTestAndMultiThreadsTest : public testing::Test { 510 protected: 511 // Starts a thread and waits for it to begin. 512 virtual void SetUp() { 513 thread_.reset(new ThreadWithParam<SpawnThreadNotifications*>( 514 &ThreadRoutine, ¬ifications_, NULL)); 515 notifications_.spawn_thread_started.WaitForNotification(); 516 } 517 // Tells the thread to finish, and reaps it. 518 // Depending on the version of the thread library in use, 519 // a manager thread might still be left running that will interfere 520 // with later death tests. This is unfortunate, but this class 521 // cleans up after itself as best it can. 522 virtual void TearDown() { 523 notifications_.spawn_thread_ok_to_terminate.Notify(); 524 } 525 526 private: 527 SpawnThreadNotifications notifications_; 528 testing::internal::scoped_ptr<ThreadWithParam<SpawnThreadNotifications*> > 529 thread_; 530 }; 531 532 #endif // GTEST_IS_THREADSAFE 533 534 // The MixedUpTestCaseTest test case verifies that Google Test will fail a 535 // test if it uses a different fixture class than what other tests in 536 // the same test case use. It deliberately contains two fixture 537 // classes with the same name but defined in different namespaces. 538 539 // The MixedUpTestCaseWithSameTestNameTest test case verifies that 540 // when the user defines two tests with the same test case name AND 541 // same test name (but in different namespaces), the second test will 542 // fail. 543 544 namespace foo { 545 546 class MixedUpTestCaseTest : public testing::Test { 547 }; 548 549 TEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {} 550 TEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {} 551 552 class MixedUpTestCaseWithSameTestNameTest : public testing::Test { 553 }; 554 555 TEST_F(MixedUpTestCaseWithSameTestNameTest, 556 TheSecondTestWithThisNameShouldFail) {} 557 558 } // namespace foo 559 560 namespace bar { 561 562 class MixedUpTestCaseTest : public testing::Test { 563 }; 564 565 // The following two tests are expected to fail. We rely on the 566 // golden file to check that Google Test generates the right error message. 567 TEST_F(MixedUpTestCaseTest, ThisShouldFail) {} 568 TEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {} 569 570 class MixedUpTestCaseWithSameTestNameTest : public testing::Test { 571 }; 572 573 // Expected to fail. We rely on the golden file to check that Google Test 574 // generates the right error message. 575 TEST_F(MixedUpTestCaseWithSameTestNameTest, 576 TheSecondTestWithThisNameShouldFail) {} 577 578 } // namespace bar 579 580 // The following two test cases verify that Google Test catches the user 581 // error of mixing TEST and TEST_F in the same test case. The first 582 // test case checks the scenario where TEST_F appears before TEST, and 583 // the second one checks where TEST appears before TEST_F. 584 585 class TEST_F_before_TEST_in_same_test_case : public testing::Test { 586 }; 587 588 TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {} 589 590 // Expected to fail. We rely on the golden file to check that Google Test 591 // generates the right error message. 592 TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {} 593 594 class TEST_before_TEST_F_in_same_test_case : public testing::Test { 595 }; 596 597 TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {} 598 599 // Expected to fail. We rely on the golden file to check that Google Test 600 // generates the right error message. 601 TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) { 602 } 603 604 // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE(). 605 int global_integer = 0; 606 607 // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables. 608 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) { 609 global_integer = 0; 610 EXPECT_NONFATAL_FAILURE({ 611 EXPECT_EQ(1, global_integer) << "Expected non-fatal failure."; 612 }, "Expected non-fatal failure."); 613 } 614 615 // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables 616 // (static or not). 617 TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) { 618 int m = 0; 619 static int n; 620 n = 1; 621 EXPECT_NONFATAL_FAILURE({ 622 EXPECT_EQ(m, n) << "Expected non-fatal failure."; 623 }, "Expected non-fatal failure."); 624 } 625 626 // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly 627 // one non-fatal failure and no fatal failure. 628 TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) { 629 EXPECT_NONFATAL_FAILURE({ 630 ADD_FAILURE() << "Expected non-fatal failure."; 631 }, "Expected non-fatal failure."); 632 } 633 634 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no 635 // non-fatal failure. 636 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) { 637 printf("(expecting a failure)\n"); 638 EXPECT_NONFATAL_FAILURE({ 639 }, ""); 640 } 641 642 // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two 643 // non-fatal failures. 644 TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) { 645 printf("(expecting a failure)\n"); 646 EXPECT_NONFATAL_FAILURE({ 647 ADD_FAILURE() << "Expected non-fatal failure 1."; 648 ADD_FAILURE() << "Expected non-fatal failure 2."; 649 }, ""); 650 } 651 652 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal 653 // failure. 654 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) { 655 printf("(expecting a failure)\n"); 656 EXPECT_NONFATAL_FAILURE({ 657 FAIL() << "Expected fatal failure."; 658 }, ""); 659 } 660 661 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being 662 // tested returns. 663 TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) { 664 printf("(expecting a failure)\n"); 665 EXPECT_NONFATAL_FAILURE({ 666 return; 667 }, ""); 668 } 669 670 #if GTEST_HAS_EXCEPTIONS 671 672 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being 673 // tested throws. 674 TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) { 675 printf("(expecting a failure)\n"); 676 try { 677 EXPECT_NONFATAL_FAILURE({ 678 throw 0; 679 }, ""); 680 } catch(int) { // NOLINT 681 } 682 } 683 684 #endif // GTEST_HAS_EXCEPTIONS 685 686 // Tests that EXPECT_FATAL_FAILURE() can reference global variables. 687 TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) { 688 global_integer = 0; 689 EXPECT_FATAL_FAILURE({ 690 ASSERT_EQ(1, global_integer) << "Expected fatal failure."; 691 }, "Expected fatal failure."); 692 } 693 694 // Tests that EXPECT_FATAL_FAILURE() can reference local static 695 // variables. 696 TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) { 697 static int n; 698 n = 1; 699 EXPECT_FATAL_FAILURE({ 700 ASSERT_EQ(0, n) << "Expected fatal failure."; 701 }, "Expected fatal failure."); 702 } 703 704 // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly 705 // one fatal failure and no non-fatal failure. 706 TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) { 707 EXPECT_FATAL_FAILURE({ 708 FAIL() << "Expected fatal failure."; 709 }, "Expected fatal failure."); 710 } 711 712 // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal 713 // failure. 714 TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) { 715 printf("(expecting a failure)\n"); 716 EXPECT_FATAL_FAILURE({ 717 }, ""); 718 } 719 720 // A helper for generating a fatal failure. 721 void FatalFailure() { 722 FAIL() << "Expected fatal failure."; 723 } 724 725 // Tests that EXPECT_FATAL_FAILURE() fails when there are two 726 // fatal failures. 727 TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) { 728 printf("(expecting a failure)\n"); 729 EXPECT_FATAL_FAILURE({ 730 FatalFailure(); 731 FatalFailure(); 732 }, ""); 733 } 734 735 // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal 736 // failure. 737 TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) { 738 printf("(expecting a failure)\n"); 739 EXPECT_FATAL_FAILURE({ 740 ADD_FAILURE() << "Expected non-fatal failure."; 741 }, ""); 742 } 743 744 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being 745 // tested returns. 746 TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) { 747 printf("(expecting a failure)\n"); 748 EXPECT_FATAL_FAILURE({ 749 return; 750 }, ""); 751 } 752 753 #if GTEST_HAS_EXCEPTIONS 754 755 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being 756 // tested throws. 757 TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) { 758 printf("(expecting a failure)\n"); 759 try { 760 EXPECT_FATAL_FAILURE({ 761 throw 0; 762 }, ""); 763 } catch(int) { // NOLINT 764 } 765 } 766 767 #endif // GTEST_HAS_EXCEPTIONS 768 769 // This #ifdef block tests the output of value-parameterized tests. 770 771 std::string ParamNameFunc(const testing::TestParamInfo<std::string>& info) { 772 return info.param; 773 } 774 775 class ParamTest : public testing::TestWithParam<std::string> { 776 }; 777 778 TEST_P(ParamTest, Success) { 779 EXPECT_EQ("a", GetParam()); 780 } 781 782 TEST_P(ParamTest, Failure) { 783 EXPECT_EQ("b", GetParam()) << "Expected failure"; 784 } 785 786 INSTANTIATE_TEST_CASE_P(PrintingStrings, 787 ParamTest, 788 testing::Values(std::string("a")), 789 ParamNameFunc); 790 791 // This #ifdef block tests the output of typed tests. 792 #if GTEST_HAS_TYPED_TEST 793 794 template <typename T> 795 class TypedTest : public testing::Test { 796 }; 797 798 TYPED_TEST_CASE(TypedTest, testing::Types<int>); 799 800 TYPED_TEST(TypedTest, Success) { 801 EXPECT_EQ(0, TypeParam()); 802 } 803 804 TYPED_TEST(TypedTest, Failure) { 805 EXPECT_EQ(1, TypeParam()) << "Expected failure"; 806 } 807 808 typedef testing::Types<char, int> TypesForTestWithNames; 809 810 template <typename T> 811 class TypedTestWithNames : public testing::Test {}; 812 813 class TypedTestNames { 814 public: 815 template <typename T> 816 static std::string GetName(int i) { 817 if (testing::internal::IsSame<T, char>::value) 818 return std::string("char") + ::testing::PrintToString(i); 819 if (testing::internal::IsSame<T, int>::value) 820 return std::string("int") + ::testing::PrintToString(i); 821 } 822 }; 823 824 TYPED_TEST_CASE(TypedTestWithNames, TypesForTestWithNames, TypedTestNames); 825 826 TYPED_TEST(TypedTestWithNames, Success) {} 827 828 TYPED_TEST(TypedTestWithNames, Failure) { FAIL(); } 829 830 #endif // GTEST_HAS_TYPED_TEST 831 832 // This #ifdef block tests the output of type-parameterized tests. 833 #if GTEST_HAS_TYPED_TEST_P 834 835 template <typename T> 836 class TypedTestP : public testing::Test { 837 }; 838 839 TYPED_TEST_CASE_P(TypedTestP); 840 841 TYPED_TEST_P(TypedTestP, Success) { 842 EXPECT_EQ(0U, TypeParam()); 843 } 844 845 TYPED_TEST_P(TypedTestP, Failure) { 846 EXPECT_EQ(1U, TypeParam()) << "Expected failure"; 847 } 848 849 REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure); 850 851 typedef testing::Types<unsigned char, unsigned int> UnsignedTypes; 852 INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes); 853 854 class TypedTestPNames { 855 public: 856 template <typename T> 857 static std::string GetName(int i) { 858 if (testing::internal::IsSame<T, unsigned char>::value) { 859 return std::string("unsignedChar") + ::testing::PrintToString(i); 860 } 861 if (testing::internal::IsSame<T, unsigned int>::value) { 862 return std::string("unsignedInt") + ::testing::PrintToString(i); 863 } 864 } 865 }; 866 867 INSTANTIATE_TYPED_TEST_CASE_P(UnsignedCustomName, TypedTestP, UnsignedTypes, 868 TypedTestPNames); 869 870 #endif // GTEST_HAS_TYPED_TEST_P 871 872 #if GTEST_HAS_DEATH_TEST 873 874 // We rely on the golden file to verify that tests whose test case 875 // name ends with DeathTest are run first. 876 877 TEST(ADeathTest, ShouldRunFirst) { 878 } 879 880 # if GTEST_HAS_TYPED_TEST 881 882 // We rely on the golden file to verify that typed tests whose test 883 // case name ends with DeathTest are run first. 884 885 template <typename T> 886 class ATypedDeathTest : public testing::Test { 887 }; 888 889 typedef testing::Types<int, double> NumericTypes; 890 TYPED_TEST_CASE(ATypedDeathTest, NumericTypes); 891 892 TYPED_TEST(ATypedDeathTest, ShouldRunFirst) { 893 } 894 895 # endif // GTEST_HAS_TYPED_TEST 896 897 # if GTEST_HAS_TYPED_TEST_P 898 899 900 // We rely on the golden file to verify that type-parameterized tests 901 // whose test case name ends with DeathTest are run first. 902 903 template <typename T> 904 class ATypeParamDeathTest : public testing::Test { 905 }; 906 907 TYPED_TEST_CASE_P(ATypeParamDeathTest); 908 909 TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) { 910 } 911 912 REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst); 913 914 INSTANTIATE_TYPED_TEST_CASE_P(My, ATypeParamDeathTest, NumericTypes); 915 916 # endif // GTEST_HAS_TYPED_TEST_P 917 918 #endif // GTEST_HAS_DEATH_TEST 919 920 // Tests various failure conditions of 921 // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}. 922 class ExpectFailureTest : public testing::Test { 923 public: // Must be public and not protected due to a bug in g++ 3.4.2. 924 enum FailureMode { 925 FATAL_FAILURE, 926 NONFATAL_FAILURE 927 }; 928 static void AddFailure(FailureMode failure) { 929 if (failure == FATAL_FAILURE) { 930 FAIL() << "Expected fatal failure."; 931 } else { 932 ADD_FAILURE() << "Expected non-fatal failure."; 933 } 934 } 935 }; 936 937 TEST_F(ExpectFailureTest, ExpectFatalFailure) { 938 // Expected fatal failure, but succeeds. 939 printf("(expecting 1 failure)\n"); 940 EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure."); 941 // Expected fatal failure, but got a non-fatal failure. 942 printf("(expecting 1 failure)\n"); 943 EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal " 944 "failure."); 945 // Wrong message. 946 printf("(expecting 1 failure)\n"); 947 EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure " 948 "expected."); 949 } 950 951 TEST_F(ExpectFailureTest, ExpectNonFatalFailure) { 952 // Expected non-fatal failure, but succeeds. 953 printf("(expecting 1 failure)\n"); 954 EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure."); 955 // Expected non-fatal failure, but got a fatal failure. 956 printf("(expecting 1 failure)\n"); 957 EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure."); 958 // Wrong message. 959 printf("(expecting 1 failure)\n"); 960 EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal " 961 "failure."); 962 } 963 964 #if GTEST_IS_THREADSAFE 965 966 class ExpectFailureWithThreadsTest : public ExpectFailureTest { 967 protected: 968 static void AddFailureInOtherThread(FailureMode failure) { 969 ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL); 970 thread.Join(); 971 } 972 }; 973 974 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) { 975 // We only intercept the current thread. 976 printf("(expecting 2 failures)\n"); 977 EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE), 978 "Expected fatal failure."); 979 } 980 981 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) { 982 // We only intercept the current thread. 983 printf("(expecting 2 failures)\n"); 984 EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE), 985 "Expected non-fatal failure."); 986 } 987 988 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest; 989 990 // Tests that the ScopedFakeTestPartResultReporter only catches failures from 991 // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD. 992 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) { 993 printf("(expecting 2 failures)\n"); 994 TestPartResultArray results; 995 { 996 ScopedFakeTestPartResultReporter reporter( 997 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD, 998 &results); 999 AddFailureInOtherThread(FATAL_FAILURE); 1000 AddFailureInOtherThread(NONFATAL_FAILURE); 1001 } 1002 // The two failures should not have been intercepted. 1003 EXPECT_EQ(0, results.size()) << "This shouldn't fail."; 1004 } 1005 1006 #endif // GTEST_IS_THREADSAFE 1007 1008 TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) { 1009 // Expected fatal failure, but succeeds. 1010 printf("(expecting 1 failure)\n"); 1011 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure."); 1012 // Expected fatal failure, but got a non-fatal failure. 1013 printf("(expecting 1 failure)\n"); 1014 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE), 1015 "Expected non-fatal failure."); 1016 // Wrong message. 1017 printf("(expecting 1 failure)\n"); 1018 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), 1019 "Some other fatal failure expected."); 1020 } 1021 1022 TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) { 1023 // Expected non-fatal failure, but succeeds. 1024 printf("(expecting 1 failure)\n"); 1025 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal " 1026 "failure."); 1027 // Expected non-fatal failure, but got a fatal failure. 1028 printf("(expecting 1 failure)\n"); 1029 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), 1030 "Expected fatal failure."); 1031 // Wrong message. 1032 printf("(expecting 1 failure)\n"); 1033 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE), 1034 "Some other non-fatal failure."); 1035 } 1036 1037 1038 // Two test environments for testing testing::AddGlobalTestEnvironment(). 1039 1040 class FooEnvironment : public testing::Environment { 1041 public: 1042 virtual void SetUp() { 1043 printf("%s", "FooEnvironment::SetUp() called.\n"); 1044 } 1045 1046 virtual void TearDown() { 1047 printf("%s", "FooEnvironment::TearDown() called.\n"); 1048 FAIL() << "Expected fatal failure."; 1049 } 1050 }; 1051 1052 class BarEnvironment : public testing::Environment { 1053 public: 1054 virtual void SetUp() { 1055 printf("%s", "BarEnvironment::SetUp() called.\n"); 1056 } 1057 1058 virtual void TearDown() { 1059 printf("%s", "BarEnvironment::TearDown() called.\n"); 1060 ADD_FAILURE() << "Expected non-fatal failure."; 1061 } 1062 }; 1063 1064 // The main function. 1065 // 1066 // The idea is to use Google Test to run all the tests we have defined (some 1067 // of them are intended to fail), and then compare the test results 1068 // with the "golden" file. 1069 int main(int argc, char **argv) { 1070 testing::GTEST_FLAG(print_time) = false; 1071 1072 // We just run the tests, knowing some of them are intended to fail. 1073 // We will use a separate Python script to compare the output of 1074 // this program with the golden file. 1075 1076 // It's hard to test InitGoogleTest() directly, as it has many 1077 // global side effects. The following line serves as a sanity test 1078 // for it. 1079 testing::InitGoogleTest(&argc, argv); 1080 bool internal_skip_environment_and_ad_hoc_tests = 1081 std::count(argv, argv + argc, 1082 std::string("internal_skip_environment_and_ad_hoc_tests")) > 0; 1083 1084 #if GTEST_HAS_DEATH_TEST 1085 if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") { 1086 // Skip the usual output capturing if we're running as the child 1087 // process of an threadsafe-style death test. 1088 # if GTEST_OS_WINDOWS 1089 posix::FReopen("nul:", "w", stdout); 1090 # else 1091 posix::FReopen("/dev/null", "w", stdout); 1092 # endif // GTEST_OS_WINDOWS 1093 return RUN_ALL_TESTS(); 1094 } 1095 #endif // GTEST_HAS_DEATH_TEST 1096 1097 if (internal_skip_environment_and_ad_hoc_tests) 1098 return RUN_ALL_TESTS(); 1099 1100 // Registers two global test environments. 1101 // The golden file verifies that they are set up in the order they 1102 // are registered, and torn down in the reverse order. 1103 testing::AddGlobalTestEnvironment(new FooEnvironment); 1104 testing::AddGlobalTestEnvironment(new BarEnvironment); 1105 #if _MSC_VER 1106 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4127 1107 #endif // _MSC_VER 1108 return RunAllTests(); 1109 } 1110