/thirdparty/breakpad/third_party/protobuf/protobuf/gtest/test/gtest_output_test_.cc

http://github.com/tomahawk-player/tomahawk · C++ · 1135 lines · 689 code · 211 blank · 235 comment · 12 complexity · 7eac4ad66974645f32451ddca97a96c2 MD5 · raw file

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