googletest-output-test_.cc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. // Copyright 2005, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // The purpose of this file is to generate Google Test output under
  31. // various conditions. The output will then be verified by
  32. // googletest-output-test.py to ensure that Google Test generates the
  33. // desired messages. Therefore, most tests in this file are MEANT TO
  34. // FAIL.
  35. #include <stdlib.h>
  36. #include "gtest/gtest-spi.h"
  37. #include "gtest/gtest.h"
  38. #include "src/gtest-internal-inl.h"
  39. #if _MSC_VER
  40. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */)
  41. #endif // _MSC_VER
  42. #if GTEST_IS_THREADSAFE
  43. using testing::ScopedFakeTestPartResultReporter;
  44. using testing::TestPartResultArray;
  45. using testing::internal::Notification;
  46. using testing::internal::ThreadWithParam;
  47. #endif
  48. namespace posix = ::testing::internal::posix;
  49. // Tests catching fatal failures.
  50. // A subroutine used by the following test.
  51. void TestEq1(int x) { ASSERT_EQ(1, x); }
  52. // This function calls a test subroutine, catches the fatal failure it
  53. // generates, and then returns early.
  54. void TryTestSubroutine() {
  55. // Calls a subrountine that yields a fatal failure.
  56. TestEq1(2);
  57. // Catches the fatal failure and aborts the test.
  58. //
  59. // The testing::Test:: prefix is necessary when calling
  60. // HasFatalFailure() outside of a TEST, TEST_F, or test fixture.
  61. if (testing::Test::HasFatalFailure()) return;
  62. // If we get here, something is wrong.
  63. FAIL() << "This should never be reached.";
  64. }
  65. TEST(PassingTest, PassingTest1) {}
  66. TEST(PassingTest, PassingTest2) {}
  67. // Tests that parameters of failing parameterized tests are printed in the
  68. // failing test summary.
  69. class FailingParamTest : public testing::TestWithParam<int> {};
  70. TEST_P(FailingParamTest, Fails) { EXPECT_EQ(1, GetParam()); }
  71. // This generates a test which will fail. Google Test is expected to print
  72. // its parameter when it outputs the list of all failed tests.
  73. INSTANTIATE_TEST_SUITE_P(PrintingFailingParams, FailingParamTest,
  74. testing::Values(2));
  75. // Tests that an empty value for the test suite basename yields just
  76. // the test name without any prior /
  77. class EmptyBasenameParamInst : public testing::TestWithParam<int> {};
  78. TEST_P(EmptyBasenameParamInst, Passes) { EXPECT_EQ(1, GetParam()); }
  79. INSTANTIATE_TEST_SUITE_P(, EmptyBasenameParamInst, testing::Values(1));
  80. static const char kGoldenString[] = "\"Line\0 1\"\nLine 2";
  81. TEST(NonfatalFailureTest, EscapesStringOperands) {
  82. std::string actual = "actual \"string\"";
  83. EXPECT_EQ(kGoldenString, actual);
  84. const char* golden = kGoldenString;
  85. EXPECT_EQ(golden, actual);
  86. }
  87. TEST(NonfatalFailureTest, DiffForLongStrings) {
  88. std::string golden_str(kGoldenString, sizeof(kGoldenString) - 1);
  89. EXPECT_EQ(golden_str, "Line 2");
  90. }
  91. // Tests catching a fatal failure in a subroutine.
  92. TEST(FatalFailureTest, FatalFailureInSubroutine) {
  93. printf("(expecting a failure that x should be 1)\n");
  94. TryTestSubroutine();
  95. }
  96. // Tests catching a fatal failure in a nested subroutine.
  97. TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
  98. printf("(expecting a failure that x should be 1)\n");
  99. // Calls a subrountine that yields a fatal failure.
  100. TryTestSubroutine();
  101. // Catches the fatal failure and aborts the test.
  102. //
  103. // When calling HasFatalFailure() inside a TEST, TEST_F, or test
  104. // fixture, the testing::Test:: prefix is not needed.
  105. if (HasFatalFailure()) return;
  106. // If we get here, something is wrong.
  107. FAIL() << "This should never be reached.";
  108. }
  109. // Tests HasFatalFailure() after a failed EXPECT check.
  110. TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
  111. printf("(expecting a failure on false)\n");
  112. EXPECT_TRUE(false); // Generates a nonfatal failure
  113. ASSERT_FALSE(HasFatalFailure()); // This should succeed.
  114. }
  115. // Tests interleaving user logging and Google Test assertions.
  116. TEST(LoggingTest, InterleavingLoggingAndAssertions) {
  117. static const int a[4] = {3, 9, 2, 6};
  118. printf("(expecting 2 failures on (3) >= (a[i]))\n");
  119. for (int i = 0; i < static_cast<int>(sizeof(a) / sizeof(*a)); i++) {
  120. printf("i == %d\n", i);
  121. EXPECT_GE(3, a[i]);
  122. }
  123. }
  124. // Tests the SCOPED_TRACE macro.
  125. // A helper function for testing SCOPED_TRACE.
  126. void SubWithoutTrace(int n) {
  127. EXPECT_EQ(1, n);
  128. ASSERT_EQ(2, n);
  129. }
  130. // Another helper function for testing SCOPED_TRACE.
  131. void SubWithTrace(int n) {
  132. SCOPED_TRACE(testing::Message() << "n = " << n);
  133. SubWithoutTrace(n);
  134. }
  135. TEST(SCOPED_TRACETest, AcceptedValues) {
  136. SCOPED_TRACE("literal string");
  137. SCOPED_TRACE(std::string("std::string"));
  138. SCOPED_TRACE(1337); // streamable type
  139. const char* null_value = nullptr;
  140. SCOPED_TRACE(null_value);
  141. ADD_FAILURE() << "Just checking that all these values work fine.";
  142. }
  143. // Tests that SCOPED_TRACE() obeys lexical scopes.
  144. TEST(SCOPED_TRACETest, ObeysScopes) {
  145. printf("(expected to fail)\n");
  146. // There should be no trace before SCOPED_TRACE() is invoked.
  147. ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
  148. {
  149. SCOPED_TRACE("Expected trace");
  150. // After SCOPED_TRACE(), a failure in the current scope should contain
  151. // the trace.
  152. ADD_FAILURE() << "This failure is expected, and should have a trace.";
  153. }
  154. // Once the control leaves the scope of the SCOPED_TRACE(), there
  155. // should be no trace again.
  156. ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
  157. }
  158. // Tests that SCOPED_TRACE works inside a loop.
  159. TEST(SCOPED_TRACETest, WorksInLoop) {
  160. printf("(expected to fail)\n");
  161. for (int i = 1; i <= 2; i++) {
  162. SCOPED_TRACE(testing::Message() << "i = " << i);
  163. SubWithoutTrace(i);
  164. }
  165. }
  166. // Tests that SCOPED_TRACE works in a subroutine.
  167. TEST(SCOPED_TRACETest, WorksInSubroutine) {
  168. printf("(expected to fail)\n");
  169. SubWithTrace(1);
  170. SubWithTrace(2);
  171. }
  172. // Tests that SCOPED_TRACE can be nested.
  173. TEST(SCOPED_TRACETest, CanBeNested) {
  174. printf("(expected to fail)\n");
  175. SCOPED_TRACE(""); // A trace without a message.
  176. SubWithTrace(2);
  177. }
  178. // Tests that multiple SCOPED_TRACEs can be used in the same scope.
  179. TEST(SCOPED_TRACETest, CanBeRepeated) {
  180. printf("(expected to fail)\n");
  181. SCOPED_TRACE("A");
  182. ADD_FAILURE()
  183. << "This failure is expected, and should contain trace point A.";
  184. SCOPED_TRACE("B");
  185. ADD_FAILURE()
  186. << "This failure is expected, and should contain trace point A and B.";
  187. {
  188. SCOPED_TRACE("C");
  189. ADD_FAILURE() << "This failure is expected, and should "
  190. << "contain trace point A, B, and C.";
  191. }
  192. SCOPED_TRACE("D");
  193. ADD_FAILURE() << "This failure is expected, and should "
  194. << "contain trace point A, B, and D.";
  195. }
  196. #if GTEST_IS_THREADSAFE
  197. // Tests that SCOPED_TRACE()s can be used concurrently from multiple
  198. // threads. Namely, an assertion should be affected by
  199. // SCOPED_TRACE()s in its own thread only.
  200. // Here's the sequence of actions that happen in the test:
  201. //
  202. // Thread A (main) | Thread B (spawned)
  203. // ===============================|================================
  204. // spawns thread B |
  205. // -------------------------------+--------------------------------
  206. // waits for n1 | SCOPED_TRACE("Trace B");
  207. // | generates failure #1
  208. // | notifies n1
  209. // -------------------------------+--------------------------------
  210. // SCOPED_TRACE("Trace A"); | waits for n2
  211. // generates failure #2 |
  212. // notifies n2 |
  213. // -------------------------------|--------------------------------
  214. // waits for n3 | generates failure #3
  215. // | trace B dies
  216. // | generates failure #4
  217. // | notifies n3
  218. // -------------------------------|--------------------------------
  219. // generates failure #5 | finishes
  220. // trace A dies |
  221. // generates failure #6 |
  222. // -------------------------------|--------------------------------
  223. // waits for thread B to finish |
  224. struct CheckPoints {
  225. Notification n1;
  226. Notification n2;
  227. Notification n3;
  228. };
  229. static void ThreadWithScopedTrace(CheckPoints* check_points) {
  230. {
  231. SCOPED_TRACE("Trace B");
  232. ADD_FAILURE() << "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() << "Expected failure #4 (in thread B, only trace A alive).";
  239. check_points->n3.Notify();
  240. }
  241. TEST(SCOPED_TRACETest, WorksConcurrently) {
  242. printf("(expecting 6 failures)\n");
  243. CheckPoints check_points;
  244. ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace, &check_points,
  245. nullptr);
  246. check_points.n1.WaitForNotification();
  247. {
  248. SCOPED_TRACE("Trace A");
  249. ADD_FAILURE()
  250. << "Expected failure #2 (in thread A, trace A & B both alive).";
  251. check_points.n2.Notify();
  252. check_points.n3.WaitForNotification();
  253. ADD_FAILURE() << "Expected failure #5 (in thread A, only trace A alive).";
  254. } // Trace A dies here.
  255. ADD_FAILURE() << "Expected failure #6 (in thread A, no trace alive).";
  256. thread.Join();
  257. }
  258. #endif // GTEST_IS_THREADSAFE
  259. // Tests basic functionality of the ScopedTrace utility (most of its features
  260. // are already tested in SCOPED_TRACETest).
  261. TEST(ScopedTraceTest, WithExplicitFileAndLine) {
  262. testing::ScopedTrace trace("explicit_file.cc", 123, "expected trace message");
  263. ADD_FAILURE() << "Check that the trace is attached to a particular location.";
  264. }
  265. TEST(DisabledTestsWarningTest,
  266. DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
  267. // This test body is intentionally empty. Its sole purpose is for
  268. // verifying that the --gtest_also_run_disabled_tests flag
  269. // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of
  270. // the test output.
  271. }
  272. // Tests using assertions outside of TEST and TEST_F.
  273. //
  274. // This function creates two failures intentionally.
  275. void AdHocTest() {
  276. printf("The non-test part of the code is expected to have 2 failures.\n\n");
  277. EXPECT_TRUE(false);
  278. EXPECT_EQ(2, 3);
  279. }
  280. // Runs all TESTs, all TEST_Fs, and the ad hoc test.
  281. int RunAllTests() {
  282. AdHocTest();
  283. return RUN_ALL_TESTS();
  284. }
  285. // Tests non-fatal failures in the fixture constructor.
  286. class NonFatalFailureInFixtureConstructorTest : public testing::Test {
  287. protected:
  288. NonFatalFailureInFixtureConstructorTest() {
  289. printf("(expecting 5 failures)\n");
  290. ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor.";
  291. }
  292. ~NonFatalFailureInFixtureConstructorTest() override {
  293. ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor.";
  294. }
  295. void SetUp() override { ADD_FAILURE() << "Expected failure #2, in SetUp()."; }
  296. void TearDown() override {
  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() override {
  311. ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor.";
  312. }
  313. void SetUp() override {
  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. void TearDown() override {
  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() { FAIL() << "Expected failure #1, in the test fixture c'tor."; }
  325. };
  326. TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) {
  327. ADD_FAILURE() << "UNEXPECTED failure in the test body. "
  328. << "We should never get here, as the test fixture c'tor "
  329. << "had a fatal failure.";
  330. }
  331. // Tests non-fatal failures in SetUp().
  332. class NonFatalFailureInSetUpTest : public testing::Test {
  333. protected:
  334. ~NonFatalFailureInSetUpTest() override { Deinit(); }
  335. void SetUp() override {
  336. printf("(expecting 4 failures)\n");
  337. ADD_FAILURE() << "Expected failure #1, in SetUp().";
  338. }
  339. void TearDown() override { FAIL() << "Expected failure #3, in TearDown()."; }
  340. private:
  341. void Deinit() { FAIL() << "Expected failure #4, in the test fixture d'tor."; }
  342. };
  343. TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) {
  344. FAIL() << "Expected failure #2, in the test function.";
  345. }
  346. // Tests fatal failures in SetUp().
  347. class FatalFailureInSetUpTest : public testing::Test {
  348. protected:
  349. ~FatalFailureInSetUpTest() override { Deinit(); }
  350. void SetUp() override {
  351. printf("(expecting 3 failures)\n");
  352. FAIL() << "Expected failure #1, in SetUp().";
  353. }
  354. void TearDown() override { FAIL() << "Expected failure #2, in TearDown()."; }
  355. private:
  356. void Deinit() { FAIL() << "Expected failure #3, in the test fixture d'tor."; }
  357. };
  358. TEST_F(FatalFailureInSetUpTest, FailureInSetUp) {
  359. FAIL() << "UNEXPECTED failure in the test function. "
  360. << "We should never get here, as SetUp() failed.";
  361. }
  362. TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
  363. ADD_FAILURE_AT("foo.cc", 42) << "Expected nonfatal failure in foo.cc";
  364. }
  365. TEST(GtestFailAtTest, MessageContainsSpecifiedFileAndLineNumber) {
  366. GTEST_FAIL_AT("foo.cc", 42) << "Expected fatal failure in foo.cc";
  367. }
  368. // The MixedUpTestSuiteTest test case verifies that Google Test will fail a
  369. // test if it uses a different fixture class than what other tests in
  370. // the same test case use. It deliberately contains two fixture
  371. // classes with the same name but defined in different namespaces.
  372. // The MixedUpTestSuiteWithSameTestNameTest test case verifies that
  373. // when the user defines two tests with the same test case name AND
  374. // same test name (but in different namespaces), the second test will
  375. // fail.
  376. namespace foo {
  377. class MixedUpTestSuiteTest : public testing::Test {};
  378. TEST_F(MixedUpTestSuiteTest, FirstTestFromNamespaceFoo) {}
  379. TEST_F(MixedUpTestSuiteTest, SecondTestFromNamespaceFoo) {}
  380. class MixedUpTestSuiteWithSameTestNameTest : public testing::Test {};
  381. TEST_F(MixedUpTestSuiteWithSameTestNameTest,
  382. TheSecondTestWithThisNameShouldFail) {}
  383. } // namespace foo
  384. namespace bar {
  385. class MixedUpTestSuiteTest : public testing::Test {};
  386. // The following two tests are expected to fail. We rely on the
  387. // golden file to check that Google Test generates the right error message.
  388. TEST_F(MixedUpTestSuiteTest, ThisShouldFail) {}
  389. TEST_F(MixedUpTestSuiteTest, ThisShouldFailToo) {}
  390. class MixedUpTestSuiteWithSameTestNameTest : public testing::Test {};
  391. // Expected to fail. We rely on the golden file to check that Google Test
  392. // generates the right error message.
  393. TEST_F(MixedUpTestSuiteWithSameTestNameTest,
  394. TheSecondTestWithThisNameShouldFail) {}
  395. } // namespace bar
  396. // The following two test cases verify that Google Test catches the user
  397. // error of mixing TEST and TEST_F in the same test case. The first
  398. // test case checks the scenario where TEST_F appears before TEST, and
  399. // the second one checks where TEST appears before TEST_F.
  400. class TEST_F_before_TEST_in_same_test_case : public testing::Test {};
  401. TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {}
  402. // Expected to fail. We rely on the golden file to check that Google Test
  403. // generates the right error message.
  404. TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {}
  405. class TEST_before_TEST_F_in_same_test_case : public testing::Test {};
  406. TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {}
  407. // Expected to fail. We rely on the golden file to check that Google Test
  408. // generates the right error message.
  409. TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {}
  410. // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE().
  411. int global_integer = 0;
  412. // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables.
  413. TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
  414. global_integer = 0;
  415. EXPECT_NONFATAL_FAILURE(
  416. { EXPECT_EQ(1, global_integer) << "Expected non-fatal failure."; },
  417. "Expected non-fatal failure.");
  418. }
  419. // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables
  420. // (static or not).
  421. TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
  422. int m = 0;
  423. static int n;
  424. n = 1;
  425. EXPECT_NONFATAL_FAILURE({ EXPECT_EQ(m, n) << "Expected non-fatal failure."; },
  426. "Expected non-fatal failure.");
  427. }
  428. // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly
  429. // one non-fatal failure and no fatal failure.
  430. TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
  431. EXPECT_NONFATAL_FAILURE({ ADD_FAILURE() << "Expected non-fatal failure."; },
  432. "Expected non-fatal failure.");
  433. }
  434. // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no
  435. // non-fatal failure.
  436. TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
  437. printf("(expecting a failure)\n");
  438. EXPECT_NONFATAL_FAILURE({}, "");
  439. }
  440. // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two
  441. // non-fatal failures.
  442. TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
  443. printf("(expecting a failure)\n");
  444. EXPECT_NONFATAL_FAILURE(
  445. {
  446. ADD_FAILURE() << "Expected non-fatal failure 1.";
  447. ADD_FAILURE() << "Expected non-fatal failure 2.";
  448. },
  449. "");
  450. }
  451. // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal
  452. // failure.
  453. TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
  454. printf("(expecting a failure)\n");
  455. EXPECT_NONFATAL_FAILURE({ FAIL() << "Expected fatal failure."; }, "");
  456. }
  457. // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
  458. // tested returns.
  459. TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
  460. printf("(expecting a failure)\n");
  461. EXPECT_NONFATAL_FAILURE({ return; }, "");
  462. }
  463. #if GTEST_HAS_EXCEPTIONS
  464. // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
  465. // tested throws.
  466. TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
  467. printf("(expecting a failure)\n");
  468. try {
  469. EXPECT_NONFATAL_FAILURE({ throw 0; }, "");
  470. } catch (int) { // NOLINT
  471. }
  472. }
  473. #endif // GTEST_HAS_EXCEPTIONS
  474. // Tests that EXPECT_FATAL_FAILURE() can reference global variables.
  475. TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
  476. global_integer = 0;
  477. EXPECT_FATAL_FAILURE(
  478. { ASSERT_EQ(1, global_integer) << "Expected fatal failure."; },
  479. "Expected fatal failure.");
  480. }
  481. // Tests that EXPECT_FATAL_FAILURE() can reference local static
  482. // variables.
  483. TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
  484. static int n;
  485. n = 1;
  486. EXPECT_FATAL_FAILURE({ ASSERT_EQ(0, n) << "Expected fatal failure."; },
  487. "Expected fatal failure.");
  488. }
  489. // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly
  490. // one fatal failure and no non-fatal failure.
  491. TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
  492. EXPECT_FATAL_FAILURE({ FAIL() << "Expected fatal failure."; },
  493. "Expected fatal failure.");
  494. }
  495. // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal
  496. // failure.
  497. TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
  498. printf("(expecting a failure)\n");
  499. EXPECT_FATAL_FAILURE({}, "");
  500. }
  501. // A helper for generating a fatal failure.
  502. void FatalFailure() { FAIL() << "Expected fatal failure."; }
  503. // Tests that EXPECT_FATAL_FAILURE() fails when there are two
  504. // fatal failures.
  505. TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
  506. printf("(expecting a failure)\n");
  507. EXPECT_FATAL_FAILURE(
  508. {
  509. FatalFailure();
  510. FatalFailure();
  511. },
  512. "");
  513. }
  514. // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal
  515. // failure.
  516. TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
  517. printf("(expecting a failure)\n");
  518. EXPECT_FATAL_FAILURE({ ADD_FAILURE() << "Expected non-fatal failure."; }, "");
  519. }
  520. // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
  521. // tested returns.
  522. TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
  523. printf("(expecting a failure)\n");
  524. EXPECT_FATAL_FAILURE({ return; }, "");
  525. }
  526. #if GTEST_HAS_EXCEPTIONS
  527. // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
  528. // tested throws.
  529. TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
  530. printf("(expecting a failure)\n");
  531. try {
  532. EXPECT_FATAL_FAILURE({ throw 0; }, "");
  533. } catch (int) { // NOLINT
  534. }
  535. }
  536. #endif // GTEST_HAS_EXCEPTIONS
  537. // This #ifdef block tests the output of value-parameterized tests.
  538. std::string ParamNameFunc(const testing::TestParamInfo<std::string>& info) {
  539. return info.param;
  540. }
  541. class ParamTest : public testing::TestWithParam<std::string> {};
  542. TEST_P(ParamTest, Success) { EXPECT_EQ("a", GetParam()); }
  543. TEST_P(ParamTest, Failure) { EXPECT_EQ("b", GetParam()) << "Expected failure"; }
  544. INSTANTIATE_TEST_SUITE_P(PrintingStrings, ParamTest,
  545. testing::Values(std::string("a")), ParamNameFunc);
  546. // The case where a suite has INSTANTIATE_TEST_SUITE_P but not TEST_P.
  547. using NoTests = ParamTest;
  548. INSTANTIATE_TEST_SUITE_P(ThisIsOdd, NoTests, ::testing::Values("Hello"));
  549. // fails under kErrorOnUninstantiatedParameterizedTest=true
  550. class DetectNotInstantiatedTest : public testing::TestWithParam<int> {};
  551. TEST_P(DetectNotInstantiatedTest, Used) {}
  552. // This would make the test failure from the above go away.
  553. // INSTANTIATE_TEST_SUITE_P(Fix, DetectNotInstantiatedTest, testing::Values(1));
  554. template <typename T>
  555. class TypedTest : public testing::Test {};
  556. TYPED_TEST_SUITE(TypedTest, testing::Types<int>);
  557. TYPED_TEST(TypedTest, Success) { EXPECT_EQ(0, TypeParam()); }
  558. TYPED_TEST(TypedTest, Failure) {
  559. EXPECT_EQ(1, TypeParam()) << "Expected failure";
  560. }
  561. typedef testing::Types<char, int> TypesForTestWithNames;
  562. template <typename T>
  563. class TypedTestWithNames : public testing::Test {};
  564. class TypedTestNames {
  565. public:
  566. template <typename T>
  567. static std::string GetName(int i) {
  568. if (std::is_same<T, char>::value)
  569. return std::string("char") + ::testing::PrintToString(i);
  570. if (std::is_same<T, int>::value)
  571. return std::string("int") + ::testing::PrintToString(i);
  572. }
  573. };
  574. TYPED_TEST_SUITE(TypedTestWithNames, TypesForTestWithNames, TypedTestNames);
  575. TYPED_TEST(TypedTestWithNames, Success) {}
  576. TYPED_TEST(TypedTestWithNames, Failure) { FAIL(); }
  577. template <typename T>
  578. class TypedTestP : public testing::Test {};
  579. TYPED_TEST_SUITE_P(TypedTestP);
  580. TYPED_TEST_P(TypedTestP, Success) { EXPECT_EQ(0U, TypeParam()); }
  581. TYPED_TEST_P(TypedTestP, Failure) {
  582. EXPECT_EQ(1U, TypeParam()) << "Expected failure";
  583. }
  584. REGISTER_TYPED_TEST_SUITE_P(TypedTestP, Success, Failure);
  585. typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
  586. INSTANTIATE_TYPED_TEST_SUITE_P(Unsigned, TypedTestP, UnsignedTypes);
  587. class TypedTestPNames {
  588. public:
  589. template <typename T>
  590. static std::string GetName(int i) {
  591. if (std::is_same<T, unsigned char>::value) {
  592. return std::string("unsignedChar") + ::testing::PrintToString(i);
  593. }
  594. if (std::is_same<T, unsigned int>::value) {
  595. return std::string("unsignedInt") + ::testing::PrintToString(i);
  596. }
  597. }
  598. };
  599. INSTANTIATE_TYPED_TEST_SUITE_P(UnsignedCustomName, TypedTestP, UnsignedTypes,
  600. TypedTestPNames);
  601. template <typename T>
  602. class DetectNotInstantiatedTypesTest : public testing::Test {};
  603. TYPED_TEST_SUITE_P(DetectNotInstantiatedTypesTest);
  604. TYPED_TEST_P(DetectNotInstantiatedTypesTest, Used) {
  605. TypeParam instantiate;
  606. (void)instantiate;
  607. }
  608. REGISTER_TYPED_TEST_SUITE_P(DetectNotInstantiatedTypesTest, Used);
  609. // kErrorOnUninstantiatedTypeParameterizedTest=true would make the above fail.
  610. // Adding the following would make that test failure go away.
  611. //
  612. // typedef ::testing::Types<char, int, unsigned int> MyTypes;
  613. // INSTANTIATE_TYPED_TEST_SUITE_P(All, DetectNotInstantiatedTypesTest, MyTypes);
  614. #if GTEST_HAS_DEATH_TEST
  615. // We rely on the golden file to verify that tests whose test case
  616. // name ends with DeathTest are run first.
  617. TEST(ADeathTest, ShouldRunFirst) {}
  618. // We rely on the golden file to verify that typed tests whose test
  619. // case name ends with DeathTest are run first.
  620. template <typename T>
  621. class ATypedDeathTest : public testing::Test {};
  622. typedef testing::Types<int, double> NumericTypes;
  623. TYPED_TEST_SUITE(ATypedDeathTest, NumericTypes);
  624. TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {}
  625. // We rely on the golden file to verify that type-parameterized tests
  626. // whose test case name ends with DeathTest are run first.
  627. template <typename T>
  628. class ATypeParamDeathTest : public testing::Test {};
  629. TYPED_TEST_SUITE_P(ATypeParamDeathTest);
  630. TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {}
  631. REGISTER_TYPED_TEST_SUITE_P(ATypeParamDeathTest, ShouldRunFirst);
  632. INSTANTIATE_TYPED_TEST_SUITE_P(My, ATypeParamDeathTest, NumericTypes);
  633. #endif // GTEST_HAS_DEATH_TEST
  634. // Tests various failure conditions of
  635. // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}.
  636. class ExpectFailureTest : public testing::Test {
  637. public: // Must be public and not protected due to a bug in g++ 3.4.2.
  638. enum FailureMode { FATAL_FAILURE, NONFATAL_FAILURE };
  639. static void AddFailure(FailureMode failure) {
  640. if (failure == FATAL_FAILURE) {
  641. FAIL() << "Expected fatal failure.";
  642. } else {
  643. ADD_FAILURE() << "Expected non-fatal failure.";
  644. }
  645. }
  646. };
  647. TEST_F(ExpectFailureTest, ExpectFatalFailure) {
  648. // Expected fatal failure, but succeeds.
  649. printf("(expecting 1 failure)\n");
  650. EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure.");
  651. // Expected fatal failure, but got a non-fatal failure.
  652. printf("(expecting 1 failure)\n");
  653. EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE),
  654. "Expected non-fatal "
  655. "failure.");
  656. // Wrong message.
  657. printf("(expecting 1 failure)\n");
  658. EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE),
  659. "Some other fatal failure "
  660. "expected.");
  661. }
  662. TEST_F(ExpectFailureTest, ExpectNonFatalFailure) {
  663. // Expected non-fatal failure, but succeeds.
  664. printf("(expecting 1 failure)\n");
  665. EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure.");
  666. // Expected non-fatal failure, but got a fatal failure.
  667. printf("(expecting 1 failure)\n");
  668. EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure.");
  669. // Wrong message.
  670. printf("(expecting 1 failure)\n");
  671. EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE),
  672. "Some other non-fatal "
  673. "failure.");
  674. }
  675. #if GTEST_IS_THREADSAFE
  676. class ExpectFailureWithThreadsTest : public ExpectFailureTest {
  677. protected:
  678. static void AddFailureInOtherThread(FailureMode failure) {
  679. ThreadWithParam<FailureMode> thread(&AddFailure, failure, nullptr);
  680. thread.Join();
  681. }
  682. };
  683. TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
  684. // We only intercept the current thread.
  685. printf("(expecting 2 failures)\n");
  686. EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE),
  687. "Expected fatal failure.");
  688. }
  689. TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
  690. // We only intercept the current thread.
  691. printf("(expecting 2 failures)\n");
  692. EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE),
  693. "Expected non-fatal failure.");
  694. }
  695. typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
  696. // Tests that the ScopedFakeTestPartResultReporter only catches failures from
  697. // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD.
  698. TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
  699. printf("(expecting 2 failures)\n");
  700. TestPartResultArray results;
  701. {
  702. ScopedFakeTestPartResultReporter reporter(
  703. ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
  704. &results);
  705. AddFailureInOtherThread(FATAL_FAILURE);
  706. AddFailureInOtherThread(NONFATAL_FAILURE);
  707. }
  708. // The two failures should not have been intercepted.
  709. EXPECT_EQ(0, results.size()) << "This shouldn't fail.";
  710. }
  711. #endif // GTEST_IS_THREADSAFE
  712. TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) {
  713. // Expected fatal failure, but succeeds.
  714. printf("(expecting 1 failure)\n");
  715. EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure.");
  716. // Expected fatal failure, but got a non-fatal failure.
  717. printf("(expecting 1 failure)\n");
  718. EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
  719. "Expected non-fatal failure.");
  720. // Wrong message.
  721. printf("(expecting 1 failure)\n");
  722. EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
  723. "Some other fatal failure expected.");
  724. }
  725. TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) {
  726. // Expected non-fatal failure, but succeeds.
  727. printf("(expecting 1 failure)\n");
  728. EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(),
  729. "Expected non-fatal "
  730. "failure.");
  731. // Expected non-fatal failure, but got a fatal failure.
  732. printf("(expecting 1 failure)\n");
  733. EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
  734. "Expected fatal failure.");
  735. // Wrong message.
  736. printf("(expecting 1 failure)\n");
  737. EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
  738. "Some other non-fatal failure.");
  739. }
  740. class DynamicFixture : public testing::Test {
  741. protected:
  742. DynamicFixture() { printf("DynamicFixture()\n"); }
  743. ~DynamicFixture() override { printf("~DynamicFixture()\n"); }
  744. void SetUp() override { printf("DynamicFixture::SetUp\n"); }
  745. void TearDown() override { printf("DynamicFixture::TearDown\n"); }
  746. static void SetUpTestSuite() { printf("DynamicFixture::SetUpTestSuite\n"); }
  747. static void TearDownTestSuite() {
  748. printf("DynamicFixture::TearDownTestSuite\n");
  749. }
  750. };
  751. template <bool Pass>
  752. class DynamicTest : public DynamicFixture {
  753. public:
  754. void TestBody() override { EXPECT_TRUE(Pass); }
  755. };
  756. auto dynamic_test = (
  757. // Register two tests with the same fixture correctly.
  758. testing::RegisterTest(
  759. "DynamicFixture", "DynamicTestPass", nullptr, nullptr, __FILE__,
  760. __LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }),
  761. testing::RegisterTest(
  762. "DynamicFixture", "DynamicTestFail", nullptr, nullptr, __FILE__,
  763. __LINE__, []() -> DynamicFixture* { return new DynamicTest<false>; }),
  764. // Register the same fixture with another name. That's fine.
  765. testing::RegisterTest(
  766. "DynamicFixtureAnotherName", "DynamicTestPass", nullptr, nullptr,
  767. __FILE__, __LINE__,
  768. []() -> DynamicFixture* { return new DynamicTest<true>; }),
  769. // Register two tests with the same fixture incorrectly.
  770. testing::RegisterTest(
  771. "BadDynamicFixture1", "FixtureBase", nullptr, nullptr, __FILE__,
  772. __LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }),
  773. testing::RegisterTest(
  774. "BadDynamicFixture1", "TestBase", nullptr, nullptr, __FILE__, __LINE__,
  775. []() -> testing::Test* { return new DynamicTest<true>; }),
  776. // Register two tests with the same fixture incorrectly by omitting the
  777. // return type.
  778. testing::RegisterTest(
  779. "BadDynamicFixture2", "FixtureBase", nullptr, nullptr, __FILE__,
  780. __LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }),
  781. testing::RegisterTest("BadDynamicFixture2", "Derived", nullptr, nullptr,
  782. __FILE__, __LINE__,
  783. []() { return new DynamicTest<true>; }));
  784. // Two test environments for testing testing::AddGlobalTestEnvironment().
  785. class FooEnvironment : public testing::Environment {
  786. public:
  787. void SetUp() override { printf("%s", "FooEnvironment::SetUp() called.\n"); }
  788. void TearDown() override {
  789. printf("%s", "FooEnvironment::TearDown() called.\n");
  790. FAIL() << "Expected fatal failure.";
  791. }
  792. };
  793. class BarEnvironment : public testing::Environment {
  794. public:
  795. void SetUp() override { printf("%s", "BarEnvironment::SetUp() called.\n"); }
  796. void TearDown() override {
  797. printf("%s", "BarEnvironment::TearDown() called.\n");
  798. ADD_FAILURE() << "Expected non-fatal failure.";
  799. }
  800. };
  801. class TestSuiteThatFailsToSetUp : public testing::Test {
  802. public:
  803. static void SetUpTestSuite() { EXPECT_TRUE(false); }
  804. };
  805. TEST_F(TestSuiteThatFailsToSetUp, ShouldNotRun) { std::abort(); }
  806. // The main function.
  807. //
  808. // The idea is to use Google Test to run all the tests we have defined (some
  809. // of them are intended to fail), and then compare the test results
  810. // with the "golden" file.
  811. int main(int argc, char** argv) {
  812. GTEST_FLAG_SET(print_time, false);
  813. // We just run the tests, knowing some of them are intended to fail.
  814. // We will use a separate Python script to compare the output of
  815. // this program with the golden file.
  816. // It's hard to test InitGoogleTest() directly, as it has many
  817. // global side effects. The following line serves as a test
  818. // for it.
  819. testing::InitGoogleTest(&argc, argv);
  820. bool internal_skip_environment_and_ad_hoc_tests =
  821. std::count(argv, argv + argc,
  822. std::string("internal_skip_environment_and_ad_hoc_tests")) > 0;
  823. #if GTEST_HAS_DEATH_TEST
  824. if (GTEST_FLAG_GET(internal_run_death_test) != "") {
  825. // Skip the usual output capturing if we're running as the child
  826. // process of an threadsafe-style death test.
  827. #if GTEST_OS_WINDOWS
  828. posix::FReopen("nul:", "w", stdout);
  829. #else
  830. posix::FReopen("/dev/null", "w", stdout);
  831. #endif // GTEST_OS_WINDOWS
  832. return RUN_ALL_TESTS();
  833. }
  834. #endif // GTEST_HAS_DEATH_TEST
  835. if (internal_skip_environment_and_ad_hoc_tests) return RUN_ALL_TESTS();
  836. // Registers two global test environments.
  837. // The golden file verifies that they are set up in the order they
  838. // are registered, and torn down in the reverse order.
  839. testing::AddGlobalTestEnvironment(new FooEnvironment);
  840. testing::AddGlobalTestEnvironment(new BarEnvironment);
  841. #if _MSC_VER
  842. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4127
  843. #endif // _MSC_VER
  844. return RunAllTests();
  845. }