?login_element?

Subversion Repositories NedoOS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. // Copyright 2005, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. //     * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. //     * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. //     * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // The purpose of this file is to generate Google Test output under
  31. // various conditions.  The output will then be verified by
  32. // googletest-output-test.py to ensure that Google Test generates the
  33. // desired messages.  Therefore, most tests in this file are MEANT TO
  34. // FAIL.
  35.  
  36. #include "gtest/gtest-spi.h"
  37. #include "gtest/gtest.h"
  38. #include "src/gtest-internal-inl.h"
  39.  
  40. #include <stdlib.h>
  41.  
  42. #if GTEST_IS_THREADSAFE
  43. using testing::ScopedFakeTestPartResultReporter;
  44. using testing::TestPartResultArray;
  45.  
  46. using testing::internal::Notification;
  47. using testing::internal::ThreadWithParam;
  48. #endif
  49.  
  50. namespace posix = ::testing::internal::posix;
  51.  
  52. // Tests catching fatal failures.
  53.  
  54. // A subroutine used by the following test.
  55. void TestEq1(int x) {
  56.   ASSERT_EQ(1, x);
  57. }
  58.  
  59. // This function calls a test subroutine, catches the fatal failure it
  60. // generates, and then returns early.
  61. void TryTestSubroutine() {
  62.   // Calls a subrountine that yields a fatal failure.
  63.   TestEq1(2);
  64.  
  65.   // Catches the fatal failure and aborts the test.
  66.   //
  67.   // The testing::Test:: prefix is necessary when calling
  68.   // HasFatalFailure() outside of a TEST, TEST_F, or test fixture.
  69.   if (testing::Test::HasFatalFailure()) return;
  70.  
  71.   // If we get here, something is wrong.
  72.   FAIL() << "This should never be reached.";
  73. }
  74.  
  75. TEST(PassingTest, PassingTest1) {
  76. }
  77.  
  78. TEST(PassingTest, PassingTest2) {
  79. }
  80.  
  81. // Tests that parameters of failing parameterized tests are printed in the
  82. // failing test summary.
  83. class FailingParamTest : public testing::TestWithParam<int> {};
  84.  
  85. TEST_P(FailingParamTest, Fails) {
  86.   EXPECT_EQ(1, GetParam());
  87. }
  88.  
  89. // This generates a test which will fail. Google Test is expected to print
  90. // its parameter when it outputs the list of all failed tests.
  91. INSTANTIATE_TEST_CASE_P(PrintingFailingParams,
  92.                         FailingParamTest,
  93.                         testing::Values(2));
  94.  
  95. static const char kGoldenString[] = "\"Line\0 1\"\nLine 2";
  96.  
  97. TEST(NonfatalFailureTest, EscapesStringOperands) {
  98.   std::string actual = "actual \"string\"";
  99.   EXPECT_EQ(kGoldenString, actual);
  100.  
  101.   const char* golden = kGoldenString;
  102.   EXPECT_EQ(golden, actual);
  103. }
  104.  
  105. TEST(NonfatalFailureTest, DiffForLongStrings) {
  106.   std::string golden_str(kGoldenString, sizeof(kGoldenString) - 1);
  107.   EXPECT_EQ(golden_str, "Line 2");
  108. }
  109.  
  110. // Tests catching a fatal failure in a subroutine.
  111. TEST(FatalFailureTest, FatalFailureInSubroutine) {
  112.   printf("(expecting a failure that x should be 1)\n");
  113.  
  114.   TryTestSubroutine();
  115. }
  116.  
  117. // Tests catching a fatal failure in a nested subroutine.
  118. TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
  119.   printf("(expecting a failure that x should be 1)\n");
  120.  
  121.   // Calls a subrountine that yields a fatal failure.
  122.   TryTestSubroutine();
  123.  
  124.   // Catches the fatal failure and aborts the test.
  125.   //
  126.   // When calling HasFatalFailure() inside a TEST, TEST_F, or test
  127.   // fixture, the testing::Test:: prefix is not needed.
  128.   if (HasFatalFailure()) return;
  129.  
  130.   // If we get here, something is wrong.
  131.   FAIL() << "This should never be reached.";
  132. }
  133.  
  134. // Tests HasFatalFailure() after a failed EXPECT check.
  135. TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
  136.   printf("(expecting a failure on false)\n");
  137.   EXPECT_TRUE(false);  // Generates a nonfatal failure
  138.   ASSERT_FALSE(HasFatalFailure());  // This should succeed.
  139. }
  140.  
  141. // Tests interleaving user logging and Google Test assertions.
  142. TEST(LoggingTest, InterleavingLoggingAndAssertions) {
  143.   static const int a[4] = {
  144.     3, 9, 2, 6
  145.   };
  146.  
  147.   printf("(expecting 2 failures on (3) >= (a[i]))\n");
  148.   for (int i = 0; i < static_cast<int>(sizeof(a)/sizeof(*a)); i++) {
  149.     printf("i == %d\n", i);
  150.     EXPECT_GE(3, a[i]);
  151.   }
  152. }
  153.  
  154. // Tests the SCOPED_TRACE macro.
  155.  
  156. // A helper function for testing SCOPED_TRACE.
  157. void SubWithoutTrace(int n) {
  158.   EXPECT_EQ(1, n);
  159.   ASSERT_EQ(2, n);
  160. }
  161.  
  162. // Another helper function for testing SCOPED_TRACE.
  163. void SubWithTrace(int n) {
  164.   SCOPED_TRACE(testing::Message() << "n = " << n);
  165.  
  166.   SubWithoutTrace(n);
  167. }
  168.  
  169. TEST(SCOPED_TRACETest, AcceptedValues) {
  170.   SCOPED_TRACE("literal string");
  171.   SCOPED_TRACE(std::string("std::string"));
  172.   SCOPED_TRACE(1337);  // streamable type
  173.   const char* null_value = NULL;
  174.   SCOPED_TRACE(null_value);
  175.  
  176.   ADD_FAILURE() << "Just checking that all these values work fine.";
  177. }
  178.  
  179. // Tests that SCOPED_TRACE() obeys lexical scopes.
  180. TEST(SCOPED_TRACETest, ObeysScopes) {
  181.   printf("(expected to fail)\n");
  182.  
  183.   // There should be no trace before SCOPED_TRACE() is invoked.
  184.   ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
  185.  
  186.   {
  187.     SCOPED_TRACE("Expected trace");
  188.     // After SCOPED_TRACE(), a failure in the current scope should contain
  189.     // the trace.
  190.     ADD_FAILURE() << "This failure is expected, and should have a trace.";
  191.   }
  192.  
  193.   // Once the control leaves the scope of the SCOPED_TRACE(), there
  194.   // should be no trace again.
  195.   ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
  196. }
  197.  
  198. // Tests that SCOPED_TRACE works inside a loop.
  199. TEST(SCOPED_TRACETest, WorksInLoop) {
  200.   printf("(expected to fail)\n");
  201.  
  202.   for (int i = 1; i <= 2; i++) {
  203.     SCOPED_TRACE(testing::Message() << "i = " << i);
  204.  
  205.     SubWithoutTrace(i);
  206.   }
  207. }
  208.  
  209. // Tests that SCOPED_TRACE works in a subroutine.
  210. TEST(SCOPED_TRACETest, WorksInSubroutine) {
  211.   printf("(expected to fail)\n");
  212.  
  213.   SubWithTrace(1);
  214.   SubWithTrace(2);
  215. }
  216.  
  217. // Tests that SCOPED_TRACE can be nested.
  218. TEST(SCOPED_TRACETest, CanBeNested) {
  219.   printf("(expected to fail)\n");
  220.  
  221.   SCOPED_TRACE("");  // A trace without a message.
  222.  
  223.   SubWithTrace(2);
  224. }
  225.  
  226. // Tests that multiple SCOPED_TRACEs can be used in the same scope.
  227. TEST(SCOPED_TRACETest, CanBeRepeated) {
  228.   printf("(expected to fail)\n");
  229.  
  230.   SCOPED_TRACE("A");
  231.   ADD_FAILURE()
  232.       << "This failure is expected, and should contain trace point A.";
  233.  
  234.   SCOPED_TRACE("B");
  235.   ADD_FAILURE()
  236.       << "This failure is expected, and should contain trace point A and B.";
  237.  
  238.   {
  239.     SCOPED_TRACE("C");
  240.     ADD_FAILURE() << "This failure is expected, and should "
  241.                   << "contain trace point A, B, and C.";
  242.   }
  243.  
  244.   SCOPED_TRACE("D");
  245.   ADD_FAILURE() << "This failure is expected, and should "
  246.                 << "contain trace point A, B, and D.";
  247. }
  248.  
  249. #if GTEST_IS_THREADSAFE
  250. // Tests that SCOPED_TRACE()s can be used concurrently from multiple
  251. // threads.  Namely, an assertion should be affected by
  252. // SCOPED_TRACE()s in its own thread only.
  253.  
  254. // Here's the sequence of actions that happen in the test:
  255. //
  256. //   Thread A (main)                | Thread B (spawned)
  257. //   ===============================|================================
  258. //   spawns thread B                |
  259. //   -------------------------------+--------------------------------
  260. //   waits for n1                   | SCOPED_TRACE("Trace B");
  261. //                                  | generates failure #1
  262. //                                  | notifies n1
  263. //   -------------------------------+--------------------------------
  264. //   SCOPED_TRACE("Trace A");       | waits for n2
  265. //   generates failure #2           |
  266. //   notifies n2                    |
  267. //   -------------------------------|--------------------------------
  268. //   waits for n3                   | generates failure #3
  269. //                                  | trace B dies
  270. //                                  | generates failure #4
  271. //                                  | notifies n3
  272. //   -------------------------------|--------------------------------
  273. //   generates failure #5           | finishes
  274. //   trace A dies                   |
  275. //   generates failure #6           |
  276. //   -------------------------------|--------------------------------
  277. //   waits for thread B to finish   |
  278.  
  279. struct CheckPoints {
  280.   Notification n1;
  281.   Notification n2;
  282.   Notification n3;
  283. };
  284.  
  285. static void ThreadWithScopedTrace(CheckPoints* check_points) {
  286.   {
  287.     SCOPED_TRACE("Trace B");
  288.     ADD_FAILURE()
  289.         << "Expected failure #1 (in thread B, only trace B alive).";
  290.     check_points->n1.Notify();
  291.     check_points->n2.WaitForNotification();
  292.  
  293.     ADD_FAILURE()
  294.         << "Expected failure #3 (in thread B, trace A & B both alive).";
  295.   }  // Trace B dies here.
  296.   ADD_FAILURE()
  297.       << "Expected failure #4 (in thread B, only trace A alive).";
  298.   check_points->n3.Notify();
  299. }
  300.  
  301. TEST(SCOPED_TRACETest, WorksConcurrently) {
  302.   printf("(expecting 6 failures)\n");
  303.  
  304.   CheckPoints check_points;
  305.   ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace,
  306.                                        &check_points,
  307.                                        NULL);
  308.   check_points.n1.WaitForNotification();
  309.  
  310.   {
  311.     SCOPED_TRACE("Trace A");
  312.     ADD_FAILURE()
  313.         << "Expected failure #2 (in thread A, trace A & B both alive).";
  314.     check_points.n2.Notify();
  315.     check_points.n3.WaitForNotification();
  316.  
  317.     ADD_FAILURE()
  318.         << "Expected failure #5 (in thread A, only trace A alive).";
  319.   }  // Trace A dies here.
  320.   ADD_FAILURE()
  321.       << "Expected failure #6 (in thread A, no trace alive).";
  322.   thread.Join();
  323. }
  324. #endif  // GTEST_IS_THREADSAFE
  325.  
  326. // Tests basic functionality of the ScopedTrace utility (most of its features
  327. // are already tested in SCOPED_TRACETest).
  328. TEST(ScopedTraceTest, WithExplicitFileAndLine) {
  329.   testing::ScopedTrace trace("explicit_file.cc", 123, "expected trace message");
  330.   ADD_FAILURE() << "Check that the trace is attached to a particular location.";
  331. }
  332.  
  333. TEST(DisabledTestsWarningTest,
  334.      DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
  335.   // This test body is intentionally empty.  Its sole purpose is for
  336.   // verifying that the --gtest_also_run_disabled_tests flag
  337.   // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of
  338.   // the test output.
  339. }
  340.  
  341. // Tests using assertions outside of TEST and TEST_F.
  342. //
  343. // This function creates two failures intentionally.
  344. void AdHocTest() {
  345.   printf("The non-test part of the code is expected to have 2 failures.\n\n");
  346.   EXPECT_TRUE(false);
  347.   EXPECT_EQ(2, 3);
  348. }
  349.  
  350. // Runs all TESTs, all TEST_Fs, and the ad hoc test.
  351. int RunAllTests() {
  352.   AdHocTest();
  353.   return RUN_ALL_TESTS();
  354. }
  355.  
  356. // Tests non-fatal failures in the fixture constructor.
  357. class NonFatalFailureInFixtureConstructorTest : public testing::Test {
  358.  protected:
  359.   NonFatalFailureInFixtureConstructorTest() {
  360.     printf("(expecting 5 failures)\n");
  361.     ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor.";
  362.   }
  363.  
  364.   ~NonFatalFailureInFixtureConstructorTest() {
  365.     ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor.";
  366.   }
  367.  
  368.   virtual void SetUp() {
  369.     ADD_FAILURE() << "Expected failure #2, in SetUp().";
  370.   }
  371.  
  372.   virtual void TearDown() {
  373.     ADD_FAILURE() << "Expected failure #4, in TearDown.";
  374.   }
  375. };
  376.  
  377. TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) {
  378.   ADD_FAILURE() << "Expected failure #3, in the test body.";
  379. }
  380.  
  381. // Tests fatal failures in the fixture constructor.
  382. class FatalFailureInFixtureConstructorTest : public testing::Test {
  383.  protected:
  384.   FatalFailureInFixtureConstructorTest() {
  385.     printf("(expecting 2 failures)\n");
  386.     Init();
  387.   }
  388.  
  389.   ~FatalFailureInFixtureConstructorTest() {
  390.     ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor.";
  391.   }
  392.  
  393.   virtual void SetUp() {
  394.     ADD_FAILURE() << "UNEXPECTED failure in SetUp().  "
  395.                   << "We should never get here, as the test fixture c'tor "
  396.                   << "had a fatal failure.";
  397.   }
  398.  
  399.   virtual void TearDown() {
  400.     ADD_FAILURE() << "UNEXPECTED failure in TearDown().  "
  401.                   << "We should never get here, as the test fixture c'tor "
  402.                   << "had a fatal failure.";
  403.   }
  404.  
  405.  private:
  406.   void Init() {
  407.     FAIL() << "Expected failure #1, in the test fixture c'tor.";
  408.   }
  409. };
  410.  
  411. TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) {
  412.   ADD_FAILURE() << "UNEXPECTED failure in the test body.  "
  413.                 << "We should never get here, as the test fixture c'tor "
  414.                 << "had a fatal failure.";
  415. }
  416.  
  417. // Tests non-fatal failures in SetUp().
  418. class NonFatalFailureInSetUpTest : public testing::Test {
  419.  protected:
  420.   virtual ~NonFatalFailureInSetUpTest() {
  421.     Deinit();
  422.   }
  423.  
  424.   virtual void SetUp() {
  425.     printf("(expecting 4 failures)\n");
  426.     ADD_FAILURE() << "Expected failure #1, in SetUp().";
  427.   }
  428.  
  429.   virtual void TearDown() {
  430.     FAIL() << "Expected failure #3, in TearDown().";
  431.   }
  432.  private:
  433.   void Deinit() {
  434.     FAIL() << "Expected failure #4, in the test fixture d'tor.";
  435.   }
  436. };
  437.  
  438. TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) {
  439.   FAIL() << "Expected failure #2, in the test function.";
  440. }
  441.  
  442. // Tests fatal failures in SetUp().
  443. class FatalFailureInSetUpTest : public testing::Test {
  444.  protected:
  445.   virtual ~FatalFailureInSetUpTest() {
  446.     Deinit();
  447.   }
  448.  
  449.   virtual void SetUp() {
  450.     printf("(expecting 3 failures)\n");
  451.     FAIL() << "Expected failure #1, in SetUp().";
  452.   }
  453.  
  454.   virtual void TearDown() {
  455.     FAIL() << "Expected failure #2, in TearDown().";
  456.   }
  457.  private:
  458.   void Deinit() {
  459.     FAIL() << "Expected failure #3, in the test fixture d'tor.";
  460.   }
  461. };
  462.  
  463. TEST_F(FatalFailureInSetUpTest, FailureInSetUp) {
  464.   FAIL() << "UNEXPECTED failure in the test function.  "
  465.          << "We should never get here, as SetUp() failed.";
  466. }
  467.  
  468. TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
  469.   ADD_FAILURE_AT("foo.cc", 42) << "Expected failure in foo.cc";
  470. }
  471.  
  472. #if GTEST_IS_THREADSAFE
  473.  
  474. // A unary function that may die.
  475. void DieIf(bool should_die) {
  476.   GTEST_CHECK_(!should_die) << " - death inside DieIf().";
  477. }
  478.  
  479. // Tests running death tests in a multi-threaded context.
  480.  
  481. // Used for coordination between the main and the spawn thread.
  482. struct SpawnThreadNotifications {
  483.   SpawnThreadNotifications() {}
  484.  
  485.   Notification spawn_thread_started;
  486.   Notification spawn_thread_ok_to_terminate;
  487.  
  488.  private:
  489.   GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications);
  490. };
  491.  
  492. // The function to be executed in the thread spawn by the
  493. // MultipleThreads test (below).
  494. static void ThreadRoutine(SpawnThreadNotifications* notifications) {
  495.   // Signals the main thread that this thread has started.
  496.   notifications->spawn_thread_started.Notify();
  497.  
  498.   // Waits for permission to finish from the main thread.
  499.   notifications->spawn_thread_ok_to_terminate.WaitForNotification();
  500. }
  501.  
  502. // This is a death-test test, but it's not named with a DeathTest
  503. // suffix.  It starts threads which might interfere with later
  504. // death tests, so it must run after all other death tests.
  505. class DeathTestAndMultiThreadsTest : public testing::Test {
  506.  protected:
  507.   // Starts a thread and waits for it to begin.
  508.   virtual void SetUp() {
  509.     thread_.reset(new ThreadWithParam<SpawnThreadNotifications*>(
  510.         &ThreadRoutine, &notifications_, NULL));
  511.     notifications_.spawn_thread_started.WaitForNotification();
  512.   }
  513.   // Tells the thread to finish, and reaps it.
  514.   // Depending on the version of the thread library in use,
  515.   // a manager thread might still be left running that will interfere
  516.   // with later death tests.  This is unfortunate, but this class
  517.   // cleans up after itself as best it can.
  518.   virtual void TearDown() {
  519.     notifications_.spawn_thread_ok_to_terminate.Notify();
  520.   }
  521.  
  522.  private:
  523.   SpawnThreadNotifications notifications_;
  524.   testing::internal::scoped_ptr<ThreadWithParam<SpawnThreadNotifications*> >
  525.       thread_;
  526. };
  527.  
  528. #endif  // GTEST_IS_THREADSAFE
  529.  
  530. // The MixedUpTestCaseTest test case verifies that Google Test will fail a
  531. // test if it uses a different fixture class than what other tests in
  532. // the same test case use.  It deliberately contains two fixture
  533. // classes with the same name but defined in different namespaces.
  534.  
  535. // The MixedUpTestCaseWithSameTestNameTest test case verifies that
  536. // when the user defines two tests with the same test case name AND
  537. // same test name (but in different namespaces), the second test will
  538. // fail.
  539.  
  540. namespace foo {
  541.  
  542. class MixedUpTestCaseTest : public testing::Test {
  543. };
  544.  
  545. TEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {}
  546. TEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {}
  547.  
  548. class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
  549. };
  550.  
  551. TEST_F(MixedUpTestCaseWithSameTestNameTest,
  552.        TheSecondTestWithThisNameShouldFail) {}
  553.  
  554. }  // namespace foo
  555.  
  556. namespace bar {
  557.  
  558. class MixedUpTestCaseTest : public testing::Test {
  559. };
  560.  
  561. // The following two tests are expected to fail.  We rely on the
  562. // golden file to check that Google Test generates the right error message.
  563. TEST_F(MixedUpTestCaseTest, ThisShouldFail) {}
  564. TEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {}
  565.  
  566. class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
  567. };
  568.  
  569. // Expected to fail.  We rely on the golden file to check that Google Test
  570. // generates the right error message.
  571. TEST_F(MixedUpTestCaseWithSameTestNameTest,
  572.        TheSecondTestWithThisNameShouldFail) {}
  573.  
  574. }  // namespace bar
  575.  
  576. // The following two test cases verify that Google Test catches the user
  577. // error of mixing TEST and TEST_F in the same test case.  The first
  578. // test case checks the scenario where TEST_F appears before TEST, and
  579. // the second one checks where TEST appears before TEST_F.
  580.  
  581. class TEST_F_before_TEST_in_same_test_case : public testing::Test {
  582. };
  583.  
  584. TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {}
  585.  
  586. // Expected to fail.  We rely on the golden file to check that Google Test
  587. // generates the right error message.
  588. TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {}
  589.  
  590. class TEST_before_TEST_F_in_same_test_case : public testing::Test {
  591. };
  592.  
  593. TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {}
  594.  
  595. // Expected to fail.  We rely on the golden file to check that Google Test
  596. // generates the right error message.
  597. TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {
  598. }
  599.  
  600. // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE().
  601. int global_integer = 0;
  602.  
  603. // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables.
  604. TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
  605.   global_integer = 0;
  606.   EXPECT_NONFATAL_FAILURE({
  607.     EXPECT_EQ(1, global_integer) << "Expected non-fatal failure.";
  608.   }, "Expected non-fatal failure.");
  609. }
  610.  
  611. // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables
  612. // (static or not).
  613. TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
  614.   int m = 0;
  615.   static int n;
  616.   n = 1;
  617.   EXPECT_NONFATAL_FAILURE({
  618.     EXPECT_EQ(m, n) << "Expected non-fatal failure.";
  619.   }, "Expected non-fatal failure.");
  620. }
  621.  
  622. // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly
  623. // one non-fatal failure and no fatal failure.
  624. TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
  625.   EXPECT_NONFATAL_FAILURE({
  626.     ADD_FAILURE() << "Expected non-fatal failure.";
  627.   }, "Expected non-fatal failure.");
  628. }
  629.  
  630. // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no
  631. // non-fatal failure.
  632. TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
  633.   printf("(expecting a failure)\n");
  634.   EXPECT_NONFATAL_FAILURE({
  635.   }, "");
  636. }
  637.  
  638. // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two
  639. // non-fatal failures.
  640. TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
  641.   printf("(expecting a failure)\n");
  642.   EXPECT_NONFATAL_FAILURE({
  643.     ADD_FAILURE() << "Expected non-fatal failure 1.";
  644.     ADD_FAILURE() << "Expected non-fatal failure 2.";
  645.   }, "");
  646. }
  647.  
  648. // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal
  649. // failure.
  650. TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
  651.   printf("(expecting a failure)\n");
  652.   EXPECT_NONFATAL_FAILURE({
  653.     FAIL() << "Expected fatal failure.";
  654.   }, "");
  655. }
  656.  
  657. // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
  658. // tested returns.
  659. TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
  660.   printf("(expecting a failure)\n");
  661.   EXPECT_NONFATAL_FAILURE({
  662.     return;
  663.   }, "");
  664. }
  665.  
  666. #if GTEST_HAS_EXCEPTIONS
  667.  
  668. // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
  669. // tested throws.
  670. TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
  671.   printf("(expecting a failure)\n");
  672.   try {
  673.     EXPECT_NONFATAL_FAILURE({
  674.       throw 0;
  675.     }, "");
  676.   } catch(int) {  // NOLINT
  677.   }
  678. }
  679.  
  680. #endif  // GTEST_HAS_EXCEPTIONS
  681.  
  682. // Tests that EXPECT_FATAL_FAILURE() can reference global variables.
  683. TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
  684.   global_integer = 0;
  685.   EXPECT_FATAL_FAILURE({
  686.     ASSERT_EQ(1, global_integer) << "Expected fatal failure.";
  687.   }, "Expected fatal failure.");
  688. }
  689.  
  690. // Tests that EXPECT_FATAL_FAILURE() can reference local static
  691. // variables.
  692. TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
  693.   static int n;
  694.   n = 1;
  695.   EXPECT_FATAL_FAILURE({
  696.     ASSERT_EQ(0, n) << "Expected fatal failure.";
  697.   }, "Expected fatal failure.");
  698. }
  699.  
  700. // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly
  701. // one fatal failure and no non-fatal failure.
  702. TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
  703.   EXPECT_FATAL_FAILURE({
  704.     FAIL() << "Expected fatal failure.";
  705.   }, "Expected fatal failure.");
  706. }
  707.  
  708. // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal
  709. // failure.
  710. TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
  711.   printf("(expecting a failure)\n");
  712.   EXPECT_FATAL_FAILURE({
  713.   }, "");
  714. }
  715.  
  716. // A helper for generating a fatal failure.
  717. void FatalFailure() {
  718.   FAIL() << "Expected fatal failure.";
  719. }
  720.  
  721. // Tests that EXPECT_FATAL_FAILURE() fails when there are two
  722. // fatal failures.
  723. TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
  724.   printf("(expecting a failure)\n");
  725.   EXPECT_FATAL_FAILURE({
  726.     FatalFailure();
  727.     FatalFailure();
  728.   }, "");
  729. }
  730.  
  731. // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal
  732. // failure.
  733. TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
  734.   printf("(expecting a failure)\n");
  735.   EXPECT_FATAL_FAILURE({
  736.     ADD_FAILURE() << "Expected non-fatal failure.";
  737.   }, "");
  738. }
  739.  
  740. // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
  741. // tested returns.
  742. TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
  743.   printf("(expecting a failure)\n");
  744.   EXPECT_FATAL_FAILURE({
  745.     return;
  746.   }, "");
  747. }
  748.  
  749. #if GTEST_HAS_EXCEPTIONS
  750.  
  751. // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
  752. // tested throws.
  753. TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
  754.   printf("(expecting a failure)\n");
  755.   try {
  756.     EXPECT_FATAL_FAILURE({
  757.       throw 0;
  758.     }, "");
  759.   } catch(int) {  // NOLINT
  760.   }
  761. }
  762.  
  763. #endif  // GTEST_HAS_EXCEPTIONS
  764.  
  765. // This #ifdef block tests the output of value-parameterized tests.
  766.  
  767. std::string ParamNameFunc(const testing::TestParamInfo<std::string>& info) {
  768.   return info.param;
  769. }
  770.  
  771. class ParamTest : public testing::TestWithParam<std::string> {
  772. };
  773.  
  774. TEST_P(ParamTest, Success) {
  775.   EXPECT_EQ("a", GetParam());
  776. }
  777.  
  778. TEST_P(ParamTest, Failure) {
  779.   EXPECT_EQ("b", GetParam()) << "Expected failure";
  780. }
  781.  
  782. INSTANTIATE_TEST_CASE_P(PrintingStrings,
  783.                         ParamTest,
  784.                         testing::Values(std::string("a")),
  785.                         ParamNameFunc);
  786.  
  787. // This #ifdef block tests the output of typed tests.
  788. #if GTEST_HAS_TYPED_TEST
  789.  
  790. template <typename T>
  791. class TypedTest : public testing::Test {
  792. };
  793.  
  794. TYPED_TEST_CASE(TypedTest, testing::Types<int>);
  795.  
  796. TYPED_TEST(TypedTest, Success) {
  797.   EXPECT_EQ(0, TypeParam());
  798. }
  799.  
  800. TYPED_TEST(TypedTest, Failure) {
  801.   EXPECT_EQ(1, TypeParam()) << "Expected failure";
  802. }
  803.  
  804. #endif  // GTEST_HAS_TYPED_TEST
  805.  
  806. // This #ifdef block tests the output of type-parameterized tests.
  807. #if GTEST_HAS_TYPED_TEST_P
  808.  
  809. template <typename T>
  810. class TypedTestP : public testing::Test {
  811. };
  812.  
  813. TYPED_TEST_CASE_P(TypedTestP);
  814.  
  815. TYPED_TEST_P(TypedTestP, Success) {
  816.   EXPECT_EQ(0U, TypeParam());
  817. }
  818.  
  819. TYPED_TEST_P(TypedTestP, Failure) {
  820.   EXPECT_EQ(1U, TypeParam()) << "Expected failure";
  821. }
  822.  
  823. REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure);
  824.  
  825. typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
  826. INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes);
  827.  
  828. #endif  // GTEST_HAS_TYPED_TEST_P
  829.  
  830. #if GTEST_HAS_DEATH_TEST
  831.  
  832. // We rely on the golden file to verify that tests whose test case
  833. // name ends with DeathTest are run first.
  834.  
  835. TEST(ADeathTest, ShouldRunFirst) {
  836. }
  837.  
  838. # if GTEST_HAS_TYPED_TEST
  839.  
  840. // We rely on the golden file to verify that typed tests whose test
  841. // case name ends with DeathTest are run first.
  842.  
  843. template <typename T>
  844. class ATypedDeathTest : public testing::Test {
  845. };
  846.  
  847. typedef testing::Types<int, double> NumericTypes;
  848. TYPED_TEST_CASE(ATypedDeathTest, NumericTypes);
  849.  
  850. TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {
  851. }
  852.  
  853. # endif  // GTEST_HAS_TYPED_TEST
  854.  
  855. # if GTEST_HAS_TYPED_TEST_P
  856.  
  857.  
  858. // We rely on the golden file to verify that type-parameterized tests
  859. // whose test case name ends with DeathTest are run first.
  860.  
  861. template <typename T>
  862. class ATypeParamDeathTest : public testing::Test {
  863. };
  864.  
  865. TYPED_TEST_CASE_P(ATypeParamDeathTest);
  866.  
  867. TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {
  868. }
  869.  
  870. REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst);
  871.  
  872. INSTANTIATE_TYPED_TEST_CASE_P(My, ATypeParamDeathTest, NumericTypes);
  873.  
  874. # endif  // GTEST_HAS_TYPED_TEST_P
  875.  
  876. #endif  // GTEST_HAS_DEATH_TEST
  877.  
  878. // Tests various failure conditions of
  879. // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}.
  880. class ExpectFailureTest : public testing::Test {
  881.  public:  // Must be public and not protected due to a bug in g++ 3.4.2.
  882.   enum FailureMode {
  883.     FATAL_FAILURE,
  884.     NONFATAL_FAILURE
  885.   };
  886.   static void AddFailure(FailureMode failure) {
  887.     if (failure == FATAL_FAILURE) {
  888.       FAIL() << "Expected fatal failure.";
  889.     } else {
  890.       ADD_FAILURE() << "Expected non-fatal failure.";
  891.     }
  892.   }
  893. };
  894.  
  895. TEST_F(ExpectFailureTest, ExpectFatalFailure) {
  896.   // Expected fatal failure, but succeeds.
  897.   printf("(expecting 1 failure)\n");
  898.   EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure.");
  899.   // Expected fatal failure, but got a non-fatal failure.
  900.   printf("(expecting 1 failure)\n");
  901.   EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal "
  902.                        "failure.");
  903.   // Wrong message.
  904.   printf("(expecting 1 failure)\n");
  905.   EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure "
  906.                        "expected.");
  907. }
  908.  
  909. TEST_F(ExpectFailureTest, ExpectNonFatalFailure) {
  910.   // Expected non-fatal failure, but succeeds.
  911.   printf("(expecting 1 failure)\n");
  912.   EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure.");
  913.   // Expected non-fatal failure, but got a fatal failure.
  914.   printf("(expecting 1 failure)\n");
  915.   EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure.");
  916.   // Wrong message.
  917.   printf("(expecting 1 failure)\n");
  918.   EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal "
  919.                           "failure.");
  920. }
  921.  
  922. #if GTEST_IS_THREADSAFE
  923.  
  924. class ExpectFailureWithThreadsTest : public ExpectFailureTest {
  925.  protected:
  926.   static void AddFailureInOtherThread(FailureMode failure) {
  927.     ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL);
  928.     thread.Join();
  929.   }
  930. };
  931.  
  932. TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
  933.   // We only intercept the current thread.
  934.   printf("(expecting 2 failures)\n");
  935.   EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE),
  936.                        "Expected fatal failure.");
  937. }
  938.  
  939. TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
  940.   // We only intercept the current thread.
  941.   printf("(expecting 2 failures)\n");
  942.   EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE),
  943.                           "Expected non-fatal failure.");
  944. }
  945.  
  946. typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
  947.  
  948. // Tests that the ScopedFakeTestPartResultReporter only catches failures from
  949. // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD.
  950. TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
  951.   printf("(expecting 2 failures)\n");
  952.   TestPartResultArray results;
  953.   {
  954.     ScopedFakeTestPartResultReporter reporter(
  955.         ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
  956.         &results);
  957.     AddFailureInOtherThread(FATAL_FAILURE);
  958.     AddFailureInOtherThread(NONFATAL_FAILURE);
  959.   }
  960.   // The two failures should not have been intercepted.
  961.   EXPECT_EQ(0, results.size()) << "This shouldn't fail.";
  962. }
  963.  
  964. #endif  // GTEST_IS_THREADSAFE
  965.  
  966. TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) {
  967.   // Expected fatal failure, but succeeds.
  968.   printf("(expecting 1 failure)\n");
  969.   EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure.");
  970.   // Expected fatal failure, but got a non-fatal failure.
  971.   printf("(expecting 1 failure)\n");
  972.   EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
  973.                                       "Expected non-fatal failure.");
  974.   // Wrong message.
  975.   printf("(expecting 1 failure)\n");
  976.   EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
  977.                                       "Some other fatal failure expected.");
  978. }
  979.  
  980. TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) {
  981.   // Expected non-fatal failure, but succeeds.
  982.   printf("(expecting 1 failure)\n");
  983.   EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal "
  984.                                          "failure.");
  985.   // Expected non-fatal failure, but got a fatal failure.
  986.   printf("(expecting 1 failure)\n");
  987.   EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
  988.                                          "Expected fatal failure.");
  989.   // Wrong message.
  990.   printf("(expecting 1 failure)\n");
  991.   EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
  992.                                          "Some other non-fatal failure.");
  993. }
  994.  
  995.  
  996. // Two test environments for testing testing::AddGlobalTestEnvironment().
  997.  
  998. class FooEnvironment : public testing::Environment {
  999.  public:
  1000.   virtual void SetUp() {
  1001.     printf("%s", "FooEnvironment::SetUp() called.\n");
  1002.   }
  1003.  
  1004.   virtual void TearDown() {
  1005.     printf("%s", "FooEnvironment::TearDown() called.\n");
  1006.     FAIL() << "Expected fatal failure.";
  1007.   }
  1008. };
  1009.  
  1010. class BarEnvironment : public testing::Environment {
  1011.  public:
  1012.   virtual void SetUp() {
  1013.     printf("%s", "BarEnvironment::SetUp() called.\n");
  1014.   }
  1015.  
  1016.   virtual void TearDown() {
  1017.     printf("%s", "BarEnvironment::TearDown() called.\n");
  1018.     ADD_FAILURE() << "Expected non-fatal failure.";
  1019.   }
  1020. };
  1021.  
  1022. // The main function.
  1023. //
  1024. // The idea is to use Google Test to run all the tests we have defined (some
  1025. // of them are intended to fail), and then compare the test results
  1026. // with the "golden" file.
  1027. int main(int argc, char **argv) {
  1028.   testing::GTEST_FLAG(print_time) = false;
  1029.  
  1030.   // We just run the tests, knowing some of them are intended to fail.
  1031.   // We will use a separate Python script to compare the output of
  1032.   // this program with the golden file.
  1033.  
  1034.   // It's hard to test InitGoogleTest() directly, as it has many
  1035.   // global side effects.  The following line serves as a sanity test
  1036.   // for it.
  1037.   testing::InitGoogleTest(&argc, argv);
  1038.   bool internal_skip_environment_and_ad_hoc_tests =
  1039.       std::count(argv, argv + argc,
  1040.                  std::string("internal_skip_environment_and_ad_hoc_tests")) > 0;
  1041.  
  1042. #if GTEST_HAS_DEATH_TEST
  1043.   if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") {
  1044.     // Skip the usual output capturing if we're running as the child
  1045.     // process of an threadsafe-style death test.
  1046. # if GTEST_OS_WINDOWS
  1047.     posix::FReopen("nul:", "w", stdout);
  1048. # else
  1049.     posix::FReopen("/dev/null", "w", stdout);
  1050. # endif  // GTEST_OS_WINDOWS
  1051.     return RUN_ALL_TESTS();
  1052.   }
  1053. #endif  // GTEST_HAS_DEATH_TEST
  1054.  
  1055.   if (internal_skip_environment_and_ad_hoc_tests)
  1056.     return RUN_ALL_TESTS();
  1057.  
  1058.   // Registers two global test environments.
  1059.   // The golden file verifies that they are set up in the order they
  1060.   // are registered, and torn down in the reverse order.
  1061.   testing::AddGlobalTestEnvironment(new FooEnvironment);
  1062.   testing::AddGlobalTestEnvironment(new BarEnvironment);
  1063.  
  1064.   return RunAllTests();
  1065. }
  1066.