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


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:55