You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1034 lines
32 KiB

  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. // gtest_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. // Author: wan@google.com (Zhanyong Wan)
  37. #include "gtest/gtest-spi.h"
  38. #include "gtest/gtest.h"
  39. // Indicates that this translation unit is part of Google Test's
  40. // implementation. It must come before gtest-internal-inl.h is
  41. // included, or there will be a compiler error. This trick is to
  42. // prevent a user from accidentally including gtest-internal-inl.h in
  43. // his code.
  44. #define GTEST_IMPLEMENTATION_ 1
  45. #include "src/gtest-internal-inl.h"
  46. #undef GTEST_IMPLEMENTATION_
  47. #include <stdlib.h>
  48. #if GTEST_IS_THREADSAFE
  49. using testing::ScopedFakeTestPartResultReporter;
  50. using testing::TestPartResultArray;
  51. using testing::internal::Notification;
  52. using testing::internal::ThreadWithParam;
  53. #endif
  54. namespace posix = ::testing::internal::posix;
  55. using testing::internal::scoped_ptr;
  56. // Tests catching fatal failures.
  57. // A subroutine used by the following test.
  58. void TestEq1(int x) {
  59. ASSERT_EQ(1, x);
  60. }
  61. // This function calls a test subroutine, catches the fatal failure it
  62. // generates, and then returns early.
  63. void TryTestSubroutine() {
  64. // Calls a subrountine that yields a fatal failure.
  65. TestEq1(2);
  66. // Catches the fatal failure and aborts the test.
  67. //
  68. // The testing::Test:: prefix is necessary when calling
  69. // HasFatalFailure() outside of a TEST, TEST_F, or test fixture.
  70. if (testing::Test::HasFatalFailure()) return;
  71. // If we get here, something is wrong.
  72. FAIL() << "This should never be reached.";
  73. }
  74. TEST(PassingTest, PassingTest1) {
  75. }
  76. TEST(PassingTest, PassingTest2) {
  77. }
  78. // Tests that parameters of failing parameterized tests are printed in the
  79. // failing test summary.
  80. class FailingParamTest : public testing::TestWithParam<int> {};
  81. TEST_P(FailingParamTest, Fails) {
  82. EXPECT_EQ(1, GetParam());
  83. }
  84. // This generates a test which will fail. Google Test is expected to print
  85. // its parameter when it outputs the list of all failed tests.
  86. INSTANTIATE_TEST_CASE_P(PrintingFailingParams,
  87. FailingParamTest,
  88. testing::Values(2));
  89. static const char kGoldenString[] = "\"Line\0 1\"\nLine 2";
  90. TEST(NonfatalFailureTest, EscapesStringOperands) {
  91. std::string actual = "actual \"string\"";
  92. EXPECT_EQ(kGoldenString, actual);
  93. const char* golden = kGoldenString;
  94. EXPECT_EQ(golden, actual);
  95. }
  96. // Tests catching a fatal failure in a subroutine.
  97. TEST(FatalFailureTest, FatalFailureInSubroutine) {
  98. printf("(expecting a failure that x should be 1)\n");
  99. TryTestSubroutine();
  100. }
  101. // Tests catching a fatal failure in a nested subroutine.
  102. TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
  103. printf("(expecting a failure that x should be 1)\n");
  104. // Calls a subrountine that yields a fatal failure.
  105. TryTestSubroutine();
  106. // Catches the fatal failure and aborts the test.
  107. //
  108. // When calling HasFatalFailure() inside a TEST, TEST_F, or test
  109. // fixture, the testing::Test:: prefix is not needed.
  110. if (HasFatalFailure()) return;
  111. // If we get here, something is wrong.
  112. FAIL() << "This should never be reached.";
  113. }
  114. // Tests HasFatalFailure() after a failed EXPECT check.
  115. TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
  116. printf("(expecting a failure on false)\n");
  117. EXPECT_TRUE(false); // Generates a nonfatal failure
  118. ASSERT_FALSE(HasFatalFailure()); // This should succeed.
  119. }
  120. // Tests interleaving user logging and Google Test assertions.
  121. TEST(LoggingTest, InterleavingLoggingAndAssertions) {
  122. static const int a[4] = {
  123. 3, 9, 2, 6
  124. };
  125. printf("(expecting 2 failures on (3) >= (a[i]))\n");
  126. for (int i = 0; i < static_cast<int>(sizeof(a)/sizeof(*a)); i++) {
  127. printf("i == %d\n", i);
  128. EXPECT_GE(3, a[i]);
  129. }
  130. }
  131. // Tests the SCOPED_TRACE macro.
  132. // A helper function for testing SCOPED_TRACE.
  133. void SubWithoutTrace(int n) {
  134. EXPECT_EQ(1, n);
  135. ASSERT_EQ(2, n);
  136. }
  137. // Another helper function for testing SCOPED_TRACE.
  138. void SubWithTrace(int n) {
  139. SCOPED_TRACE(testing::Message() << "n = " << n);
  140. SubWithoutTrace(n);
  141. }
  142. // Tests that SCOPED_TRACE() obeys lexical scopes.
  143. TEST(SCOPED_TRACETest, ObeysScopes) {
  144. printf("(expected to fail)\n");
  145. // There should be no trace before SCOPED_TRACE() is invoked.
  146. ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
  147. {
  148. SCOPED_TRACE("Expected trace");
  149. // After SCOPED_TRACE(), a failure in the current scope should contain
  150. // the trace.
  151. ADD_FAILURE() << "This failure is expected, and should have a trace.";
  152. }
  153. // Once the control leaves the scope of the SCOPED_TRACE(), there
  154. // should be no trace again.
  155. ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
  156. }
  157. // Tests that SCOPED_TRACE works inside a loop.
  158. TEST(SCOPED_TRACETest, WorksInLoop) {
  159. printf("(expected to fail)\n");
  160. for (int i = 1; i <= 2; i++) {
  161. SCOPED_TRACE(testing::Message() << "i = " << i);
  162. SubWithoutTrace(i);
  163. }
  164. }
  165. // Tests that SCOPED_TRACE works in a subroutine.
  166. TEST(SCOPED_TRACETest, WorksInSubroutine) {
  167. printf("(expected to fail)\n");
  168. SubWithTrace(1);
  169. SubWithTrace(2);
  170. }
  171. // Tests that SCOPED_TRACE can be nested.
  172. TEST(SCOPED_TRACETest, CanBeNested) {
  173. printf("(expected to fail)\n");
  174. SCOPED_TRACE(""); // A trace without a message.
  175. SubWithTrace(2);
  176. }
  177. // Tests that multiple SCOPED_TRACEs can be used in the same scope.
  178. TEST(SCOPED_TRACETest, CanBeRepeated) {
  179. printf("(expected to fail)\n");
  180. SCOPED_TRACE("A");
  181. ADD_FAILURE()
  182. << "This failure is expected, and should contain trace point A.";
  183. SCOPED_TRACE("B");
  184. ADD_FAILURE()
  185. << "This failure is expected, and should contain trace point A and B.";
  186. {
  187. SCOPED_TRACE("C");
  188. ADD_FAILURE() << "This failure is expected, and should "
  189. << "contain trace point A, B, and C.";
  190. }
  191. SCOPED_TRACE("D");
  192. ADD_FAILURE() << "This failure is expected, and should "
  193. << "contain trace point A, B, and D.";
  194. }
  195. #if GTEST_IS_THREADSAFE
  196. // Tests that SCOPED_TRACE()s can be used concurrently from multiple
  197. // threads. Namely, an assertion should be affected by
  198. // SCOPED_TRACE()s in its own thread only.
  199. // Here's the sequence of actions that happen in the test:
  200. //
  201. // Thread A (main) | Thread B (spawned)
  202. // ===============================|================================
  203. // spawns thread B |
  204. // -------------------------------+--------------------------------
  205. // waits for n1 | SCOPED_TRACE("Trace B");
  206. // | generates failure #1
  207. // | notifies n1
  208. // -------------------------------+--------------------------------
  209. // SCOPED_TRACE("Trace A"); | waits for n2
  210. // generates failure #2 |
  211. // notifies n2 |
  212. // -------------------------------|--------------------------------
  213. // waits for n3 | generates failure #3
  214. // | trace B dies
  215. // | generates failure #4
  216. // | notifies n3
  217. // -------------------------------|--------------------------------
  218. // generates failure #5 | finishes
  219. // trace A dies |
  220. // generates failure #6 |
  221. // -------------------------------|--------------------------------
  222. // waits for thread B to finish |
  223. struct CheckPoints {
  224. Notification n1;
  225. Notification n2;
  226. Notification n3;
  227. };
  228. static void ThreadWithScopedTrace(CheckPoints* check_points) {
  229. {
  230. SCOPED_TRACE("Trace B");
  231. ADD_FAILURE()
  232. << "Expected failure #1 (in thread B, only trace B alive).";
  233. check_points->n1.Notify();
  234. check_points->n2.WaitForNotification();
  235. ADD_FAILURE()
  236. << "Expected failure #3 (in thread B, trace A & B both alive).";
  237. } // Trace B dies here.
  238. ADD_FAILURE()
  239. << "Expected failure #4 (in thread B, only trace A alive).";
  240. check_points->n3.Notify();
  241. }
  242. TEST(SCOPED_TRACETest, WorksConcurrently) {
  243. printf("(expecting 6 failures)\n");
  244. CheckPoints check_points;
  245. ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace,
  246. &check_points,
  247. NULL);
  248. check_points.n1.WaitForNotification();
  249. {
  250. SCOPED_TRACE("Trace A");
  251. ADD_FAILURE()
  252. << "Expected failure #2 (in thread A, trace A & B both alive).";
  253. check_points.n2.Notify();
  254. check_points.n3.WaitForNotification();
  255. ADD_FAILURE()
  256. << "Expected failure #5 (in thread A, only trace A alive).";
  257. } // Trace A dies here.
  258. ADD_FAILURE()
  259. << "Expected failure #6 (in thread A, no trace alive).";
  260. thread.Join();
  261. }
  262. #endif // GTEST_IS_THREADSAFE
  263. TEST(DisabledTestsWarningTest,
  264. DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
  265. // This test body is intentionally empty. Its sole purpose is for
  266. // verifying that the --gtest_also_run_disabled_tests flag
  267. // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of
  268. // the test output.
  269. }
  270. // Tests using assertions outside of TEST and TEST_F.
  271. //
  272. // This function creates two failures intentionally.
  273. void AdHocTest() {
  274. printf("The non-test part of the code is expected to have 2 failures.\n\n");
  275. EXPECT_TRUE(false);
  276. EXPECT_EQ(2, 3);
  277. }
  278. // Runs all TESTs, all TEST_Fs, and the ad hoc test.
  279. int RunAllTests() {
  280. AdHocTest();
  281. return RUN_ALL_TESTS();
  282. }
  283. // Tests non-fatal failures in the fixture constructor.
  284. class NonFatalFailureInFixtureConstructorTest : public testing::Test {
  285. protected:
  286. NonFatalFailureInFixtureConstructorTest() {
  287. printf("(expecting 5 failures)\n");
  288. ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor.";
  289. }
  290. ~NonFatalFailureInFixtureConstructorTest() {
  291. ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor.";
  292. }
  293. virtual void SetUp() {
  294. ADD_FAILURE() << "Expected failure #2, in SetUp().";
  295. }
  296. virtual void TearDown() {
  297. ADD_FAILURE() << "Expected failure #4, in TearDown.";
  298. }
  299. };
  300. TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) {
  301. ADD_FAILURE() << "Expected failure #3, in the test body.";
  302. }
  303. // Tests fatal failures in the fixture constructor.
  304. class FatalFailureInFixtureConstructorTest : public testing::Test {
  305. protected:
  306. FatalFailureInFixtureConstructorTest() {
  307. printf("(expecting 2 failures)\n");
  308. Init();
  309. }
  310. ~FatalFailureInFixtureConstructorTest() {
  311. ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor.";
  312. }
  313. virtual void SetUp() {
  314. ADD_FAILURE() << "UNEXPECTED failure in SetUp(). "
  315. << "We should never get here, as the test fixture c'tor "
  316. << "had a fatal failure.";
  317. }
  318. virtual void TearDown() {
  319. ADD_FAILURE() << "UNEXPECTED failure in TearDown(). "
  320. << "We should never get here, as the test fixture c'tor "
  321. << "had a fatal failure.";
  322. }
  323. private:
  324. void Init() {
  325. FAIL() << "Expected failure #1, in the test fixture c'tor.";
  326. }
  327. };
  328. TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) {
  329. ADD_FAILURE() << "UNEXPECTED failure in the test body. "
  330. << "We should never get here, as the test fixture c'tor "
  331. << "had a fatal failure.";
  332. }
  333. // Tests non-fatal failures in SetUp().
  334. class NonFatalFailureInSetUpTest : public testing::Test {
  335. protected:
  336. virtual ~NonFatalFailureInSetUpTest() {
  337. Deinit();
  338. }
  339. virtual void SetUp() {
  340. printf("(expecting 4 failures)\n");
  341. ADD_FAILURE() << "Expected failure #1, in SetUp().";
  342. }
  343. virtual void TearDown() {
  344. FAIL() << "Expected failure #3, in TearDown().";
  345. }
  346. private:
  347. void Deinit() {
  348. FAIL() << "Expected failure #4, in the test fixture d'tor.";
  349. }
  350. };
  351. TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) {
  352. FAIL() << "Expected failure #2, in the test function.";
  353. }
  354. // Tests fatal failures in SetUp().
  355. class FatalFailureInSetUpTest : public testing::Test {
  356. protected:
  357. virtual ~FatalFailureInSetUpTest() {
  358. Deinit();
  359. }
  360. virtual void SetUp() {
  361. printf("(expecting 3 failures)\n");
  362. FAIL() << "Expected failure #1, in SetUp().";
  363. }
  364. virtual void TearDown() {
  365. FAIL() << "Expected failure #2, in TearDown().";
  366. }
  367. private:
  368. void Deinit() {
  369. FAIL() << "Expected failure #3, in the test fixture d'tor.";
  370. }
  371. };
  372. TEST_F(FatalFailureInSetUpTest, FailureInSetUp) {
  373. FAIL() << "UNEXPECTED failure in the test function. "
  374. << "We should never get here, as SetUp() failed.";
  375. }
  376. TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
  377. ADD_FAILURE_AT("foo.cc", 42) << "Expected failure in foo.cc";
  378. }
  379. #if GTEST_IS_THREADSAFE
  380. // A unary function that may die.
  381. void DieIf(bool should_die) {
  382. GTEST_CHECK_(!should_die) << " - death inside DieIf().";
  383. }
  384. // Tests running death tests in a multi-threaded context.
  385. // Used for coordination between the main and the spawn thread.
  386. struct SpawnThreadNotifications {
  387. SpawnThreadNotifications() {}
  388. Notification spawn_thread_started;
  389. Notification spawn_thread_ok_to_terminate;
  390. private:
  391. GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications);
  392. };
  393. // The function to be executed in the thread spawn by the
  394. // MultipleThreads test (below).
  395. static void ThreadRoutine(SpawnThreadNotifications* notifications) {
  396. // Signals the main thread that this thread has started.
  397. notifications->spawn_thread_started.Notify();
  398. // Waits for permission to finish from the main thread.
  399. notifications->spawn_thread_ok_to_terminate.WaitForNotification();
  400. }
  401. // This is a death-test test, but it's not named with a DeathTest
  402. // suffix. It starts threads which might interfere with later
  403. // death tests, so it must run after all other death tests.
  404. class DeathTestAndMultiThreadsTest : public testing::Test {
  405. protected:
  406. // Starts a thread and waits for it to begin.
  407. virtual void SetUp() {
  408. thread_.reset(new ThreadWithParam<SpawnThreadNotifications*>(
  409. &ThreadRoutine, &notifications_, NULL));
  410. notifications_.spawn_thread_started.WaitForNotification();
  411. }
  412. // Tells the thread to finish, and reaps it.
  413. // Depending on the version of the thread library in use,
  414. // a manager thread might still be left running that will interfere
  415. // with later death tests. This is unfortunate, but this class
  416. // cleans up after itself as best it can.
  417. virtual void TearDown() {
  418. notifications_.spawn_thread_ok_to_terminate.Notify();
  419. }
  420. private:
  421. SpawnThreadNotifications notifications_;
  422. scoped_ptr<ThreadWithParam<SpawnThreadNotifications*> > thread_;
  423. };
  424. #endif // GTEST_IS_THREADSAFE
  425. // The MixedUpTestCaseTest test case verifies that Google Test will fail a
  426. // test if it uses a different fixture class than what other tests in
  427. // the same test case use. It deliberately contains two fixture
  428. // classes with the same name but defined in different namespaces.
  429. // The MixedUpTestCaseWithSameTestNameTest test case verifies that
  430. // when the user defines two tests with the same test case name AND
  431. // same test name (but in different namespaces), the second test will
  432. // fail.
  433. namespace foo {
  434. class MixedUpTestCaseTest : public testing::Test {
  435. };
  436. TEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {}
  437. TEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {}
  438. class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
  439. };
  440. TEST_F(MixedUpTestCaseWithSameTestNameTest,
  441. TheSecondTestWithThisNameShouldFail) {}
  442. } // namespace foo
  443. namespace bar {
  444. class MixedUpTestCaseTest : public testing::Test {
  445. };
  446. // The following two tests are expected to fail. We rely on the
  447. // golden file to check that Google Test generates the right error message.
  448. TEST_F(MixedUpTestCaseTest, ThisShouldFail) {}
  449. TEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {}
  450. class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
  451. };
  452. // Expected to fail. We rely on the golden file to check that Google Test
  453. // generates the right error message.
  454. TEST_F(MixedUpTestCaseWithSameTestNameTest,
  455. TheSecondTestWithThisNameShouldFail) {}
  456. } // namespace bar
  457. // The following two test cases verify that Google Test catches the user
  458. // error of mixing TEST and TEST_F in the same test case. The first
  459. // test case checks the scenario where TEST_F appears before TEST, and
  460. // the second one checks where TEST appears before TEST_F.
  461. class TEST_F_before_TEST_in_same_test_case : public testing::Test {
  462. };
  463. TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {}
  464. // Expected to fail. We rely on the golden file to check that Google Test
  465. // generates the right error message.
  466. TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {}
  467. class TEST_before_TEST_F_in_same_test_case : public testing::Test {
  468. };
  469. TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {}
  470. // Expected to fail. We rely on the golden file to check that Google Test
  471. // generates the right error message.
  472. TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {
  473. }
  474. // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE().
  475. int global_integer = 0;
  476. // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables.
  477. TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
  478. global_integer = 0;
  479. EXPECT_NONFATAL_FAILURE({
  480. EXPECT_EQ(1, global_integer) << "Expected non-fatal failure.";
  481. }, "Expected non-fatal failure.");
  482. }
  483. // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables
  484. // (static or not).
  485. TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
  486. int m = 0;
  487. static int n;
  488. n = 1;
  489. EXPECT_NONFATAL_FAILURE({
  490. EXPECT_EQ(m, n) << "Expected non-fatal failure.";
  491. }, "Expected non-fatal failure.");
  492. }
  493. // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly
  494. // one non-fatal failure and no fatal failure.
  495. TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
  496. EXPECT_NONFATAL_FAILURE({
  497. ADD_FAILURE() << "Expected non-fatal failure.";
  498. }, "Expected non-fatal failure.");
  499. }
  500. // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no
  501. // non-fatal failure.
  502. TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
  503. printf("(expecting a failure)\n");
  504. EXPECT_NONFATAL_FAILURE({
  505. }, "");
  506. }
  507. // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two
  508. // non-fatal failures.
  509. TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
  510. printf("(expecting a failure)\n");
  511. EXPECT_NONFATAL_FAILURE({
  512. ADD_FAILURE() << "Expected non-fatal failure 1.";
  513. ADD_FAILURE() << "Expected non-fatal failure 2.";
  514. }, "");
  515. }
  516. // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal
  517. // failure.
  518. TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
  519. printf("(expecting a failure)\n");
  520. EXPECT_NONFATAL_FAILURE({
  521. FAIL() << "Expected fatal failure.";
  522. }, "");
  523. }
  524. // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
  525. // tested returns.
  526. TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
  527. printf("(expecting a failure)\n");
  528. EXPECT_NONFATAL_FAILURE({
  529. return;
  530. }, "");
  531. }
  532. #if GTEST_HAS_EXCEPTIONS
  533. // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
  534. // tested throws.
  535. TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
  536. printf("(expecting a failure)\n");
  537. try {
  538. EXPECT_NONFATAL_FAILURE({
  539. throw 0;
  540. }, "");
  541. } catch(int) { // NOLINT
  542. }
  543. }
  544. #endif // GTEST_HAS_EXCEPTIONS
  545. // Tests that EXPECT_FATAL_FAILURE() can reference global variables.
  546. TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
  547. global_integer = 0;
  548. EXPECT_FATAL_FAILURE({
  549. ASSERT_EQ(1, global_integer) << "Expected fatal failure.";
  550. }, "Expected fatal failure.");
  551. }
  552. // Tests that EXPECT_FATAL_FAILURE() can reference local static
  553. // variables.
  554. TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
  555. static int n;
  556. n = 1;
  557. EXPECT_FATAL_FAILURE({
  558. ASSERT_EQ(0, n) << "Expected fatal failure.";
  559. }, "Expected fatal failure.");
  560. }
  561. // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly
  562. // one fatal failure and no non-fatal failure.
  563. TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
  564. EXPECT_FATAL_FAILURE({
  565. FAIL() << "Expected fatal failure.";
  566. }, "Expected fatal failure.");
  567. }
  568. // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal
  569. // failure.
  570. TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
  571. printf("(expecting a failure)\n");
  572. EXPECT_FATAL_FAILURE({
  573. }, "");
  574. }
  575. // A helper for generating a fatal failure.
  576. void FatalFailure() {
  577. FAIL() << "Expected fatal failure.";
  578. }
  579. // Tests that EXPECT_FATAL_FAILURE() fails when there are two
  580. // fatal failures.
  581. TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
  582. printf("(expecting a failure)\n");
  583. EXPECT_FATAL_FAILURE({
  584. FatalFailure();
  585. FatalFailure();
  586. }, "");
  587. }
  588. // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal
  589. // failure.
  590. TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
  591. printf("(expecting a failure)\n");
  592. EXPECT_FATAL_FAILURE({
  593. ADD_FAILURE() << "Expected non-fatal failure.";
  594. }, "");
  595. }
  596. // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
  597. // tested returns.
  598. TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
  599. printf("(expecting a failure)\n");
  600. EXPECT_FATAL_FAILURE({
  601. return;
  602. }, "");
  603. }
  604. #if GTEST_HAS_EXCEPTIONS
  605. // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
  606. // tested throws.
  607. TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
  608. printf("(expecting a failure)\n");
  609. try {
  610. EXPECT_FATAL_FAILURE({
  611. throw 0;
  612. }, "");
  613. } catch(int) { // NOLINT
  614. }
  615. }
  616. #endif // GTEST_HAS_EXCEPTIONS
  617. // This #ifdef block tests the output of typed tests.
  618. #if GTEST_HAS_TYPED_TEST
  619. template <typename T>
  620. class TypedTest : public testing::Test {
  621. };
  622. TYPED_TEST_CASE(TypedTest, testing::Types<int>);
  623. TYPED_TEST(TypedTest, Success) {
  624. EXPECT_EQ(0, TypeParam());
  625. }
  626. TYPED_TEST(TypedTest, Failure) {
  627. EXPECT_EQ(1, TypeParam()) << "Expected failure";
  628. }
  629. #endif // GTEST_HAS_TYPED_TEST
  630. // This #ifdef block tests the output of type-parameterized tests.
  631. #if GTEST_HAS_TYPED_TEST_P
  632. template <typename T>
  633. class TypedTestP : public testing::Test {
  634. };
  635. TYPED_TEST_CASE_P(TypedTestP);
  636. TYPED_TEST_P(TypedTestP, Success) {
  637. EXPECT_EQ(0U, TypeParam());
  638. }
  639. TYPED_TEST_P(TypedTestP, Failure) {
  640. EXPECT_EQ(1U, TypeParam()) << "Expected failure";
  641. }
  642. REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure);
  643. typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
  644. INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes);
  645. #endif // GTEST_HAS_TYPED_TEST_P
  646. #if GTEST_HAS_DEATH_TEST
  647. // We rely on the golden file to verify that tests whose test case
  648. // name ends with DeathTest are run first.
  649. TEST(ADeathTest, ShouldRunFirst) {
  650. }
  651. # if GTEST_HAS_TYPED_TEST
  652. // We rely on the golden file to verify that typed tests whose test
  653. // case name ends with DeathTest are run first.
  654. template <typename T>
  655. class ATypedDeathTest : public testing::Test {
  656. };
  657. typedef testing::Types<int, double> NumericTypes;
  658. TYPED_TEST_CASE(ATypedDeathTest, NumericTypes);
  659. TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {
  660. }
  661. # endif // GTEST_HAS_TYPED_TEST
  662. # if GTEST_HAS_TYPED_TEST_P
  663. // We rely on the golden file to verify that type-parameterized tests
  664. // whose test case name ends with DeathTest are run first.
  665. template <typename T>
  666. class ATypeParamDeathTest : public testing::Test {
  667. };
  668. TYPED_TEST_CASE_P(ATypeParamDeathTest);
  669. TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {
  670. }
  671. REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst);
  672. INSTANTIATE_TYPED_TEST_CASE_P(My, ATypeParamDeathTest, NumericTypes);
  673. # endif // GTEST_HAS_TYPED_TEST_P
  674. #endif // GTEST_HAS_DEATH_TEST
  675. // Tests various failure conditions of
  676. // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}.
  677. class ExpectFailureTest : public testing::Test {
  678. public: // Must be public and not protected due to a bug in g++ 3.4.2.
  679. enum FailureMode {
  680. FATAL_FAILURE,
  681. NONFATAL_FAILURE
  682. };
  683. static void AddFailure(FailureMode failure) {
  684. if (failure == FATAL_FAILURE) {
  685. FAIL() << "Expected fatal failure.";
  686. } else {
  687. ADD_FAILURE() << "Expected non-fatal failure.";
  688. }
  689. }
  690. };
  691. TEST_F(ExpectFailureTest, ExpectFatalFailure) {
  692. // Expected fatal failure, but succeeds.
  693. printf("(expecting 1 failure)\n");
  694. EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure.");
  695. // Expected fatal failure, but got a non-fatal failure.
  696. printf("(expecting 1 failure)\n");
  697. EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal "
  698. "failure.");
  699. // Wrong message.
  700. printf("(expecting 1 failure)\n");
  701. EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure "
  702. "expected.");
  703. }
  704. TEST_F(ExpectFailureTest, ExpectNonFatalFailure) {
  705. // Expected non-fatal failure, but succeeds.
  706. printf("(expecting 1 failure)\n");
  707. EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure.");
  708. // Expected non-fatal failure, but got a fatal failure.
  709. printf("(expecting 1 failure)\n");
  710. EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure.");
  711. // Wrong message.
  712. printf("(expecting 1 failure)\n");
  713. EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal "
  714. "failure.");
  715. }
  716. #if GTEST_IS_THREADSAFE
  717. class ExpectFailureWithThreadsTest : public ExpectFailureTest {
  718. protected:
  719. static void AddFailureInOtherThread(FailureMode failure) {
  720. ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL);
  721. thread.Join();
  722. }
  723. };
  724. TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
  725. // We only intercept the current thread.
  726. printf("(expecting 2 failures)\n");
  727. EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE),
  728. "Expected fatal failure.");
  729. }
  730. TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
  731. // We only intercept the current thread.
  732. printf("(expecting 2 failures)\n");
  733. EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE),
  734. "Expected non-fatal failure.");
  735. }
  736. typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
  737. // Tests that the ScopedFakeTestPartResultReporter only catches failures from
  738. // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD.
  739. TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
  740. printf("(expecting 2 failures)\n");
  741. TestPartResultArray results;
  742. {
  743. ScopedFakeTestPartResultReporter reporter(
  744. ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
  745. &results);
  746. AddFailureInOtherThread(FATAL_FAILURE);
  747. AddFailureInOtherThread(NONFATAL_FAILURE);
  748. }
  749. // The two failures should not have been intercepted.
  750. EXPECT_EQ(0, results.size()) << "This shouldn't fail.";
  751. }
  752. #endif // GTEST_IS_THREADSAFE
  753. TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) {
  754. // Expected fatal failure, but succeeds.
  755. printf("(expecting 1 failure)\n");
  756. EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure.");
  757. // Expected fatal failure, but got a non-fatal failure.
  758. printf("(expecting 1 failure)\n");
  759. EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
  760. "Expected non-fatal failure.");
  761. // Wrong message.
  762. printf("(expecting 1 failure)\n");
  763. EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
  764. "Some other fatal failure expected.");
  765. }
  766. TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) {
  767. // Expected non-fatal failure, but succeeds.
  768. printf("(expecting 1 failure)\n");
  769. EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal "
  770. "failure.");
  771. // Expected non-fatal failure, but got a fatal failure.
  772. printf("(expecting 1 failure)\n");
  773. EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
  774. "Expected fatal failure.");
  775. // Wrong message.
  776. printf("(expecting 1 failure)\n");
  777. EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
  778. "Some other non-fatal failure.");
  779. }
  780. // Two test environments for testing testing::AddGlobalTestEnvironment().
  781. class FooEnvironment : public testing::Environment {
  782. public:
  783. virtual void SetUp() {
  784. printf("%s", "FooEnvironment::SetUp() called.\n");
  785. }
  786. virtual void TearDown() {
  787. printf("%s", "FooEnvironment::TearDown() called.\n");
  788. FAIL() << "Expected fatal failure.";
  789. }
  790. };
  791. class BarEnvironment : public testing::Environment {
  792. public:
  793. virtual void SetUp() {
  794. printf("%s", "BarEnvironment::SetUp() called.\n");
  795. }
  796. virtual void TearDown() {
  797. printf("%s", "BarEnvironment::TearDown() called.\n");
  798. ADD_FAILURE() << "Expected non-fatal failure.";
  799. }
  800. };
  801. bool GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = false;
  802. // The main function.
  803. //
  804. // The idea is to use Google Test to run all the tests we have defined (some
  805. // of them are intended to fail), and then compare the test results
  806. // with the "golden" file.
  807. int main(int argc, char **argv) {
  808. testing::GTEST_FLAG(print_time) = false;
  809. // We just run the tests, knowing some of them are intended to fail.
  810. // We will use a separate Python script to compare the output of
  811. // this program with the golden file.
  812. // It's hard to test InitGoogleTest() directly, as it has many
  813. // global side effects. The following line serves as a sanity test
  814. // for it.
  815. testing::InitGoogleTest(&argc, argv);
  816. if (argc >= 2 &&
  817. (std::string(argv[1]) ==
  818. "--gtest_internal_skip_environment_and_ad_hoc_tests"))
  819. GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = true;
  820. #if GTEST_HAS_DEATH_TEST
  821. if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") {
  822. // Skip the usual output capturing if we're running as the child
  823. // process of an threadsafe-style death test.
  824. # if GTEST_OS_WINDOWS
  825. posix::FReopen("nul:", "w", stdout);
  826. # else
  827. posix::FReopen("/dev/null", "w", stdout);
  828. # endif // GTEST_OS_WINDOWS
  829. return RUN_ALL_TESTS();
  830. }
  831. #endif // GTEST_HAS_DEATH_TEST
  832. if (GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests))
  833. return RUN_ALL_TESTS();
  834. // Registers two global test environments.
  835. // The golden file verifies that they are set up in the order they
  836. // are registered, and torn down in the reverse order.
  837. testing::AddGlobalTestEnvironment(new FooEnvironment);
  838. testing::AddGlobalTestEnvironment(new BarEnvironment);
  839. return RunAllTests();
  840. }