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