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

1157 lines
36 KiB

  1. // Copyright 2005, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. //
  30. // The purpose of this file is to generate Google Test output under
  31. // various conditions. The output will then be verified by
  32. // 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. #if GTEST_IS_THREADSAFE
  388. // A unary function that may die.
  389. void DieIf(bool should_die) {
  390. GTEST_CHECK_(!should_die) << " - death inside DieIf().";
  391. }
  392. // Tests running death tests in a multi-threaded context.
  393. // Used for coordination between the main and the spawn thread.
  394. struct SpawnThreadNotifications {
  395. SpawnThreadNotifications() {}
  396. Notification spawn_thread_started;
  397. Notification spawn_thread_ok_to_terminate;
  398. private:
  399. GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications);
  400. };
  401. // The function to be executed in the thread spawn by the
  402. // MultipleThreads test (below).
  403. static void ThreadRoutine(SpawnThreadNotifications* notifications) {
  404. // Signals the main thread that this thread has started.
  405. notifications->spawn_thread_started.Notify();
  406. // Waits for permission to finish from the main thread.
  407. notifications->spawn_thread_ok_to_terminate.WaitForNotification();
  408. }
  409. // This is a death-test test, but it's not named with a DeathTest
  410. // suffix. It starts threads which might interfere with later
  411. // death tests, so it must run after all other death tests.
  412. class DeathTestAndMultiThreadsTest : public testing::Test {
  413. protected:
  414. // Starts a thread and waits for it to begin.
  415. void SetUp() override {
  416. thread_.reset(new ThreadWithParam<SpawnThreadNotifications*>(
  417. &ThreadRoutine, &notifications_, nullptr));
  418. notifications_.spawn_thread_started.WaitForNotification();
  419. }
  420. // Tells the thread to finish, and reaps it.
  421. // Depending on the version of the thread library in use,
  422. // a manager thread might still be left running that will interfere
  423. // with later death tests. This is unfortunate, but this class
  424. // cleans up after itself as best it can.
  425. void TearDown() override {
  426. notifications_.spawn_thread_ok_to_terminate.Notify();
  427. }
  428. private:
  429. SpawnThreadNotifications notifications_;
  430. std::unique_ptr<ThreadWithParam<SpawnThreadNotifications*> > thread_;
  431. };
  432. #endif // GTEST_IS_THREADSAFE
  433. // The MixedUpTestSuiteTest test case verifies that Google Test will fail a
  434. // test if it uses a different fixture class than what other tests in
  435. // the same test case use. It deliberately contains two fixture
  436. // classes with the same name but defined in different namespaces.
  437. // The MixedUpTestSuiteWithSameTestNameTest test case verifies that
  438. // when the user defines two tests with the same test case name AND
  439. // same test name (but in different namespaces), the second test will
  440. // fail.
  441. namespace foo {
  442. class MixedUpTestSuiteTest : public testing::Test {
  443. };
  444. TEST_F(MixedUpTestSuiteTest, FirstTestFromNamespaceFoo) {}
  445. TEST_F(MixedUpTestSuiteTest, SecondTestFromNamespaceFoo) {}
  446. class MixedUpTestSuiteWithSameTestNameTest : public testing::Test {
  447. };
  448. TEST_F(MixedUpTestSuiteWithSameTestNameTest,
  449. TheSecondTestWithThisNameShouldFail) {}
  450. } // namespace foo
  451. namespace bar {
  452. class MixedUpTestSuiteTest : public testing::Test {
  453. };
  454. // The following two tests are expected to fail. We rely on the
  455. // golden file to check that Google Test generates the right error message.
  456. TEST_F(MixedUpTestSuiteTest, ThisShouldFail) {}
  457. TEST_F(MixedUpTestSuiteTest, ThisShouldFailToo) {}
  458. class MixedUpTestSuiteWithSameTestNameTest : public testing::Test {
  459. };
  460. // Expected to fail. We rely on the golden file to check that Google Test
  461. // generates the right error message.
  462. TEST_F(MixedUpTestSuiteWithSameTestNameTest,
  463. TheSecondTestWithThisNameShouldFail) {}
  464. } // namespace bar
  465. // The following two test cases verify that Google Test catches the user
  466. // error of mixing TEST and TEST_F in the same test case. The first
  467. // test case checks the scenario where TEST_F appears before TEST, and
  468. // the second one checks where TEST appears before TEST_F.
  469. class TEST_F_before_TEST_in_same_test_case : public testing::Test {
  470. };
  471. TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {}
  472. // Expected to fail. We rely on the golden file to check that Google Test
  473. // generates the right error message.
  474. TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {}
  475. class TEST_before_TEST_F_in_same_test_case : public testing::Test {
  476. };
  477. TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {}
  478. // Expected to fail. We rely on the golden file to check that Google Test
  479. // generates the right error message.
  480. TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {
  481. }
  482. // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE().
  483. int global_integer = 0;
  484. // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables.
  485. TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
  486. global_integer = 0;
  487. EXPECT_NONFATAL_FAILURE({
  488. EXPECT_EQ(1, global_integer) << "Expected non-fatal failure.";
  489. }, "Expected non-fatal failure.");
  490. }
  491. // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables
  492. // (static or not).
  493. TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
  494. int m = 0;
  495. static int n;
  496. n = 1;
  497. EXPECT_NONFATAL_FAILURE({
  498. EXPECT_EQ(m, n) << "Expected non-fatal failure.";
  499. }, "Expected non-fatal failure.");
  500. }
  501. // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly
  502. // one non-fatal failure and no fatal failure.
  503. TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
  504. EXPECT_NONFATAL_FAILURE({
  505. ADD_FAILURE() << "Expected non-fatal failure.";
  506. }, "Expected non-fatal failure.");
  507. }
  508. // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no
  509. // non-fatal failure.
  510. TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
  511. printf("(expecting a failure)\n");
  512. EXPECT_NONFATAL_FAILURE({
  513. }, "");
  514. }
  515. // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two
  516. // non-fatal failures.
  517. TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
  518. printf("(expecting a failure)\n");
  519. EXPECT_NONFATAL_FAILURE({
  520. ADD_FAILURE() << "Expected non-fatal failure 1.";
  521. ADD_FAILURE() << "Expected non-fatal failure 2.";
  522. }, "");
  523. }
  524. // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal
  525. // failure.
  526. TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
  527. printf("(expecting a failure)\n");
  528. EXPECT_NONFATAL_FAILURE({
  529. FAIL() << "Expected fatal failure.";
  530. }, "");
  531. }
  532. // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
  533. // tested returns.
  534. TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
  535. printf("(expecting a failure)\n");
  536. EXPECT_NONFATAL_FAILURE({
  537. return;
  538. }, "");
  539. }
  540. #if GTEST_HAS_EXCEPTIONS
  541. // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
  542. // tested throws.
  543. TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
  544. printf("(expecting a failure)\n");
  545. try {
  546. EXPECT_NONFATAL_FAILURE({
  547. throw 0;
  548. }, "");
  549. } catch(int) { // NOLINT
  550. }
  551. }
  552. #endif // GTEST_HAS_EXCEPTIONS
  553. // Tests that EXPECT_FATAL_FAILURE() can reference global variables.
  554. TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
  555. global_integer = 0;
  556. EXPECT_FATAL_FAILURE({
  557. ASSERT_EQ(1, global_integer) << "Expected fatal failure.";
  558. }, "Expected fatal failure.");
  559. }
  560. // Tests that EXPECT_FATAL_FAILURE() can reference local static
  561. // variables.
  562. TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
  563. static int n;
  564. n = 1;
  565. EXPECT_FATAL_FAILURE({
  566. ASSERT_EQ(0, n) << "Expected fatal failure.";
  567. }, "Expected fatal failure.");
  568. }
  569. // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly
  570. // one fatal failure and no non-fatal failure.
  571. TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
  572. EXPECT_FATAL_FAILURE({
  573. FAIL() << "Expected fatal failure.";
  574. }, "Expected fatal failure.");
  575. }
  576. // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal
  577. // failure.
  578. TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
  579. printf("(expecting a failure)\n");
  580. EXPECT_FATAL_FAILURE({
  581. }, "");
  582. }
  583. // A helper for generating a fatal failure.
  584. void FatalFailure() {
  585. FAIL() << "Expected fatal failure.";
  586. }
  587. // Tests that EXPECT_FATAL_FAILURE() fails when there are two
  588. // fatal failures.
  589. TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
  590. printf("(expecting a failure)\n");
  591. EXPECT_FATAL_FAILURE({
  592. FatalFailure();
  593. FatalFailure();
  594. }, "");
  595. }
  596. // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal
  597. // failure.
  598. TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
  599. printf("(expecting a failure)\n");
  600. EXPECT_FATAL_FAILURE({
  601. ADD_FAILURE() << "Expected non-fatal failure.";
  602. }, "");
  603. }
  604. // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
  605. // tested returns.
  606. TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
  607. printf("(expecting a failure)\n");
  608. EXPECT_FATAL_FAILURE({
  609. return;
  610. }, "");
  611. }
  612. #if GTEST_HAS_EXCEPTIONS
  613. // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
  614. // tested throws.
  615. TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
  616. printf("(expecting a failure)\n");
  617. try {
  618. EXPECT_FATAL_FAILURE({
  619. throw 0;
  620. }, "");
  621. } catch(int) { // NOLINT
  622. }
  623. }
  624. #endif // GTEST_HAS_EXCEPTIONS
  625. // This #ifdef block tests the output of value-parameterized tests.
  626. std::string ParamNameFunc(const testing::TestParamInfo<std::string>& info) {
  627. return info.param;
  628. }
  629. class ParamTest : public testing::TestWithParam<std::string> {
  630. };
  631. TEST_P(ParamTest, Success) {
  632. EXPECT_EQ("a", GetParam());
  633. }
  634. TEST_P(ParamTest, Failure) {
  635. EXPECT_EQ("b", GetParam()) << "Expected failure";
  636. }
  637. INSTANTIATE_TEST_SUITE_P(PrintingStrings,
  638. ParamTest,
  639. testing::Values(std::string("a")),
  640. ParamNameFunc);
  641. // This #ifdef block tests the output of typed tests.
  642. #if GTEST_HAS_TYPED_TEST
  643. template <typename T>
  644. class TypedTest : public testing::Test {
  645. };
  646. TYPED_TEST_SUITE(TypedTest, testing::Types<int>);
  647. TYPED_TEST(TypedTest, Success) {
  648. EXPECT_EQ(0, TypeParam());
  649. }
  650. TYPED_TEST(TypedTest, Failure) {
  651. EXPECT_EQ(1, TypeParam()) << "Expected failure";
  652. }
  653. typedef testing::Types<char, int> TypesForTestWithNames;
  654. template <typename T>
  655. class TypedTestWithNames : public testing::Test {};
  656. class TypedTestNames {
  657. public:
  658. template <typename T>
  659. static std::string GetName(int i) {
  660. if (std::is_same<T, char>::value)
  661. return std::string("char") + ::testing::PrintToString(i);
  662. if (std::is_same<T, int>::value)
  663. return std::string("int") + ::testing::PrintToString(i);
  664. }
  665. };
  666. TYPED_TEST_SUITE(TypedTestWithNames, TypesForTestWithNames, TypedTestNames);
  667. TYPED_TEST(TypedTestWithNames, Success) {}
  668. TYPED_TEST(TypedTestWithNames, Failure) { FAIL(); }
  669. #endif // GTEST_HAS_TYPED_TEST
  670. // This #ifdef block tests the output of type-parameterized tests.
  671. #if GTEST_HAS_TYPED_TEST_P
  672. template <typename T>
  673. class TypedTestP : public testing::Test {
  674. };
  675. TYPED_TEST_SUITE_P(TypedTestP);
  676. TYPED_TEST_P(TypedTestP, Success) {
  677. EXPECT_EQ(0U, TypeParam());
  678. }
  679. TYPED_TEST_P(TypedTestP, Failure) {
  680. EXPECT_EQ(1U, TypeParam()) << "Expected failure";
  681. }
  682. REGISTER_TYPED_TEST_SUITE_P(TypedTestP, Success, Failure);
  683. typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
  684. INSTANTIATE_TYPED_TEST_SUITE_P(Unsigned, TypedTestP, UnsignedTypes);
  685. class TypedTestPNames {
  686. public:
  687. template <typename T>
  688. static std::string GetName(int i) {
  689. if (std::is_same<T, unsigned char>::value) {
  690. return std::string("unsignedChar") + ::testing::PrintToString(i);
  691. }
  692. if (std::is_same<T, unsigned int>::value) {
  693. return std::string("unsignedInt") + ::testing::PrintToString(i);
  694. }
  695. }
  696. };
  697. INSTANTIATE_TYPED_TEST_SUITE_P(UnsignedCustomName, TypedTestP, UnsignedTypes,
  698. TypedTestPNames);
  699. #endif // GTEST_HAS_TYPED_TEST_P
  700. #if GTEST_HAS_DEATH_TEST
  701. // We rely on the golden file to verify that tests whose test case
  702. // name ends with DeathTest are run first.
  703. TEST(ADeathTest, ShouldRunFirst) {
  704. }
  705. # if GTEST_HAS_TYPED_TEST
  706. // We rely on the golden file to verify that typed tests whose test
  707. // case name ends with DeathTest are run first.
  708. template <typename T>
  709. class ATypedDeathTest : public testing::Test {
  710. };
  711. typedef testing::Types<int, double> NumericTypes;
  712. TYPED_TEST_SUITE(ATypedDeathTest, NumericTypes);
  713. TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {
  714. }
  715. # endif // GTEST_HAS_TYPED_TEST
  716. # if GTEST_HAS_TYPED_TEST_P
  717. // We rely on the golden file to verify that type-parameterized tests
  718. // whose test case name ends with DeathTest are run first.
  719. template <typename T>
  720. class ATypeParamDeathTest : public testing::Test {
  721. };
  722. TYPED_TEST_SUITE_P(ATypeParamDeathTest);
  723. TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {
  724. }
  725. REGISTER_TYPED_TEST_SUITE_P(ATypeParamDeathTest, ShouldRunFirst);
  726. INSTANTIATE_TYPED_TEST_SUITE_P(My, ATypeParamDeathTest, NumericTypes);
  727. # endif // GTEST_HAS_TYPED_TEST_P
  728. #endif // GTEST_HAS_DEATH_TEST
  729. // Tests various failure conditions of
  730. // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}.
  731. class ExpectFailureTest : public testing::Test {
  732. public: // Must be public and not protected due to a bug in g++ 3.4.2.
  733. enum FailureMode {
  734. FATAL_FAILURE,
  735. NONFATAL_FAILURE
  736. };
  737. static void AddFailure(FailureMode failure) {
  738. if (failure == FATAL_FAILURE) {
  739. FAIL() << "Expected fatal failure.";
  740. } else {
  741. ADD_FAILURE() << "Expected non-fatal failure.";
  742. }
  743. }
  744. };
  745. TEST_F(ExpectFailureTest, ExpectFatalFailure) {
  746. // Expected fatal failure, but succeeds.
  747. printf("(expecting 1 failure)\n");
  748. EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure.");
  749. // Expected fatal failure, but got a non-fatal failure.
  750. printf("(expecting 1 failure)\n");
  751. EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal "
  752. "failure.");
  753. // Wrong message.
  754. printf("(expecting 1 failure)\n");
  755. EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure "
  756. "expected.");
  757. }
  758. TEST_F(ExpectFailureTest, ExpectNonFatalFailure) {
  759. // Expected non-fatal failure, but succeeds.
  760. printf("(expecting 1 failure)\n");
  761. EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure.");
  762. // Expected non-fatal failure, but got a fatal failure.
  763. printf("(expecting 1 failure)\n");
  764. EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure.");
  765. // Wrong message.
  766. printf("(expecting 1 failure)\n");
  767. EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal "
  768. "failure.");
  769. }
  770. #if GTEST_IS_THREADSAFE
  771. class ExpectFailureWithThreadsTest : public ExpectFailureTest {
  772. protected:
  773. static void AddFailureInOtherThread(FailureMode failure) {
  774. ThreadWithParam<FailureMode> thread(&AddFailure, failure, nullptr);
  775. thread.Join();
  776. }
  777. };
  778. TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
  779. // We only intercept the current thread.
  780. printf("(expecting 2 failures)\n");
  781. EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE),
  782. "Expected fatal failure.");
  783. }
  784. TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
  785. // We only intercept the current thread.
  786. printf("(expecting 2 failures)\n");
  787. EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE),
  788. "Expected non-fatal failure.");
  789. }
  790. typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
  791. // Tests that the ScopedFakeTestPartResultReporter only catches failures from
  792. // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD.
  793. TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
  794. printf("(expecting 2 failures)\n");
  795. TestPartResultArray results;
  796. {
  797. ScopedFakeTestPartResultReporter reporter(
  798. ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
  799. &results);
  800. AddFailureInOtherThread(FATAL_FAILURE);
  801. AddFailureInOtherThread(NONFATAL_FAILURE);
  802. }
  803. // The two failures should not have been intercepted.
  804. EXPECT_EQ(0, results.size()) << "This shouldn't fail.";
  805. }
  806. #endif // GTEST_IS_THREADSAFE
  807. TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) {
  808. // Expected fatal failure, but succeeds.
  809. printf("(expecting 1 failure)\n");
  810. EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure.");
  811. // Expected fatal failure, but got a non-fatal failure.
  812. printf("(expecting 1 failure)\n");
  813. EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
  814. "Expected non-fatal failure.");
  815. // Wrong message.
  816. printf("(expecting 1 failure)\n");
  817. EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
  818. "Some other fatal failure expected.");
  819. }
  820. TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) {
  821. // Expected non-fatal failure, but succeeds.
  822. printf("(expecting 1 failure)\n");
  823. EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal "
  824. "failure.");
  825. // Expected non-fatal failure, but got a fatal failure.
  826. printf("(expecting 1 failure)\n");
  827. EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
  828. "Expected fatal failure.");
  829. // Wrong message.
  830. printf("(expecting 1 failure)\n");
  831. EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
  832. "Some other non-fatal failure.");
  833. }
  834. class DynamicFixture : public testing::Test {
  835. protected:
  836. DynamicFixture() { printf("DynamicFixture()\n"); }
  837. ~DynamicFixture() override { printf("~DynamicFixture()\n"); }
  838. void SetUp() override { printf("DynamicFixture::SetUp\n"); }
  839. void TearDown() override { printf("DynamicFixture::TearDown\n"); }
  840. static void SetUpTestSuite() { printf("DynamicFixture::SetUpTestSuite\n"); }
  841. static void TearDownTestSuite() {
  842. printf("DynamicFixture::TearDownTestSuite\n");
  843. }
  844. };
  845. template <bool Pass>
  846. class DynamicTest : public DynamicFixture {
  847. public:
  848. void TestBody() override { EXPECT_TRUE(Pass); }
  849. };
  850. auto dynamic_test = (
  851. // Register two tests with the same fixture correctly.
  852. testing::RegisterTest(
  853. "DynamicFixture", "DynamicTestPass", nullptr, nullptr, __FILE__,
  854. __LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }),
  855. testing::RegisterTest(
  856. "DynamicFixture", "DynamicTestFail", nullptr, nullptr, __FILE__,
  857. __LINE__, []() -> DynamicFixture* { return new DynamicTest<false>; }),
  858. // Register the same fixture with another name. That's fine.
  859. testing::RegisterTest(
  860. "DynamicFixtureAnotherName", "DynamicTestPass", nullptr, nullptr,
  861. __FILE__, __LINE__,
  862. []() -> DynamicFixture* { return new DynamicTest<true>; }),
  863. // Register two tests with the same fixture incorrectly.
  864. testing::RegisterTest(
  865. "BadDynamicFixture1", "FixtureBase", nullptr, nullptr, __FILE__,
  866. __LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }),
  867. testing::RegisterTest(
  868. "BadDynamicFixture1", "TestBase", nullptr, nullptr, __FILE__, __LINE__,
  869. []() -> testing::Test* { return new DynamicTest<true>; }),
  870. // Register two tests with the same fixture incorrectly by ommiting the
  871. // return type.
  872. testing::RegisterTest(
  873. "BadDynamicFixture2", "FixtureBase", nullptr, nullptr, __FILE__,
  874. __LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }),
  875. testing::RegisterTest("BadDynamicFixture2", "Derived", nullptr, nullptr,
  876. __FILE__, __LINE__,
  877. []() { return new DynamicTest<true>; }));
  878. // Two test environments for testing testing::AddGlobalTestEnvironment().
  879. class FooEnvironment : public testing::Environment {
  880. public:
  881. void SetUp() override { printf("%s", "FooEnvironment::SetUp() called.\n"); }
  882. void TearDown() override {
  883. printf("%s", "FooEnvironment::TearDown() called.\n");
  884. FAIL() << "Expected fatal failure.";
  885. }
  886. };
  887. class BarEnvironment : public testing::Environment {
  888. public:
  889. void SetUp() override { printf("%s", "BarEnvironment::SetUp() called.\n"); }
  890. void TearDown() override {
  891. printf("%s", "BarEnvironment::TearDown() called.\n");
  892. ADD_FAILURE() << "Expected non-fatal failure.";
  893. }
  894. };
  895. // The main function.
  896. //
  897. // The idea is to use Google Test to run all the tests we have defined (some
  898. // of them are intended to fail), and then compare the test results
  899. // with the "golden" file.
  900. int main(int argc, char **argv) {
  901. testing::GTEST_FLAG(print_time) = false;
  902. // We just run the tests, knowing some of them are intended to fail.
  903. // We will use a separate Python script to compare the output of
  904. // this program with the golden file.
  905. // It's hard to test InitGoogleTest() directly, as it has many
  906. // global side effects. The following line serves as a sanity test
  907. // for it.
  908. testing::InitGoogleTest(&argc, argv);
  909. bool internal_skip_environment_and_ad_hoc_tests =
  910. std::count(argv, argv + argc,
  911. std::string("internal_skip_environment_and_ad_hoc_tests")) > 0;
  912. #if GTEST_HAS_DEATH_TEST
  913. if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") {
  914. // Skip the usual output capturing if we're running as the child
  915. // process of an threadsafe-style death test.
  916. # if GTEST_OS_WINDOWS
  917. posix::FReopen("nul:", "w", stdout);
  918. # else
  919. posix::FReopen("/dev/null", "w", stdout);
  920. # endif // GTEST_OS_WINDOWS
  921. return RUN_ALL_TESTS();
  922. }
  923. #endif // GTEST_HAS_DEATH_TEST
  924. if (internal_skip_environment_and_ad_hoc_tests)
  925. return RUN_ALL_TESTS();
  926. // Registers two global test environments.
  927. // The golden file verifies that they are set up in the order they
  928. // are registered, and torn down in the reverse order.
  929. testing::AddGlobalTestEnvironment(new FooEnvironment);
  930. testing::AddGlobalTestEnvironment(new BarEnvironment);
  931. #if _MSC_VER
  932. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4127
  933. #endif // _MSC_VER
  934. return RunAllTests();
  935. }