gtest_output_test_.cc
Go to the documentation of this file.
00001 // Copyright 2005, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // The purpose of this file is to generate Google Test output under
00031 // various conditions.  The output will then be verified by
00032 // gtest_output_test.py to ensure that Google Test generates the
00033 // desired messages.  Therefore, most tests in this file are MEANT TO
00034 // FAIL.
00035 //
00036 // Author: wan@google.com (Zhanyong Wan)
00037 
00038 #include "gtest/gtest-spi.h"
00039 #include "gtest/gtest.h"
00040 
00041 // Indicates that this translation unit is part of Google Test's
00042 // implementation.  It must come before gtest-internal-inl.h is
00043 // included, or there will be a compiler error.  This trick is to
00044 // prevent a user from accidentally including gtest-internal-inl.h in
00045 // his code.
00046 #define GTEST_IMPLEMENTATION_ 1
00047 #include "src/gtest-internal-inl.h"
00048 #undef GTEST_IMPLEMENTATION_
00049 
00050 #include <stdlib.h>
00051 
00052 #if GTEST_IS_THREADSAFE
00053 using testing::ScopedFakeTestPartResultReporter;
00054 using testing::TestPartResultArray;
00055 
00056 using testing::internal::Notification;
00057 using testing::internal::ThreadWithParam;
00058 #endif
00059 
00060 namespace posix = ::testing::internal::posix;
00061 using testing::internal::scoped_ptr;
00062 
00063 // Tests catching fatal failures.
00064 
00065 // A subroutine used by the following test.
00066 void TestEq1(int x) {
00067   ASSERT_EQ(1, x);
00068 }
00069 
00070 // This function calls a test subroutine, catches the fatal failure it
00071 // generates, and then returns early.
00072 void TryTestSubroutine() {
00073   // Calls a subrountine that yields a fatal failure.
00074   TestEq1(2);
00075 
00076   // Catches the fatal failure and aborts the test.
00077   //
00078   // The testing::Test:: prefix is necessary when calling
00079   // HasFatalFailure() outside of a TEST, TEST_F, or test fixture.
00080   if (testing::Test::HasFatalFailure()) return;
00081 
00082   // If we get here, something is wrong.
00083   FAIL() << "This should never be reached.";
00084 }
00085 
00086 TEST(PassingTest, PassingTest1) {
00087 }
00088 
00089 TEST(PassingTest, PassingTest2) {
00090 }
00091 
00092 // Tests that parameters of failing parameterized tests are printed in the
00093 // failing test summary.
00094 class FailingParamTest : public testing::TestWithParam<int> {};
00095 
00096 TEST_P(FailingParamTest, Fails) {
00097   EXPECT_EQ(1, GetParam());
00098 }
00099 
00100 // This generates a test which will fail. Google Test is expected to print
00101 // its parameter when it outputs the list of all failed tests.
00102 INSTANTIATE_TEST_CASE_P(PrintingFailingParams,
00103                         FailingParamTest,
00104                         testing::Values(2));
00105 
00106 static const char kGoldenString[] = "\"Line\0 1\"\nLine 2";
00107 
00108 TEST(NonfatalFailureTest, EscapesStringOperands) {
00109   std::string actual = "actual \"string\"";
00110   EXPECT_EQ(kGoldenString, actual);
00111 
00112   const char* golden = kGoldenString;
00113   EXPECT_EQ(golden, actual);
00114 }
00115 
00116 TEST(NonfatalFailureTest, DiffForLongStrings) {
00117   std::string golden_str(kGoldenString, sizeof(kGoldenString) - 1);
00118   EXPECT_EQ(golden_str, "Line 2");
00119 }
00120 
00121 // Tests catching a fatal failure in a subroutine.
00122 TEST(FatalFailureTest, FatalFailureInSubroutine) {
00123   printf("(expecting a failure that x should be 1)\n");
00124 
00125   TryTestSubroutine();
00126 }
00127 
00128 // Tests catching a fatal failure in a nested subroutine.
00129 TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
00130   printf("(expecting a failure that x should be 1)\n");
00131 
00132   // Calls a subrountine that yields a fatal failure.
00133   TryTestSubroutine();
00134 
00135   // Catches the fatal failure and aborts the test.
00136   //
00137   // When calling HasFatalFailure() inside a TEST, TEST_F, or test
00138   // fixture, the testing::Test:: prefix is not needed.
00139   if (HasFatalFailure()) return;
00140 
00141   // If we get here, something is wrong.
00142   FAIL() << "This should never be reached.";
00143 }
00144 
00145 // Tests HasFatalFailure() after a failed EXPECT check.
00146 TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
00147   printf("(expecting a failure on false)\n");
00148   EXPECT_TRUE(false);  // Generates a nonfatal failure
00149   ASSERT_FALSE(HasFatalFailure());  // This should succeed.
00150 }
00151 
00152 // Tests interleaving user logging and Google Test assertions.
00153 TEST(LoggingTest, InterleavingLoggingAndAssertions) {
00154   static const int a[4] = {
00155     3, 9, 2, 6
00156   };
00157 
00158   printf("(expecting 2 failures on (3) >= (a[i]))\n");
00159   for (int i = 0; i < static_cast<int>(sizeof(a)/sizeof(*a)); i++) {
00160     printf("i == %d\n", i);
00161     EXPECT_GE(3, a[i]);
00162   }
00163 }
00164 
00165 // Tests the SCOPED_TRACE macro.
00166 
00167 // A helper function for testing SCOPED_TRACE.
00168 void SubWithoutTrace(int n) {
00169   EXPECT_EQ(1, n);
00170   ASSERT_EQ(2, n);
00171 }
00172 
00173 // Another helper function for testing SCOPED_TRACE.
00174 void SubWithTrace(int n) {
00175   SCOPED_TRACE(testing::Message() << "n = " << n);
00176 
00177   SubWithoutTrace(n);
00178 }
00179 
00180 // Tests that SCOPED_TRACE() obeys lexical scopes.
00181 TEST(SCOPED_TRACETest, ObeysScopes) {
00182   printf("(expected to fail)\n");
00183 
00184   // There should be no trace before SCOPED_TRACE() is invoked.
00185   ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
00186 
00187   {
00188     SCOPED_TRACE("Expected trace");
00189     // After SCOPED_TRACE(), a failure in the current scope should contain
00190     // the trace.
00191     ADD_FAILURE() << "This failure is expected, and should have a trace.";
00192   }
00193 
00194   // Once the control leaves the scope of the SCOPED_TRACE(), there
00195   // should be no trace again.
00196   ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
00197 }
00198 
00199 // Tests that SCOPED_TRACE works inside a loop.
00200 TEST(SCOPED_TRACETest, WorksInLoop) {
00201   printf("(expected to fail)\n");
00202 
00203   for (int i = 1; i <= 2; i++) {
00204     SCOPED_TRACE(testing::Message() << "i = " << i);
00205 
00206     SubWithoutTrace(i);
00207   }
00208 }
00209 
00210 // Tests that SCOPED_TRACE works in a subroutine.
00211 TEST(SCOPED_TRACETest, WorksInSubroutine) {
00212   printf("(expected to fail)\n");
00213 
00214   SubWithTrace(1);
00215   SubWithTrace(2);
00216 }
00217 
00218 // Tests that SCOPED_TRACE can be nested.
00219 TEST(SCOPED_TRACETest, CanBeNested) {
00220   printf("(expected to fail)\n");
00221 
00222   SCOPED_TRACE("");  // A trace without a message.
00223 
00224   SubWithTrace(2);
00225 }
00226 
00227 // Tests that multiple SCOPED_TRACEs can be used in the same scope.
00228 TEST(SCOPED_TRACETest, CanBeRepeated) {
00229   printf("(expected to fail)\n");
00230 
00231   SCOPED_TRACE("A");
00232   ADD_FAILURE()
00233       << "This failure is expected, and should contain trace point A.";
00234 
00235   SCOPED_TRACE("B");
00236   ADD_FAILURE()
00237       << "This failure is expected, and should contain trace point A and B.";
00238 
00239   {
00240     SCOPED_TRACE("C");
00241     ADD_FAILURE() << "This failure is expected, and should "
00242                   << "contain trace point A, B, and C.";
00243   }
00244 
00245   SCOPED_TRACE("D");
00246   ADD_FAILURE() << "This failure is expected, and should "
00247                 << "contain trace point A, B, and D.";
00248 }
00249 
00250 #if GTEST_IS_THREADSAFE
00251 // Tests that SCOPED_TRACE()s can be used concurrently from multiple
00252 // threads.  Namely, an assertion should be affected by
00253 // SCOPED_TRACE()s in its own thread only.
00254 
00255 // Here's the sequence of actions that happen in the test:
00256 //
00257 //   Thread A (main)                | Thread B (spawned)
00258 //   ===============================|================================
00259 //   spawns thread B                |
00260 //   -------------------------------+--------------------------------
00261 //   waits for n1                   | SCOPED_TRACE("Trace B");
00262 //                                  | generates failure #1
00263 //                                  | notifies n1
00264 //   -------------------------------+--------------------------------
00265 //   SCOPED_TRACE("Trace A");       | waits for n2
00266 //   generates failure #2           |
00267 //   notifies n2                    |
00268 //   -------------------------------|--------------------------------
00269 //   waits for n3                   | generates failure #3
00270 //                                  | trace B dies
00271 //                                  | generates failure #4
00272 //                                  | notifies n3
00273 //   -------------------------------|--------------------------------
00274 //   generates failure #5           | finishes
00275 //   trace A dies                   |
00276 //   generates failure #6           |
00277 //   -------------------------------|--------------------------------
00278 //   waits for thread B to finish   |
00279 
00280 struct CheckPoints {
00281   Notification n1;
00282   Notification n2;
00283   Notification n3;
00284 };
00285 
00286 static void ThreadWithScopedTrace(CheckPoints* check_points) {
00287   {
00288     SCOPED_TRACE("Trace B");
00289     ADD_FAILURE()
00290         << "Expected failure #1 (in thread B, only trace B alive).";
00291     check_points->n1.Notify();
00292     check_points->n2.WaitForNotification();
00293 
00294     ADD_FAILURE()
00295         << "Expected failure #3 (in thread B, trace A & B both alive).";
00296   }  // Trace B dies here.
00297   ADD_FAILURE()
00298       << "Expected failure #4 (in thread B, only trace A alive).";
00299   check_points->n3.Notify();
00300 }
00301 
00302 TEST(SCOPED_TRACETest, WorksConcurrently) {
00303   printf("(expecting 6 failures)\n");
00304 
00305   CheckPoints check_points;
00306   ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace,
00307                                        &check_points,
00308                                        NULL);
00309   check_points.n1.WaitForNotification();
00310 
00311   {
00312     SCOPED_TRACE("Trace A");
00313     ADD_FAILURE()
00314         << "Expected failure #2 (in thread A, trace A & B both alive).";
00315     check_points.n2.Notify();
00316     check_points.n3.WaitForNotification();
00317 
00318     ADD_FAILURE()
00319         << "Expected failure #5 (in thread A, only trace A alive).";
00320   }  // Trace A dies here.
00321   ADD_FAILURE()
00322       << "Expected failure #6 (in thread A, no trace alive).";
00323   thread.Join();
00324 }
00325 #endif  // GTEST_IS_THREADSAFE
00326 
00327 TEST(DisabledTestsWarningTest,
00328      DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
00329   // This test body is intentionally empty.  Its sole purpose is for
00330   // verifying that the --gtest_also_run_disabled_tests flag
00331   // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of
00332   // the test output.
00333 }
00334 
00335 // Tests using assertions outside of TEST and TEST_F.
00336 //
00337 // This function creates two failures intentionally.
00338 void AdHocTest() {
00339   printf("The non-test part of the code is expected to have 2 failures.\n\n");
00340   EXPECT_TRUE(false);
00341   EXPECT_EQ(2, 3);
00342 }
00343 
00344 // Runs all TESTs, all TEST_Fs, and the ad hoc test.
00345 int RunAllTests() {
00346   AdHocTest();
00347   return RUN_ALL_TESTS();
00348 }
00349 
00350 // Tests non-fatal failures in the fixture constructor.
00351 class NonFatalFailureInFixtureConstructorTest : public testing::Test {
00352  protected:
00353   NonFatalFailureInFixtureConstructorTest() {
00354     printf("(expecting 5 failures)\n");
00355     ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor.";
00356   }
00357 
00358   ~NonFatalFailureInFixtureConstructorTest() {
00359     ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor.";
00360   }
00361 
00362   virtual void SetUp() {
00363     ADD_FAILURE() << "Expected failure #2, in SetUp().";
00364   }
00365 
00366   virtual void TearDown() {
00367     ADD_FAILURE() << "Expected failure #4, in TearDown.";
00368   }
00369 };
00370 
00371 TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) {
00372   ADD_FAILURE() << "Expected failure #3, in the test body.";
00373 }
00374 
00375 // Tests fatal failures in the fixture constructor.
00376 class FatalFailureInFixtureConstructorTest : public testing::Test {
00377  protected:
00378   FatalFailureInFixtureConstructorTest() {
00379     printf("(expecting 2 failures)\n");
00380     Init();
00381   }
00382 
00383   ~FatalFailureInFixtureConstructorTest() {
00384     ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor.";
00385   }
00386 
00387   virtual void SetUp() {
00388     ADD_FAILURE() << "UNEXPECTED failure in SetUp().  "
00389                   << "We should never get here, as the test fixture c'tor "
00390                   << "had a fatal failure.";
00391   }
00392 
00393   virtual void TearDown() {
00394     ADD_FAILURE() << "UNEXPECTED failure in TearDown().  "
00395                   << "We should never get here, as the test fixture c'tor "
00396                   << "had a fatal failure.";
00397   }
00398 
00399  private:
00400   void Init() {
00401     FAIL() << "Expected failure #1, in the test fixture c'tor.";
00402   }
00403 };
00404 
00405 TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) {
00406   ADD_FAILURE() << "UNEXPECTED failure in the test body.  "
00407                 << "We should never get here, as the test fixture c'tor "
00408                 << "had a fatal failure.";
00409 }
00410 
00411 // Tests non-fatal failures in SetUp().
00412 class NonFatalFailureInSetUpTest : public testing::Test {
00413  protected:
00414   virtual ~NonFatalFailureInSetUpTest() {
00415     Deinit();
00416   }
00417 
00418   virtual void SetUp() {
00419     printf("(expecting 4 failures)\n");
00420     ADD_FAILURE() << "Expected failure #1, in SetUp().";
00421   }
00422 
00423   virtual void TearDown() {
00424     FAIL() << "Expected failure #3, in TearDown().";
00425   }
00426  private:
00427   void Deinit() {
00428     FAIL() << "Expected failure #4, in the test fixture d'tor.";
00429   }
00430 };
00431 
00432 TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) {
00433   FAIL() << "Expected failure #2, in the test function.";
00434 }
00435 
00436 // Tests fatal failures in SetUp().
00437 class FatalFailureInSetUpTest : public testing::Test {
00438  protected:
00439   virtual ~FatalFailureInSetUpTest() {
00440     Deinit();
00441   }
00442 
00443   virtual void SetUp() {
00444     printf("(expecting 3 failures)\n");
00445     FAIL() << "Expected failure #1, in SetUp().";
00446   }
00447 
00448   virtual void TearDown() {
00449     FAIL() << "Expected failure #2, in TearDown().";
00450   }
00451  private:
00452   void Deinit() {
00453     FAIL() << "Expected failure #3, in the test fixture d'tor.";
00454   }
00455 };
00456 
00457 TEST_F(FatalFailureInSetUpTest, FailureInSetUp) {
00458   FAIL() << "UNEXPECTED failure in the test function.  "
00459          << "We should never get here, as SetUp() failed.";
00460 }
00461 
00462 TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
00463   ADD_FAILURE_AT("foo.cc", 42) << "Expected failure in foo.cc";
00464 }
00465 
00466 #if GTEST_IS_THREADSAFE
00467 
00468 // A unary function that may die.
00469 void DieIf(bool should_die) {
00470   GTEST_CHECK_(!should_die) << " - death inside DieIf().";
00471 }
00472 
00473 // Tests running death tests in a multi-threaded context.
00474 
00475 // Used for coordination between the main and the spawn thread.
00476 struct SpawnThreadNotifications {
00477   SpawnThreadNotifications() {}
00478 
00479   Notification spawn_thread_started;
00480   Notification spawn_thread_ok_to_terminate;
00481 
00482  private:
00483   GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications);
00484 };
00485 
00486 // The function to be executed in the thread spawn by the
00487 // MultipleThreads test (below).
00488 static void ThreadRoutine(SpawnThreadNotifications* notifications) {
00489   // Signals the main thread that this thread has started.
00490   notifications->spawn_thread_started.Notify();
00491 
00492   // Waits for permission to finish from the main thread.
00493   notifications->spawn_thread_ok_to_terminate.WaitForNotification();
00494 }
00495 
00496 // This is a death-test test, but it's not named with a DeathTest
00497 // suffix.  It starts threads which might interfere with later
00498 // death tests, so it must run after all other death tests.
00499 class DeathTestAndMultiThreadsTest : public testing::Test {
00500  protected:
00501   // Starts a thread and waits for it to begin.
00502   virtual void SetUp() {
00503     thread_.reset(new ThreadWithParam<SpawnThreadNotifications*>(
00504         &ThreadRoutine, &notifications_, NULL));
00505     notifications_.spawn_thread_started.WaitForNotification();
00506   }
00507   // Tells the thread to finish, and reaps it.
00508   // Depending on the version of the thread library in use,
00509   // a manager thread might still be left running that will interfere
00510   // with later death tests.  This is unfortunate, but this class
00511   // cleans up after itself as best it can.
00512   virtual void TearDown() {
00513     notifications_.spawn_thread_ok_to_terminate.Notify();
00514   }
00515 
00516  private:
00517   SpawnThreadNotifications notifications_;
00518   scoped_ptr<ThreadWithParam<SpawnThreadNotifications*> > thread_;
00519 };
00520 
00521 #endif  // GTEST_IS_THREADSAFE
00522 
00523 // The MixedUpTestCaseTest test case verifies that Google Test will fail a
00524 // test if it uses a different fixture class than what other tests in
00525 // the same test case use.  It deliberately contains two fixture
00526 // classes with the same name but defined in different namespaces.
00527 
00528 // The MixedUpTestCaseWithSameTestNameTest test case verifies that
00529 // when the user defines two tests with the same test case name AND
00530 // same test name (but in different namespaces), the second test will
00531 // fail.
00532 
00533 namespace foo {
00534 
00535 class MixedUpTestCaseTest : public testing::Test {
00536 };
00537 
00538 TEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {}
00539 TEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {}
00540 
00541 class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
00542 };
00543 
00544 TEST_F(MixedUpTestCaseWithSameTestNameTest,
00545        TheSecondTestWithThisNameShouldFail) {}
00546 
00547 }  // namespace foo
00548 
00549 namespace bar {
00550 
00551 class MixedUpTestCaseTest : public testing::Test {
00552 };
00553 
00554 // The following two tests are expected to fail.  We rely on the
00555 // golden file to check that Google Test generates the right error message.
00556 TEST_F(MixedUpTestCaseTest, ThisShouldFail) {}
00557 TEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {}
00558 
00559 class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
00560 };
00561 
00562 // Expected to fail.  We rely on the golden file to check that Google Test
00563 // generates the right error message.
00564 TEST_F(MixedUpTestCaseWithSameTestNameTest,
00565        TheSecondTestWithThisNameShouldFail) {}
00566 
00567 }  // namespace bar
00568 
00569 // The following two test cases verify that Google Test catches the user
00570 // error of mixing TEST and TEST_F in the same test case.  The first
00571 // test case checks the scenario where TEST_F appears before TEST, and
00572 // the second one checks where TEST appears before TEST_F.
00573 
00574 class TEST_F_before_TEST_in_same_test_case : public testing::Test {
00575 };
00576 
00577 TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {}
00578 
00579 // Expected to fail.  We rely on the golden file to check that Google Test
00580 // generates the right error message.
00581 TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {}
00582 
00583 class TEST_before_TEST_F_in_same_test_case : public testing::Test {
00584 };
00585 
00586 TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {}
00587 
00588 // Expected to fail.  We rely on the golden file to check that Google Test
00589 // generates the right error message.
00590 TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {
00591 }
00592 
00593 // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE().
00594 int global_integer = 0;
00595 
00596 // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables.
00597 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
00598   global_integer = 0;
00599   EXPECT_NONFATAL_FAILURE({
00600     EXPECT_EQ(1, global_integer) << "Expected non-fatal failure.";
00601   }, "Expected non-fatal failure.");
00602 }
00603 
00604 // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables
00605 // (static or not).
00606 TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
00607   int m = 0;
00608   static int n;
00609   n = 1;
00610   EXPECT_NONFATAL_FAILURE({
00611     EXPECT_EQ(m, n) << "Expected non-fatal failure.";
00612   }, "Expected non-fatal failure.");
00613 }
00614 
00615 // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly
00616 // one non-fatal failure and no fatal failure.
00617 TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
00618   EXPECT_NONFATAL_FAILURE({
00619     ADD_FAILURE() << "Expected non-fatal failure.";
00620   }, "Expected non-fatal failure.");
00621 }
00622 
00623 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no
00624 // non-fatal failure.
00625 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
00626   printf("(expecting a failure)\n");
00627   EXPECT_NONFATAL_FAILURE({
00628   }, "");
00629 }
00630 
00631 // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two
00632 // non-fatal failures.
00633 TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
00634   printf("(expecting a failure)\n");
00635   EXPECT_NONFATAL_FAILURE({
00636     ADD_FAILURE() << "Expected non-fatal failure 1.";
00637     ADD_FAILURE() << "Expected non-fatal failure 2.";
00638   }, "");
00639 }
00640 
00641 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal
00642 // failure.
00643 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
00644   printf("(expecting a failure)\n");
00645   EXPECT_NONFATAL_FAILURE({
00646     FAIL() << "Expected fatal failure.";
00647   }, "");
00648 }
00649 
00650 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
00651 // tested returns.
00652 TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
00653   printf("(expecting a failure)\n");
00654   EXPECT_NONFATAL_FAILURE({
00655     return;
00656   }, "");
00657 }
00658 
00659 #if GTEST_HAS_EXCEPTIONS
00660 
00661 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
00662 // tested throws.
00663 TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
00664   printf("(expecting a failure)\n");
00665   try {
00666     EXPECT_NONFATAL_FAILURE({
00667       throw 0;
00668     }, "");
00669   } catch(int) {  // NOLINT
00670   }
00671 }
00672 
00673 #endif  // GTEST_HAS_EXCEPTIONS
00674 
00675 // Tests that EXPECT_FATAL_FAILURE() can reference global variables.
00676 TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
00677   global_integer = 0;
00678   EXPECT_FATAL_FAILURE({
00679     ASSERT_EQ(1, global_integer) << "Expected fatal failure.";
00680   }, "Expected fatal failure.");
00681 }
00682 
00683 // Tests that EXPECT_FATAL_FAILURE() can reference local static
00684 // variables.
00685 TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
00686   static int n;
00687   n = 1;
00688   EXPECT_FATAL_FAILURE({
00689     ASSERT_EQ(0, n) << "Expected fatal failure.";
00690   }, "Expected fatal failure.");
00691 }
00692 
00693 // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly
00694 // one fatal failure and no non-fatal failure.
00695 TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
00696   EXPECT_FATAL_FAILURE({
00697     FAIL() << "Expected fatal failure.";
00698   }, "Expected fatal failure.");
00699 }
00700 
00701 // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal
00702 // failure.
00703 TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
00704   printf("(expecting a failure)\n");
00705   EXPECT_FATAL_FAILURE({
00706   }, "");
00707 }
00708 
00709 // A helper for generating a fatal failure.
00710 void FatalFailure() {
00711   FAIL() << "Expected fatal failure.";
00712 }
00713 
00714 // Tests that EXPECT_FATAL_FAILURE() fails when there are two
00715 // fatal failures.
00716 TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
00717   printf("(expecting a failure)\n");
00718   EXPECT_FATAL_FAILURE({
00719     FatalFailure();
00720     FatalFailure();
00721   }, "");
00722 }
00723 
00724 // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal
00725 // failure.
00726 TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
00727   printf("(expecting a failure)\n");
00728   EXPECT_FATAL_FAILURE({
00729     ADD_FAILURE() << "Expected non-fatal failure.";
00730   }, "");
00731 }
00732 
00733 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
00734 // tested returns.
00735 TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
00736   printf("(expecting a failure)\n");
00737   EXPECT_FATAL_FAILURE({
00738     return;
00739   }, "");
00740 }
00741 
00742 #if GTEST_HAS_EXCEPTIONS
00743 
00744 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
00745 // tested throws.
00746 TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
00747   printf("(expecting a failure)\n");
00748   try {
00749     EXPECT_FATAL_FAILURE({
00750       throw 0;
00751     }, "");
00752   } catch(int) {  // NOLINT
00753   }
00754 }
00755 
00756 #endif  // GTEST_HAS_EXCEPTIONS
00757 
00758 // This #ifdef block tests the output of typed tests.
00759 #if GTEST_HAS_TYPED_TEST
00760 
00761 template <typename T>
00762 class TypedTest : public testing::Test {
00763 };
00764 
00765 TYPED_TEST_CASE(TypedTest, testing::Types<int>);
00766 
00767 TYPED_TEST(TypedTest, Success) {
00768   EXPECT_EQ(0, TypeParam());
00769 }
00770 
00771 TYPED_TEST(TypedTest, Failure) {
00772   EXPECT_EQ(1, TypeParam()) << "Expected failure";
00773 }
00774 
00775 #endif  // GTEST_HAS_TYPED_TEST
00776 
00777 // This #ifdef block tests the output of type-parameterized tests.
00778 #if GTEST_HAS_TYPED_TEST_P
00779 
00780 template <typename T>
00781 class TypedTestP : public testing::Test {
00782 };
00783 
00784 TYPED_TEST_CASE_P(TypedTestP);
00785 
00786 TYPED_TEST_P(TypedTestP, Success) {
00787   EXPECT_EQ(0U, TypeParam());
00788 }
00789 
00790 TYPED_TEST_P(TypedTestP, Failure) {
00791   EXPECT_EQ(1U, TypeParam()) << "Expected failure";
00792 }
00793 
00794 REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure);
00795 
00796 typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
00797 INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes);
00798 
00799 #endif  // GTEST_HAS_TYPED_TEST_P
00800 
00801 #if GTEST_HAS_DEATH_TEST
00802 
00803 // We rely on the golden file to verify that tests whose test case
00804 // name ends with DeathTest are run first.
00805 
00806 TEST(ADeathTest, ShouldRunFirst) {
00807 }
00808 
00809 # if GTEST_HAS_TYPED_TEST
00810 
00811 // We rely on the golden file to verify that typed tests whose test
00812 // case name ends with DeathTest are run first.
00813 
00814 template <typename T>
00815 class ATypedDeathTest : public testing::Test {
00816 };
00817 
00818 typedef testing::Types<int, double> NumericTypes;
00819 TYPED_TEST_CASE(ATypedDeathTest, NumericTypes);
00820 
00821 TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {
00822 }
00823 
00824 # endif  // GTEST_HAS_TYPED_TEST
00825 
00826 # if GTEST_HAS_TYPED_TEST_P
00827 
00828 
00829 // We rely on the golden file to verify that type-parameterized tests
00830 // whose test case name ends with DeathTest are run first.
00831 
00832 template <typename T>
00833 class ATypeParamDeathTest : public testing::Test {
00834 };
00835 
00836 TYPED_TEST_CASE_P(ATypeParamDeathTest);
00837 
00838 TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {
00839 }
00840 
00841 REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst);
00842 
00843 INSTANTIATE_TYPED_TEST_CASE_P(My, ATypeParamDeathTest, NumericTypes);
00844 
00845 # endif  // GTEST_HAS_TYPED_TEST_P
00846 
00847 #endif  // GTEST_HAS_DEATH_TEST
00848 
00849 // Tests various failure conditions of
00850 // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}.
00851 class ExpectFailureTest : public testing::Test {
00852  public:  // Must be public and not protected due to a bug in g++ 3.4.2.
00853   enum FailureMode {
00854     FATAL_FAILURE,
00855     NONFATAL_FAILURE
00856   };
00857   static void AddFailure(FailureMode failure) {
00858     if (failure == FATAL_FAILURE) {
00859       FAIL() << "Expected fatal failure.";
00860     } else {
00861       ADD_FAILURE() << "Expected non-fatal failure.";
00862     }
00863   }
00864 };
00865 
00866 TEST_F(ExpectFailureTest, ExpectFatalFailure) {
00867   // Expected fatal failure, but succeeds.
00868   printf("(expecting 1 failure)\n");
00869   EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure.");
00870   // Expected fatal failure, but got a non-fatal failure.
00871   printf("(expecting 1 failure)\n");
00872   EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal "
00873                        "failure.");
00874   // Wrong message.
00875   printf("(expecting 1 failure)\n");
00876   EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure "
00877                        "expected.");
00878 }
00879 
00880 TEST_F(ExpectFailureTest, ExpectNonFatalFailure) {
00881   // Expected non-fatal failure, but succeeds.
00882   printf("(expecting 1 failure)\n");
00883   EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure.");
00884   // Expected non-fatal failure, but got a fatal failure.
00885   printf("(expecting 1 failure)\n");
00886   EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure.");
00887   // Wrong message.
00888   printf("(expecting 1 failure)\n");
00889   EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal "
00890                           "failure.");
00891 }
00892 
00893 #if GTEST_IS_THREADSAFE
00894 
00895 class ExpectFailureWithThreadsTest : public ExpectFailureTest {
00896  protected:
00897   static void AddFailureInOtherThread(FailureMode failure) {
00898     ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL);
00899     thread.Join();
00900   }
00901 };
00902 
00903 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
00904   // We only intercept the current thread.
00905   printf("(expecting 2 failures)\n");
00906   EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE),
00907                        "Expected fatal failure.");
00908 }
00909 
00910 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
00911   // We only intercept the current thread.
00912   printf("(expecting 2 failures)\n");
00913   EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE),
00914                           "Expected non-fatal failure.");
00915 }
00916 
00917 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
00918 
00919 // Tests that the ScopedFakeTestPartResultReporter only catches failures from
00920 // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD.
00921 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
00922   printf("(expecting 2 failures)\n");
00923   TestPartResultArray results;
00924   {
00925     ScopedFakeTestPartResultReporter reporter(
00926         ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
00927         &results);
00928     AddFailureInOtherThread(FATAL_FAILURE);
00929     AddFailureInOtherThread(NONFATAL_FAILURE);
00930   }
00931   // The two failures should not have been intercepted.
00932   EXPECT_EQ(0, results.size()) << "This shouldn't fail.";
00933 }
00934 
00935 #endif  // GTEST_IS_THREADSAFE
00936 
00937 TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) {
00938   // Expected fatal failure, but succeeds.
00939   printf("(expecting 1 failure)\n");
00940   EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure.");
00941   // Expected fatal failure, but got a non-fatal failure.
00942   printf("(expecting 1 failure)\n");
00943   EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
00944                                       "Expected non-fatal failure.");
00945   // Wrong message.
00946   printf("(expecting 1 failure)\n");
00947   EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
00948                                       "Some other fatal failure expected.");
00949 }
00950 
00951 TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) {
00952   // Expected non-fatal failure, but succeeds.
00953   printf("(expecting 1 failure)\n");
00954   EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal "
00955                                          "failure.");
00956   // Expected non-fatal failure, but got a fatal failure.
00957   printf("(expecting 1 failure)\n");
00958   EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
00959                                          "Expected fatal failure.");
00960   // Wrong message.
00961   printf("(expecting 1 failure)\n");
00962   EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
00963                                          "Some other non-fatal failure.");
00964 }
00965 
00966 
00967 // Two test environments for testing testing::AddGlobalTestEnvironment().
00968 
00969 class FooEnvironment : public testing::Environment {
00970  public:
00971   virtual void SetUp() {
00972     printf("%s", "FooEnvironment::SetUp() called.\n");
00973   }
00974 
00975   virtual void TearDown() {
00976     printf("%s", "FooEnvironment::TearDown() called.\n");
00977     FAIL() << "Expected fatal failure.";
00978   }
00979 };
00980 
00981 class BarEnvironment : public testing::Environment {
00982  public:
00983   virtual void SetUp() {
00984     printf("%s", "BarEnvironment::SetUp() called.\n");
00985   }
00986 
00987   virtual void TearDown() {
00988     printf("%s", "BarEnvironment::TearDown() called.\n");
00989     ADD_FAILURE() << "Expected non-fatal failure.";
00990   }
00991 };
00992 
00993 bool GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = false;
00994 
00995 // The main function.
00996 //
00997 // The idea is to use Google Test to run all the tests we have defined (some
00998 // of them are intended to fail), and then compare the test results
00999 // with the "golden" file.
01000 int main(int argc, char **argv) {
01001   testing::GTEST_FLAG(print_time) = false;
01002 
01003   // We just run the tests, knowing some of them are intended to fail.
01004   // We will use a separate Python script to compare the output of
01005   // this program with the golden file.
01006 
01007   // It's hard to test InitGoogleTest() directly, as it has many
01008   // global side effects.  The following line serves as a sanity test
01009   // for it.
01010   testing::InitGoogleTest(&argc, argv);
01011   if (argc >= 2 &&
01012       (std::string(argv[1]) ==
01013        "--gtest_internal_skip_environment_and_ad_hoc_tests"))
01014     GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = true;
01015 
01016 #if GTEST_HAS_DEATH_TEST
01017   if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") {
01018     // Skip the usual output capturing if we're running as the child
01019     // process of an threadsafe-style death test.
01020 # if GTEST_OS_WINDOWS
01021     posix::FReopen("nul:", "w", stdout);
01022 # else
01023     posix::FReopen("/dev/null", "w", stdout);
01024 # endif  // GTEST_OS_WINDOWS
01025     return RUN_ALL_TESTS();
01026   }
01027 #endif  // GTEST_HAS_DEATH_TEST
01028 
01029   if (GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests))
01030     return RUN_ALL_TESTS();
01031 
01032   // Registers two global test environments.
01033   // The golden file verifies that they are set up in the order they
01034   // are registered, and torn down in the reverse order.
01035   testing::AddGlobalTestEnvironment(new FooEnvironment);
01036   testing::AddGlobalTestEnvironment(new BarEnvironment);
01037 
01038   return RunAllTests();
01039 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04