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