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