38 #include "gtest/gtest-spi.h" 39 #include "gtest/gtest.h" 46 #define GTEST_IMPLEMENTATION_ 1 47 #include "src/gtest-internal-inl.h" 48 #undef GTEST_IMPLEMENTATION_ 52 #if GTEST_IS_THREADSAFE 56 using testing::internal::Notification;
57 using testing::internal::ThreadWithParam;
60 namespace posix = ::testing::internal::posix;
83 FAIL() <<
"This should never be reached.";
86 TEST(PassingTest, PassingTest1) {
89 TEST(PassingTest, PassingTest2) {
108 TEST(NonfatalFailureTest, EscapesStringOperands) {
117 TEST(FatalFailureTest, FatalFailureInSubroutine) {
118 printf(
"(expecting a failure that x should be 1)\n");
124 TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
125 printf(
"(expecting a failure that x should be 1)\n");
137 FAIL() <<
"This should never be reached.";
141 TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
142 printf(
"(expecting a failure on false)\n");
148 TEST(LoggingTest, InterleavingLoggingAndAssertions) {
149 static const int a[4] = {
153 printf(
"(expecting 2 failures on (3) >= (a[i]))\n");
154 for (
int i = 0; i < static_cast<int>(
sizeof(a)/
sizeof(*a)); i++) {
176 TEST(SCOPED_TRACETest, ObeysScopes) {
177 printf(
"(expected to fail)\n");
180 ADD_FAILURE() <<
"This failure is expected, and shouldn't have a trace.";
186 ADD_FAILURE() <<
"This failure is expected, and should have a trace.";
191 ADD_FAILURE() <<
"This failure is expected, and shouldn't have a trace.";
195 TEST(SCOPED_TRACETest, WorksInLoop) {
196 printf(
"(expected to fail)\n");
198 for (
int i = 1; i <= 2; i++) {
206 TEST(SCOPED_TRACETest, WorksInSubroutine) {
207 printf(
"(expected to fail)\n");
214 TEST(SCOPED_TRACETest, CanBeNested) {
215 printf(
"(expected to fail)\n");
223 TEST(SCOPED_TRACETest, CanBeRepeated) {
224 printf(
"(expected to fail)\n");
228 <<
"This failure is expected, and should contain trace point A.";
232 <<
"This failure is expected, and should contain trace point A and B.";
236 ADD_FAILURE() <<
"This failure is expected, and should " 237 <<
"contain trace point A, B, and C.";
241 ADD_FAILURE() <<
"This failure is expected, and should " 242 <<
"contain trace point A, B, and D.";
245 #if GTEST_IS_THREADSAFE 281 static void ThreadWithScopedTrace(CheckPoints* check_points) {
285 <<
"Expected failure #1 (in thread B, only trace B alive).";
286 check_points->n1.Notify();
287 check_points->n2.WaitForNotification();
290 <<
"Expected failure #3 (in thread B, trace A & B both alive).";
293 <<
"Expected failure #4 (in thread B, only trace A alive).";
294 check_points->n3.Notify();
297 TEST(SCOPED_TRACETest, WorksConcurrently) {
298 printf(
"(expecting 6 failures)\n");
300 CheckPoints check_points;
301 ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace,
304 check_points.n1.WaitForNotification();
309 <<
"Expected failure #2 (in thread A, trace A & B both alive).";
310 check_points.n2.Notify();
311 check_points.n3.WaitForNotification();
314 <<
"Expected failure #5 (in thread A, only trace A alive).";
317 <<
"Expected failure #6 (in thread A, no trace alive).";
320 #endif // GTEST_IS_THREADSAFE 323 DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
334 printf(
"The non-test part of the code is expected to have 2 failures.\n\n");
349 printf(
"(expecting 5 failures)\n");
350 ADD_FAILURE() <<
"Expected failure #1, in the test fixture c'tor.";
354 ADD_FAILURE() <<
"Expected failure #5, in the test fixture d'tor.";
358 ADD_FAILURE() <<
"Expected failure #2, in SetUp().";
362 ADD_FAILURE() <<
"Expected failure #4, in TearDown.";
367 ADD_FAILURE() <<
"Expected failure #3, in the test body.";
374 printf(
"(expecting 2 failures)\n");
379 ADD_FAILURE() <<
"Expected failure #2, in the test fixture d'tor.";
384 <<
"We should never get here, as the test fixture c'tor " 385 <<
"had a fatal failure.";
389 ADD_FAILURE() <<
"UNEXPECTED failure in TearDown(). " 390 <<
"We should never get here, as the test fixture c'tor " 391 <<
"had a fatal failure.";
396 FAIL() <<
"Expected failure #1, in the test fixture c'tor.";
401 ADD_FAILURE() <<
"UNEXPECTED failure in the test body. " 402 <<
"We should never get here, as the test fixture c'tor " 403 <<
"had a fatal failure.";
414 printf(
"(expecting 4 failures)\n");
415 ADD_FAILURE() <<
"Expected failure #1, in SetUp().";
419 FAIL() <<
"Expected failure #3, in TearDown().";
423 FAIL() <<
"Expected failure #4, in the test fixture d'tor.";
428 FAIL() <<
"Expected failure #2, in the test function.";
439 printf(
"(expecting 3 failures)\n");
440 FAIL() <<
"Expected failure #1, in SetUp().";
444 FAIL() <<
"Expected failure #2, in TearDown().";
448 FAIL() <<
"Expected failure #3, in the test fixture d'tor.";
453 FAIL() <<
"UNEXPECTED failure in the test function. " 454 <<
"We should never get here, as SetUp() failed.";
457 TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
461 #if GTEST_IS_THREADSAFE 464 void DieIf(
bool should_die) {
465 GTEST_CHECK_(!should_die) <<
" - death inside DieIf().";
471 struct SpawnThreadNotifications {
472 SpawnThreadNotifications() {}
474 Notification spawn_thread_started;
475 Notification spawn_thread_ok_to_terminate;
483 static void ThreadRoutine(SpawnThreadNotifications* notifications) {
485 notifications->spawn_thread_started.Notify();
488 notifications->spawn_thread_ok_to_terminate.WaitForNotification();
497 virtual void SetUp() {
498 thread_.reset(
new ThreadWithParam<SpawnThreadNotifications*>(
499 &ThreadRoutine, ¬ifications_, NULL));
500 notifications_.spawn_thread_started.WaitForNotification();
508 notifications_.spawn_thread_ok_to_terminate.Notify();
512 SpawnThreadNotifications notifications_;
516 #endif // GTEST_IS_THREADSAFE 533 TEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {}
534 TEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {}
536 class MixedUpTestCaseWithSameTestNameTest :
public testing::Test {
539 TEST_F(MixedUpTestCaseWithSameTestNameTest,
540 TheSecondTestWithThisNameShouldFail) {}
551 TEST_F(MixedUpTestCaseTest, ThisShouldFail) {}
552 TEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {}
554 class MixedUpTestCaseWithSameTestNameTest :
public testing::Test {
559 TEST_F(MixedUpTestCaseWithSameTestNameTest,
560 TheSecondTestWithThisNameShouldFail) {}
592 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
596 },
"Expected non-fatal failure.");
601 TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
606 EXPECT_EQ(m, n) <<
"Expected non-fatal failure.";
607 },
"Expected non-fatal failure.");
612 TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
615 },
"Expected non-fatal failure.");
620 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
621 printf(
"(expecting a failure)\n");
628 TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
629 printf(
"(expecting a failure)\n");
638 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
639 printf(
"(expecting a failure)\n");
641 FAIL() <<
"Expected fatal failure.";
647 TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
648 printf(
"(expecting a failure)\n");
654 #if GTEST_HAS_EXCEPTIONS 658 TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
659 printf(
"(expecting a failure)\n");
668 #endif // GTEST_HAS_EXCEPTIONS 671 TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
675 },
"Expected fatal failure.");
680 TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
684 ASSERT_EQ(0, n) <<
"Expected fatal failure.";
685 },
"Expected fatal failure.");
690 TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
692 FAIL() <<
"Expected fatal failure.";
693 },
"Expected fatal failure.");
698 TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
699 printf(
"(expecting a failure)\n");
706 FAIL() <<
"Expected fatal failure.";
711 TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
712 printf(
"(expecting a failure)\n");
721 TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
722 printf(
"(expecting a failure)\n");
730 TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
731 printf(
"(expecting a failure)\n");
737 #if GTEST_HAS_EXCEPTIONS 741 TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
742 printf(
"(expecting a failure)\n");
751 #endif // GTEST_HAS_EXCEPTIONS 754 #if GTEST_HAS_TYPED_TEST 756 template <
typename T>
767 EXPECT_EQ(1, TypeParam()) <<
"Expected failure";
770 #endif // GTEST_HAS_TYPED_TEST 773 #if GTEST_HAS_TYPED_TEST_P 775 template <
typename T>
786 EXPECT_EQ(1U, TypeParam()) <<
"Expected failure";
791 typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
794 #endif // GTEST_HAS_TYPED_TEST_P 796 #if GTEST_HAS_DEATH_TEST 801 TEST(ADeathTest, ShouldRunFirst) {
804 # if GTEST_HAS_TYPED_TEST 809 template <
typename T>
813 typedef testing::Types<int, double> NumericTypes;
819 # endif // GTEST_HAS_TYPED_TEST 821 # if GTEST_HAS_TYPED_TEST_P 827 template <
typename T>
840 # endif // GTEST_HAS_TYPED_TEST_P 842 #endif // GTEST_HAS_DEATH_TEST 853 if (failure == FATAL_FAILURE) {
854 FAIL() <<
"Expected fatal failure.";
863 printf(
"(expecting 1 failure)\n");
866 printf(
"(expecting 1 failure)\n");
870 printf(
"(expecting 1 failure)\n");
877 printf(
"(expecting 1 failure)\n");
880 printf(
"(expecting 1 failure)\n");
883 printf(
"(expecting 1 failure)\n");
888 #if GTEST_IS_THREADSAFE 892 static void AddFailureInOtherThread(FailureMode failure) {
893 ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL);
898 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
900 printf(
"(expecting 2 failures)\n");
902 "Expected fatal failure.");
905 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
907 printf(
"(expecting 2 failures)\n");
909 "Expected non-fatal failure.");
912 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
916 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
917 printf(
"(expecting 2 failures)\n");
918 TestPartResultArray results;
920 ScopedFakeTestPartResultReporter reporter(
921 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
923 AddFailureInOtherThread(FATAL_FAILURE);
924 AddFailureInOtherThread(NONFATAL_FAILURE);
927 EXPECT_EQ(0, results.size()) <<
"This shouldn't fail.";
930 #endif // GTEST_IS_THREADSAFE 934 printf(
"(expecting 1 failure)\n");
937 printf(
"(expecting 1 failure)\n");
939 "Expected non-fatal failure.");
941 printf(
"(expecting 1 failure)\n");
943 "Some other fatal failure expected.");
948 printf(
"(expecting 1 failure)\n");
952 printf(
"(expecting 1 failure)\n");
954 "Expected fatal failure.");
956 printf(
"(expecting 1 failure)\n");
958 "Some other non-fatal failure.");
967 printf(
"%s",
"FooEnvironment::SetUp() called.\n");
971 printf(
"%s",
"FooEnvironment::TearDown() called.\n");
972 FAIL() <<
"Expected fatal failure.";
979 printf(
"%s",
"BarEnvironment::SetUp() called.\n");
983 printf(
"%s",
"BarEnvironment::TearDown() called.\n");
988 bool GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) =
false;
995 int main(
int argc,
char **argv) {
1008 "--gtest_internal_skip_environment_and_ad_hoc_tests"))
1009 GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) =
true;
1011 #if GTEST_HAS_DEATH_TEST 1015 # if GTEST_OS_WINDOWS 1019 # endif // GTEST_OS_WINDOWS 1022 #endif // GTEST_HAS_DEATH_TEST 1024 if (
GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests))
static bool HasFatalFailure()
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
void SubWithoutTrace(int n)
#define EXPECT_FATAL_FAILURE(statement, substr)
TYPED_TEST_CASE_P(TypeParamTest)
~NonFatalFailureInFixtureConstructorTest()
#define EXPECT_NONFATAL_FAILURE(statement, substr)
Environment * AddGlobalTestEnvironment(Environment *env)
GTEST_DISALLOW_COPY_AND_ASSIGN_(Test)
TYPED_TEST_P(TypeParamTest, TestA)
NonFatalFailureInFixtureConstructorTest()
#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr)
FILE * FReopen(const char *path, const char *mode, FILE *stream)
REGISTER_TYPED_TEST_CASE_P(TypeParamTest, TestA, TestB)
#define EXPECT_TRUE(condition)
TYPED_TEST_CASE(TypedTest, MyTypes)
TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor)
INSTANTIATE_TYPED_TEST_CASE_P(My, TypeParamTest, MyTypes)
virtual ~FatalFailureInSetUpTest()
~FatalFailureInFixtureConstructorTest()
static const char kGoldenString[]
virtual ~NonFatalFailureInSetUpTest()
bool GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests)
#define EXPECT_GE(val1, val2)
INSTANTIATE_TEST_CASE_P(PrintingFailingParams, FailingParamTest, testing::Values(2))
#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr)
static void AddFailure(FailureMode failure)
TEST_P(FailingParamTest, Fails)
#define ASSERT_EQ(val1, val2)
FatalFailureInFixtureConstructorTest()
#define ADD_FAILURE_AT(file, line)
TYPED_TEST(TypedTest, TestA)
void InitGoogleTest(int *argc, char **argv)
internal::ValueArray1< T1 > Values(T1 v1)
#define SCOPED_TRACE(message)
#define GTEST_CHECK_(condition)
int main(int argc, char **argv)
#define EXPECT_EQ(expected, actual)
const ParamType & GetParam() const
TEST(PassingTest, PassingTest1)
#define ASSERT_FALSE(condition)
void printf(BasicWriter< Char > &w, BasicCStringRef< Char > format, ArgList args)