]> git.proxmox.com Git - ceph.git/blob - ceph/src/s3select/rapidjson/thirdparty/gtest/googletest/test/gtest_output_test_.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / s3select / rapidjson / thirdparty / gtest / googletest / test / gtest_output_test_.cc
1 // Copyright 2005, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // The purpose of this file is to generate Google Test output under
31 // various conditions. The output will then be verified by
32 // gtest_output_test.py to ensure that Google Test generates the
33 // desired messages. Therefore, most tests in this file are MEANT TO
34 // FAIL.
35 //
36 // Author: wan@google.com (Zhanyong Wan)
37
38 #include "gtest/gtest-spi.h"
39 #include "gtest/gtest.h"
40 #include "src/gtest-internal-inl.h"
41
42 #include <stdlib.h>
43
44 #if GTEST_IS_THREADSAFE
45 using testing::ScopedFakeTestPartResultReporter;
46 using testing::TestPartResultArray;
47
48 using testing::internal::Notification;
49 using testing::internal::ThreadWithParam;
50 #endif
51
52 namespace posix = ::testing::internal::posix;
53
54 // Tests catching fatal failures.
55
56 // A subroutine used by the following test.
57 void TestEq1(int x) {
58 ASSERT_EQ(1, x);
59 }
60
61 // This function calls a test subroutine, catches the fatal failure it
62 // generates, and then returns early.
63 void TryTestSubroutine() {
64 // Calls a subrountine that yields a fatal failure.
65 TestEq1(2);
66
67 // Catches the fatal failure and aborts the test.
68 //
69 // The testing::Test:: prefix is necessary when calling
70 // HasFatalFailure() outside of a TEST, TEST_F, or test fixture.
71 if (testing::Test::HasFatalFailure()) return;
72
73 // If we get here, something is wrong.
74 FAIL() << "This should never be reached.";
75 }
76
77 TEST(PassingTest, PassingTest1) {
78 }
79
80 TEST(PassingTest, PassingTest2) {
81 }
82
83 // Tests that parameters of failing parameterized tests are printed in the
84 // failing test summary.
85 class FailingParamTest : public testing::TestWithParam<int> {};
86
87 TEST_P(FailingParamTest, Fails) {
88 EXPECT_EQ(1, GetParam());
89 }
90
91 // This generates a test which will fail. Google Test is expected to print
92 // its parameter when it outputs the list of all failed tests.
93 INSTANTIATE_TEST_CASE_P(PrintingFailingParams,
94 FailingParamTest,
95 testing::Values(2));
96
97 static const char kGoldenString[] = "\"Line\0 1\"\nLine 2";
98
99 TEST(NonfatalFailureTest, EscapesStringOperands) {
100 std::string actual = "actual \"string\"";
101 EXPECT_EQ(kGoldenString, actual);
102
103 const char* golden = kGoldenString;
104 EXPECT_EQ(golden, actual);
105 }
106
107 TEST(NonfatalFailureTest, DiffForLongStrings) {
108 std::string golden_str(kGoldenString, sizeof(kGoldenString) - 1);
109 EXPECT_EQ(golden_str, "Line 2");
110 }
111
112 // Tests catching a fatal failure in a subroutine.
113 TEST(FatalFailureTest, FatalFailureInSubroutine) {
114 printf("(expecting a failure that x should be 1)\n");
115
116 TryTestSubroutine();
117 }
118
119 // Tests catching a fatal failure in a nested subroutine.
120 TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
121 printf("(expecting a failure that x should be 1)\n");
122
123 // Calls a subrountine that yields a fatal failure.
124 TryTestSubroutine();
125
126 // Catches the fatal failure and aborts the test.
127 //
128 // When calling HasFatalFailure() inside a TEST, TEST_F, or test
129 // fixture, the testing::Test:: prefix is not needed.
130 if (HasFatalFailure()) return;
131
132 // If we get here, something is wrong.
133 FAIL() << "This should never be reached.";
134 }
135
136 // Tests HasFatalFailure() after a failed EXPECT check.
137 TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
138 printf("(expecting a failure on false)\n");
139 EXPECT_TRUE(false); // Generates a nonfatal failure
140 ASSERT_FALSE(HasFatalFailure()); // This should succeed.
141 }
142
143 // Tests interleaving user logging and Google Test assertions.
144 TEST(LoggingTest, InterleavingLoggingAndAssertions) {
145 static const int a[4] = {
146 3, 9, 2, 6
147 };
148
149 printf("(expecting 2 failures on (3) >= (a[i]))\n");
150 for (int i = 0; i < static_cast<int>(sizeof(a)/sizeof(*a)); i++) {
151 printf("i == %d\n", i);
152 EXPECT_GE(3, a[i]);
153 }
154 }
155
156 // Tests the SCOPED_TRACE macro.
157
158 // A helper function for testing SCOPED_TRACE.
159 void SubWithoutTrace(int n) {
160 EXPECT_EQ(1, n);
161 ASSERT_EQ(2, n);
162 }
163
164 // Another helper function for testing SCOPED_TRACE.
165 void SubWithTrace(int n) {
166 SCOPED_TRACE(testing::Message() << "n = " << n);
167
168 SubWithoutTrace(n);
169 }
170
171 TEST(SCOPED_TRACETest, AcceptedValues) {
172 SCOPED_TRACE("literal string");
173 SCOPED_TRACE(std::string("std::string"));
174 SCOPED_TRACE(1337); // streamable type
175 const char* null_value = NULL;
176 SCOPED_TRACE(null_value);
177
178 ADD_FAILURE() << "Just checking that all these values work fine.";
179 }
180
181 // Tests that SCOPED_TRACE() obeys lexical scopes.
182 TEST(SCOPED_TRACETest, ObeysScopes) {
183 printf("(expected to fail)\n");
184
185 // There should be no trace before SCOPED_TRACE() is invoked.
186 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
187
188 {
189 SCOPED_TRACE("Expected trace");
190 // After SCOPED_TRACE(), a failure in the current scope should contain
191 // the trace.
192 ADD_FAILURE() << "This failure is expected, and should have a trace.";
193 }
194
195 // Once the control leaves the scope of the SCOPED_TRACE(), there
196 // should be no trace again.
197 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
198 }
199
200 // Tests that SCOPED_TRACE works inside a loop.
201 TEST(SCOPED_TRACETest, WorksInLoop) {
202 printf("(expected to fail)\n");
203
204 for (int i = 1; i <= 2; i++) {
205 SCOPED_TRACE(testing::Message() << "i = " << i);
206
207 SubWithoutTrace(i);
208 }
209 }
210
211 // Tests that SCOPED_TRACE works in a subroutine.
212 TEST(SCOPED_TRACETest, WorksInSubroutine) {
213 printf("(expected to fail)\n");
214
215 SubWithTrace(1);
216 SubWithTrace(2);
217 }
218
219 // Tests that SCOPED_TRACE can be nested.
220 TEST(SCOPED_TRACETest, CanBeNested) {
221 printf("(expected to fail)\n");
222
223 SCOPED_TRACE(""); // A trace without a message.
224
225 SubWithTrace(2);
226 }
227
228 // Tests that multiple SCOPED_TRACEs can be used in the same scope.
229 TEST(SCOPED_TRACETest, CanBeRepeated) {
230 printf("(expected to fail)\n");
231
232 SCOPED_TRACE("A");
233 ADD_FAILURE()
234 << "This failure is expected, and should contain trace point A.";
235
236 SCOPED_TRACE("B");
237 ADD_FAILURE()
238 << "This failure is expected, and should contain trace point A and B.";
239
240 {
241 SCOPED_TRACE("C");
242 ADD_FAILURE() << "This failure is expected, and should "
243 << "contain trace point A, B, and C.";
244 }
245
246 SCOPED_TRACE("D");
247 ADD_FAILURE() << "This failure is expected, and should "
248 << "contain trace point A, B, and D.";
249 }
250
251 #if GTEST_IS_THREADSAFE
252 // Tests that SCOPED_TRACE()s can be used concurrently from multiple
253 // threads. Namely, an assertion should be affected by
254 // SCOPED_TRACE()s in its own thread only.
255
256 // Here's the sequence of actions that happen in the test:
257 //
258 // Thread A (main) | Thread B (spawned)
259 // ===============================|================================
260 // spawns thread B |
261 // -------------------------------+--------------------------------
262 // waits for n1 | SCOPED_TRACE("Trace B");
263 // | generates failure #1
264 // | notifies n1
265 // -------------------------------+--------------------------------
266 // SCOPED_TRACE("Trace A"); | waits for n2
267 // generates failure #2 |
268 // notifies n2 |
269 // -------------------------------|--------------------------------
270 // waits for n3 | generates failure #3
271 // | trace B dies
272 // | generates failure #4
273 // | notifies n3
274 // -------------------------------|--------------------------------
275 // generates failure #5 | finishes
276 // trace A dies |
277 // generates failure #6 |
278 // -------------------------------|--------------------------------
279 // waits for thread B to finish |
280
281 struct CheckPoints {
282 Notification n1;
283 Notification n2;
284 Notification n3;
285 };
286
287 static void ThreadWithScopedTrace(CheckPoints* check_points) {
288 {
289 SCOPED_TRACE("Trace B");
290 ADD_FAILURE()
291 << "Expected failure #1 (in thread B, only trace B alive).";
292 check_points->n1.Notify();
293 check_points->n2.WaitForNotification();
294
295 ADD_FAILURE()
296 << "Expected failure #3 (in thread B, trace A & B both alive).";
297 } // Trace B dies here.
298 ADD_FAILURE()
299 << "Expected failure #4 (in thread B, only trace A alive).";
300 check_points->n3.Notify();
301 }
302
303 TEST(SCOPED_TRACETest, WorksConcurrently) {
304 printf("(expecting 6 failures)\n");
305
306 CheckPoints check_points;
307 ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace,
308 &check_points,
309 NULL);
310 check_points.n1.WaitForNotification();
311
312 {
313 SCOPED_TRACE("Trace A");
314 ADD_FAILURE()
315 << "Expected failure #2 (in thread A, trace A & B both alive).";
316 check_points.n2.Notify();
317 check_points.n3.WaitForNotification();
318
319 ADD_FAILURE()
320 << "Expected failure #5 (in thread A, only trace A alive).";
321 } // Trace A dies here.
322 ADD_FAILURE()
323 << "Expected failure #6 (in thread A, no trace alive).";
324 thread.Join();
325 }
326 #endif // GTEST_IS_THREADSAFE
327
328 // Tests basic functionality of the ScopedTrace utility (most of its features
329 // are already tested in SCOPED_TRACETest).
330 TEST(ScopedTraceTest, WithExplicitFileAndLine) {
331 testing::ScopedTrace trace("explicit_file.cc", 123, "expected trace message");
332 ADD_FAILURE() << "Check that the trace is attached to a particular location.";
333 }
334
335 TEST(DisabledTestsWarningTest,
336 DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
337 // This test body is intentionally empty. Its sole purpose is for
338 // verifying that the --gtest_also_run_disabled_tests flag
339 // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of
340 // the test output.
341 }
342
343 // Tests using assertions outside of TEST and TEST_F.
344 //
345 // This function creates two failures intentionally.
346 void AdHocTest() {
347 printf("The non-test part of the code is expected to have 2 failures.\n\n");
348 EXPECT_TRUE(false);
349 EXPECT_EQ(2, 3);
350 }
351
352 // Runs all TESTs, all TEST_Fs, and the ad hoc test.
353 int RunAllTests() {
354 AdHocTest();
355 return RUN_ALL_TESTS();
356 }
357
358 // Tests non-fatal failures in the fixture constructor.
359 class NonFatalFailureInFixtureConstructorTest : public testing::Test {
360 protected:
361 NonFatalFailureInFixtureConstructorTest() {
362 printf("(expecting 5 failures)\n");
363 ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor.";
364 }
365
366 ~NonFatalFailureInFixtureConstructorTest() {
367 ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor.";
368 }
369
370 virtual void SetUp() {
371 ADD_FAILURE() << "Expected failure #2, in SetUp().";
372 }
373
374 virtual void TearDown() {
375 ADD_FAILURE() << "Expected failure #4, in TearDown.";
376 }
377 };
378
379 TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) {
380 ADD_FAILURE() << "Expected failure #3, in the test body.";
381 }
382
383 // Tests fatal failures in the fixture constructor.
384 class FatalFailureInFixtureConstructorTest : public testing::Test {
385 protected:
386 FatalFailureInFixtureConstructorTest() {
387 printf("(expecting 2 failures)\n");
388 Init();
389 }
390
391 ~FatalFailureInFixtureConstructorTest() {
392 ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor.";
393 }
394
395 virtual void SetUp() {
396 ADD_FAILURE() << "UNEXPECTED failure in SetUp(). "
397 << "We should never get here, as the test fixture c'tor "
398 << "had a fatal failure.";
399 }
400
401 virtual void TearDown() {
402 ADD_FAILURE() << "UNEXPECTED failure in TearDown(). "
403 << "We should never get here, as the test fixture c'tor "
404 << "had a fatal failure.";
405 }
406
407 private:
408 void Init() {
409 FAIL() << "Expected failure #1, in the test fixture c'tor.";
410 }
411 };
412
413 TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) {
414 ADD_FAILURE() << "UNEXPECTED failure in the test body. "
415 << "We should never get here, as the test fixture c'tor "
416 << "had a fatal failure.";
417 }
418
419 // Tests non-fatal failures in SetUp().
420 class NonFatalFailureInSetUpTest : public testing::Test {
421 protected:
422 virtual ~NonFatalFailureInSetUpTest() {
423 Deinit();
424 }
425
426 virtual void SetUp() {
427 printf("(expecting 4 failures)\n");
428 ADD_FAILURE() << "Expected failure #1, in SetUp().";
429 }
430
431 virtual void TearDown() {
432 FAIL() << "Expected failure #3, in TearDown().";
433 }
434 private:
435 void Deinit() {
436 FAIL() << "Expected failure #4, in the test fixture d'tor.";
437 }
438 };
439
440 TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) {
441 FAIL() << "Expected failure #2, in the test function.";
442 }
443
444 // Tests fatal failures in SetUp().
445 class FatalFailureInSetUpTest : public testing::Test {
446 protected:
447 virtual ~FatalFailureInSetUpTest() {
448 Deinit();
449 }
450
451 virtual void SetUp() {
452 printf("(expecting 3 failures)\n");
453 FAIL() << "Expected failure #1, in SetUp().";
454 }
455
456 virtual void TearDown() {
457 FAIL() << "Expected failure #2, in TearDown().";
458 }
459 private:
460 void Deinit() {
461 FAIL() << "Expected failure #3, in the test fixture d'tor.";
462 }
463 };
464
465 TEST_F(FatalFailureInSetUpTest, FailureInSetUp) {
466 FAIL() << "UNEXPECTED failure in the test function. "
467 << "We should never get here, as SetUp() failed.";
468 }
469
470 TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
471 ADD_FAILURE_AT("foo.cc", 42) << "Expected failure in foo.cc";
472 }
473
474 #if GTEST_IS_THREADSAFE
475
476 // A unary function that may die.
477 void DieIf(bool should_die) {
478 GTEST_CHECK_(!should_die) << " - death inside DieIf().";
479 }
480
481 // Tests running death tests in a multi-threaded context.
482
483 // Used for coordination between the main and the spawn thread.
484 struct SpawnThreadNotifications {
485 SpawnThreadNotifications() {}
486
487 Notification spawn_thread_started;
488 Notification spawn_thread_ok_to_terminate;
489
490 private:
491 GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications);
492 };
493
494 // The function to be executed in the thread spawn by the
495 // MultipleThreads test (below).
496 static void ThreadRoutine(SpawnThreadNotifications* notifications) {
497 // Signals the main thread that this thread has started.
498 notifications->spawn_thread_started.Notify();
499
500 // Waits for permission to finish from the main thread.
501 notifications->spawn_thread_ok_to_terminate.WaitForNotification();
502 }
503
504 // This is a death-test test, but it's not named with a DeathTest
505 // suffix. It starts threads which might interfere with later
506 // death tests, so it must run after all other death tests.
507 class DeathTestAndMultiThreadsTest : public testing::Test {
508 protected:
509 // Starts a thread and waits for it to begin.
510 virtual void SetUp() {
511 thread_.reset(new ThreadWithParam<SpawnThreadNotifications*>(
512 &ThreadRoutine, &notifications_, NULL));
513 notifications_.spawn_thread_started.WaitForNotification();
514 }
515 // Tells the thread to finish, and reaps it.
516 // Depending on the version of the thread library in use,
517 // a manager thread might still be left running that will interfere
518 // with later death tests. This is unfortunate, but this class
519 // cleans up after itself as best it can.
520 virtual void TearDown() {
521 notifications_.spawn_thread_ok_to_terminate.Notify();
522 }
523
524 private:
525 SpawnThreadNotifications notifications_;
526 testing::internal::scoped_ptr<ThreadWithParam<SpawnThreadNotifications*> >
527 thread_;
528 };
529
530 #endif // GTEST_IS_THREADSAFE
531
532 // The MixedUpTestCaseTest test case verifies that Google Test will fail a
533 // test if it uses a different fixture class than what other tests in
534 // the same test case use. It deliberately contains two fixture
535 // classes with the same name but defined in different namespaces.
536
537 // The MixedUpTestCaseWithSameTestNameTest test case verifies that
538 // when the user defines two tests with the same test case name AND
539 // same test name (but in different namespaces), the second test will
540 // fail.
541
542 namespace foo {
543
544 class MixedUpTestCaseTest : public testing::Test {
545 };
546
547 TEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {}
548 TEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {}
549
550 class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
551 };
552
553 TEST_F(MixedUpTestCaseWithSameTestNameTest,
554 TheSecondTestWithThisNameShouldFail) {}
555
556 } // namespace foo
557
558 namespace bar {
559
560 class MixedUpTestCaseTest : public testing::Test {
561 };
562
563 // The following two tests are expected to fail. We rely on the
564 // golden file to check that Google Test generates the right error message.
565 TEST_F(MixedUpTestCaseTest, ThisShouldFail) {}
566 TEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {}
567
568 class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
569 };
570
571 // Expected to fail. We rely on the golden file to check that Google Test
572 // generates the right error message.
573 TEST_F(MixedUpTestCaseWithSameTestNameTest,
574 TheSecondTestWithThisNameShouldFail) {}
575
576 } // namespace bar
577
578 // The following two test cases verify that Google Test catches the user
579 // error of mixing TEST and TEST_F in the same test case. The first
580 // test case checks the scenario where TEST_F appears before TEST, and
581 // the second one checks where TEST appears before TEST_F.
582
583 class TEST_F_before_TEST_in_same_test_case : public testing::Test {
584 };
585
586 TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {}
587
588 // Expected to fail. We rely on the golden file to check that Google Test
589 // generates the right error message.
590 TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {}
591
592 class TEST_before_TEST_F_in_same_test_case : public testing::Test {
593 };
594
595 TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {}
596
597 // Expected to fail. We rely on the golden file to check that Google Test
598 // generates the right error message.
599 TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {
600 }
601
602 // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE().
603 int global_integer = 0;
604
605 // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables.
606 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
607 global_integer = 0;
608 EXPECT_NONFATAL_FAILURE({
609 EXPECT_EQ(1, global_integer) << "Expected non-fatal failure.";
610 }, "Expected non-fatal failure.");
611 }
612
613 // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables
614 // (static or not).
615 TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
616 int m = 0;
617 static int n;
618 n = 1;
619 EXPECT_NONFATAL_FAILURE({
620 EXPECT_EQ(m, n) << "Expected non-fatal failure.";
621 }, "Expected non-fatal failure.");
622 }
623
624 // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly
625 // one non-fatal failure and no fatal failure.
626 TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
627 EXPECT_NONFATAL_FAILURE({
628 ADD_FAILURE() << "Expected non-fatal failure.";
629 }, "Expected non-fatal failure.");
630 }
631
632 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no
633 // non-fatal failure.
634 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
635 printf("(expecting a failure)\n");
636 EXPECT_NONFATAL_FAILURE({
637 }, "");
638 }
639
640 // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two
641 // non-fatal failures.
642 TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
643 printf("(expecting a failure)\n");
644 EXPECT_NONFATAL_FAILURE({
645 ADD_FAILURE() << "Expected non-fatal failure 1.";
646 ADD_FAILURE() << "Expected non-fatal failure 2.";
647 }, "");
648 }
649
650 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal
651 // failure.
652 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
653 printf("(expecting a failure)\n");
654 EXPECT_NONFATAL_FAILURE({
655 FAIL() << "Expected fatal failure.";
656 }, "");
657 }
658
659 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
660 // tested returns.
661 TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
662 printf("(expecting a failure)\n");
663 EXPECT_NONFATAL_FAILURE({
664 return;
665 }, "");
666 }
667
668 #if GTEST_HAS_EXCEPTIONS
669
670 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
671 // tested throws.
672 TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
673 printf("(expecting a failure)\n");
674 try {
675 EXPECT_NONFATAL_FAILURE({
676 throw 0;
677 }, "");
678 } catch(int) { // NOLINT
679 }
680 }
681
682 #endif // GTEST_HAS_EXCEPTIONS
683
684 // Tests that EXPECT_FATAL_FAILURE() can reference global variables.
685 TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
686 global_integer = 0;
687 EXPECT_FATAL_FAILURE({
688 ASSERT_EQ(1, global_integer) << "Expected fatal failure.";
689 }, "Expected fatal failure.");
690 }
691
692 // Tests that EXPECT_FATAL_FAILURE() can reference local static
693 // variables.
694 TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
695 static int n;
696 n = 1;
697 EXPECT_FATAL_FAILURE({
698 ASSERT_EQ(0, n) << "Expected fatal failure.";
699 }, "Expected fatal failure.");
700 }
701
702 // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly
703 // one fatal failure and no non-fatal failure.
704 TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
705 EXPECT_FATAL_FAILURE({
706 FAIL() << "Expected fatal failure.";
707 }, "Expected fatal failure.");
708 }
709
710 // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal
711 // failure.
712 TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
713 printf("(expecting a failure)\n");
714 EXPECT_FATAL_FAILURE({
715 }, "");
716 }
717
718 // A helper for generating a fatal failure.
719 void FatalFailure() {
720 FAIL() << "Expected fatal failure.";
721 }
722
723 // Tests that EXPECT_FATAL_FAILURE() fails when there are two
724 // fatal failures.
725 TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
726 printf("(expecting a failure)\n");
727 EXPECT_FATAL_FAILURE({
728 FatalFailure();
729 FatalFailure();
730 }, "");
731 }
732
733 // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal
734 // failure.
735 TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
736 printf("(expecting a failure)\n");
737 EXPECT_FATAL_FAILURE({
738 ADD_FAILURE() << "Expected non-fatal failure.";
739 }, "");
740 }
741
742 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
743 // tested returns.
744 TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
745 printf("(expecting a failure)\n");
746 EXPECT_FATAL_FAILURE({
747 return;
748 }, "");
749 }
750
751 #if GTEST_HAS_EXCEPTIONS
752
753 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
754 // tested throws.
755 TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
756 printf("(expecting a failure)\n");
757 try {
758 EXPECT_FATAL_FAILURE({
759 throw 0;
760 }, "");
761 } catch(int) { // NOLINT
762 }
763 }
764
765 #endif // GTEST_HAS_EXCEPTIONS
766
767 // This #ifdef block tests the output of value-parameterized tests.
768
769 std::string ParamNameFunc(const testing::TestParamInfo<std::string>& info) {
770 return info.param;
771 }
772
773 class ParamTest : public testing::TestWithParam<std::string> {
774 };
775
776 TEST_P(ParamTest, Success) {
777 EXPECT_EQ("a", GetParam());
778 }
779
780 TEST_P(ParamTest, Failure) {
781 EXPECT_EQ("b", GetParam()) << "Expected failure";
782 }
783
784 INSTANTIATE_TEST_CASE_P(PrintingStrings,
785 ParamTest,
786 testing::Values(std::string("a")),
787 ParamNameFunc);
788
789 // This #ifdef block tests the output of typed tests.
790 #if GTEST_HAS_TYPED_TEST
791
792 template <typename T>
793 class TypedTest : public testing::Test {
794 };
795
796 TYPED_TEST_CASE(TypedTest, testing::Types<int>);
797
798 TYPED_TEST(TypedTest, Success) {
799 EXPECT_EQ(0, TypeParam());
800 }
801
802 TYPED_TEST(TypedTest, Failure) {
803 EXPECT_EQ(1, TypeParam()) << "Expected failure";
804 }
805
806 #endif // GTEST_HAS_TYPED_TEST
807
808 // This #ifdef block tests the output of type-parameterized tests.
809 #if GTEST_HAS_TYPED_TEST_P
810
811 template <typename T>
812 class TypedTestP : public testing::Test {
813 };
814
815 TYPED_TEST_CASE_P(TypedTestP);
816
817 TYPED_TEST_P(TypedTestP, Success) {
818 EXPECT_EQ(0U, TypeParam());
819 }
820
821 TYPED_TEST_P(TypedTestP, Failure) {
822 EXPECT_EQ(1U, TypeParam()) << "Expected failure";
823 }
824
825 REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure);
826
827 typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
828 INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes);
829
830 #endif // GTEST_HAS_TYPED_TEST_P
831
832 #if GTEST_HAS_DEATH_TEST
833
834 // We rely on the golden file to verify that tests whose test case
835 // name ends with DeathTest are run first.
836
837 TEST(ADeathTest, ShouldRunFirst) {
838 }
839
840 # if GTEST_HAS_TYPED_TEST
841
842 // We rely on the golden file to verify that typed tests whose test
843 // case name ends with DeathTest are run first.
844
845 template <typename T>
846 class ATypedDeathTest : public testing::Test {
847 };
848
849 typedef testing::Types<int, double> NumericTypes;
850 TYPED_TEST_CASE(ATypedDeathTest, NumericTypes);
851
852 TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {
853 }
854
855 # endif // GTEST_HAS_TYPED_TEST
856
857 # if GTEST_HAS_TYPED_TEST_P
858
859
860 // We rely on the golden file to verify that type-parameterized tests
861 // whose test case name ends with DeathTest are run first.
862
863 template <typename T>
864 class ATypeParamDeathTest : public testing::Test {
865 };
866
867 TYPED_TEST_CASE_P(ATypeParamDeathTest);
868
869 TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {
870 }
871
872 REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst);
873
874 INSTANTIATE_TYPED_TEST_CASE_P(My, ATypeParamDeathTest, NumericTypes);
875
876 # endif // GTEST_HAS_TYPED_TEST_P
877
878 #endif // GTEST_HAS_DEATH_TEST
879
880 // Tests various failure conditions of
881 // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}.
882 class ExpectFailureTest : public testing::Test {
883 public: // Must be public and not protected due to a bug in g++ 3.4.2.
884 enum FailureMode {
885 FATAL_FAILURE,
886 NONFATAL_FAILURE
887 };
888 static void AddFailure(FailureMode failure) {
889 if (failure == FATAL_FAILURE) {
890 FAIL() << "Expected fatal failure.";
891 } else {
892 ADD_FAILURE() << "Expected non-fatal failure.";
893 }
894 }
895 };
896
897 TEST_F(ExpectFailureTest, ExpectFatalFailure) {
898 // Expected fatal failure, but succeeds.
899 printf("(expecting 1 failure)\n");
900 EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure.");
901 // Expected fatal failure, but got a non-fatal failure.
902 printf("(expecting 1 failure)\n");
903 EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal "
904 "failure.");
905 // Wrong message.
906 printf("(expecting 1 failure)\n");
907 EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure "
908 "expected.");
909 }
910
911 TEST_F(ExpectFailureTest, ExpectNonFatalFailure) {
912 // Expected non-fatal failure, but succeeds.
913 printf("(expecting 1 failure)\n");
914 EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure.");
915 // Expected non-fatal failure, but got a fatal failure.
916 printf("(expecting 1 failure)\n");
917 EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure.");
918 // Wrong message.
919 printf("(expecting 1 failure)\n");
920 EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal "
921 "failure.");
922 }
923
924 #if GTEST_IS_THREADSAFE
925
926 class ExpectFailureWithThreadsTest : public ExpectFailureTest {
927 protected:
928 static void AddFailureInOtherThread(FailureMode failure) {
929 ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL);
930 thread.Join();
931 }
932 };
933
934 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
935 // We only intercept the current thread.
936 printf("(expecting 2 failures)\n");
937 EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE),
938 "Expected fatal failure.");
939 }
940
941 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
942 // We only intercept the current thread.
943 printf("(expecting 2 failures)\n");
944 EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE),
945 "Expected non-fatal failure.");
946 }
947
948 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
949
950 // Tests that the ScopedFakeTestPartResultReporter only catches failures from
951 // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD.
952 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
953 printf("(expecting 2 failures)\n");
954 TestPartResultArray results;
955 {
956 ScopedFakeTestPartResultReporter reporter(
957 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
958 &results);
959 AddFailureInOtherThread(FATAL_FAILURE);
960 AddFailureInOtherThread(NONFATAL_FAILURE);
961 }
962 // The two failures should not have been intercepted.
963 EXPECT_EQ(0, results.size()) << "This shouldn't fail.";
964 }
965
966 #endif // GTEST_IS_THREADSAFE
967
968 TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) {
969 // Expected fatal failure, but succeeds.
970 printf("(expecting 1 failure)\n");
971 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure.");
972 // Expected fatal failure, but got a non-fatal failure.
973 printf("(expecting 1 failure)\n");
974 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
975 "Expected non-fatal failure.");
976 // Wrong message.
977 printf("(expecting 1 failure)\n");
978 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
979 "Some other fatal failure expected.");
980 }
981
982 TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) {
983 // Expected non-fatal failure, but succeeds.
984 printf("(expecting 1 failure)\n");
985 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal "
986 "failure.");
987 // Expected non-fatal failure, but got a fatal failure.
988 printf("(expecting 1 failure)\n");
989 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
990 "Expected fatal failure.");
991 // Wrong message.
992 printf("(expecting 1 failure)\n");
993 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
994 "Some other non-fatal failure.");
995 }
996
997
998 // Two test environments for testing testing::AddGlobalTestEnvironment().
999
1000 class FooEnvironment : public testing::Environment {
1001 public:
1002 virtual void SetUp() {
1003 printf("%s", "FooEnvironment::SetUp() called.\n");
1004 }
1005
1006 virtual void TearDown() {
1007 printf("%s", "FooEnvironment::TearDown() called.\n");
1008 FAIL() << "Expected fatal failure.";
1009 }
1010 };
1011
1012 class BarEnvironment : public testing::Environment {
1013 public:
1014 virtual void SetUp() {
1015 printf("%s", "BarEnvironment::SetUp() called.\n");
1016 }
1017
1018 virtual void TearDown() {
1019 printf("%s", "BarEnvironment::TearDown() called.\n");
1020 ADD_FAILURE() << "Expected non-fatal failure.";
1021 }
1022 };
1023
1024 // The main function.
1025 //
1026 // The idea is to use Google Test to run all the tests we have defined (some
1027 // of them are intended to fail), and then compare the test results
1028 // with the "golden" file.
1029 int main(int argc, char **argv) {
1030 testing::GTEST_FLAG(print_time) = false;
1031
1032 // We just run the tests, knowing some of them are intended to fail.
1033 // We will use a separate Python script to compare the output of
1034 // this program with the golden file.
1035
1036 // It's hard to test InitGoogleTest() directly, as it has many
1037 // global side effects. The following line serves as a sanity test
1038 // for it.
1039 testing::InitGoogleTest(&argc, argv);
1040 bool internal_skip_environment_and_ad_hoc_tests =
1041 std::count(argv, argv + argc,
1042 std::string("internal_skip_environment_and_ad_hoc_tests")) > 0;
1043
1044 #if GTEST_HAS_DEATH_TEST
1045 if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") {
1046 // Skip the usual output capturing if we're running as the child
1047 // process of an threadsafe-style death test.
1048 # if GTEST_OS_WINDOWS
1049 posix::FReopen("nul:", "w", stdout);
1050 # else
1051 posix::FReopen("/dev/null", "w", stdout);
1052 # endif // GTEST_OS_WINDOWS
1053 return RUN_ALL_TESTS();
1054 }
1055 #endif // GTEST_HAS_DEATH_TEST
1056
1057 if (internal_skip_environment_and_ad_hoc_tests)
1058 return RUN_ALL_TESTS();
1059
1060 // Registers two global test environments.
1061 // The golden file verifies that they are set up in the order they
1062 // are registered, and torn down in the reverse order.
1063 testing::AddGlobalTestEnvironment(new FooEnvironment);
1064 testing::AddGlobalTestEnvironment(new BarEnvironment);
1065
1066 return RunAllTests();
1067 }