36 #include "gtest/gtest-spi.h"
37 #include "gtest/gtest.h"
38 #include "src/gtest-internal-inl.h"
46 #if GTEST_IS_THREADSAFE
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) {
109 TEST(NonfatalFailureTest, EscapesStringOperands) {
117 TEST(NonfatalFailureTest, DiffForLongStrings) {
123 TEST(FatalFailureTest, FatalFailureInSubroutine) {
124 printf(
"(expecting a failure that x should be 1)\n");
130 TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
131 printf(
"(expecting a failure that x should be 1)\n");
140 if (HasFatalFailure())
return;
143 FAIL() <<
"This should never be reached.";
147 TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
148 printf(
"(expecting a failure on false)\n");
154 TEST(LoggingTest, InterleavingLoggingAndAssertions) {
155 static const int a[4] = {
159 printf(
"(expecting 2 failures on (3) >= (a[i]))\n");
160 for (
int i = 0; i < static_cast<int>(
sizeof(
a)/
sizeof(*
a));
i++) {
181 TEST(SCOPED_TRACETest, AcceptedValues) {
185 const char* null_value =
nullptr;
188 ADD_FAILURE() <<
"Just checking that all these values work fine.";
192 TEST(SCOPED_TRACETest, ObeysScopes) {
193 printf(
"(expected to fail)\n");
196 ADD_FAILURE() <<
"This failure is expected, and shouldn't have a trace.";
202 ADD_FAILURE() <<
"This failure is expected, and should have a trace.";
207 ADD_FAILURE() <<
"This failure is expected, and shouldn't have a trace.";
211 TEST(SCOPED_TRACETest, WorksInLoop) {
212 printf(
"(expected to fail)\n");
214 for (
int i = 1;
i <= 2;
i++) {
222 TEST(SCOPED_TRACETest, WorksInSubroutine) {
223 printf(
"(expected to fail)\n");
230 TEST(SCOPED_TRACETest, CanBeNested) {
231 printf(
"(expected to fail)\n");
239 TEST(SCOPED_TRACETest, CanBeRepeated) {
240 printf(
"(expected to fail)\n");
244 <<
"This failure is expected, and should contain trace point A.";
248 <<
"This failure is expected, and should contain trace point A and B.";
252 ADD_FAILURE() <<
"This failure is expected, and should "
253 <<
"contain trace point A, B, and C.";
257 ADD_FAILURE() <<
"This failure is expected, and should "
258 <<
"contain trace point A, B, and D.";
261 #if GTEST_IS_THREADSAFE
297 static void ThreadWithScopedTrace(CheckPoints* check_points) {
301 <<
"Expected failure #1 (in thread B, only trace B alive).";
302 check_points->n1.Notify();
303 check_points->n2.WaitForNotification();
306 <<
"Expected failure #3 (in thread B, trace A & B both alive).";
309 <<
"Expected failure #4 (in thread B, only trace A alive).";
310 check_points->n3.Notify();
313 TEST(SCOPED_TRACETest, WorksConcurrently) {
314 printf(
"(expecting 6 failures)\n");
316 CheckPoints check_points;
317 ThreadWithParam<CheckPoints*>
thread(&ThreadWithScopedTrace, &check_points,
319 check_points.n1.WaitForNotification();
324 <<
"Expected failure #2 (in thread A, trace A & B both alive).";
325 check_points.n2.Notify();
326 check_points.n3.WaitForNotification();
329 <<
"Expected failure #5 (in thread A, only trace A alive).";
332 <<
"Expected failure #6 (in thread A, no trace alive).";
335 #endif // GTEST_IS_THREADSAFE
339 TEST(ScopedTraceTest, WithExplicitFileAndLine) {
341 ADD_FAILURE() <<
"Check that the trace is attached to a particular location.";
345 DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
356 printf(
"The non-test part of the code is expected to have 2 failures.\n\n");
371 printf(
"(expecting 5 failures)\n");
372 ADD_FAILURE() <<
"Expected failure #1, in the test fixture c'tor.";
376 ADD_FAILURE() <<
"Expected failure #5, in the test fixture d'tor.";
382 ADD_FAILURE() <<
"Expected failure #4, in TearDown.";
387 ADD_FAILURE() <<
"Expected failure #3, in the test body.";
394 printf(
"(expecting 2 failures)\n");
399 ADD_FAILURE() <<
"Expected failure #2, in the test fixture d'tor.";
404 <<
"We should never get here, as the test fixture c'tor "
405 <<
"had a fatal failure.";
409 ADD_FAILURE() <<
"UNEXPECTED failure in TearDown(). "
410 <<
"We should never get here, as the test fixture c'tor "
411 <<
"had a fatal failure.";
416 FAIL() <<
"Expected failure #1, in the test fixture c'tor.";
421 ADD_FAILURE() <<
"UNEXPECTED failure in the test body. "
422 <<
"We should never get here, as the test fixture c'tor "
423 <<
"had a fatal failure.";
432 printf(
"(expecting 4 failures)\n");
433 ADD_FAILURE() <<
"Expected failure #1, in SetUp().";
436 void TearDown()
override {
FAIL() <<
"Expected failure #3, in TearDown()."; }
440 FAIL() <<
"Expected failure #4, in the test fixture d'tor.";
445 FAIL() <<
"Expected failure #2, in the test function.";
454 printf(
"(expecting 3 failures)\n");
455 FAIL() <<
"Expected failure #1, in SetUp().";
458 void TearDown()
override {
FAIL() <<
"Expected failure #2, in TearDown()."; }
462 FAIL() <<
"Expected failure #3, in the test fixture d'tor.";
467 FAIL() <<
"UNEXPECTED failure in the test function. "
468 <<
"We should never get here, as SetUp() failed.";
471 TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
472 ADD_FAILURE_AT(
"foo.cc", 42) <<
"Expected nonfatal failure in foo.cc";
475 TEST(GtestFailAtTest, MessageContainsSpecifiedFileAndLineNumber) {
476 GTEST_FAIL_AT(
"foo.cc", 42) <<
"Expected fatal failure in foo.cc";
479 #if GTEST_IS_THREADSAFE
482 void DieIf(
bool should_die) {
483 GTEST_CHECK_(!should_die) <<
" - death inside DieIf().";
489 struct SpawnThreadNotifications {
490 SpawnThreadNotifications() {}
492 Notification spawn_thread_started;
493 Notification spawn_thread_ok_to_terminate;
501 static void ThreadRoutine(SpawnThreadNotifications* notifications) {
503 notifications->spawn_thread_started.Notify();
506 notifications->spawn_thread_ok_to_terminate.WaitForNotification();
515 void SetUp()
override {
516 thread_.reset(
new ThreadWithParam<SpawnThreadNotifications*>(
517 &ThreadRoutine, ¬ifications_,
nullptr));
518 notifications_.spawn_thread_started.WaitForNotification();
526 notifications_.spawn_thread_ok_to_terminate.Notify();
530 SpawnThreadNotifications notifications_;
531 std::unique_ptr<ThreadWithParam<SpawnThreadNotifications*> >
thread_;
534 #endif // GTEST_IS_THREADSAFE
551 TEST_F(MixedUpTestSuiteTest, FirstTestFromNamespaceFoo) {}
552 TEST_F(MixedUpTestSuiteTest, SecondTestFromNamespaceFoo) {}
557 TEST_F(MixedUpTestSuiteWithSameTestNameTest,
558 TheSecondTestWithThisNameShouldFail) {}
578 TheSecondTestWithThisNameShouldFail) {}
610 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
614 },
"Expected non-fatal failure.");
619 TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
625 },
"Expected non-fatal failure.");
630 TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
633 },
"Expected non-fatal failure.");
638 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
639 printf(
"(expecting a failure)\n");
646 TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
647 printf(
"(expecting a failure)\n");
656 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
657 printf(
"(expecting a failure)\n");
659 FAIL() <<
"Expected fatal failure.";
665 TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
666 printf(
"(expecting a failure)\n");
672 #if GTEST_HAS_EXCEPTIONS
676 TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
677 printf(
"(expecting a failure)\n");
686 #endif // GTEST_HAS_EXCEPTIONS
689 TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
693 },
"Expected fatal failure.");
698 TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
703 },
"Expected fatal failure.");
708 TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
710 FAIL() <<
"Expected fatal failure.";
711 },
"Expected fatal failure.");
716 TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
717 printf(
"(expecting a failure)\n");
724 FAIL() <<
"Expected fatal failure.";
729 TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
730 printf(
"(expecting a failure)\n");
739 TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
740 printf(
"(expecting a failure)\n");
748 TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
749 printf(
"(expecting a failure)\n");
755 #if GTEST_HAS_EXCEPTIONS
759 TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
760 printf(
"(expecting a failure)\n");
769 #endif // GTEST_HAS_EXCEPTIONS
785 EXPECT_EQ(
"b", GetParam()) <<
"Expected failure";
794 #if GTEST_HAS_TYPED_TEST
796 template <
typename T>
807 EXPECT_EQ(1, TypeParam()) <<
"Expected failure";
812 template <
typename T>
817 template <
typename T>
832 #endif // GTEST_HAS_TYPED_TEST
835 #if GTEST_HAS_TYPED_TEST_P
837 template <
typename T>
848 EXPECT_EQ(1U, TypeParam()) <<
"Expected failure";
858 template <
typename T>
872 #endif // GTEST_HAS_TYPED_TEST_P
874 #if GTEST_HAS_DEATH_TEST
879 TEST(ADeathTest, ShouldRunFirst) {
882 # if GTEST_HAS_TYPED_TEST
887 template <
typename T>
897 # endif // GTEST_HAS_TYPED_TEST
899 # if GTEST_HAS_TYPED_TEST_P
905 template <
typename T>
918 # endif // GTEST_HAS_TYPED_TEST_P
920 #endif // GTEST_HAS_DEATH_TEST
932 FAIL() <<
"Expected fatal failure.";
941 printf(
"(expecting 1 failure)\n");
944 printf(
"(expecting 1 failure)\n");
948 printf(
"(expecting 1 failure)\n");
955 printf(
"(expecting 1 failure)\n");
958 printf(
"(expecting 1 failure)\n");
961 printf(
"(expecting 1 failure)\n");
966 #if GTEST_IS_THREADSAFE
970 static void AddFailureInOtherThread(FailureMode failure) {
971 ThreadWithParam<FailureMode>
thread(&AddFailure, failure,
nullptr);
976 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
978 printf(
"(expecting 2 failures)\n");
980 "Expected fatal failure.");
983 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
985 printf(
"(expecting 2 failures)\n");
987 "Expected non-fatal failure.");
990 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
994 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
995 printf(
"(expecting 2 failures)\n");
998 ScopedFakeTestPartResultReporter reporter(
999 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
1002 AddFailureInOtherThread(NONFATAL_FAILURE);
1008 #endif // GTEST_IS_THREADSAFE
1012 printf(
"(expecting 1 failure)\n");
1015 printf(
"(expecting 1 failure)\n");
1017 "Expected non-fatal failure.");
1019 printf(
"(expecting 1 failure)\n");
1021 "Some other fatal failure expected.");
1026 printf(
"(expecting 1 failure)\n");
1030 printf(
"(expecting 1 failure)\n");
1032 "Expected fatal failure.");
1034 printf(
"(expecting 1 failure)\n");
1036 "Some other non-fatal failure.");
1048 printf(
"DynamicFixture::TearDownTestSuite\n");
1052 template <
bool Pass>
1061 "DynamicFixture",
"DynamicTestPass",
nullptr,
nullptr, __FILE__,
1064 "DynamicFixture",
"DynamicTestFail",
nullptr,
nullptr, __FILE__,
1069 "DynamicFixtureAnotherName",
"DynamicTestPass",
nullptr,
nullptr,
1075 "BadDynamicFixture1",
"FixtureBase",
nullptr,
nullptr, __FILE__,
1078 "BadDynamicFixture1",
"TestBase",
nullptr,
nullptr, __FILE__, __LINE__,
1084 "BadDynamicFixture2",
"FixtureBase",
nullptr,
nullptr, __FILE__,
1094 void SetUp()
override {
printf(
"%s",
"FooEnvironment::SetUp() called.\n"); }
1097 printf(
"%s",
"FooEnvironment::TearDown() called.\n");
1098 FAIL() <<
"Expected fatal failure.";
1104 void SetUp()
override {
printf(
"%s",
"BarEnvironment::SetUp() called.\n"); }
1107 printf(
"%s",
"BarEnvironment::TearDown() called.\n");
1128 bool internal_skip_environment_and_ad_hoc_tests =
1130 std::string(
"internal_skip_environment_and_ad_hoc_tests")) > 0;
1132 #if GTEST_HAS_DEATH_TEST
1136 # if GTEST_OS_WINDOWS
1140 # endif // GTEST_OS_WINDOWS
1143 #endif // GTEST_HAS_DEATH_TEST
1145 if (internal_skip_environment_and_ad_hoc_tests)