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";
494 TEST_F(MixedUpTestSuiteTest, FirstTestFromNamespaceFoo) {}
495 TEST_F(MixedUpTestSuiteTest, SecondTestFromNamespaceFoo) {}
497 class MixedUpTestSuiteWithSameTestNameTest :
public testing::Test {
500 TEST_F(MixedUpTestSuiteWithSameTestNameTest,
501 TheSecondTestWithThisNameShouldFail) {}
512 TEST_F(MixedUpTestSuiteTest, ThisShouldFail) {}
513 TEST_F(MixedUpTestSuiteTest, ThisShouldFailToo) {}
515 class MixedUpTestSuiteWithSameTestNameTest :
public testing::Test {
520 TEST_F(MixedUpTestSuiteWithSameTestNameTest,
521 TheSecondTestWithThisNameShouldFail) {}
553 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
557 },
"Expected non-fatal failure.");
562 TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
568 },
"Expected non-fatal failure.");
573 TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
576 },
"Expected non-fatal failure.");
581 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
582 printf(
"(expecting a failure)\n");
589 TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
590 printf(
"(expecting a failure)\n");
599 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
600 printf(
"(expecting a failure)\n");
602 FAIL() <<
"Expected fatal failure.";
608 TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
609 printf(
"(expecting a failure)\n");
615 #if GTEST_HAS_EXCEPTIONS
619 TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
620 printf(
"(expecting a failure)\n");
629 #endif // GTEST_HAS_EXCEPTIONS
632 TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
636 },
"Expected fatal failure.");
641 TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
646 },
"Expected fatal failure.");
651 TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
653 FAIL() <<
"Expected fatal failure.";
654 },
"Expected fatal failure.");
659 TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
660 printf(
"(expecting a failure)\n");
667 FAIL() <<
"Expected fatal failure.";
672 TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
673 printf(
"(expecting a failure)\n");
682 TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
683 printf(
"(expecting a failure)\n");
691 TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
692 printf(
"(expecting a failure)\n");
698 #if GTEST_HAS_EXCEPTIONS
702 TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
703 printf(
"(expecting a failure)\n");
712 #endif // GTEST_HAS_EXCEPTIONS
728 EXPECT_EQ(
"b", GetParam()) <<
"Expected failure";
747 template <
typename T>
758 EXPECT_EQ(1, TypeParam()) <<
"Expected failure";
763 template <
typename T>
768 template <
typename T>
783 template <
typename T>
794 EXPECT_EQ(1U, TypeParam()) <<
"Expected failure";
804 template <
typename T>
818 template <
typename T>
822 TypeParam instantiate;
833 #if GTEST_HAS_DEATH_TEST
838 TEST(ADeathTest, ShouldRunFirst) {
844 template <
typename T>
858 template <
typename T>
871 #endif // GTEST_HAS_DEATH_TEST
883 FAIL() <<
"Expected fatal failure.";
892 printf(
"(expecting 1 failure)\n");
895 printf(
"(expecting 1 failure)\n");
899 printf(
"(expecting 1 failure)\n");
906 printf(
"(expecting 1 failure)\n");
909 printf(
"(expecting 1 failure)\n");
912 printf(
"(expecting 1 failure)\n");
917 #if GTEST_IS_THREADSAFE
921 static void AddFailureInOtherThread(FailureMode failure) {
922 ThreadWithParam<FailureMode>
thread(&AddFailure, failure,
nullptr);
927 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
929 printf(
"(expecting 2 failures)\n");
931 "Expected fatal failure.");
934 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
936 printf(
"(expecting 2 failures)\n");
938 "Expected non-fatal failure.");
941 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
945 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
946 printf(
"(expecting 2 failures)\n");
949 ScopedFakeTestPartResultReporter reporter(
950 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
953 AddFailureInOtherThread(NONFATAL_FAILURE);
959 #endif // GTEST_IS_THREADSAFE
963 printf(
"(expecting 1 failure)\n");
966 printf(
"(expecting 1 failure)\n");
968 "Expected non-fatal failure.");
970 printf(
"(expecting 1 failure)\n");
972 "Some other fatal failure expected.");
977 printf(
"(expecting 1 failure)\n");
981 printf(
"(expecting 1 failure)\n");
983 "Expected fatal failure.");
985 printf(
"(expecting 1 failure)\n");
987 "Some other non-fatal failure.");
999 printf(
"DynamicFixture::TearDownTestSuite\n");
1003 template <
bool Pass>
1012 "DynamicFixture",
"DynamicTestPass",
nullptr,
nullptr, __FILE__,
1015 "DynamicFixture",
"DynamicTestFail",
nullptr,
nullptr, __FILE__,
1020 "DynamicFixtureAnotherName",
"DynamicTestPass",
nullptr,
nullptr,
1026 "BadDynamicFixture1",
"FixtureBase",
nullptr,
nullptr, __FILE__,
1029 "BadDynamicFixture1",
"TestBase",
nullptr,
nullptr, __FILE__, __LINE__,
1035 "BadDynamicFixture2",
"FixtureBase",
nullptr,
nullptr, __FILE__,
1045 void SetUp()
override {
printf(
"%s",
"FooEnvironment::SetUp() called.\n"); }
1048 printf(
"%s",
"FooEnvironment::TearDown() called.\n");
1049 FAIL() <<
"Expected fatal failure.";
1055 void SetUp()
override {
printf(
"%s",
"BarEnvironment::SetUp() called.\n"); }
1058 printf(
"%s",
"BarEnvironment::TearDown() called.\n");
1087 bool internal_skip_environment_and_ad_hoc_tests =
1089 std::string(
"internal_skip_environment_and_ad_hoc_tests")) > 0;
1091 #if GTEST_HAS_DEATH_TEST
1095 # if GTEST_OS_WINDOWS
1099 # endif // GTEST_OS_WINDOWS
1102 #endif // GTEST_HAS_DEATH_TEST
1104 if (internal_skip_environment_and_ad_hoc_tests)