00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include "gtest/gtest-spi.h"
00039 #include "gtest/gtest.h"
00040
00041
00042
00043
00044
00045
00046 #define GTEST_IMPLEMENTATION_ 1
00047 #include "src/gtest-internal-inl.h"
00048 #undef GTEST_IMPLEMENTATION_
00049
00050 #include <stdlib.h>
00051
00052 #if GTEST_IS_THREADSAFE
00053 using testing::ScopedFakeTestPartResultReporter;
00054 using testing::TestPartResultArray;
00055
00056 using testing::internal::Notification;
00057 using testing::internal::ThreadWithParam;
00058 #endif
00059
00060 namespace posix = ::testing::internal::posix;
00061 using testing::internal::scoped_ptr;
00062
00063
00064
00065
00066 void TestEq1(int x) {
00067 ASSERT_EQ(1, x);
00068 }
00069
00070
00071
00072 void TryTestSubroutine() {
00073
00074 TestEq1(2);
00075
00076
00077
00078
00079
00080 if (testing::Test::HasFatalFailure()) return;
00081
00082
00083 FAIL() << "This should never be reached.";
00084 }
00085
00086 TEST(PassingTest, PassingTest1) {
00087 }
00088
00089 TEST(PassingTest, PassingTest2) {
00090 }
00091
00092
00093
00094 class FailingParamTest : public testing::TestWithParam<int> {};
00095
00096 TEST_P(FailingParamTest, Fails) {
00097 EXPECT_EQ(1, GetParam());
00098 }
00099
00100
00101
00102 INSTANTIATE_TEST_CASE_P(PrintingFailingParams,
00103 FailingParamTest,
00104 testing::Values(2));
00105
00106 static const char kGoldenString[] = "\"Line\0 1\"\nLine 2";
00107
00108 TEST(NonfatalFailureTest, EscapesStringOperands) {
00109 std::string actual = "actual \"string\"";
00110 EXPECT_EQ(kGoldenString, actual);
00111
00112 const char* golden = kGoldenString;
00113 EXPECT_EQ(golden, actual);
00114 }
00115
00116 TEST(NonfatalFailureTest, DiffForLongStrings) {
00117 std::string golden_str(kGoldenString, sizeof(kGoldenString) - 1);
00118 EXPECT_EQ(golden_str, "Line 2");
00119 }
00120
00121
00122 TEST(FatalFailureTest, FatalFailureInSubroutine) {
00123 printf("(expecting a failure that x should be 1)\n");
00124
00125 TryTestSubroutine();
00126 }
00127
00128
00129 TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
00130 printf("(expecting a failure that x should be 1)\n");
00131
00132
00133 TryTestSubroutine();
00134
00135
00136
00137
00138
00139 if (HasFatalFailure()) return;
00140
00141
00142 FAIL() << "This should never be reached.";
00143 }
00144
00145
00146 TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
00147 printf("(expecting a failure on false)\n");
00148 EXPECT_TRUE(false);
00149 ASSERT_FALSE(HasFatalFailure());
00150 }
00151
00152
00153 TEST(LoggingTest, InterleavingLoggingAndAssertions) {
00154 static const int a[4] = {
00155 3, 9, 2, 6
00156 };
00157
00158 printf("(expecting 2 failures on (3) >= (a[i]))\n");
00159 for (int i = 0; i < static_cast<int>(sizeof(a)/sizeof(*a)); i++) {
00160 printf("i == %d\n", i);
00161 EXPECT_GE(3, a[i]);
00162 }
00163 }
00164
00165
00166
00167
00168 void SubWithoutTrace(int n) {
00169 EXPECT_EQ(1, n);
00170 ASSERT_EQ(2, n);
00171 }
00172
00173
00174 void SubWithTrace(int n) {
00175 SCOPED_TRACE(testing::Message() << "n = " << n);
00176
00177 SubWithoutTrace(n);
00178 }
00179
00180
00181 TEST(SCOPED_TRACETest, ObeysScopes) {
00182 printf("(expected to fail)\n");
00183
00184
00185 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
00186
00187 {
00188 SCOPED_TRACE("Expected trace");
00189
00190
00191 ADD_FAILURE() << "This failure is expected, and should have a trace.";
00192 }
00193
00194
00195
00196 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
00197 }
00198
00199
00200 TEST(SCOPED_TRACETest, WorksInLoop) {
00201 printf("(expected to fail)\n");
00202
00203 for (int i = 1; i <= 2; i++) {
00204 SCOPED_TRACE(testing::Message() << "i = " << i);
00205
00206 SubWithoutTrace(i);
00207 }
00208 }
00209
00210
00211 TEST(SCOPED_TRACETest, WorksInSubroutine) {
00212 printf("(expected to fail)\n");
00213
00214 SubWithTrace(1);
00215 SubWithTrace(2);
00216 }
00217
00218
00219 TEST(SCOPED_TRACETest, CanBeNested) {
00220 printf("(expected to fail)\n");
00221
00222 SCOPED_TRACE("");
00223
00224 SubWithTrace(2);
00225 }
00226
00227
00228 TEST(SCOPED_TRACETest, CanBeRepeated) {
00229 printf("(expected to fail)\n");
00230
00231 SCOPED_TRACE("A");
00232 ADD_FAILURE()
00233 << "This failure is expected, and should contain trace point A.";
00234
00235 SCOPED_TRACE("B");
00236 ADD_FAILURE()
00237 << "This failure is expected, and should contain trace point A and B.";
00238
00239 {
00240 SCOPED_TRACE("C");
00241 ADD_FAILURE() << "This failure is expected, and should "
00242 << "contain trace point A, B, and C.";
00243 }
00244
00245 SCOPED_TRACE("D");
00246 ADD_FAILURE() << "This failure is expected, and should "
00247 << "contain trace point A, B, and D.";
00248 }
00249
00250 #if GTEST_IS_THREADSAFE
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280 struct CheckPoints {
00281 Notification n1;
00282 Notification n2;
00283 Notification n3;
00284 };
00285
00286 static void ThreadWithScopedTrace(CheckPoints* check_points) {
00287 {
00288 SCOPED_TRACE("Trace B");
00289 ADD_FAILURE()
00290 << "Expected failure #1 (in thread B, only trace B alive).";
00291 check_points->n1.Notify();
00292 check_points->n2.WaitForNotification();
00293
00294 ADD_FAILURE()
00295 << "Expected failure #3 (in thread B, trace A & B both alive).";
00296 }
00297 ADD_FAILURE()
00298 << "Expected failure #4 (in thread B, only trace A alive).";
00299 check_points->n3.Notify();
00300 }
00301
00302 TEST(SCOPED_TRACETest, WorksConcurrently) {
00303 printf("(expecting 6 failures)\n");
00304
00305 CheckPoints check_points;
00306 ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace,
00307 &check_points,
00308 NULL);
00309 check_points.n1.WaitForNotification();
00310
00311 {
00312 SCOPED_TRACE("Trace A");
00313 ADD_FAILURE()
00314 << "Expected failure #2 (in thread A, trace A & B both alive).";
00315 check_points.n2.Notify();
00316 check_points.n3.WaitForNotification();
00317
00318 ADD_FAILURE()
00319 << "Expected failure #5 (in thread A, only trace A alive).";
00320 }
00321 ADD_FAILURE()
00322 << "Expected failure #6 (in thread A, no trace alive).";
00323 thread.Join();
00324 }
00325 #endif // GTEST_IS_THREADSAFE
00326
00327 TEST(DisabledTestsWarningTest,
00328 DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
00329
00330
00331
00332
00333 }
00334
00335
00336
00337
00338 void AdHocTest() {
00339 printf("The non-test part of the code is expected to have 2 failures.\n\n");
00340 EXPECT_TRUE(false);
00341 EXPECT_EQ(2, 3);
00342 }
00343
00344
00345 int RunAllTests() {
00346 AdHocTest();
00347 return RUN_ALL_TESTS();
00348 }
00349
00350
00351 class NonFatalFailureInFixtureConstructorTest : public testing::Test {
00352 protected:
00353 NonFatalFailureInFixtureConstructorTest() {
00354 printf("(expecting 5 failures)\n");
00355 ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor.";
00356 }
00357
00358 ~NonFatalFailureInFixtureConstructorTest() {
00359 ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor.";
00360 }
00361
00362 virtual void SetUp() {
00363 ADD_FAILURE() << "Expected failure #2, in SetUp().";
00364 }
00365
00366 virtual void TearDown() {
00367 ADD_FAILURE() << "Expected failure #4, in TearDown.";
00368 }
00369 };
00370
00371 TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) {
00372 ADD_FAILURE() << "Expected failure #3, in the test body.";
00373 }
00374
00375
00376 class FatalFailureInFixtureConstructorTest : public testing::Test {
00377 protected:
00378 FatalFailureInFixtureConstructorTest() {
00379 printf("(expecting 2 failures)\n");
00380 Init();
00381 }
00382
00383 ~FatalFailureInFixtureConstructorTest() {
00384 ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor.";
00385 }
00386
00387 virtual void SetUp() {
00388 ADD_FAILURE() << "UNEXPECTED failure in SetUp(). "
00389 << "We should never get here, as the test fixture c'tor "
00390 << "had a fatal failure.";
00391 }
00392
00393 virtual void TearDown() {
00394 ADD_FAILURE() << "UNEXPECTED failure in TearDown(). "
00395 << "We should never get here, as the test fixture c'tor "
00396 << "had a fatal failure.";
00397 }
00398
00399 private:
00400 void Init() {
00401 FAIL() << "Expected failure #1, in the test fixture c'tor.";
00402 }
00403 };
00404
00405 TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) {
00406 ADD_FAILURE() << "UNEXPECTED failure in the test body. "
00407 << "We should never get here, as the test fixture c'tor "
00408 << "had a fatal failure.";
00409 }
00410
00411
00412 class NonFatalFailureInSetUpTest : public testing::Test {
00413 protected:
00414 virtual ~NonFatalFailureInSetUpTest() {
00415 Deinit();
00416 }
00417
00418 virtual void SetUp() {
00419 printf("(expecting 4 failures)\n");
00420 ADD_FAILURE() << "Expected failure #1, in SetUp().";
00421 }
00422
00423 virtual void TearDown() {
00424 FAIL() << "Expected failure #3, in TearDown().";
00425 }
00426 private:
00427 void Deinit() {
00428 FAIL() << "Expected failure #4, in the test fixture d'tor.";
00429 }
00430 };
00431
00432 TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) {
00433 FAIL() << "Expected failure #2, in the test function.";
00434 }
00435
00436
00437 class FatalFailureInSetUpTest : public testing::Test {
00438 protected:
00439 virtual ~FatalFailureInSetUpTest() {
00440 Deinit();
00441 }
00442
00443 virtual void SetUp() {
00444 printf("(expecting 3 failures)\n");
00445 FAIL() << "Expected failure #1, in SetUp().";
00446 }
00447
00448 virtual void TearDown() {
00449 FAIL() << "Expected failure #2, in TearDown().";
00450 }
00451 private:
00452 void Deinit() {
00453 FAIL() << "Expected failure #3, in the test fixture d'tor.";
00454 }
00455 };
00456
00457 TEST_F(FatalFailureInSetUpTest, FailureInSetUp) {
00458 FAIL() << "UNEXPECTED failure in the test function. "
00459 << "We should never get here, as SetUp() failed.";
00460 }
00461
00462 TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
00463 ADD_FAILURE_AT("foo.cc", 42) << "Expected failure in foo.cc";
00464 }
00465
00466 #if GTEST_IS_THREADSAFE
00467
00468
00469 void DieIf(bool should_die) {
00470 GTEST_CHECK_(!should_die) << " - death inside DieIf().";
00471 }
00472
00473
00474
00475
00476 struct SpawnThreadNotifications {
00477 SpawnThreadNotifications() {}
00478
00479 Notification spawn_thread_started;
00480 Notification spawn_thread_ok_to_terminate;
00481
00482 private:
00483 GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications);
00484 };
00485
00486
00487
00488 static void ThreadRoutine(SpawnThreadNotifications* notifications) {
00489
00490 notifications->spawn_thread_started.Notify();
00491
00492
00493 notifications->spawn_thread_ok_to_terminate.WaitForNotification();
00494 }
00495
00496
00497
00498
00499 class DeathTestAndMultiThreadsTest : public testing::Test {
00500 protected:
00501
00502 virtual void SetUp() {
00503 thread_.reset(new ThreadWithParam<SpawnThreadNotifications*>(
00504 &ThreadRoutine, ¬ifications_, NULL));
00505 notifications_.spawn_thread_started.WaitForNotification();
00506 }
00507
00508
00509
00510
00511
00512 virtual void TearDown() {
00513 notifications_.spawn_thread_ok_to_terminate.Notify();
00514 }
00515
00516 private:
00517 SpawnThreadNotifications notifications_;
00518 scoped_ptr<ThreadWithParam<SpawnThreadNotifications*> > thread_;
00519 };
00520
00521 #endif // GTEST_IS_THREADSAFE
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533 namespace foo {
00534
00535 class MixedUpTestCaseTest : public testing::Test {
00536 };
00537
00538 TEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {}
00539 TEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {}
00540
00541 class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
00542 };
00543
00544 TEST_F(MixedUpTestCaseWithSameTestNameTest,
00545 TheSecondTestWithThisNameShouldFail) {}
00546
00547 }
00548
00549 namespace bar {
00550
00551 class MixedUpTestCaseTest : public testing::Test {
00552 };
00553
00554
00555
00556 TEST_F(MixedUpTestCaseTest, ThisShouldFail) {}
00557 TEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {}
00558
00559 class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
00560 };
00561
00562
00563
00564 TEST_F(MixedUpTestCaseWithSameTestNameTest,
00565 TheSecondTestWithThisNameShouldFail) {}
00566
00567 }
00568
00569
00570
00571
00572
00573
00574 class TEST_F_before_TEST_in_same_test_case : public testing::Test {
00575 };
00576
00577 TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {}
00578
00579
00580
00581 TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {}
00582
00583 class TEST_before_TEST_F_in_same_test_case : public testing::Test {
00584 };
00585
00586 TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {}
00587
00588
00589
00590 TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {
00591 }
00592
00593
00594 int global_integer = 0;
00595
00596
00597 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
00598 global_integer = 0;
00599 EXPECT_NONFATAL_FAILURE({
00600 EXPECT_EQ(1, global_integer) << "Expected non-fatal failure.";
00601 }, "Expected non-fatal failure.");
00602 }
00603
00604
00605
00606 TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
00607 int m = 0;
00608 static int n;
00609 n = 1;
00610 EXPECT_NONFATAL_FAILURE({
00611 EXPECT_EQ(m, n) << "Expected non-fatal failure.";
00612 }, "Expected non-fatal failure.");
00613 }
00614
00615
00616
00617 TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
00618 EXPECT_NONFATAL_FAILURE({
00619 ADD_FAILURE() << "Expected non-fatal failure.";
00620 }, "Expected non-fatal failure.");
00621 }
00622
00623
00624
00625 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
00626 printf("(expecting a failure)\n");
00627 EXPECT_NONFATAL_FAILURE({
00628 }, "");
00629 }
00630
00631
00632
00633 TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
00634 printf("(expecting a failure)\n");
00635 EXPECT_NONFATAL_FAILURE({
00636 ADD_FAILURE() << "Expected non-fatal failure 1.";
00637 ADD_FAILURE() << "Expected non-fatal failure 2.";
00638 }, "");
00639 }
00640
00641
00642
00643 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
00644 printf("(expecting a failure)\n");
00645 EXPECT_NONFATAL_FAILURE({
00646 FAIL() << "Expected fatal failure.";
00647 }, "");
00648 }
00649
00650
00651
00652 TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
00653 printf("(expecting a failure)\n");
00654 EXPECT_NONFATAL_FAILURE({
00655 return;
00656 }, "");
00657 }
00658
00659 #if GTEST_HAS_EXCEPTIONS
00660
00661
00662
00663 TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
00664 printf("(expecting a failure)\n");
00665 try {
00666 EXPECT_NONFATAL_FAILURE({
00667 throw 0;
00668 }, "");
00669 } catch(int) {
00670 }
00671 }
00672
00673 #endif // GTEST_HAS_EXCEPTIONS
00674
00675
00676 TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
00677 global_integer = 0;
00678 EXPECT_FATAL_FAILURE({
00679 ASSERT_EQ(1, global_integer) << "Expected fatal failure.";
00680 }, "Expected fatal failure.");
00681 }
00682
00683
00684
00685 TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
00686 static int n;
00687 n = 1;
00688 EXPECT_FATAL_FAILURE({
00689 ASSERT_EQ(0, n) << "Expected fatal failure.";
00690 }, "Expected fatal failure.");
00691 }
00692
00693
00694
00695 TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
00696 EXPECT_FATAL_FAILURE({
00697 FAIL() << "Expected fatal failure.";
00698 }, "Expected fatal failure.");
00699 }
00700
00701
00702
00703 TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
00704 printf("(expecting a failure)\n");
00705 EXPECT_FATAL_FAILURE({
00706 }, "");
00707 }
00708
00709
00710 void FatalFailure() {
00711 FAIL() << "Expected fatal failure.";
00712 }
00713
00714
00715
00716 TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
00717 printf("(expecting a failure)\n");
00718 EXPECT_FATAL_FAILURE({
00719 FatalFailure();
00720 FatalFailure();
00721 }, "");
00722 }
00723
00724
00725
00726 TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
00727 printf("(expecting a failure)\n");
00728 EXPECT_FATAL_FAILURE({
00729 ADD_FAILURE() << "Expected non-fatal failure.";
00730 }, "");
00731 }
00732
00733
00734
00735 TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
00736 printf("(expecting a failure)\n");
00737 EXPECT_FATAL_FAILURE({
00738 return;
00739 }, "");
00740 }
00741
00742 #if GTEST_HAS_EXCEPTIONS
00743
00744
00745
00746 TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
00747 printf("(expecting a failure)\n");
00748 try {
00749 EXPECT_FATAL_FAILURE({
00750 throw 0;
00751 }, "");
00752 } catch(int) {
00753 }
00754 }
00755
00756 #endif // GTEST_HAS_EXCEPTIONS
00757
00758
00759 #if GTEST_HAS_TYPED_TEST
00760
00761 template <typename T>
00762 class TypedTest : public testing::Test {
00763 };
00764
00765 TYPED_TEST_CASE(TypedTest, testing::Types<int>);
00766
00767 TYPED_TEST(TypedTest, Success) {
00768 EXPECT_EQ(0, TypeParam());
00769 }
00770
00771 TYPED_TEST(TypedTest, Failure) {
00772 EXPECT_EQ(1, TypeParam()) << "Expected failure";
00773 }
00774
00775 #endif // GTEST_HAS_TYPED_TEST
00776
00777
00778 #if GTEST_HAS_TYPED_TEST_P
00779
00780 template <typename T>
00781 class TypedTestP : public testing::Test {
00782 };
00783
00784 TYPED_TEST_CASE_P(TypedTestP);
00785
00786 TYPED_TEST_P(TypedTestP, Success) {
00787 EXPECT_EQ(0U, TypeParam());
00788 }
00789
00790 TYPED_TEST_P(TypedTestP, Failure) {
00791 EXPECT_EQ(1U, TypeParam()) << "Expected failure";
00792 }
00793
00794 REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure);
00795
00796 typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
00797 INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes);
00798
00799 #endif // GTEST_HAS_TYPED_TEST_P
00800
00801 #if GTEST_HAS_DEATH_TEST
00802
00803
00804
00805
00806 TEST(ADeathTest, ShouldRunFirst) {
00807 }
00808
00809 # if GTEST_HAS_TYPED_TEST
00810
00811
00812
00813
00814 template <typename T>
00815 class ATypedDeathTest : public testing::Test {
00816 };
00817
00818 typedef testing::Types<int, double> NumericTypes;
00819 TYPED_TEST_CASE(ATypedDeathTest, NumericTypes);
00820
00821 TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {
00822 }
00823
00824 # endif // GTEST_HAS_TYPED_TEST
00825
00826 # if GTEST_HAS_TYPED_TEST_P
00827
00828
00829
00830
00831
00832 template <typename T>
00833 class ATypeParamDeathTest : public testing::Test {
00834 };
00835
00836 TYPED_TEST_CASE_P(ATypeParamDeathTest);
00837
00838 TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {
00839 }
00840
00841 REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst);
00842
00843 INSTANTIATE_TYPED_TEST_CASE_P(My, ATypeParamDeathTest, NumericTypes);
00844
00845 # endif // GTEST_HAS_TYPED_TEST_P
00846
00847 #endif // GTEST_HAS_DEATH_TEST
00848
00849
00850
00851 class ExpectFailureTest : public testing::Test {
00852 public:
00853 enum FailureMode {
00854 FATAL_FAILURE,
00855 NONFATAL_FAILURE
00856 };
00857 static void AddFailure(FailureMode failure) {
00858 if (failure == FATAL_FAILURE) {
00859 FAIL() << "Expected fatal failure.";
00860 } else {
00861 ADD_FAILURE() << "Expected non-fatal failure.";
00862 }
00863 }
00864 };
00865
00866 TEST_F(ExpectFailureTest, ExpectFatalFailure) {
00867
00868 printf("(expecting 1 failure)\n");
00869 EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure.");
00870
00871 printf("(expecting 1 failure)\n");
00872 EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal "
00873 "failure.");
00874
00875 printf("(expecting 1 failure)\n");
00876 EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure "
00877 "expected.");
00878 }
00879
00880 TEST_F(ExpectFailureTest, ExpectNonFatalFailure) {
00881
00882 printf("(expecting 1 failure)\n");
00883 EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure.");
00884
00885 printf("(expecting 1 failure)\n");
00886 EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure.");
00887
00888 printf("(expecting 1 failure)\n");
00889 EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal "
00890 "failure.");
00891 }
00892
00893 #if GTEST_IS_THREADSAFE
00894
00895 class ExpectFailureWithThreadsTest : public ExpectFailureTest {
00896 protected:
00897 static void AddFailureInOtherThread(FailureMode failure) {
00898 ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL);
00899 thread.Join();
00900 }
00901 };
00902
00903 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
00904
00905 printf("(expecting 2 failures)\n");
00906 EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE),
00907 "Expected fatal failure.");
00908 }
00909
00910 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
00911
00912 printf("(expecting 2 failures)\n");
00913 EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE),
00914 "Expected non-fatal failure.");
00915 }
00916
00917 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
00918
00919
00920
00921 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
00922 printf("(expecting 2 failures)\n");
00923 TestPartResultArray results;
00924 {
00925 ScopedFakeTestPartResultReporter reporter(
00926 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
00927 &results);
00928 AddFailureInOtherThread(FATAL_FAILURE);
00929 AddFailureInOtherThread(NONFATAL_FAILURE);
00930 }
00931
00932 EXPECT_EQ(0, results.size()) << "This shouldn't fail.";
00933 }
00934
00935 #endif // GTEST_IS_THREADSAFE
00936
00937 TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) {
00938
00939 printf("(expecting 1 failure)\n");
00940 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure.");
00941
00942 printf("(expecting 1 failure)\n");
00943 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
00944 "Expected non-fatal failure.");
00945
00946 printf("(expecting 1 failure)\n");
00947 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
00948 "Some other fatal failure expected.");
00949 }
00950
00951 TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) {
00952
00953 printf("(expecting 1 failure)\n");
00954 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal "
00955 "failure.");
00956
00957 printf("(expecting 1 failure)\n");
00958 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
00959 "Expected fatal failure.");
00960
00961 printf("(expecting 1 failure)\n");
00962 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
00963 "Some other non-fatal failure.");
00964 }
00965
00966
00967
00968
00969 class FooEnvironment : public testing::Environment {
00970 public:
00971 virtual void SetUp() {
00972 printf("%s", "FooEnvironment::SetUp() called.\n");
00973 }
00974
00975 virtual void TearDown() {
00976 printf("%s", "FooEnvironment::TearDown() called.\n");
00977 FAIL() << "Expected fatal failure.";
00978 }
00979 };
00980
00981 class BarEnvironment : public testing::Environment {
00982 public:
00983 virtual void SetUp() {
00984 printf("%s", "BarEnvironment::SetUp() called.\n");
00985 }
00986
00987 virtual void TearDown() {
00988 printf("%s", "BarEnvironment::TearDown() called.\n");
00989 ADD_FAILURE() << "Expected non-fatal failure.";
00990 }
00991 };
00992
00993 bool GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = false;
00994
00995
00996
00997
00998
00999
01000 int main(int argc, char **argv) {
01001 testing::GTEST_FLAG(print_time) = false;
01002
01003
01004
01005
01006
01007
01008
01009
01010 testing::InitGoogleTest(&argc, argv);
01011 if (argc >= 2 &&
01012 (std::string(argv[1]) ==
01013 "--gtest_internal_skip_environment_and_ad_hoc_tests"))
01014 GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = true;
01015
01016 #if GTEST_HAS_DEATH_TEST
01017 if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") {
01018
01019
01020 # if GTEST_OS_WINDOWS
01021 posix::FReopen("nul:", "w", stdout);
01022 # else
01023 posix::FReopen("/dev/null", "w", stdout);
01024 # endif // GTEST_OS_WINDOWS
01025 return RUN_ALL_TESTS();
01026 }
01027 #endif // GTEST_HAS_DEATH_TEST
01028
01029 if (GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests))
01030 return RUN_ALL_TESTS();
01031
01032
01033
01034
01035 testing::AddGlobalTestEnvironment(new FooEnvironment);
01036 testing::AddGlobalTestEnvironment(new BarEnvironment);
01037
01038 return RunAllTests();
01039 }