1 // Copyright 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // The purpose of this file is to generate Google Test output under
31 // various conditions. The output will then be verified by
32 // googletest-output-test.py to ensure that Google Test generates the
33 // desired messages. Therefore, most tests in this file are MEANT TO
36 #include "gtest/gtest-spi.h"
37 #include "gtest/gtest.h"
38 #include "src/gtest-internal-inl.h"
43 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */)
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;
56 // Tests catching fatal failures.
58 // A subroutine used by the following test.
63 // This function calls a test subroutine, catches the fatal failure it
64 // generates, and then returns early.
65 void TryTestSubroutine() {
66 // Calls a subrountine that yields a fatal failure.
69 // Catches the fatal failure and aborts the test.
71 // The testing::Test:: prefix is necessary when calling
72 // HasFatalFailure() outside of a TEST, TEST_F, or test fixture.
73 if (testing::Test::HasFatalFailure()) return;
75 // If we get here, something is wrong.
76 FAIL() << "This should never be reached.";
79 TEST(PassingTest, PassingTest1) {
82 TEST(PassingTest, PassingTest2) {
85 // Tests that parameters of failing parameterized tests are printed in the
86 // failing test summary.
87 class FailingParamTest : public testing::TestWithParam<int> {};
89 TEST_P(FailingParamTest, Fails) {
90 EXPECT_EQ(1, GetParam());
93 // This generates a test which will fail. Google Test is expected to print
94 // its parameter when it outputs the list of all failed tests.
95 INSTANTIATE_TEST_CASE_P(PrintingFailingParams,
99 static const char kGoldenString[] = "\"Line\0 1\"\nLine 2";
101 TEST(NonfatalFailureTest, EscapesStringOperands) {
102 std::string actual = "actual \"string\"";
103 EXPECT_EQ(kGoldenString, actual);
105 const char* golden = kGoldenString;
106 EXPECT_EQ(golden, actual);
109 TEST(NonfatalFailureTest, DiffForLongStrings) {
110 std::string golden_str(kGoldenString, sizeof(kGoldenString) - 1);
111 EXPECT_EQ(golden_str, "Line 2");
114 // Tests catching a fatal failure in a subroutine.
115 TEST(FatalFailureTest, FatalFailureInSubroutine) {
116 printf("(expecting a failure that x should be 1)\n");
121 // Tests catching a fatal failure in a nested subroutine.
122 TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
123 printf("(expecting a failure that x should be 1)\n");
125 // Calls a subrountine that yields a fatal failure.
128 // Catches the fatal failure and aborts the test.
130 // When calling HasFatalFailure() inside a TEST, TEST_F, or test
131 // fixture, the testing::Test:: prefix is not needed.
132 if (HasFatalFailure()) return;
134 // If we get here, something is wrong.
135 FAIL() << "This should never be reached.";
138 // Tests HasFatalFailure() after a failed EXPECT check.
139 TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
140 printf("(expecting a failure on false)\n");
141 EXPECT_TRUE(false); // Generates a nonfatal failure
142 ASSERT_FALSE(HasFatalFailure()); // This should succeed.
145 // Tests interleaving user logging and Google Test assertions.
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);
158 // Tests the SCOPED_TRACE macro.
160 // A helper function for testing SCOPED_TRACE.
161 void SubWithoutTrace(int n) {
166 // Another helper function for testing SCOPED_TRACE.
167 void SubWithTrace(int n) {
168 SCOPED_TRACE(testing::Message() << "n = " << n);
173 TEST(SCOPED_TRACETest, AcceptedValues) {
174 SCOPED_TRACE("literal string");
175 SCOPED_TRACE(std::string("std::string"));
176 SCOPED_TRACE(1337); // streamable type
177 const char* null_value = NULL;
178 SCOPED_TRACE(null_value);
180 ADD_FAILURE() << "Just checking that all these values work fine.";
183 // Tests that SCOPED_TRACE() obeys lexical scopes.
184 TEST(SCOPED_TRACETest, ObeysScopes) {
185 printf("(expected to fail)\n");
187 // There should be no trace before SCOPED_TRACE() is invoked.
188 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
191 SCOPED_TRACE("Expected trace");
192 // After SCOPED_TRACE(), a failure in the current scope should contain
194 ADD_FAILURE() << "This failure is expected, and should have a trace.";
197 // Once the control leaves the scope of the SCOPED_TRACE(), there
198 // should be no trace again.
199 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
202 // Tests that SCOPED_TRACE works inside a loop.
203 TEST(SCOPED_TRACETest, WorksInLoop) {
204 printf("(expected to fail)\n");
206 for (int i = 1; i <= 2; i++) {
207 SCOPED_TRACE(testing::Message() << "i = " << i);
213 // Tests that SCOPED_TRACE works in a subroutine.
214 TEST(SCOPED_TRACETest, WorksInSubroutine) {
215 printf("(expected to fail)\n");
221 // Tests that SCOPED_TRACE can be nested.
222 TEST(SCOPED_TRACETest, CanBeNested) {
223 printf("(expected to fail)\n");
225 SCOPED_TRACE(""); // A trace without a message.
230 // Tests that multiple SCOPED_TRACEs can be used in the same scope.
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
254 // Tests that SCOPED_TRACE()s can be used concurrently from multiple
255 // threads. Namely, an assertion should be affected by
256 // SCOPED_TRACE()s in its own thread only.
258 // Here's the sequence of actions that happen in the test:
260 // Thread A (main) | Thread B (spawned)
261 // ===============================|================================
263 // -------------------------------+--------------------------------
264 // waits for n1 | SCOPED_TRACE("Trace B");
265 // | generates failure #1
267 // -------------------------------+--------------------------------
268 // SCOPED_TRACE("Trace A"); | waits for n2
269 // generates failure #2 |
271 // -------------------------------|--------------------------------
272 // waits for n3 | generates failure #3
274 // | generates failure #4
276 // -------------------------------|--------------------------------
277 // generates failure #5 | finishes
279 // generates failure #6 |
280 // -------------------------------|--------------------------------
281 // waits for thread B to finish |
289 static void ThreadWithScopedTrace(CheckPoints* check_points) {
291 SCOPED_TRACE("Trace B");
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).";
299 } // Trace B dies here.
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,
312 check_points.n1.WaitForNotification();
315 SCOPED_TRACE("Trace A");
317 << "Expected failure #2 (in thread A, trace A & B both alive).";
318 check_points.n2.Notify();
319 check_points.n3.WaitForNotification();
322 << "Expected failure #5 (in thread A, only trace A alive).";
323 } // Trace A dies here.
325 << "Expected failure #6 (in thread A, no trace alive).";
328 #endif // GTEST_IS_THREADSAFE
330 // Tests basic functionality of the ScopedTrace utility (most of its features
331 // are already tested in SCOPED_TRACETest).
332 TEST(ScopedTraceTest, WithExplicitFileAndLine) {
333 testing::ScopedTrace trace("explicit_file.cc", 123, "expected trace message");
334 ADD_FAILURE() << "Check that the trace is attached to a particular location.";
337 TEST(DisabledTestsWarningTest,
338 DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
339 // This test body is intentionally empty. Its sole purpose is for
340 // verifying that the --gtest_also_run_disabled_tests flag
341 // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of
345 // Tests using assertions outside of TEST and TEST_F.
347 // This function creates two failures intentionally.
349 printf("The non-test part of the code is expected to have 2 failures.\n\n");
354 // Runs all TESTs, all TEST_Fs, and the ad hoc test.
357 return RUN_ALL_TESTS();
360 // Tests non-fatal failures in the fixture constructor.
361 class NonFatalFailureInFixtureConstructorTest : public testing::Test {
363 NonFatalFailureInFixtureConstructorTest() {
364 printf("(expecting 5 failures)\n");
365 ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor.";
368 ~NonFatalFailureInFixtureConstructorTest() {
369 ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor.";
372 virtual void SetUp() {
373 ADD_FAILURE() << "Expected failure #2, in SetUp().";
376 virtual void TearDown() {
377 ADD_FAILURE() << "Expected failure #4, in TearDown.";
381 TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) {
382 ADD_FAILURE() << "Expected failure #3, in the test body.";
385 // Tests fatal failures in the fixture constructor.
386 class FatalFailureInFixtureConstructorTest : public testing::Test {
388 FatalFailureInFixtureConstructorTest() {
389 printf("(expecting 2 failures)\n");
393 ~FatalFailureInFixtureConstructorTest() {
394 ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor.";
397 virtual void SetUp() {
398 ADD_FAILURE() << "UNEXPECTED failure in SetUp(). "
399 << "We should never get here, as the test fixture c'tor "
400 << "had a fatal failure.";
403 virtual void TearDown() {
404 ADD_FAILURE() << "UNEXPECTED failure in TearDown(). "
405 << "We should never get here, as the test fixture c'tor "
406 << "had a fatal failure.";
411 FAIL() << "Expected failure #1, in the test fixture c'tor.";
415 TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) {
416 ADD_FAILURE() << "UNEXPECTED failure in the test body. "
417 << "We should never get here, as the test fixture c'tor "
418 << "had a fatal failure.";
421 // Tests non-fatal failures in SetUp().
422 class NonFatalFailureInSetUpTest : public testing::Test {
424 virtual ~NonFatalFailureInSetUpTest() {
428 virtual void SetUp() {
429 printf("(expecting 4 failures)\n");
430 ADD_FAILURE() << "Expected failure #1, in SetUp().";
433 virtual void TearDown() {
434 FAIL() << "Expected failure #3, in TearDown().";
438 FAIL() << "Expected failure #4, in the test fixture d'tor.";
442 TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) {
443 FAIL() << "Expected failure #2, in the test function.";
446 // Tests fatal failures in SetUp().
447 class FatalFailureInSetUpTest : public testing::Test {
449 virtual ~FatalFailureInSetUpTest() {
453 virtual void SetUp() {
454 printf("(expecting 3 failures)\n");
455 FAIL() << "Expected failure #1, in SetUp().";
458 virtual void TearDown() {
459 FAIL() << "Expected failure #2, in TearDown().";
463 FAIL() << "Expected failure #3, in the test fixture d'tor.";
467 TEST_F(FatalFailureInSetUpTest, FailureInSetUp) {
468 FAIL() << "UNEXPECTED failure in the test function. "
469 << "We should never get here, as SetUp() failed.";
472 TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
473 ADD_FAILURE_AT("foo.cc", 42) << "Expected failure in foo.cc";
476 #if GTEST_IS_THREADSAFE
478 // A unary function that may die.
479 void DieIf(bool should_die) {
480 GTEST_CHECK_(!should_die) << " - death inside DieIf().";
483 // Tests running death tests in a multi-threaded context.
485 // Used for coordination between the main and the spawn thread.
486 struct SpawnThreadNotifications {
487 SpawnThreadNotifications() {}
489 Notification spawn_thread_started;
490 Notification spawn_thread_ok_to_terminate;
493 GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications);
496 // The function to be executed in the thread spawn by the
497 // MultipleThreads test (below).
498 static void ThreadRoutine(SpawnThreadNotifications* notifications) {
499 // Signals the main thread that this thread has started.
500 notifications->spawn_thread_started.Notify();
502 // Waits for permission to finish from the main thread.
503 notifications->spawn_thread_ok_to_terminate.WaitForNotification();
506 // This is a death-test test, but it's not named with a DeathTest
507 // suffix. It starts threads which might interfere with later
508 // death tests, so it must run after all other death tests.
509 class DeathTestAndMultiThreadsTest : public testing::Test {
511 // Starts a thread and waits for it to begin.
512 virtual void SetUp() {
513 thread_.reset(new ThreadWithParam<SpawnThreadNotifications*>(
514 &ThreadRoutine, ¬ifications_, NULL));
515 notifications_.spawn_thread_started.WaitForNotification();
517 // Tells the thread to finish, and reaps it.
518 // Depending on the version of the thread library in use,
519 // a manager thread might still be left running that will interfere
520 // with later death tests. This is unfortunate, but this class
521 // cleans up after itself as best it can.
522 virtual void TearDown() {
523 notifications_.spawn_thread_ok_to_terminate.Notify();
527 SpawnThreadNotifications notifications_;
528 testing::internal::scoped_ptr<ThreadWithParam<SpawnThreadNotifications*> >
532 #endif // GTEST_IS_THREADSAFE
534 // The MixedUpTestCaseTest test case verifies that Google Test will fail a
535 // test if it uses a different fixture class than what other tests in
536 // the same test case use. It deliberately contains two fixture
537 // classes with the same name but defined in different namespaces.
539 // The MixedUpTestCaseWithSameTestNameTest test case verifies that
540 // when the user defines two tests with the same test case name AND
541 // same test name (but in different namespaces), the second test will
546 class MixedUpTestCaseTest : public testing::Test {
549 TEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {}
550 TEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {}
552 class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
555 TEST_F(MixedUpTestCaseWithSameTestNameTest,
556 TheSecondTestWithThisNameShouldFail) {}
562 class MixedUpTestCaseTest : public testing::Test {
565 // The following two tests are expected to fail. We rely on the
566 // golden file to check that Google Test generates the right error message.
567 TEST_F(MixedUpTestCaseTest, ThisShouldFail) {}
568 TEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {}
570 class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
573 // Expected to fail. We rely on the golden file to check that Google Test
574 // generates the right error message.
575 TEST_F(MixedUpTestCaseWithSameTestNameTest,
576 TheSecondTestWithThisNameShouldFail) {}
580 // The following two test cases verify that Google Test catches the user
581 // error of mixing TEST and TEST_F in the same test case. The first
582 // test case checks the scenario where TEST_F appears before TEST, and
583 // the second one checks where TEST appears before TEST_F.
585 class TEST_F_before_TEST_in_same_test_case : public testing::Test {
588 TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {}
590 // Expected to fail. We rely on the golden file to check that Google Test
591 // generates the right error message.
592 TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {}
594 class TEST_before_TEST_F_in_same_test_case : public testing::Test {
597 TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {}
599 // Expected to fail. We rely on the golden file to check that Google Test
600 // generates the right error message.
601 TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {
604 // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE().
605 int global_integer = 0;
607 // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables.
608 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
610 EXPECT_NONFATAL_FAILURE({
611 EXPECT_EQ(1, global_integer) << "Expected non-fatal failure.";
612 }, "Expected non-fatal failure.");
615 // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables
617 TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
621 EXPECT_NONFATAL_FAILURE({
622 EXPECT_EQ(m, n) << "Expected non-fatal failure.";
623 }, "Expected non-fatal failure.");
626 // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly
627 // one non-fatal failure and no fatal failure.
628 TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
629 EXPECT_NONFATAL_FAILURE({
630 ADD_FAILURE() << "Expected non-fatal failure.";
631 }, "Expected non-fatal failure.");
634 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no
635 // non-fatal failure.
636 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
637 printf("(expecting a failure)\n");
638 EXPECT_NONFATAL_FAILURE({
642 // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two
643 // non-fatal failures.
644 TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
645 printf("(expecting a failure)\n");
646 EXPECT_NONFATAL_FAILURE({
647 ADD_FAILURE() << "Expected non-fatal failure 1.";
648 ADD_FAILURE() << "Expected non-fatal failure 2.";
652 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal
654 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
655 printf("(expecting a failure)\n");
656 EXPECT_NONFATAL_FAILURE({
657 FAIL() << "Expected fatal failure.";
661 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
663 TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
664 printf("(expecting a failure)\n");
665 EXPECT_NONFATAL_FAILURE({
670 #if GTEST_HAS_EXCEPTIONS
672 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
674 TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
675 printf("(expecting a failure)\n");
677 EXPECT_NONFATAL_FAILURE({
680 } catch(int) { // NOLINT
684 #endif // GTEST_HAS_EXCEPTIONS
686 // Tests that EXPECT_FATAL_FAILURE() can reference global variables.
687 TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
689 EXPECT_FATAL_FAILURE({
690 ASSERT_EQ(1, global_integer) << "Expected fatal failure.";
691 }, "Expected fatal failure.");
694 // Tests that EXPECT_FATAL_FAILURE() can reference local static
696 TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
699 EXPECT_FATAL_FAILURE({
700 ASSERT_EQ(0, n) << "Expected fatal failure.";
701 }, "Expected fatal failure.");
704 // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly
705 // one fatal failure and no non-fatal failure.
706 TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
707 EXPECT_FATAL_FAILURE({
708 FAIL() << "Expected fatal failure.";
709 }, "Expected fatal failure.");
712 // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal
714 TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
715 printf("(expecting a failure)\n");
716 EXPECT_FATAL_FAILURE({
720 // A helper for generating a fatal failure.
721 void FatalFailure() {
722 FAIL() << "Expected fatal failure.";
725 // Tests that EXPECT_FATAL_FAILURE() fails when there are two
727 TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
728 printf("(expecting a failure)\n");
729 EXPECT_FATAL_FAILURE({
735 // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal
737 TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
738 printf("(expecting a failure)\n");
739 EXPECT_FATAL_FAILURE({
740 ADD_FAILURE() << "Expected non-fatal failure.";
744 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
746 TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
747 printf("(expecting a failure)\n");
748 EXPECT_FATAL_FAILURE({
753 #if GTEST_HAS_EXCEPTIONS
755 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
757 TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
758 printf("(expecting a failure)\n");
760 EXPECT_FATAL_FAILURE({
763 } catch(int) { // NOLINT
767 #endif // GTEST_HAS_EXCEPTIONS
769 // This #ifdef block tests the output of value-parameterized tests.
771 std::string ParamNameFunc(const testing::TestParamInfo<std::string>& info) {
775 class ParamTest : public testing::TestWithParam<std::string> {
778 TEST_P(ParamTest, Success) {
779 EXPECT_EQ("a", GetParam());
782 TEST_P(ParamTest, Failure) {
783 EXPECT_EQ("b", GetParam()) << "Expected failure";
786 INSTANTIATE_TEST_CASE_P(PrintingStrings,
788 testing::Values(std::string("a")),
791 // This #ifdef block tests the output of typed tests.
792 #if GTEST_HAS_TYPED_TEST
794 template <typename T>
795 class TypedTest : public testing::Test {
798 TYPED_TEST_CASE(TypedTest, testing::Types<int>);
800 TYPED_TEST(TypedTest, Success) {
801 EXPECT_EQ(0, TypeParam());
804 TYPED_TEST(TypedTest, Failure) {
805 EXPECT_EQ(1, TypeParam()) << "Expected failure";
808 typedef testing::Types<char, int> TypesForTestWithNames;
810 template <typename T>
811 class TypedTestWithNames : public testing::Test {};
813 class TypedTestNames {
815 template <typename T>
816 static std::string GetName(int i) {
817 if (testing::internal::IsSame<T, char>::value)
818 return std::string("char") + ::testing::PrintToString(i);
819 if (testing::internal::IsSame<T, int>::value)
820 return std::string("int") + ::testing::PrintToString(i);
824 TYPED_TEST_CASE(TypedTestWithNames, TypesForTestWithNames, TypedTestNames);
826 TYPED_TEST(TypedTestWithNames, Success) {}
828 TYPED_TEST(TypedTestWithNames, Failure) { FAIL(); }
830 #endif // GTEST_HAS_TYPED_TEST
832 // This #ifdef block tests the output of type-parameterized tests.
833 #if GTEST_HAS_TYPED_TEST_P
835 template <typename T>
836 class TypedTestP : public testing::Test {
839 TYPED_TEST_CASE_P(TypedTestP);
841 TYPED_TEST_P(TypedTestP, Success) {
842 EXPECT_EQ(0U, TypeParam());
845 TYPED_TEST_P(TypedTestP, Failure) {
846 EXPECT_EQ(1U, TypeParam()) << "Expected failure";
849 REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure);
851 typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
852 INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes);
854 class TypedTestPNames {
856 template <typename T>
857 static std::string GetName(int i) {
858 if (testing::internal::IsSame<T, unsigned char>::value) {
859 return std::string("unsignedChar") + ::testing::PrintToString(i);
861 if (testing::internal::IsSame<T, unsigned int>::value) {
862 return std::string("unsignedInt") + ::testing::PrintToString(i);
867 INSTANTIATE_TYPED_TEST_CASE_P(UnsignedCustomName, TypedTestP, UnsignedTypes,
870 #endif // GTEST_HAS_TYPED_TEST_P
872 #if GTEST_HAS_DEATH_TEST
874 // We rely on the golden file to verify that tests whose test case
875 // name ends with DeathTest are run first.
877 TEST(ADeathTest, ShouldRunFirst) {
880 # if GTEST_HAS_TYPED_TEST
882 // We rely on the golden file to verify that typed tests whose test
883 // case name ends with DeathTest are run first.
885 template <typename T>
886 class ATypedDeathTest : public testing::Test {
889 typedef testing::Types<int, double> NumericTypes;
890 TYPED_TEST_CASE(ATypedDeathTest, NumericTypes);
892 TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {
895 # endif // GTEST_HAS_TYPED_TEST
897 # if GTEST_HAS_TYPED_TEST_P
900 // We rely on the golden file to verify that type-parameterized tests
901 // whose test case name ends with DeathTest are run first.
903 template <typename T>
904 class ATypeParamDeathTest : public testing::Test {
907 TYPED_TEST_CASE_P(ATypeParamDeathTest);
909 TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {
912 REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst);
914 INSTANTIATE_TYPED_TEST_CASE_P(My, ATypeParamDeathTest, NumericTypes);
916 # endif // GTEST_HAS_TYPED_TEST_P
918 #endif // GTEST_HAS_DEATH_TEST
920 // Tests various failure conditions of
921 // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}.
922 class ExpectFailureTest : public testing::Test {
923 public: // Must be public and not protected due to a bug in g++ 3.4.2.
928 static void AddFailure(FailureMode failure) {
929 if (failure == FATAL_FAILURE) {
930 FAIL() << "Expected fatal failure.";
932 ADD_FAILURE() << "Expected non-fatal failure.";
937 TEST_F(ExpectFailureTest, ExpectFatalFailure) {
938 // Expected fatal failure, but succeeds.
939 printf("(expecting 1 failure)\n");
940 EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure.");
941 // Expected fatal failure, but got a non-fatal failure.
942 printf("(expecting 1 failure)\n");
943 EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal "
946 printf("(expecting 1 failure)\n");
947 EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure "
951 TEST_F(ExpectFailureTest, ExpectNonFatalFailure) {
952 // Expected non-fatal failure, but succeeds.
953 printf("(expecting 1 failure)\n");
954 EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure.");
955 // Expected non-fatal failure, but got a fatal failure.
956 printf("(expecting 1 failure)\n");
957 EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure.");
959 printf("(expecting 1 failure)\n");
960 EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal "
964 #if GTEST_IS_THREADSAFE
966 class ExpectFailureWithThreadsTest : public ExpectFailureTest {
968 static void AddFailureInOtherThread(FailureMode failure) {
969 ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL);
974 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
975 // We only intercept the current thread.
976 printf("(expecting 2 failures)\n");
977 EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE),
978 "Expected fatal failure.");
981 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
982 // We only intercept the current thread.
983 printf("(expecting 2 failures)\n");
984 EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE),
985 "Expected non-fatal failure.");
988 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
990 // Tests that the ScopedFakeTestPartResultReporter only catches failures from
991 // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD.
992 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
993 printf("(expecting 2 failures)\n");
994 TestPartResultArray results;
996 ScopedFakeTestPartResultReporter reporter(
997 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
999 AddFailureInOtherThread(FATAL_FAILURE);
1000 AddFailureInOtherThread(NONFATAL_FAILURE);
1002 // The two failures should not have been intercepted.
1003 EXPECT_EQ(0, results.size()) << "This shouldn't fail.";
1006 #endif // GTEST_IS_THREADSAFE
1008 TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) {
1009 // Expected fatal failure, but succeeds.
1010 printf("(expecting 1 failure)\n");
1011 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure.");
1012 // Expected fatal failure, but got a non-fatal failure.
1013 printf("(expecting 1 failure)\n");
1014 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
1015 "Expected non-fatal failure.");
1017 printf("(expecting 1 failure)\n");
1018 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
1019 "Some other fatal failure expected.");
1022 TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) {
1023 // Expected non-fatal failure, but succeeds.
1024 printf("(expecting 1 failure)\n");
1025 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal "
1027 // Expected non-fatal failure, but got a fatal failure.
1028 printf("(expecting 1 failure)\n");
1029 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
1030 "Expected fatal failure.");
1032 printf("(expecting 1 failure)\n");
1033 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
1034 "Some other non-fatal failure.");
1038 // Two test environments for testing testing::AddGlobalTestEnvironment().
1040 class FooEnvironment : public testing::Environment {
1042 virtual void SetUp() {
1043 printf("%s", "FooEnvironment::SetUp() called.\n");
1046 virtual void TearDown() {
1047 printf("%s", "FooEnvironment::TearDown() called.\n");
1048 FAIL() << "Expected fatal failure.";
1052 class BarEnvironment : public testing::Environment {
1054 virtual void SetUp() {
1055 printf("%s", "BarEnvironment::SetUp() called.\n");
1058 virtual void TearDown() {
1059 printf("%s", "BarEnvironment::TearDown() called.\n");
1060 ADD_FAILURE() << "Expected non-fatal failure.";
1064 // The main function.
1066 // The idea is to use Google Test to run all the tests we have defined (some
1067 // of them are intended to fail), and then compare the test results
1068 // with the "golden" file.
1069 int main(int argc, char **argv) {
1070 testing::GTEST_FLAG(print_time) = false;
1072 // We just run the tests, knowing some of them are intended to fail.
1073 // We will use a separate Python script to compare the output of
1074 // this program with the golden file.
1076 // It's hard to test InitGoogleTest() directly, as it has many
1077 // global side effects. The following line serves as a sanity test
1079 testing::InitGoogleTest(&argc, argv);
1080 bool internal_skip_environment_and_ad_hoc_tests =
1081 std::count(argv, argv + argc,
1082 std::string("internal_skip_environment_and_ad_hoc_tests")) > 0;
1084 #if GTEST_HAS_DEATH_TEST
1085 if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") {
1086 // Skip the usual output capturing if we're running as the child
1087 // process of an threadsafe-style death test.
1088 # if GTEST_OS_WINDOWS
1089 posix::FReopen("nul:", "w", stdout);
1091 posix::FReopen("/dev/null", "w", stdout);
1092 # endif // GTEST_OS_WINDOWS
1093 return RUN_ALL_TESTS();
1095 #endif // GTEST_HAS_DEATH_TEST
1097 if (internal_skip_environment_and_ad_hoc_tests)
1098 return RUN_ALL_TESTS();
1100 // Registers two global test environments.
1101 // The golden file verifies that they are set up in the order they
1102 // are registered, and torn down in the reverse order.
1103 testing::AddGlobalTestEnvironment(new FooEnvironment);
1104 testing::AddGlobalTestEnvironment(new BarEnvironment);
1106 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4127
1108 return RunAllTests();