source: gtest-1.7.0/test/gtest_output_test_.cc@ 12756

Last change on this file since 12756 was 12746, checked in by hock@…, 11 years ago

integrated the Google Testing Framework (gtest)

and wrote an Hello World test, to ensure the framework is working..

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