michael@0: // Copyright 2005, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // The purpose of this file is to generate Google Test output under michael@0: // various conditions. The output will then be verified by michael@0: // gtest_output_test.py to ensure that Google Test generates the michael@0: // desired messages. Therefore, most tests in this file are MEANT TO michael@0: // FAIL. michael@0: // michael@0: // Author: wan@google.com (Zhanyong Wan) michael@0: michael@0: #include "gtest/gtest-spi.h" michael@0: #include "gtest/gtest.h" michael@0: michael@0: // Indicates that this translation unit is part of Google Test's michael@0: // implementation. It must come before gtest-internal-inl.h is michael@0: // included, or there will be a compiler error. This trick is to michael@0: // prevent a user from accidentally including gtest-internal-inl.h in michael@0: // his code. michael@0: #define GTEST_IMPLEMENTATION_ 1 michael@0: #include "src/gtest-internal-inl.h" michael@0: #undef GTEST_IMPLEMENTATION_ michael@0: michael@0: #include michael@0: michael@0: #if GTEST_IS_THREADSAFE michael@0: using testing::ScopedFakeTestPartResultReporter; michael@0: using testing::TestPartResultArray; michael@0: michael@0: using testing::internal::Notification; michael@0: using testing::internal::ThreadWithParam; michael@0: #endif michael@0: michael@0: namespace posix = ::testing::internal::posix; michael@0: using testing::internal::String; michael@0: using testing::internal::scoped_ptr; michael@0: michael@0: // Tests catching fatal failures. michael@0: michael@0: // A subroutine used by the following test. michael@0: void TestEq1(int x) { michael@0: ASSERT_EQ(1, x); michael@0: } michael@0: michael@0: // This function calls a test subroutine, catches the fatal failure it michael@0: // generates, and then returns early. michael@0: void TryTestSubroutine() { michael@0: // Calls a subrountine that yields a fatal failure. michael@0: TestEq1(2); michael@0: michael@0: // Catches the fatal failure and aborts the test. michael@0: // michael@0: // The testing::Test:: prefix is necessary when calling michael@0: // HasFatalFailure() outside of a TEST, TEST_F, or test fixture. michael@0: if (testing::Test::HasFatalFailure()) return; michael@0: michael@0: // If we get here, something is wrong. michael@0: FAIL() << "This should never be reached."; michael@0: } michael@0: michael@0: TEST(PassingTest, PassingTest1) { michael@0: } michael@0: michael@0: TEST(PassingTest, PassingTest2) { michael@0: } michael@0: michael@0: // Tests that parameters of failing parameterized tests are printed in the michael@0: // failing test summary. michael@0: class FailingParamTest : public testing::TestWithParam {}; michael@0: michael@0: TEST_P(FailingParamTest, Fails) { michael@0: EXPECT_EQ(1, GetParam()); michael@0: } michael@0: michael@0: // This generates a test which will fail. Google Test is expected to print michael@0: // its parameter when it outputs the list of all failed tests. michael@0: INSTANTIATE_TEST_CASE_P(PrintingFailingParams, michael@0: FailingParamTest, michael@0: testing::Values(2)); michael@0: michael@0: static const char kGoldenString[] = "\"Line\0 1\"\nLine 2"; michael@0: michael@0: TEST(NonfatalFailureTest, EscapesStringOperands) { michael@0: std::string actual = "actual \"string\""; michael@0: EXPECT_EQ(kGoldenString, actual); michael@0: michael@0: const char* golden = kGoldenString; michael@0: EXPECT_EQ(golden, actual); michael@0: } michael@0: michael@0: // Tests catching a fatal failure in a subroutine. michael@0: TEST(FatalFailureTest, FatalFailureInSubroutine) { michael@0: printf("(expecting a failure that x should be 1)\n"); michael@0: michael@0: TryTestSubroutine(); michael@0: } michael@0: michael@0: // Tests catching a fatal failure in a nested subroutine. michael@0: TEST(FatalFailureTest, FatalFailureInNestedSubroutine) { michael@0: printf("(expecting a failure that x should be 1)\n"); michael@0: michael@0: // Calls a subrountine that yields a fatal failure. michael@0: TryTestSubroutine(); michael@0: michael@0: // Catches the fatal failure and aborts the test. michael@0: // michael@0: // When calling HasFatalFailure() inside a TEST, TEST_F, or test michael@0: // fixture, the testing::Test:: prefix is not needed. michael@0: if (HasFatalFailure()) return; michael@0: michael@0: // If we get here, something is wrong. michael@0: FAIL() << "This should never be reached."; michael@0: } michael@0: michael@0: // Tests HasFatalFailure() after a failed EXPECT check. michael@0: TEST(FatalFailureTest, NonfatalFailureInSubroutine) { michael@0: printf("(expecting a failure on false)\n"); michael@0: EXPECT_TRUE(false); // Generates a nonfatal failure michael@0: ASSERT_FALSE(HasFatalFailure()); // This should succeed. michael@0: } michael@0: michael@0: // Tests interleaving user logging and Google Test assertions. michael@0: TEST(LoggingTest, InterleavingLoggingAndAssertions) { michael@0: static const int a[4] = { michael@0: 3, 9, 2, 6 michael@0: }; michael@0: michael@0: printf("(expecting 2 failures on (3) >= (a[i]))\n"); michael@0: for (int i = 0; i < static_cast(sizeof(a)/sizeof(*a)); i++) { michael@0: printf("i == %d\n", i); michael@0: EXPECT_GE(3, a[i]); michael@0: } michael@0: } michael@0: michael@0: // Tests the SCOPED_TRACE macro. michael@0: michael@0: // A helper function for testing SCOPED_TRACE. michael@0: void SubWithoutTrace(int n) { michael@0: EXPECT_EQ(1, n); michael@0: ASSERT_EQ(2, n); michael@0: } michael@0: michael@0: // Another helper function for testing SCOPED_TRACE. michael@0: void SubWithTrace(int n) { michael@0: SCOPED_TRACE(testing::Message() << "n = " << n); michael@0: michael@0: SubWithoutTrace(n); michael@0: } michael@0: michael@0: // Tests that SCOPED_TRACE() obeys lexical scopes. michael@0: TEST(SCOPED_TRACETest, ObeysScopes) { michael@0: printf("(expected to fail)\n"); michael@0: michael@0: // There should be no trace before SCOPED_TRACE() is invoked. michael@0: ADD_FAILURE() << "This failure is expected, and shouldn't have a trace."; michael@0: michael@0: { michael@0: SCOPED_TRACE("Expected trace"); michael@0: // After SCOPED_TRACE(), a failure in the current scope should contain michael@0: // the trace. michael@0: ADD_FAILURE() << "This failure is expected, and should have a trace."; michael@0: } michael@0: michael@0: // Once the control leaves the scope of the SCOPED_TRACE(), there michael@0: // should be no trace again. michael@0: ADD_FAILURE() << "This failure is expected, and shouldn't have a trace."; michael@0: } michael@0: michael@0: // Tests that SCOPED_TRACE works inside a loop. michael@0: TEST(SCOPED_TRACETest, WorksInLoop) { michael@0: printf("(expected to fail)\n"); michael@0: michael@0: for (int i = 1; i <= 2; i++) { michael@0: SCOPED_TRACE(testing::Message() << "i = " << i); michael@0: michael@0: SubWithoutTrace(i); michael@0: } michael@0: } michael@0: michael@0: // Tests that SCOPED_TRACE works in a subroutine. michael@0: TEST(SCOPED_TRACETest, WorksInSubroutine) { michael@0: printf("(expected to fail)\n"); michael@0: michael@0: SubWithTrace(1); michael@0: SubWithTrace(2); michael@0: } michael@0: michael@0: // Tests that SCOPED_TRACE can be nested. michael@0: TEST(SCOPED_TRACETest, CanBeNested) { michael@0: printf("(expected to fail)\n"); michael@0: michael@0: SCOPED_TRACE(""); // A trace without a message. michael@0: michael@0: SubWithTrace(2); michael@0: } michael@0: michael@0: // Tests that multiple SCOPED_TRACEs can be used in the same scope. michael@0: TEST(SCOPED_TRACETest, CanBeRepeated) { michael@0: printf("(expected to fail)\n"); michael@0: michael@0: SCOPED_TRACE("A"); michael@0: ADD_FAILURE() michael@0: << "This failure is expected, and should contain trace point A."; michael@0: michael@0: SCOPED_TRACE("B"); michael@0: ADD_FAILURE() michael@0: << "This failure is expected, and should contain trace point A and B."; michael@0: michael@0: { michael@0: SCOPED_TRACE("C"); michael@0: ADD_FAILURE() << "This failure is expected, and should " michael@0: << "contain trace point A, B, and C."; michael@0: } michael@0: michael@0: SCOPED_TRACE("D"); michael@0: ADD_FAILURE() << "This failure is expected, and should " michael@0: << "contain trace point A, B, and D."; michael@0: } michael@0: michael@0: #if GTEST_IS_THREADSAFE michael@0: // Tests that SCOPED_TRACE()s can be used concurrently from multiple michael@0: // threads. Namely, an assertion should be affected by michael@0: // SCOPED_TRACE()s in its own thread only. michael@0: michael@0: // Here's the sequence of actions that happen in the test: michael@0: // michael@0: // Thread A (main) | Thread B (spawned) michael@0: // ===============================|================================ michael@0: // spawns thread B | michael@0: // -------------------------------+-------------------------------- michael@0: // waits for n1 | SCOPED_TRACE("Trace B"); michael@0: // | generates failure #1 michael@0: // | notifies n1 michael@0: // -------------------------------+-------------------------------- michael@0: // SCOPED_TRACE("Trace A"); | waits for n2 michael@0: // generates failure #2 | michael@0: // notifies n2 | michael@0: // -------------------------------|-------------------------------- michael@0: // waits for n3 | generates failure #3 michael@0: // | trace B dies michael@0: // | generates failure #4 michael@0: // | notifies n3 michael@0: // -------------------------------|-------------------------------- michael@0: // generates failure #5 | finishes michael@0: // trace A dies | michael@0: // generates failure #6 | michael@0: // -------------------------------|-------------------------------- michael@0: // waits for thread B to finish | michael@0: michael@0: struct CheckPoints { michael@0: Notification n1; michael@0: Notification n2; michael@0: Notification n3; michael@0: }; michael@0: michael@0: static void ThreadWithScopedTrace(CheckPoints* check_points) { michael@0: { michael@0: SCOPED_TRACE("Trace B"); michael@0: ADD_FAILURE() michael@0: << "Expected failure #1 (in thread B, only trace B alive)."; michael@0: check_points->n1.Notify(); michael@0: check_points->n2.WaitForNotification(); michael@0: michael@0: ADD_FAILURE() michael@0: << "Expected failure #3 (in thread B, trace A & B both alive)."; michael@0: } // Trace B dies here. michael@0: ADD_FAILURE() michael@0: << "Expected failure #4 (in thread B, only trace A alive)."; michael@0: check_points->n3.Notify(); michael@0: } michael@0: michael@0: TEST(SCOPED_TRACETest, WorksConcurrently) { michael@0: printf("(expecting 6 failures)\n"); michael@0: michael@0: CheckPoints check_points; michael@0: ThreadWithParam thread(&ThreadWithScopedTrace, michael@0: &check_points, michael@0: NULL); michael@0: check_points.n1.WaitForNotification(); michael@0: michael@0: { michael@0: SCOPED_TRACE("Trace A"); michael@0: ADD_FAILURE() michael@0: << "Expected failure #2 (in thread A, trace A & B both alive)."; michael@0: check_points.n2.Notify(); michael@0: check_points.n3.WaitForNotification(); michael@0: michael@0: ADD_FAILURE() michael@0: << "Expected failure #5 (in thread A, only trace A alive)."; michael@0: } // Trace A dies here. michael@0: ADD_FAILURE() michael@0: << "Expected failure #6 (in thread A, no trace alive)."; michael@0: thread.Join(); michael@0: } michael@0: #endif // GTEST_IS_THREADSAFE michael@0: michael@0: TEST(DisabledTestsWarningTest, michael@0: DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) { michael@0: // This test body is intentionally empty. Its sole purpose is for michael@0: // verifying that the --gtest_also_run_disabled_tests flag michael@0: // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of michael@0: // the test output. michael@0: } michael@0: michael@0: // Tests using assertions outside of TEST and TEST_F. michael@0: // michael@0: // This function creates two failures intentionally. michael@0: void AdHocTest() { michael@0: printf("The non-test part of the code is expected to have 2 failures.\n\n"); michael@0: EXPECT_TRUE(false); michael@0: EXPECT_EQ(2, 3); michael@0: } michael@0: michael@0: // Runs all TESTs, all TEST_Fs, and the ad hoc test. michael@0: int RunAllTests() { michael@0: AdHocTest(); michael@0: return RUN_ALL_TESTS(); michael@0: } michael@0: michael@0: // Tests non-fatal failures in the fixture constructor. michael@0: class NonFatalFailureInFixtureConstructorTest : public testing::Test { michael@0: protected: michael@0: NonFatalFailureInFixtureConstructorTest() { michael@0: printf("(expecting 5 failures)\n"); michael@0: ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor."; michael@0: } michael@0: michael@0: ~NonFatalFailureInFixtureConstructorTest() { michael@0: ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor."; michael@0: } michael@0: michael@0: virtual void SetUp() { michael@0: ADD_FAILURE() << "Expected failure #2, in SetUp()."; michael@0: } michael@0: michael@0: virtual void TearDown() { michael@0: ADD_FAILURE() << "Expected failure #4, in TearDown."; michael@0: } michael@0: }; michael@0: michael@0: TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) { michael@0: ADD_FAILURE() << "Expected failure #3, in the test body."; michael@0: } michael@0: michael@0: // Tests fatal failures in the fixture constructor. michael@0: class FatalFailureInFixtureConstructorTest : public testing::Test { michael@0: protected: michael@0: FatalFailureInFixtureConstructorTest() { michael@0: printf("(expecting 2 failures)\n"); michael@0: Init(); michael@0: } michael@0: michael@0: ~FatalFailureInFixtureConstructorTest() { michael@0: ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor."; michael@0: } michael@0: michael@0: virtual void SetUp() { michael@0: ADD_FAILURE() << "UNEXPECTED failure in SetUp(). " michael@0: << "We should never get here, as the test fixture c'tor " michael@0: << "had a fatal failure."; michael@0: } michael@0: michael@0: virtual void TearDown() { michael@0: ADD_FAILURE() << "UNEXPECTED failure in TearDown(). " michael@0: << "We should never get here, as the test fixture c'tor " michael@0: << "had a fatal failure."; michael@0: } michael@0: michael@0: private: michael@0: void Init() { michael@0: FAIL() << "Expected failure #1, in the test fixture c'tor."; michael@0: } michael@0: }; michael@0: michael@0: TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) { michael@0: ADD_FAILURE() << "UNEXPECTED failure in the test body. " michael@0: << "We should never get here, as the test fixture c'tor " michael@0: << "had a fatal failure."; michael@0: } michael@0: michael@0: // Tests non-fatal failures in SetUp(). michael@0: class NonFatalFailureInSetUpTest : public testing::Test { michael@0: protected: michael@0: virtual ~NonFatalFailureInSetUpTest() { michael@0: Deinit(); michael@0: } michael@0: michael@0: virtual void SetUp() { michael@0: printf("(expecting 4 failures)\n"); michael@0: ADD_FAILURE() << "Expected failure #1, in SetUp()."; michael@0: } michael@0: michael@0: virtual void TearDown() { michael@0: FAIL() << "Expected failure #3, in TearDown()."; michael@0: } michael@0: private: michael@0: void Deinit() { michael@0: FAIL() << "Expected failure #4, in the test fixture d'tor."; michael@0: } michael@0: }; michael@0: michael@0: TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) { michael@0: FAIL() << "Expected failure #2, in the test function."; michael@0: } michael@0: michael@0: // Tests fatal failures in SetUp(). michael@0: class FatalFailureInSetUpTest : public testing::Test { michael@0: protected: michael@0: virtual ~FatalFailureInSetUpTest() { michael@0: Deinit(); michael@0: } michael@0: michael@0: virtual void SetUp() { michael@0: printf("(expecting 3 failures)\n"); michael@0: FAIL() << "Expected failure #1, in SetUp()."; michael@0: } michael@0: michael@0: virtual void TearDown() { michael@0: FAIL() << "Expected failure #2, in TearDown()."; michael@0: } michael@0: private: michael@0: void Deinit() { michael@0: FAIL() << "Expected failure #3, in the test fixture d'tor."; michael@0: } michael@0: }; michael@0: michael@0: TEST_F(FatalFailureInSetUpTest, FailureInSetUp) { michael@0: FAIL() << "UNEXPECTED failure in the test function. " michael@0: << "We should never get here, as SetUp() failed."; michael@0: } michael@0: michael@0: TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) { michael@0: ADD_FAILURE_AT("foo.cc", 42) << "Expected failure in foo.cc"; michael@0: } michael@0: michael@0: #if GTEST_IS_THREADSAFE michael@0: michael@0: // A unary function that may die. michael@0: void DieIf(bool should_die) { michael@0: GTEST_CHECK_(!should_die) << " - death inside DieIf()."; michael@0: } michael@0: michael@0: // Tests running death tests in a multi-threaded context. michael@0: michael@0: // Used for coordination between the main and the spawn thread. michael@0: struct SpawnThreadNotifications { michael@0: SpawnThreadNotifications() {} michael@0: michael@0: Notification spawn_thread_started; michael@0: Notification spawn_thread_ok_to_terminate; michael@0: michael@0: private: michael@0: GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications); michael@0: }; michael@0: michael@0: // The function to be executed in the thread spawn by the michael@0: // MultipleThreads test (below). michael@0: static void ThreadRoutine(SpawnThreadNotifications* notifications) { michael@0: // Signals the main thread that this thread has started. michael@0: notifications->spawn_thread_started.Notify(); michael@0: michael@0: // Waits for permission to finish from the main thread. michael@0: notifications->spawn_thread_ok_to_terminate.WaitForNotification(); michael@0: } michael@0: michael@0: // This is a death-test test, but it's not named with a DeathTest michael@0: // suffix. It starts threads which might interfere with later michael@0: // death tests, so it must run after all other death tests. michael@0: class DeathTestAndMultiThreadsTest : public testing::Test { michael@0: protected: michael@0: // Starts a thread and waits for it to begin. michael@0: virtual void SetUp() { michael@0: thread_.reset(new ThreadWithParam( michael@0: &ThreadRoutine, ¬ifications_, NULL)); michael@0: notifications_.spawn_thread_started.WaitForNotification(); michael@0: } michael@0: // Tells the thread to finish, and reaps it. michael@0: // Depending on the version of the thread library in use, michael@0: // a manager thread might still be left running that will interfere michael@0: // with later death tests. This is unfortunate, but this class michael@0: // cleans up after itself as best it can. michael@0: virtual void TearDown() { michael@0: notifications_.spawn_thread_ok_to_terminate.Notify(); michael@0: } michael@0: michael@0: private: michael@0: SpawnThreadNotifications notifications_; michael@0: scoped_ptr > thread_; michael@0: }; michael@0: michael@0: #endif // GTEST_IS_THREADSAFE michael@0: michael@0: // The MixedUpTestCaseTest test case verifies that Google Test will fail a michael@0: // test if it uses a different fixture class than what other tests in michael@0: // the same test case use. It deliberately contains two fixture michael@0: // classes with the same name but defined in different namespaces. michael@0: michael@0: // The MixedUpTestCaseWithSameTestNameTest test case verifies that michael@0: // when the user defines two tests with the same test case name AND michael@0: // same test name (but in different namespaces), the second test will michael@0: // fail. michael@0: michael@0: namespace foo { michael@0: michael@0: class MixedUpTestCaseTest : public testing::Test { michael@0: }; michael@0: michael@0: TEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {} michael@0: TEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {} michael@0: michael@0: class MixedUpTestCaseWithSameTestNameTest : public testing::Test { michael@0: }; michael@0: michael@0: TEST_F(MixedUpTestCaseWithSameTestNameTest, michael@0: TheSecondTestWithThisNameShouldFail) {} michael@0: michael@0: } // namespace foo michael@0: michael@0: namespace bar { michael@0: michael@0: class MixedUpTestCaseTest : public testing::Test { michael@0: }; michael@0: michael@0: // The following two tests are expected to fail. We rely on the michael@0: // golden file to check that Google Test generates the right error message. michael@0: TEST_F(MixedUpTestCaseTest, ThisShouldFail) {} michael@0: TEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {} michael@0: michael@0: class MixedUpTestCaseWithSameTestNameTest : public testing::Test { michael@0: }; michael@0: michael@0: // Expected to fail. We rely on the golden file to check that Google Test michael@0: // generates the right error message. michael@0: TEST_F(MixedUpTestCaseWithSameTestNameTest, michael@0: TheSecondTestWithThisNameShouldFail) {} michael@0: michael@0: } // namespace bar michael@0: michael@0: // The following two test cases verify that Google Test catches the user michael@0: // error of mixing TEST and TEST_F in the same test case. The first michael@0: // test case checks the scenario where TEST_F appears before TEST, and michael@0: // the second one checks where TEST appears before TEST_F. michael@0: michael@0: class TEST_F_before_TEST_in_same_test_case : public testing::Test { michael@0: }; michael@0: michael@0: TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {} michael@0: michael@0: // Expected to fail. We rely on the golden file to check that Google Test michael@0: // generates the right error message. michael@0: TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {} michael@0: michael@0: class TEST_before_TEST_F_in_same_test_case : public testing::Test { michael@0: }; michael@0: michael@0: TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {} michael@0: michael@0: // Expected to fail. We rely on the golden file to check that Google Test michael@0: // generates the right error message. michael@0: TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) { michael@0: } michael@0: michael@0: // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE(). michael@0: int global_integer = 0; michael@0: michael@0: // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables. michael@0: TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) { michael@0: global_integer = 0; michael@0: EXPECT_NONFATAL_FAILURE({ michael@0: EXPECT_EQ(1, global_integer) << "Expected non-fatal failure."; michael@0: }, "Expected non-fatal failure."); michael@0: } michael@0: michael@0: // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables michael@0: // (static or not). michael@0: TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) { michael@0: int m = 0; michael@0: static int n; michael@0: n = 1; michael@0: EXPECT_NONFATAL_FAILURE({ michael@0: EXPECT_EQ(m, n) << "Expected non-fatal failure."; michael@0: }, "Expected non-fatal failure."); michael@0: } michael@0: michael@0: // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly michael@0: // one non-fatal failure and no fatal failure. michael@0: TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) { michael@0: EXPECT_NONFATAL_FAILURE({ michael@0: ADD_FAILURE() << "Expected non-fatal failure."; michael@0: }, "Expected non-fatal failure."); michael@0: } michael@0: michael@0: // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no michael@0: // non-fatal failure. michael@0: TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) { michael@0: printf("(expecting a failure)\n"); michael@0: EXPECT_NONFATAL_FAILURE({ michael@0: }, ""); michael@0: } michael@0: michael@0: // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two michael@0: // non-fatal failures. michael@0: TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) { michael@0: printf("(expecting a failure)\n"); michael@0: EXPECT_NONFATAL_FAILURE({ michael@0: ADD_FAILURE() << "Expected non-fatal failure 1."; michael@0: ADD_FAILURE() << "Expected non-fatal failure 2."; michael@0: }, ""); michael@0: } michael@0: michael@0: // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal michael@0: // failure. michael@0: TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) { michael@0: printf("(expecting a failure)\n"); michael@0: EXPECT_NONFATAL_FAILURE({ michael@0: FAIL() << "Expected fatal failure."; michael@0: }, ""); michael@0: } michael@0: michael@0: // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being michael@0: // tested returns. michael@0: TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) { michael@0: printf("(expecting a failure)\n"); michael@0: EXPECT_NONFATAL_FAILURE({ michael@0: return; michael@0: }, ""); michael@0: } michael@0: michael@0: #if GTEST_HAS_EXCEPTIONS michael@0: michael@0: // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being michael@0: // tested throws. michael@0: TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) { michael@0: printf("(expecting a failure)\n"); michael@0: try { michael@0: EXPECT_NONFATAL_FAILURE({ michael@0: throw 0; michael@0: }, ""); michael@0: } catch(int) { // NOLINT michael@0: } michael@0: } michael@0: michael@0: #endif // GTEST_HAS_EXCEPTIONS michael@0: michael@0: // Tests that EXPECT_FATAL_FAILURE() can reference global variables. michael@0: TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) { michael@0: global_integer = 0; michael@0: EXPECT_FATAL_FAILURE({ michael@0: ASSERT_EQ(1, global_integer) << "Expected fatal failure."; michael@0: }, "Expected fatal failure."); michael@0: } michael@0: michael@0: // Tests that EXPECT_FATAL_FAILURE() can reference local static michael@0: // variables. michael@0: TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) { michael@0: static int n; michael@0: n = 1; michael@0: EXPECT_FATAL_FAILURE({ michael@0: ASSERT_EQ(0, n) << "Expected fatal failure."; michael@0: }, "Expected fatal failure."); michael@0: } michael@0: michael@0: // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly michael@0: // one fatal failure and no non-fatal failure. michael@0: TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) { michael@0: EXPECT_FATAL_FAILURE({ michael@0: FAIL() << "Expected fatal failure."; michael@0: }, "Expected fatal failure."); michael@0: } michael@0: michael@0: // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal michael@0: // failure. michael@0: TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) { michael@0: printf("(expecting a failure)\n"); michael@0: EXPECT_FATAL_FAILURE({ michael@0: }, ""); michael@0: } michael@0: michael@0: // A helper for generating a fatal failure. michael@0: void FatalFailure() { michael@0: FAIL() << "Expected fatal failure."; michael@0: } michael@0: michael@0: // Tests that EXPECT_FATAL_FAILURE() fails when there are two michael@0: // fatal failures. michael@0: TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) { michael@0: printf("(expecting a failure)\n"); michael@0: EXPECT_FATAL_FAILURE({ michael@0: FatalFailure(); michael@0: FatalFailure(); michael@0: }, ""); michael@0: } michael@0: michael@0: // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal michael@0: // failure. michael@0: TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) { michael@0: printf("(expecting a failure)\n"); michael@0: EXPECT_FATAL_FAILURE({ michael@0: ADD_FAILURE() << "Expected non-fatal failure."; michael@0: }, ""); michael@0: } michael@0: michael@0: // Tests that EXPECT_FATAL_FAILURE() fails when the statement being michael@0: // tested returns. michael@0: TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) { michael@0: printf("(expecting a failure)\n"); michael@0: EXPECT_FATAL_FAILURE({ michael@0: return; michael@0: }, ""); michael@0: } michael@0: michael@0: #if GTEST_HAS_EXCEPTIONS michael@0: michael@0: // Tests that EXPECT_FATAL_FAILURE() fails when the statement being michael@0: // tested throws. michael@0: TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) { michael@0: printf("(expecting a failure)\n"); michael@0: try { michael@0: EXPECT_FATAL_FAILURE({ michael@0: throw 0; michael@0: }, ""); michael@0: } catch(int) { // NOLINT michael@0: } michael@0: } michael@0: michael@0: #endif // GTEST_HAS_EXCEPTIONS michael@0: michael@0: // This #ifdef block tests the output of typed tests. michael@0: #if GTEST_HAS_TYPED_TEST michael@0: michael@0: template michael@0: class TypedTest : public testing::Test { michael@0: }; michael@0: michael@0: TYPED_TEST_CASE(TypedTest, testing::Types); michael@0: michael@0: TYPED_TEST(TypedTest, Success) { michael@0: EXPECT_EQ(0, TypeParam()); michael@0: } michael@0: michael@0: TYPED_TEST(TypedTest, Failure) { michael@0: EXPECT_EQ(1, TypeParam()) << "Expected failure"; michael@0: } michael@0: michael@0: #endif // GTEST_HAS_TYPED_TEST michael@0: michael@0: // This #ifdef block tests the output of type-parameterized tests. michael@0: #if GTEST_HAS_TYPED_TEST_P michael@0: michael@0: template michael@0: class TypedTestP : public testing::Test { michael@0: }; michael@0: michael@0: TYPED_TEST_CASE_P(TypedTestP); michael@0: michael@0: TYPED_TEST_P(TypedTestP, Success) { michael@0: EXPECT_EQ(0U, TypeParam()); michael@0: } michael@0: michael@0: TYPED_TEST_P(TypedTestP, Failure) { michael@0: EXPECT_EQ(1U, TypeParam()) << "Expected failure"; michael@0: } michael@0: michael@0: REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure); michael@0: michael@0: typedef testing::Types UnsignedTypes; michael@0: INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes); michael@0: michael@0: #endif // GTEST_HAS_TYPED_TEST_P michael@0: michael@0: #if GTEST_HAS_DEATH_TEST michael@0: michael@0: // We rely on the golden file to verify that tests whose test case michael@0: // name ends with DeathTest are run first. michael@0: michael@0: TEST(ADeathTest, ShouldRunFirst) { michael@0: } michael@0: michael@0: # if GTEST_HAS_TYPED_TEST michael@0: michael@0: // We rely on the golden file to verify that typed tests whose test michael@0: // case name ends with DeathTest are run first. michael@0: michael@0: template michael@0: class ATypedDeathTest : public testing::Test { michael@0: }; michael@0: michael@0: typedef testing::Types NumericTypes; michael@0: TYPED_TEST_CASE(ATypedDeathTest, NumericTypes); michael@0: michael@0: TYPED_TEST(ATypedDeathTest, ShouldRunFirst) { michael@0: } michael@0: michael@0: # endif // GTEST_HAS_TYPED_TEST michael@0: michael@0: # if GTEST_HAS_TYPED_TEST_P michael@0: michael@0: michael@0: // We rely on the golden file to verify that type-parameterized tests michael@0: // whose test case name ends with DeathTest are run first. michael@0: michael@0: template michael@0: class ATypeParamDeathTest : public testing::Test { michael@0: }; michael@0: michael@0: TYPED_TEST_CASE_P(ATypeParamDeathTest); michael@0: michael@0: TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) { michael@0: } michael@0: michael@0: REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst); michael@0: michael@0: INSTANTIATE_TYPED_TEST_CASE_P(My, ATypeParamDeathTest, NumericTypes); michael@0: michael@0: # endif // GTEST_HAS_TYPED_TEST_P michael@0: michael@0: #endif // GTEST_HAS_DEATH_TEST michael@0: michael@0: // Tests various failure conditions of michael@0: // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}. michael@0: class ExpectFailureTest : public testing::Test { michael@0: public: // Must be public and not protected due to a bug in g++ 3.4.2. michael@0: enum FailureMode { michael@0: FATAL_FAILURE, michael@0: NONFATAL_FAILURE michael@0: }; michael@0: static void AddFailure(FailureMode failure) { michael@0: if (failure == FATAL_FAILURE) { michael@0: FAIL() << "Expected fatal failure."; michael@0: } else { michael@0: ADD_FAILURE() << "Expected non-fatal failure."; michael@0: } michael@0: } michael@0: }; michael@0: michael@0: TEST_F(ExpectFailureTest, ExpectFatalFailure) { michael@0: // Expected fatal failure, but succeeds. michael@0: printf("(expecting 1 failure)\n"); michael@0: EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure."); michael@0: // Expected fatal failure, but got a non-fatal failure. michael@0: printf("(expecting 1 failure)\n"); michael@0: EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal " michael@0: "failure."); michael@0: // Wrong message. michael@0: printf("(expecting 1 failure)\n"); michael@0: EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure " michael@0: "expected."); michael@0: } michael@0: michael@0: TEST_F(ExpectFailureTest, ExpectNonFatalFailure) { michael@0: // Expected non-fatal failure, but succeeds. michael@0: printf("(expecting 1 failure)\n"); michael@0: EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure."); michael@0: // Expected non-fatal failure, but got a fatal failure. michael@0: printf("(expecting 1 failure)\n"); michael@0: EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure."); michael@0: // Wrong message. michael@0: printf("(expecting 1 failure)\n"); michael@0: EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal " michael@0: "failure."); michael@0: } michael@0: michael@0: #if GTEST_IS_THREADSAFE michael@0: michael@0: class ExpectFailureWithThreadsTest : public ExpectFailureTest { michael@0: protected: michael@0: static void AddFailureInOtherThread(FailureMode failure) { michael@0: ThreadWithParam thread(&AddFailure, failure, NULL); michael@0: thread.Join(); michael@0: } michael@0: }; michael@0: michael@0: TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) { michael@0: // We only intercept the current thread. michael@0: printf("(expecting 2 failures)\n"); michael@0: EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE), michael@0: "Expected fatal failure."); michael@0: } michael@0: michael@0: TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) { michael@0: // We only intercept the current thread. michael@0: printf("(expecting 2 failures)\n"); michael@0: EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE), michael@0: "Expected non-fatal failure."); michael@0: } michael@0: michael@0: typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest; michael@0: michael@0: // Tests that the ScopedFakeTestPartResultReporter only catches failures from michael@0: // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD. michael@0: TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) { michael@0: printf("(expecting 2 failures)\n"); michael@0: TestPartResultArray results; michael@0: { michael@0: ScopedFakeTestPartResultReporter reporter( michael@0: ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD, michael@0: &results); michael@0: AddFailureInOtherThread(FATAL_FAILURE); michael@0: AddFailureInOtherThread(NONFATAL_FAILURE); michael@0: } michael@0: // The two failures should not have been intercepted. michael@0: EXPECT_EQ(0, results.size()) << "This shouldn't fail."; michael@0: } michael@0: michael@0: #endif // GTEST_IS_THREADSAFE michael@0: michael@0: TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) { michael@0: // Expected fatal failure, but succeeds. michael@0: printf("(expecting 1 failure)\n"); michael@0: EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure."); michael@0: // Expected fatal failure, but got a non-fatal failure. michael@0: printf("(expecting 1 failure)\n"); michael@0: EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE), michael@0: "Expected non-fatal failure."); michael@0: // Wrong message. michael@0: printf("(expecting 1 failure)\n"); michael@0: EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), michael@0: "Some other fatal failure expected."); michael@0: } michael@0: michael@0: TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) { michael@0: // Expected non-fatal failure, but succeeds. michael@0: printf("(expecting 1 failure)\n"); michael@0: EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal " michael@0: "failure."); michael@0: // Expected non-fatal failure, but got a fatal failure. michael@0: printf("(expecting 1 failure)\n"); michael@0: EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), michael@0: "Expected fatal failure."); michael@0: // Wrong message. michael@0: printf("(expecting 1 failure)\n"); michael@0: EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE), michael@0: "Some other non-fatal failure."); michael@0: } michael@0: michael@0: michael@0: // Two test environments for testing testing::AddGlobalTestEnvironment(). michael@0: michael@0: class FooEnvironment : public testing::Environment { michael@0: public: michael@0: virtual void SetUp() { michael@0: printf("%s", "FooEnvironment::SetUp() called.\n"); michael@0: } michael@0: michael@0: virtual void TearDown() { michael@0: printf("%s", "FooEnvironment::TearDown() called.\n"); michael@0: FAIL() << "Expected fatal failure."; michael@0: } michael@0: }; michael@0: michael@0: class BarEnvironment : public testing::Environment { michael@0: public: michael@0: virtual void SetUp() { michael@0: printf("%s", "BarEnvironment::SetUp() called.\n"); michael@0: } michael@0: michael@0: virtual void TearDown() { michael@0: printf("%s", "BarEnvironment::TearDown() called.\n"); michael@0: ADD_FAILURE() << "Expected non-fatal failure."; michael@0: } michael@0: }; michael@0: michael@0: bool GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = false; michael@0: michael@0: // The main function. michael@0: // michael@0: // The idea is to use Google Test to run all the tests we have defined (some michael@0: // of them are intended to fail), and then compare the test results michael@0: // with the "golden" file. michael@0: int main(int argc, char **argv) { michael@0: testing::GTEST_FLAG(print_time) = false; michael@0: michael@0: // We just run the tests, knowing some of them are intended to fail. michael@0: // We will use a separate Python script to compare the output of michael@0: // this program with the golden file. michael@0: michael@0: // It's hard to test InitGoogleTest() directly, as it has many michael@0: // global side effects. The following line serves as a sanity test michael@0: // for it. michael@0: testing::InitGoogleTest(&argc, argv); michael@0: if (argc >= 2 && michael@0: String(argv[1]) == "--gtest_internal_skip_environment_and_ad_hoc_tests") michael@0: GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = true; michael@0: michael@0: #if GTEST_HAS_DEATH_TEST michael@0: if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") { michael@0: // Skip the usual output capturing if we're running as the child michael@0: // process of an threadsafe-style death test. michael@0: # if GTEST_OS_WINDOWS michael@0: posix::FReopen("nul:", "w", stdout); michael@0: # else michael@0: posix::FReopen("/dev/null", "w", stdout); michael@0: # endif // GTEST_OS_WINDOWS michael@0: return RUN_ALL_TESTS(); michael@0: } michael@0: #endif // GTEST_HAS_DEATH_TEST michael@0: michael@0: if (GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests)) michael@0: return RUN_ALL_TESTS(); michael@0: michael@0: // Registers two global test environments. michael@0: // The golden file verifies that they are set up in the order they michael@0: // are registered, and torn down in the reverse order. michael@0: testing::AddGlobalTestEnvironment(new FooEnvironment); michael@0: testing::AddGlobalTestEnvironment(new BarEnvironment); michael@0: michael@0: return RunAllTests(); michael@0: }