46#if GTEST_IS_THREADSAFE
47using testing::ScopedFakeTestPartResultReporter;
48using testing::TestPartResultArray;
50using testing::internal::Notification;
51using testing::internal::ThreadWithParam;
54namespace posix = ::testing::internal::posix;
76 FAIL() <<
"This should never be reached.";
79TEST(PassingTest, PassingTest1) {
82TEST(PassingTest, PassingTest2) {
107static const char kGoldenString[] =
"\"Line\0 1\"\nLine 2";
109TEST(NonfatalFailureTest, EscapesStringOperands) {
110 std::string actual =
"actual \"string\"";
113 const char* golden = kGoldenString;
117TEST(NonfatalFailureTest, DiffForLongStrings) {
118 std::string golden_str(kGoldenString,
sizeof(kGoldenString) - 1);
123TEST(FatalFailureTest, FatalFailureInSubroutine) {
124 printf(
"(expecting a failure that x should be 1)\n");
130TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
131 printf(
"(expecting a failure that x should be 1)\n");
140 if (HasFatalFailure())
return;
143 FAIL() <<
"This should never be reached.";
147TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
148 printf(
"(expecting a failure on false)\n");
154TEST(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++) {
161 printf(
"i == %d\n",
i);
181TEST(SCOPED_TRACETest, AcceptedValues) {
185 const char* null_value =
nullptr;
188 ADD_FAILURE() <<
"Just checking that all these values work fine.";
192TEST(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.";
211TEST(SCOPED_TRACETest, WorksInLoop) {
212 printf(
"(expected to fail)\n");
214 for (
int i = 1;
i <= 2;
i++) {
222TEST(SCOPED_TRACETest, WorksInSubroutine) {
223 printf(
"(expected to fail)\n");
230TEST(SCOPED_TRACETest, CanBeNested) {
231 printf(
"(expected to fail)\n");
239TEST(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
297static 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();
313TEST(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).";
339TEST(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.";
471TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
472 ADD_FAILURE_AT(
"foo.cc", 42) <<
"Expected nonfatal failure in foo.cc";
475TEST(GtestFailAtTest, MessageContainsSpecifiedFileAndLineNumber) {
476 GTEST_FAIL_AT(
"foo.cc", 42) <<
"Expected fatal failure in foo.cc";
501 TheSecondTestWithThisNameShouldFail) {}
521 TheSecondTestWithThisNameShouldFail) {}
553TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
557 },
"Expected non-fatal failure.");
562TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
567 EXPECT_EQ(m, n) <<
"Expected non-fatal failure.";
568 },
"Expected non-fatal failure.");
573TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
576 },
"Expected non-fatal failure.");
581TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
582 printf(
"(expecting a failure)\n");
589TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
590 printf(
"(expecting a failure)\n");
599TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
600 printf(
"(expecting a failure)\n");
602 FAIL() <<
"Expected fatal failure.";
608TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
609 printf(
"(expecting a failure)\n");
615#if GTEST_HAS_EXCEPTIONS
619TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
620 printf(
"(expecting a failure)\n");
632TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
636 },
"Expected fatal failure.");
641TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
645 ASSERT_EQ(0, n) <<
"Expected fatal failure.";
646 },
"Expected fatal failure.");
651TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
653 FAIL() <<
"Expected fatal failure.";
654 },
"Expected fatal failure.");
659TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
660 printf(
"(expecting a failure)\n");
667 FAIL() <<
"Expected fatal failure.";
672TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
673 printf(
"(expecting a failure)\n");
682TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
683 printf(
"(expecting a failure)\n");
691TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
692 printf(
"(expecting a failure)\n");
698#if GTEST_HAS_EXCEPTIONS
702TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
703 printf(
"(expecting a failure)\n");
728 EXPECT_EQ(
"b", GetParam()) <<
"Expected failure";
758 EXPECT_EQ(1, TypeParam()) <<
"Expected failure";
768 template <
typename T>
794 EXPECT_EQ(1U, TypeParam()) <<
"Expected failure";
804 template <
typename T>
822 TypeParam instantiate;
833#if GTEST_HAS_DEATH_TEST
838TEST(ADeathTest, ShouldRunFirst) {
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);
927TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
929 printf(
"(expecting 2 failures)\n");
931 "Expected fatal failure.");
934TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
936 printf(
"(expecting 2 failures)\n");
938 "Expected non-fatal failure.");
941typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
945TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
946 printf(
"(expecting 2 failures)\n");
947 TestPartResultArray results;
949 ScopedFakeTestPartResultReporter reporter(
950 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
952 AddFailureInOtherThread(FATAL_FAILURE);
953 AddFailureInOtherThread(NONFATAL_FAILURE);
956 EXPECT_EQ(0, results.size()) <<
"This shouldn't fail.";
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.");
994 void SetUp()
override { printf(
"DynamicFixture::SetUp\n"); }
995 void TearDown()
override { printf(
"DynamicFixture::TearDown\n"); }
999 printf(
"DynamicFixture::TearDownTestSuite\n");
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");
1079 bool internal_skip_environment_and_ad_hoc_tests =
1081 std::string(
"internal_skip_environment_and_ad_hoc_tests")) > 0;
1083#if GTEST_HAS_DEATH_TEST
1087# if GTEST_OS_WINDOWS
1096 if (internal_skip_environment_and_ad_hoc_tests)
#define ASSERT_EQ(val1, val2)
#define EXPECT_EQ(val1, val2)
#define ADD_FAILURE_AT(file, line)
#define SCOPED_TRACE(message)
#define ASSERT_FALSE(condition)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
#define EXPECT_GE(val1, val2)
#define EXPECT_TRUE(condition)
#define GTEST_FAIL_AT(file, line)
#define EXPECT_FATAL_FAILURE(statement, substr)
#define EXPECT_NONFATAL_FAILURE(statement, substr)
#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr)
#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr)
#define GTEST_DISABLE_MSC_WARNINGS_PUSH_(warnings)
#define GTEST_DISABLE_MSC_WARNINGS_POP_()
INSTANTIATE_TYPED_TEST_SUITE_P(Unsigned, TypedTestP, UnsignedTypes)
REGISTER_TYPED_TEST_SUITE_P(TypedTestP, Success, Failure)
TYPED_TEST_SUITE_P(TypedTestP)
int main(int argc, char **argv)
testing::Types< char, int > TypesForTestWithNames
testing::Types< unsigned char, unsigned int > UnsignedTypes
std::string ParamNameFunc(const testing::TestParamInfo< std::string > &info)
TEST(PassingTest, PassingTest1)
TEST_P(FailingParamTest, Fails)
INSTANTIATE_TEST_SUITE_P(PrintingFailingParams, FailingParamTest, testing::Values(2))
void SubWithoutTrace(int n)
TYPED_TEST(TypedTest, Success)
TYPED_TEST_SUITE(TypedTest, testing::Types< int >)
TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor)
TYPED_TEST_P(TypedTestP, Success)
Environment * AddGlobalTestEnvironment(Environment *env)
TestInfo * RegisterTest(const char *test_suite_name, const char *test_name, const char *type_param, const char *value_param, const char *file, int line, Factory factory)
::std::string PrintToString(const T &value)
internal::ValueArray< T... > Values(T... v)
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
FILE * FReopen(const char *path, const char *mode, FILE *stream)
TEST_F(MixedUpTestSuiteTest, FirstTestFromNamespaceFoo)
TEST_F(MixedUpTestSuiteTest, ThisShouldFail)
Types< int, long > NumericTypes
static bool HasFatalFailure()
~NonFatalFailureInFixtureConstructorTest() override
NonFatalFailureInFixtureConstructorTest()
FatalFailureInFixtureConstructorTest()
~FatalFailureInFixtureConstructorTest() override
~NonFatalFailureInSetUpTest() override
~FatalFailureInSetUpTest() override
static std::string GetName(int i)
static std::string GetName(int i)
static void AddFailure(FailureMode failure)
static void TearDownTestSuite()
static void SetUpTestSuite()
~DynamicFixture() override