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 <stdlib.h> 37 38 #include <algorithm> 39 #include <string> 40 41 #include "gtest/gtest-spi.h" 42 #include "gtest/gtest.h" 43 #include "src/gtest-internal-inl.h" 44 45 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */) 46 47 #ifdef GTEST_IS_THREADSAFE 48 using testing::ScopedFakeTestPartResultReporter; 49 using testing::TestPartResultArray; 50 51 using testing::internal::Notification; 52 using testing::internal::ThreadWithParam; 53 #endif 54 55 namespace posix = ::testing::internal::posix; 56 57 // Tests catching fatal failures. 58 59 // A subroutine used by the following test. 60 void TestEq1(int x) { ASSERT_EQ(1, x); } 61 62 // This function calls a test subroutine, catches the fatal failure it 63 // generates, and then returns early. 64 void TryTestSubroutine() { 65 // Calls a subrountine that yields a fatal failure. 66 TestEq1(2); 67 68 // Catches the fatal failure and aborts the test. 69 // 70 // The testing::Test:: prefix is necessary when calling 71 // HasFatalFailure() outside of a TEST, TEST_F, or test fixture. 72 if (testing::Test::HasFatalFailure()) return; 73 74 // If we get here, something is wrong. 75 FAIL() << "This should never be reached."; 76 } 77 78 TEST(PassingTest, PassingTest1) {} 79 80 TEST(PassingTest, PassingTest2) {} 81 82 // Tests that parameters of failing parameterized tests are printed in the 83 // failing test summary. 84 class FailingParamTest : public testing::TestWithParam<int> {}; 85 86 TEST_P(FailingParamTest, Fails) { EXPECT_EQ(1, GetParam()); } 87 88 // This generates a test which will fail. Google Test is expected to print 89 // its parameter when it outputs the list of all failed tests. 90 INSTANTIATE_TEST_SUITE_P(PrintingFailingParams, FailingParamTest, 91 testing::Values(2)); 92 93 // Tests that an empty value for the test suite basename yields just 94 // the test name without any prior / 95 class EmptyBasenameParamInst : public testing::TestWithParam<int> {}; 96 97 TEST_P(EmptyBasenameParamInst, Passes) { EXPECT_EQ(1, GetParam()); } 98 99 INSTANTIATE_TEST_SUITE_P(, EmptyBasenameParamInst, testing::Values(1)); 100 101 static const char kGoldenString[] = "\"Line\0 1\"\nLine 2"; 102 103 TEST(NonfatalFailureTest, EscapesStringOperands) { 104 std::string actual = "actual \"string\""; 105 EXPECT_EQ(kGoldenString, actual); 106 107 const char* golden = kGoldenString; 108 EXPECT_EQ(golden, actual); 109 } 110 111 TEST(NonfatalFailureTest, DiffForLongStrings) { 112 std::string golden_str(kGoldenString, sizeof(kGoldenString) - 1); 113 EXPECT_EQ(golden_str, "Line 2"); 114 } 115 116 // Tests catching a fatal failure in a subroutine. 117 TEST(FatalFailureTest, FatalFailureInSubroutine) { 118 printf("(expecting a failure that x should be 1)\n"); 119 120 TryTestSubroutine(); 121 } 122 123 // Tests catching a fatal failure in a nested subroutine. 124 TEST(FatalFailureTest, FatalFailureInNestedSubroutine) { 125 printf("(expecting a failure that x should be 1)\n"); 126 127 // Calls a subrountine that yields a fatal failure. 128 TryTestSubroutine(); 129 130 // Catches the fatal failure and aborts the test. 131 // 132 // When calling HasFatalFailure() inside a TEST, TEST_F, or test 133 // fixture, the testing::Test:: prefix is not needed. 134 if (HasFatalFailure()) return; 135 136 // If we get here, something is wrong. 137 FAIL() << "This should never be reached."; 138 } 139 140 // Tests HasFatalFailure() after a failed EXPECT check. 141 TEST(FatalFailureTest, NonfatalFailureInSubroutine) { 142 printf("(expecting a failure on false)\n"); 143 EXPECT_TRUE(false); // Generates a nonfatal failure 144 ASSERT_FALSE(HasFatalFailure()); // This should succeed. 145 } 146 147 // Tests interleaving user logging and Google Test assertions. 148 TEST(LoggingTest, InterleavingLoggingAndAssertions) { 149 static const int a[4] = {3, 9, 2, 6}; 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 = nullptr; 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 #ifdef 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() << "Expected failure #1 (in thread B, only trace B alive)."; 293 check_points->n1.Notify(); 294 check_points->n2.WaitForNotification(); 295 296 ADD_FAILURE() 297 << "Expected failure #3 (in thread B, trace A & B both alive)."; 298 } // Trace B dies here. 299 ADD_FAILURE() << "Expected failure #4 (in thread B, only trace A alive)."; 300 check_points->n3.Notify(); 301 } 302 303 TEST(SCOPED_TRACETest, WorksConcurrently) { 304 printf("(expecting 6 failures)\n"); 305 306 CheckPoints check_points; 307 ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace, &check_points, 308 nullptr); 309 check_points.n1.WaitForNotification(); 310 311 { 312 SCOPED_TRACE("Trace A"); 313 ADD_FAILURE() 314 << "Expected failure #2 (in thread A, trace A & B both alive)."; 315 check_points.n2.Notify(); 316 check_points.n3.WaitForNotification(); 317 318 ADD_FAILURE() << "Expected failure #5 (in thread A, only trace A alive)."; 319 } // Trace A dies here. 320 ADD_FAILURE() << "Expected failure #6 (in thread A, no trace alive)."; 321 thread.Join(); 322 } 323 #endif // GTEST_IS_THREADSAFE 324 325 // Tests basic functionality of the ScopedTrace utility (most of its features 326 // are already tested in SCOPED_TRACETest). 327 TEST(ScopedTraceTest, WithExplicitFileAndLine) { 328 testing::ScopedTrace trace("explicit_file.cc", 123, "expected trace message"); 329 ADD_FAILURE() << "Check that the trace is attached to a particular location."; 330 } 331 332 TEST(DisabledTestsWarningTest, 333 DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) { 334 // This test body is intentionally empty. Its sole purpose is for 335 // verifying that the --gtest_also_run_disabled_tests flag 336 // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of 337 // the test output. 338 } 339 340 // Tests using assertions outside of TEST and TEST_F. 341 // 342 // This function creates two failures intentionally. 343 void AdHocTest() { 344 printf("The non-test part of the code is expected to have 2 failures.\n\n"); 345 EXPECT_TRUE(false); 346 EXPECT_EQ(2, 3); 347 } 348 349 // Runs all TESTs, all TEST_Fs, and the ad hoc test. 350 int RunAllTests() { 351 AdHocTest(); 352 return RUN_ALL_TESTS(); 353 } 354 355 // Tests non-fatal failures in the fixture constructor. 356 class NonFatalFailureInFixtureConstructorTest : public testing::Test { 357 protected: 358 NonFatalFailureInFixtureConstructorTest() { 359 printf("(expecting 5 failures)\n"); 360 ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor."; 361 } 362 363 ~NonFatalFailureInFixtureConstructorTest() override { 364 ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor."; 365 } 366 367 void SetUp() override { ADD_FAILURE() << "Expected failure #2, in SetUp()."; } 368 369 void TearDown() override { 370 ADD_FAILURE() << "Expected failure #4, in TearDown."; 371 } 372 }; 373 374 TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) { 375 ADD_FAILURE() << "Expected failure #3, in the test body."; 376 } 377 378 // Tests fatal failures in the fixture constructor. 379 class FatalFailureInFixtureConstructorTest : public testing::Test { 380 protected: 381 FatalFailureInFixtureConstructorTest() { 382 printf("(expecting 2 failures)\n"); 383 Init(); 384 } 385 386 ~FatalFailureInFixtureConstructorTest() override { 387 ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor."; 388 } 389 390 void SetUp() override { 391 ADD_FAILURE() << "UNEXPECTED failure in SetUp(). " 392 << "We should never get here, as the test fixture c'tor " 393 << "had a fatal failure."; 394 } 395 396 void TearDown() override { 397 ADD_FAILURE() << "UNEXPECTED failure in TearDown(). " 398 << "We should never get here, as the test fixture c'tor " 399 << "had a fatal failure."; 400 } 401 402 private: 403 void Init() { FAIL() << "Expected failure #1, in the test fixture c'tor."; } 404 }; 405 406 TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) { 407 ADD_FAILURE() << "UNEXPECTED failure in the test body. " 408 << "We should never get here, as the test fixture c'tor " 409 << "had a fatal failure."; 410 } 411 412 // Tests non-fatal failures in SetUp(). 413 class NonFatalFailureInSetUpTest : public testing::Test { 414 protected: 415 ~NonFatalFailureInSetUpTest() override { Deinit(); } 416 417 void SetUp() override { 418 printf("(expecting 4 failures)\n"); 419 ADD_FAILURE() << "Expected failure #1, in SetUp()."; 420 } 421 422 void TearDown() override { FAIL() << "Expected failure #3, in TearDown()."; } 423 424 private: 425 void Deinit() { FAIL() << "Expected failure #4, in the test fixture d'tor."; } 426 }; 427 428 TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) { 429 FAIL() << "Expected failure #2, in the test function."; 430 } 431 432 // Tests fatal failures in SetUp(). 433 class FatalFailureInSetUpTest : public testing::Test { 434 protected: 435 ~FatalFailureInSetUpTest() override { Deinit(); } 436 437 void SetUp() override { 438 printf("(expecting 3 failures)\n"); 439 FAIL() << "Expected failure #1, in SetUp()."; 440 } 441 442 void TearDown() override { FAIL() << "Expected failure #2, in TearDown()."; } 443 444 private: 445 void Deinit() { FAIL() << "Expected failure #3, in the test fixture d'tor."; } 446 }; 447 448 TEST_F(FatalFailureInSetUpTest, FailureInSetUp) { 449 FAIL() << "UNEXPECTED failure in the test function. " 450 << "We should never get here, as SetUp() failed."; 451 } 452 453 TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) { 454 ADD_FAILURE_AT("foo.cc", 42) << "Expected nonfatal failure in foo.cc"; 455 } 456 457 TEST(GtestFailAtTest, MessageContainsSpecifiedFileAndLineNumber) { 458 GTEST_FAIL_AT("foo.cc", 42) << "Expected fatal failure in foo.cc"; 459 } 460 461 // The MixedUpTestSuiteTest test case verifies that Google Test will fail a 462 // test if it uses a different fixture class than what other tests in 463 // the same test case use. It deliberately contains two fixture 464 // classes with the same name but defined in different namespaces. 465 466 // The MixedUpTestSuiteWithSameTestNameTest test case verifies that 467 // when the user defines two tests with the same test case name AND 468 // same test name (but in different namespaces), the second test will 469 // fail. 470 471 namespace foo { 472 473 class MixedUpTestSuiteTest : public testing::Test {}; 474 475 TEST_F(MixedUpTestSuiteTest, FirstTestFromNamespaceFoo) {} 476 TEST_F(MixedUpTestSuiteTest, SecondTestFromNamespaceFoo) {} 477 478 class MixedUpTestSuiteWithSameTestNameTest : public testing::Test {}; 479 480 TEST_F(MixedUpTestSuiteWithSameTestNameTest, 481 TheSecondTestWithThisNameShouldFail) {} 482 483 } // namespace foo 484 485 namespace bar { 486 487 class MixedUpTestSuiteTest : public testing::Test {}; 488 489 // The following two tests are expected to fail. We rely on the 490 // golden file to check that Google Test generates the right error message. 491 TEST_F(MixedUpTestSuiteTest, ThisShouldFail) {} 492 TEST_F(MixedUpTestSuiteTest, ThisShouldFailToo) {} 493 494 class MixedUpTestSuiteWithSameTestNameTest : public testing::Test {}; 495 496 // Expected to fail. We rely on the golden file to check that Google Test 497 // generates the right error message. 498 TEST_F(MixedUpTestSuiteWithSameTestNameTest, 499 TheSecondTestWithThisNameShouldFail) {} 500 501 } // namespace bar 502 503 // The following two test cases verify that Google Test catches the user 504 // error of mixing TEST and TEST_F in the same test case. The first 505 // test case checks the scenario where TEST_F appears before TEST, and 506 // the second one checks where TEST appears before TEST_F. 507 508 class TEST_F_before_TEST_in_same_test_case : public testing::Test {}; 509 510 TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {} 511 512 // Expected to fail. We rely on the golden file to check that Google Test 513 // generates the right error message. 514 TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {} 515 516 class TEST_before_TEST_F_in_same_test_case : public testing::Test {}; 517 518 TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {} 519 520 // Expected to fail. We rely on the golden file to check that Google Test 521 // generates the right error message. 522 TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {} 523 524 // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE(). 525 int global_integer = 0; 526 527 // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables. 528 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) { 529 global_integer = 0; 530 EXPECT_NONFATAL_FAILURE( 531 { EXPECT_EQ(1, global_integer) << "Expected non-fatal failure."; }, 532 "Expected non-fatal failure."); 533 } 534 535 // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables 536 // (static or not). 537 TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) { 538 int m = 0; 539 static int n; 540 n = 1; 541 EXPECT_NONFATAL_FAILURE({ EXPECT_EQ(m, n) << "Expected non-fatal failure."; }, 542 "Expected non-fatal failure."); 543 } 544 545 // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly 546 // one non-fatal failure and no fatal failure. 547 TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) { 548 EXPECT_NONFATAL_FAILURE({ ADD_FAILURE() << "Expected non-fatal failure."; }, 549 "Expected non-fatal failure."); 550 } 551 552 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no 553 // non-fatal failure. 554 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) { 555 printf("(expecting a failure)\n"); 556 EXPECT_NONFATAL_FAILURE({}, ""); 557 } 558 559 // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two 560 // non-fatal failures. 561 TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) { 562 printf("(expecting a failure)\n"); 563 EXPECT_NONFATAL_FAILURE( 564 { 565 ADD_FAILURE() << "Expected non-fatal failure 1."; 566 ADD_FAILURE() << "Expected non-fatal failure 2."; 567 }, 568 ""); 569 } 570 571 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal 572 // failure. 573 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) { 574 printf("(expecting a failure)\n"); 575 EXPECT_NONFATAL_FAILURE({ FAIL() << "Expected fatal failure."; }, ""); 576 } 577 578 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being 579 // tested returns. 580 TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) { 581 printf("(expecting a failure)\n"); 582 EXPECT_NONFATAL_FAILURE({ return; }, ""); 583 } 584 585 #if GTEST_HAS_EXCEPTIONS 586 587 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being 588 // tested throws. 589 TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) { 590 printf("(expecting a failure)\n"); 591 try { 592 EXPECT_NONFATAL_FAILURE({ throw 0; }, ""); 593 } catch (int) { // NOLINT 594 } 595 } 596 597 #endif // GTEST_HAS_EXCEPTIONS 598 599 // Tests that EXPECT_FATAL_FAILURE() can reference global variables. 600 TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) { 601 global_integer = 0; 602 EXPECT_FATAL_FAILURE( 603 { ASSERT_EQ(1, global_integer) << "Expected fatal failure."; }, 604 "Expected fatal failure."); 605 } 606 607 // Tests that EXPECT_FATAL_FAILURE() can reference local static 608 // variables. 609 TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) { 610 static int n; 611 n = 1; 612 EXPECT_FATAL_FAILURE({ ASSERT_EQ(0, n) << "Expected fatal failure."; }, 613 "Expected fatal failure."); 614 } 615 616 // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly 617 // one fatal failure and no non-fatal failure. 618 TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) { 619 EXPECT_FATAL_FAILURE({ FAIL() << "Expected fatal failure."; }, 620 "Expected fatal failure."); 621 } 622 623 // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal 624 // failure. 625 TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) { 626 printf("(expecting a failure)\n"); 627 EXPECT_FATAL_FAILURE({}, ""); 628 } 629 630 // A helper for generating a fatal failure. 631 void FatalFailure() { FAIL() << "Expected fatal failure."; } 632 633 // Tests that EXPECT_FATAL_FAILURE() fails when there are two 634 // fatal failures. 635 TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) { 636 printf("(expecting a failure)\n"); 637 EXPECT_FATAL_FAILURE( 638 { 639 FatalFailure(); 640 FatalFailure(); 641 }, 642 ""); 643 } 644 645 // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal 646 // failure. 647 TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) { 648 printf("(expecting a failure)\n"); 649 EXPECT_FATAL_FAILURE({ ADD_FAILURE() << "Expected non-fatal failure."; }, ""); 650 } 651 652 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being 653 // tested returns. 654 TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) { 655 printf("(expecting a failure)\n"); 656 EXPECT_FATAL_FAILURE({ return; }, ""); 657 } 658 659 #if GTEST_HAS_EXCEPTIONS 660 661 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being 662 // tested throws. 663 TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) { 664 printf("(expecting a failure)\n"); 665 try { 666 EXPECT_FATAL_FAILURE({ throw 0; }, ""); 667 } catch (int) { // NOLINT 668 } 669 } 670 671 #endif // GTEST_HAS_EXCEPTIONS 672 673 // This #ifdef block tests the output of value-parameterized tests. 674 675 std::string ParamNameFunc(const testing::TestParamInfo<std::string>& info) { 676 return info.param; 677 } 678 679 class ParamTest : public testing::TestWithParam<std::string> {}; 680 681 TEST_P(ParamTest, Success) { EXPECT_EQ("a", GetParam()); } 682 683 TEST_P(ParamTest, Failure) { EXPECT_EQ("b", GetParam()) << "Expected failure"; } 684 685 INSTANTIATE_TEST_SUITE_P(PrintingStrings, ParamTest, 686 testing::Values(std::string("a")), ParamNameFunc); 687 688 // The case where a suite has INSTANTIATE_TEST_SUITE_P but not TEST_P. 689 using NoTests = ParamTest; 690 INSTANTIATE_TEST_SUITE_P(ThisIsOdd, NoTests, ::testing::Values("Hello")); 691 692 // fails under kErrorOnUninstantiatedParameterizedTest=true 693 class DetectNotInstantiatedTest : public testing::TestWithParam<int> {}; 694 TEST_P(DetectNotInstantiatedTest, Used) {} 695 696 // This would make the test failure from the above go away. 697 // INSTANTIATE_TEST_SUITE_P(Fix, DetectNotInstantiatedTest, testing::Values(1)); 698 699 template <typename T> 700 class TypedTest : public testing::Test {}; 701 702 TYPED_TEST_SUITE(TypedTest, testing::Types<int>); 703 704 TYPED_TEST(TypedTest, Success) { EXPECT_EQ(0, TypeParam()); } 705 706 TYPED_TEST(TypedTest, Failure) { 707 EXPECT_EQ(1, TypeParam()) << "Expected failure"; 708 } 709 710 typedef testing::Types<char, int> TypesForTestWithNames; 711 712 template <typename T> 713 class TypedTestWithNames : public testing::Test {}; 714 715 class TypedTestNames { 716 public: 717 template <typename T> 718 static std::string GetName(int i) { 719 if (std::is_same<T, char>::value) 720 return std::string("char") + ::testing::PrintToString(i); 721 if (std::is_same<T, int>::value) 722 return std::string("int") + ::testing::PrintToString(i); 723 } 724 }; 725 726 TYPED_TEST_SUITE(TypedTestWithNames, TypesForTestWithNames, TypedTestNames); 727 728 TYPED_TEST(TypedTestWithNames, Success) {} 729 730 TYPED_TEST(TypedTestWithNames, Failure) { FAIL(); } 731 732 template <typename T> 733 class TypedTestP : public testing::Test {}; 734 735 TYPED_TEST_SUITE_P(TypedTestP); 736 737 TYPED_TEST_P(TypedTestP, Success) { EXPECT_EQ(0U, TypeParam()); } 738 739 TYPED_TEST_P(TypedTestP, Failure) { 740 EXPECT_EQ(1U, TypeParam()) << "Expected failure"; 741 } 742 743 REGISTER_TYPED_TEST_SUITE_P(TypedTestP, Success, Failure); 744 745 typedef testing::Types<unsigned char, unsigned int> UnsignedTypes; 746 INSTANTIATE_TYPED_TEST_SUITE_P(Unsigned, TypedTestP, UnsignedTypes); 747 748 class TypedTestPNames { 749 public: 750 template <typename T> 751 static std::string GetName(int i) { 752 if (std::is_same<T, unsigned char>::value) { 753 return std::string("unsignedChar") + ::testing::PrintToString(i); 754 } 755 if (std::is_same<T, unsigned int>::value) { 756 return std::string("unsignedInt") + ::testing::PrintToString(i); 757 } 758 } 759 }; 760 761 INSTANTIATE_TYPED_TEST_SUITE_P(UnsignedCustomName, TypedTestP, UnsignedTypes, 762 TypedTestPNames); 763 764 template <typename T> 765 class DetectNotInstantiatedTypesTest : public testing::Test {}; 766 TYPED_TEST_SUITE_P(DetectNotInstantiatedTypesTest); 767 TYPED_TEST_P(DetectNotInstantiatedTypesTest, Used) { 768 TypeParam instantiate; 769 (void)instantiate; 770 } 771 REGISTER_TYPED_TEST_SUITE_P(DetectNotInstantiatedTypesTest, Used); 772 773 // kErrorOnUninstantiatedTypeParameterizedTest=true would make the above fail. 774 // Adding the following would make that test failure go away. 775 // 776 // typedef ::testing::Types<char, int, unsigned int> MyTypes; 777 // INSTANTIATE_TYPED_TEST_SUITE_P(All, DetectNotInstantiatedTypesTest, MyTypes); 778 779 #ifdef GTEST_HAS_DEATH_TEST 780 781 // We rely on the golden file to verify that tests whose test case 782 // name ends with DeathTest are run first. 783 784 TEST(ADeathTest, ShouldRunFirst) {} 785 786 // We rely on the golden file to verify that typed tests whose test 787 // case name ends with DeathTest are run first. 788 789 template <typename T> 790 class ATypedDeathTest : public testing::Test {}; 791 792 typedef testing::Types<int, double> NumericTypes; 793 TYPED_TEST_SUITE(ATypedDeathTest, NumericTypes); 794 795 TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {} 796 797 // We rely on the golden file to verify that type-parameterized tests 798 // whose test case name ends with DeathTest are run first. 799 800 template <typename T> 801 class ATypeParamDeathTest : public testing::Test {}; 802 803 TYPED_TEST_SUITE_P(ATypeParamDeathTest); 804 805 TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {} 806 807 REGISTER_TYPED_TEST_SUITE_P(ATypeParamDeathTest, ShouldRunFirst); 808 809 INSTANTIATE_TYPED_TEST_SUITE_P(My, ATypeParamDeathTest, NumericTypes); 810 811 #endif // GTEST_HAS_DEATH_TEST 812 813 // Tests various failure conditions of 814 // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}. 815 class ExpectFailureTest : public testing::Test { 816 public: // Must be public and not protected due to a bug in g++ 3.4.2. 817 enum FailureMode { FATAL_FAILURE, NONFATAL_FAILURE }; 818 static void AddFailure(FailureMode failure) { 819 if (failure == FATAL_FAILURE) { 820 FAIL() << "Expected fatal failure."; 821 } else { 822 ADD_FAILURE() << "Expected non-fatal failure."; 823 } 824 } 825 }; 826 827 TEST_F(ExpectFailureTest, ExpectFatalFailure) { 828 // Expected fatal failure, but succeeds. 829 printf("(expecting 1 failure)\n"); 830 EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure."); 831 // Expected fatal failure, but got a non-fatal failure. 832 printf("(expecting 1 failure)\n"); 833 EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), 834 "Expected non-fatal " 835 "failure."); 836 // Wrong message. 837 printf("(expecting 1 failure)\n"); 838 EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), 839 "Some other fatal failure " 840 "expected."); 841 } 842 843 TEST_F(ExpectFailureTest, ExpectNonFatalFailure) { 844 // Expected non-fatal failure, but succeeds. 845 printf("(expecting 1 failure)\n"); 846 EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure."); 847 // Expected non-fatal failure, but got a fatal failure. 848 printf("(expecting 1 failure)\n"); 849 EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure."); 850 // Wrong message. 851 printf("(expecting 1 failure)\n"); 852 EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), 853 "Some other non-fatal " 854 "failure."); 855 } 856 857 #ifdef GTEST_IS_THREADSAFE 858 859 class ExpectFailureWithThreadsTest : public ExpectFailureTest { 860 protected: 861 static void AddFailureInOtherThread(FailureMode failure) { 862 ThreadWithParam<FailureMode> thread(&AddFailure, failure, nullptr); 863 thread.Join(); 864 } 865 }; 866 867 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) { 868 // We only intercept the current thread. 869 printf("(expecting 2 failures)\n"); 870 EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE), 871 "Expected fatal failure."); 872 } 873 874 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) { 875 // We only intercept the current thread. 876 printf("(expecting 2 failures)\n"); 877 EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE), 878 "Expected non-fatal failure."); 879 } 880 881 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest; 882 883 // Tests that the ScopedFakeTestPartResultReporter only catches failures from 884 // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD. 885 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) { 886 printf("(expecting 2 failures)\n"); 887 TestPartResultArray results; 888 { 889 ScopedFakeTestPartResultReporter reporter( 890 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD, 891 &results); 892 AddFailureInOtherThread(FATAL_FAILURE); 893 AddFailureInOtherThread(NONFATAL_FAILURE); 894 } 895 // The two failures should not have been intercepted. 896 EXPECT_EQ(0, results.size()) << "This shouldn't fail."; 897 } 898 899 #endif // GTEST_IS_THREADSAFE 900 901 TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) { 902 // Expected fatal failure, but succeeds. 903 printf("(expecting 1 failure)\n"); 904 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure."); 905 // Expected fatal failure, but got a non-fatal failure. 906 printf("(expecting 1 failure)\n"); 907 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE), 908 "Expected non-fatal failure."); 909 // Wrong message. 910 printf("(expecting 1 failure)\n"); 911 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), 912 "Some other fatal failure expected."); 913 } 914 915 TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) { 916 // Expected non-fatal failure, but succeeds. 917 printf("(expecting 1 failure)\n"); 918 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), 919 "Expected non-fatal " 920 "failure."); 921 // Expected non-fatal failure, but got a fatal failure. 922 printf("(expecting 1 failure)\n"); 923 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), 924 "Expected fatal failure."); 925 // Wrong message. 926 printf("(expecting 1 failure)\n"); 927 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE), 928 "Some other non-fatal failure."); 929 } 930 931 class DynamicFixture : public testing::Test { 932 protected: 933 DynamicFixture() { printf("DynamicFixture()\n"); } 934 ~DynamicFixture() override { printf("~DynamicFixture()\n"); } 935 void SetUp() override { printf("DynamicFixture::SetUp\n"); } 936 void TearDown() override { printf("DynamicFixture::TearDown\n"); } 937 938 static void SetUpTestSuite() { printf("DynamicFixture::SetUpTestSuite\n"); } 939 static void TearDownTestSuite() { 940 printf("DynamicFixture::TearDownTestSuite\n"); 941 } 942 }; 943 944 template <bool Pass> 945 class DynamicTest : public DynamicFixture { 946 public: 947 void TestBody() override { EXPECT_TRUE(Pass); } 948 }; 949 950 auto dynamic_test = ( 951 // Register two tests with the same fixture correctly. 952 testing::RegisterTest( 953 "DynamicFixture", "DynamicTestPass", nullptr, nullptr, __FILE__, 954 __LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }), 955 testing::RegisterTest( 956 "DynamicFixture", "DynamicTestFail", nullptr, nullptr, __FILE__, 957 __LINE__, []() -> DynamicFixture* { return new DynamicTest<false>; }), 958 959 // Register the same fixture with another name. That's fine. 960 testing::RegisterTest( 961 "DynamicFixtureAnotherName", "DynamicTestPass", nullptr, nullptr, 962 __FILE__, __LINE__, 963 []() -> DynamicFixture* { return new DynamicTest<true>; }), 964 965 // Register two tests with the same fixture incorrectly. 966 testing::RegisterTest( 967 "BadDynamicFixture1", "FixtureBase", nullptr, nullptr, __FILE__, 968 __LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }), 969 testing::RegisterTest( 970 "BadDynamicFixture1", "TestBase", nullptr, nullptr, __FILE__, __LINE__, 971 []() -> testing::Test* { return new DynamicTest<true>; }), 972 973 // Register two tests with the same fixture incorrectly by omitting the 974 // return type. 975 testing::RegisterTest( 976 "BadDynamicFixture2", "FixtureBase", nullptr, nullptr, __FILE__, 977 __LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }), 978 testing::RegisterTest("BadDynamicFixture2", "Derived", nullptr, nullptr, 979 __FILE__, __LINE__, 980 []() { return new DynamicTest<true>; })); 981 982 // Two test environments for testing testing::AddGlobalTestEnvironment(). 983 984 class FooEnvironment : public testing::Environment { 985 public: 986 void SetUp() override { printf("%s", "FooEnvironment::SetUp() called.\n"); } 987 988 void TearDown() override { 989 printf("%s", "FooEnvironment::TearDown() called.\n"); 990 FAIL() << "Expected fatal failure."; 991 } 992 }; 993 994 class BarEnvironment : public testing::Environment { 995 public: 996 void SetUp() override { printf("%s", "BarEnvironment::SetUp() called.\n"); } 997 998 void TearDown() override { 999 printf("%s", "BarEnvironment::TearDown() called.\n"); 1000 ADD_FAILURE() << "Expected non-fatal failure."; 1001 } 1002 }; 1003 1004 class TestSuiteThatFailsToSetUp : public testing::Test { 1005 public: 1006 static void SetUpTestSuite() { EXPECT_TRUE(false); } 1007 }; 1008 TEST_F(TestSuiteThatFailsToSetUp, ShouldNotRun) { std::abort(); } 1009 1010 class TestSuiteThatSkipsInSetUp : public testing::Test { 1011 public: 1012 static void SetUpTestSuite() { GTEST_SKIP() << "Skip entire test suite"; } 1013 }; 1014 TEST_F(TestSuiteThatSkipsInSetUp, ShouldNotRun) { std::abort(); } 1015 1016 // The main function. 1017 // 1018 // The idea is to use Google Test to run all the tests we have defined (some 1019 // of them are intended to fail), and then compare the test results 1020 // with the "golden" file. 1021 int main(int argc, char** argv) { 1022 GTEST_FLAG_SET(print_time, false); 1023 1024 // We just run the tests, knowing some of them are intended to fail. 1025 // We will use a separate Python script to compare the output of 1026 // this program with the golden file. 1027 1028 // It's hard to test InitGoogleTest() directly, as it has many 1029 // global side effects. The following line serves as a test 1030 // for it. 1031 testing::InitGoogleTest(&argc, argv); 1032 bool internal_skip_environment_and_ad_hoc_tests = 1033 std::count(argv, argv + argc, 1034 std::string("internal_skip_environment_and_ad_hoc_tests")) > 0; 1035 1036 #ifdef GTEST_HAS_DEATH_TEST 1037 if (!GTEST_FLAG_GET(internal_run_death_test).empty()) { 1038 // Skip the usual output capturing if we're running as the child 1039 // process of an threadsafe-style death test. 1040 #if defined(GTEST_OS_WINDOWS) 1041 posix::FReopen("nul:", "w", stdout); 1042 #else 1043 posix::FReopen("/dev/null", "w", stdout); 1044 #endif // GTEST_OS_WINDOWS 1045 return RUN_ALL_TESTS(); 1046 } 1047 #endif // GTEST_HAS_DEATH_TEST 1048 1049 if (internal_skip_environment_and_ad_hoc_tests) return RUN_ALL_TESTS(); 1050 1051 // Registers two global test environments. 1052 // The golden file verifies that they are set up in the order they 1053 // are registered, and torn down in the reverse order. 1054 testing::AddGlobalTestEnvironment(new FooEnvironment); 1055 testing::AddGlobalTestEnvironment(new BarEnvironment); 1056 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4127 1057 return RunAllTests(); 1058 } 1059