googletest-output-test_.cc 35 KB

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