gtest_output_test_.cc 32 KB

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