46 #if GTEST_IS_THREADSAFE
47 using testing::ScopedFakeTestPartResultReporter;
48 using testing::TestPartResultArray;
50 using testing::internal::Notification;
51 using testing::internal::ThreadWithParam;
54 namespace posix = ::testing::internal::posix;
76 FAIL() <<
"This should never be reached.";
79 TEST(PassingTest, PassingTest1) {
82 TEST(PassingTest, PassingTest2) {
101 TEST(NonfatalFailureTest, EscapesStringOperands) {
109 TEST(NonfatalFailureTest, DiffForLongStrings) {
115 TEST(FatalFailureTest, FatalFailureInSubroutine) {
116 printf(
"(expecting a failure that x should be 1)\n");
122 TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
123 printf(
"(expecting a failure that x should be 1)\n");
132 if (HasFatalFailure())
return;
135 FAIL() <<
"This should never be reached.";
139 TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
140 printf(
"(expecting a failure on false)\n");
146 TEST(LoggingTest, InterleavingLoggingAndAssertions) {
147 static const int a[4] = {
151 printf(
"(expecting 2 failures on (3) >= (a[i]))\n");
152 for (
int i = 0; i < static_cast<int>(
sizeof(
a)/
sizeof(*
a));
i++) {
153 printf(
"i == %d\n",
i);
173 TEST(SCOPED_TRACETest, AcceptedValues) {
177 const char* null_value =
nullptr;
180 ADD_FAILURE() <<
"Just checking that all these values work fine.";
184 TEST(SCOPED_TRACETest, ObeysScopes) {
185 printf(
"(expected to fail)\n");
188 ADD_FAILURE() <<
"This failure is expected, and shouldn't have a trace.";
194 ADD_FAILURE() <<
"This failure is expected, and should have a trace.";
199 ADD_FAILURE() <<
"This failure is expected, and shouldn't have a trace.";
203 TEST(SCOPED_TRACETest, WorksInLoop) {
204 printf(
"(expected to fail)\n");
206 for (
int i = 1;
i <= 2;
i++) {
214 TEST(SCOPED_TRACETest, WorksInSubroutine) {
215 printf(
"(expected to fail)\n");
222 TEST(SCOPED_TRACETest, CanBeNested) {
223 printf(
"(expected to fail)\n");
231 TEST(SCOPED_TRACETest, CanBeRepeated) {
232 printf(
"(expected to fail)\n");
236 <<
"This failure is expected, and should contain trace point A.";
240 <<
"This failure is expected, and should contain trace point A and B.";
244 ADD_FAILURE() <<
"This failure is expected, and should "
245 <<
"contain trace point A, B, and C.";
249 ADD_FAILURE() <<
"This failure is expected, and should "
250 <<
"contain trace point A, B, and D.";
253 #if GTEST_IS_THREADSAFE
289 static void ThreadWithScopedTrace(CheckPoints* check_points) {
293 <<
"Expected failure #1 (in thread B, only trace B alive).";
294 check_points->n1.Notify();
295 check_points->n2.WaitForNotification();
298 <<
"Expected failure #3 (in thread B, trace A & B both alive).";
301 <<
"Expected failure #4 (in thread B, only trace A alive).";
302 check_points->n3.Notify();
305 TEST(SCOPED_TRACETest, WorksConcurrently) {
306 printf(
"(expecting 6 failures)\n");
308 CheckPoints check_points;
309 ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace, &check_points,
311 check_points.n1.WaitForNotification();
316 <<
"Expected failure #2 (in thread A, trace A & B both alive).";
317 check_points.n2.Notify();
318 check_points.n3.WaitForNotification();
321 <<
"Expected failure #5 (in thread A, only trace A alive).";
324 <<
"Expected failure #6 (in thread A, no trace alive).";
327 #endif // GTEST_IS_THREADSAFE
331 TEST(ScopedTraceTest, WithExplicitFileAndLine) {
333 ADD_FAILURE() <<
"Check that the trace is attached to a particular location.";
337 DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
348 printf(
"The non-test part of the code is expected to have 2 failures.\n\n");
363 printf(
"(expecting 5 failures)\n");
364 ADD_FAILURE() <<
"Expected failure #1, in the test fixture c'tor.";
368 ADD_FAILURE() <<
"Expected failure #5, in the test fixture d'tor.";
374 ADD_FAILURE() <<
"Expected failure #4, in TearDown.";
379 ADD_FAILURE() <<
"Expected failure #3, in the test body.";
386 printf(
"(expecting 2 failures)\n");
391 ADD_FAILURE() <<
"Expected failure #2, in the test fixture d'tor.";
396 <<
"We should never get here, as the test fixture c'tor "
397 <<
"had a fatal failure.";
401 ADD_FAILURE() <<
"UNEXPECTED failure in TearDown(). "
402 <<
"We should never get here, as the test fixture c'tor "
403 <<
"had a fatal failure.";
408 FAIL() <<
"Expected failure #1, in the test fixture c'tor.";
413 ADD_FAILURE() <<
"UNEXPECTED failure in the test body. "
414 <<
"We should never get here, as the test fixture c'tor "
415 <<
"had a fatal failure.";
424 printf(
"(expecting 4 failures)\n");
425 ADD_FAILURE() <<
"Expected failure #1, in SetUp().";
428 void TearDown()
override {
FAIL() <<
"Expected failure #3, in TearDown()."; }
432 FAIL() <<
"Expected failure #4, in the test fixture d'tor.";
437 FAIL() <<
"Expected failure #2, in the test function.";
446 printf(
"(expecting 3 failures)\n");
447 FAIL() <<
"Expected failure #1, in SetUp().";
450 void TearDown()
override {
FAIL() <<
"Expected failure #2, in TearDown()."; }
454 FAIL() <<
"Expected failure #3, in the test fixture d'tor.";
459 FAIL() <<
"UNEXPECTED failure in the test function. "
460 <<
"We should never get here, as SetUp() failed.";
463 TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
467 #if GTEST_IS_THREADSAFE
470 void DieIf(
bool should_die) {
471 GTEST_CHECK_(!should_die) <<
" - death inside DieIf().";
477 struct SpawnThreadNotifications {
478 SpawnThreadNotifications() {}
480 Notification spawn_thread_started;
481 Notification spawn_thread_ok_to_terminate;
489 static void ThreadRoutine(SpawnThreadNotifications* notifications) {
491 notifications->spawn_thread_started.Notify();
494 notifications->spawn_thread_ok_to_terminate.WaitForNotification();
503 void SetUp()
override {
504 thread_.reset(
new ThreadWithParam<SpawnThreadNotifications*>(
505 &ThreadRoutine, ¬ifications_,
nullptr));
506 notifications_.spawn_thread_started.WaitForNotification();
514 notifications_.spawn_thread_ok_to_terminate.Notify();
518 SpawnThreadNotifications notifications_;
519 std::unique_ptr<ThreadWithParam<SpawnThreadNotifications*> > thread_;
522 #endif // GTEST_IS_THREADSAFE
546 TheSecondTestWithThisNameShouldFail) {}
566 TheSecondTestWithThisNameShouldFail) {}
598 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
602 },
"Expected non-fatal failure.");
607 TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
613 },
"Expected non-fatal failure.");
618 TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
621 },
"Expected non-fatal failure.");
626 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
627 printf(
"(expecting a failure)\n");
634 TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
635 printf(
"(expecting a failure)\n");
644 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
645 printf(
"(expecting a failure)\n");
647 FAIL() <<
"Expected fatal failure.";
653 TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
654 printf(
"(expecting a failure)\n");
660 #if GTEST_HAS_EXCEPTIONS
664 TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
665 printf(
"(expecting a failure)\n");
674 #endif // GTEST_HAS_EXCEPTIONS
677 TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
681 },
"Expected fatal failure.");
686 TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
691 },
"Expected fatal failure.");
696 TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
698 FAIL() <<
"Expected fatal failure.";
699 },
"Expected fatal failure.");
704 TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
705 printf(
"(expecting a failure)\n");
712 FAIL() <<
"Expected fatal failure.";
717 TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
718 printf(
"(expecting a failure)\n");
727 TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
728 printf(
"(expecting a failure)\n");
736 TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
737 printf(
"(expecting a failure)\n");
743 #if GTEST_HAS_EXCEPTIONS
747 TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
748 printf(
"(expecting a failure)\n");
757 #endif // GTEST_HAS_EXCEPTIONS
773 EXPECT_EQ(
"b", GetParam()) <<
"Expected failure";
782 #if GTEST_HAS_TYPED_TEST
784 template <
typename T>
795 EXPECT_EQ(1, TypeParam()) <<
"Expected failure";
798 typedef testing::Types<char, int> TypesForTestWithNames;
800 template <
typename T>
803 class TypedTestNames {
805 template <
typename T>
814 TYPED_TEST_SUITE(TypedTestWithNames, TypesForTestWithNames, TypedTestNames);
820 #endif // GTEST_HAS_TYPED_TEST
823 #if GTEST_HAS_TYPED_TEST_P
825 template <
typename T>
836 EXPECT_EQ(1U, TypeParam()) <<
"Expected failure";
841 typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
844 class TypedTestPNames {
846 template <
typename T>
860 #endif // GTEST_HAS_TYPED_TEST_P
862 #if GTEST_HAS_DEATH_TEST
867 TEST(ADeathTest, ShouldRunFirst) {
870 # if GTEST_HAS_TYPED_TEST
875 template <
typename T>
879 typedef testing::Types<int, double> NumericTypes;
885 # endif // GTEST_HAS_TYPED_TEST
887 # if GTEST_HAS_TYPED_TEST_P
893 template <
typename T>
906 # endif // GTEST_HAS_TYPED_TEST_P
908 #endif // GTEST_HAS_DEATH_TEST
920 FAIL() <<
"Expected fatal failure.";
929 printf(
"(expecting 1 failure)\n");
932 printf(
"(expecting 1 failure)\n");
936 printf(
"(expecting 1 failure)\n");
943 printf(
"(expecting 1 failure)\n");
946 printf(
"(expecting 1 failure)\n");
949 printf(
"(expecting 1 failure)\n");
954 #if GTEST_IS_THREADSAFE
958 static void AddFailureInOtherThread(FailureMode failure) {
959 ThreadWithParam<FailureMode> thread(&AddFailure, failure,
nullptr);
964 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
966 printf(
"(expecting 2 failures)\n");
968 "Expected fatal failure.");
971 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
973 printf(
"(expecting 2 failures)\n");
975 "Expected non-fatal failure.");
978 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
982 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
983 printf(
"(expecting 2 failures)\n");
986 ScopedFakeTestPartResultReporter reporter(
987 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
989 AddFailureInOtherThread(FATAL_FAILURE);
990 AddFailureInOtherThread(NONFATAL_FAILURE);
996 #endif // GTEST_IS_THREADSAFE
1000 printf(
"(expecting 1 failure)\n");
1003 printf(
"(expecting 1 failure)\n");
1005 "Expected non-fatal failure.");
1007 printf(
"(expecting 1 failure)\n");
1009 "Some other fatal failure expected.");
1014 printf(
"(expecting 1 failure)\n");
1018 printf(
"(expecting 1 failure)\n");
1020 "Expected fatal failure.");
1022 printf(
"(expecting 1 failure)\n");
1024 "Some other non-fatal failure.");
1031 void SetUp()
override { printf(
"DynamicFixture::SetUp\n"); }
1032 void TearDown()
override { printf(
"DynamicFixture::TearDown\n"); }
1036 printf(
"DynamicFixture::TearDownTestSuite\n");
1040 template <
bool Pass>
1049 "DynamicFixture",
"DynamicTestPass",
nullptr,
nullptr, __FILE__,
1052 "DynamicFixture",
"DynamicTestFail",
nullptr,
nullptr, __FILE__,
1057 "DynamicFixtureAnotherName",
"DynamicTestPass",
nullptr,
nullptr,
1063 "BadDynamicFixture1",
"FixtureBase",
nullptr,
nullptr, __FILE__,
1066 "BadDynamicFixture1",
"TestBase",
nullptr,
nullptr, __FILE__, __LINE__,
1072 "BadDynamicFixture2",
"FixtureBase",
nullptr,
nullptr, __FILE__,
1082 void SetUp()
override { printf(
"%s",
"FooEnvironment::SetUp() called.\n"); }
1085 printf(
"%s",
"FooEnvironment::TearDown() called.\n");
1086 FAIL() <<
"Expected fatal failure.";
1092 void SetUp()
override { printf(
"%s",
"BarEnvironment::SetUp() called.\n"); }
1095 printf(
"%s",
"BarEnvironment::TearDown() called.\n");
1116 bool internal_skip_environment_and_ad_hoc_tests =
1118 std::string(
"internal_skip_environment_and_ad_hoc_tests")) > 0;
1120 #if GTEST_HAS_DEATH_TEST
1124 # if GTEST_OS_WINDOWS
1128 # endif // GTEST_OS_WINDOWS
1131 #endif // GTEST_HAS_DEATH_TEST
1133 if (internal_skip_environment_and_ad_hoc_tests)