Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
gtest_output_test_.cc
Go to the documentation of this file.
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"
41
42#include <stdlib.h>
43
44#if GTEST_IS_THREADSAFE
47
48using testing::internal::Notification;
49using testing::internal::ThreadWithParam;
50#endif
51
52namespace posix = ::testing::internal::posix;
53
54// Tests catching fatal failures.
55
56// A subroutine used by the following test.
57void 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.
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.
72
73 // If we get here, something is wrong.
74 FAIL() << "This should never be reached.";
75}
76
77TEST(PassingTest, PassingTest1) {
78}
79
80TEST(PassingTest, PassingTest2) {
81}
82
83// Tests that parameters of failing parameterized tests are printed in the
84// failing test summary.
86
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.
93INSTANTIATE_TEST_CASE_P(PrintingFailingParams,
96
97static const char kGoldenString[] = "\"Line\0 1\"\nLine 2";
98
99TEST(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
107TEST(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.
113TEST(FatalFailureTest, FatalFailureInSubroutine) {
114 printf("(expecting a failure that x should be 1)\n");
115
117}
118
119// Tests catching a fatal failure in a nested subroutine.
120TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
121 printf("(expecting a failure that x should be 1)\n");
122
123 // Calls a subrountine that yields a fatal failure.
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.
137TEST(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.
144TEST(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.
159void SubWithoutTrace(int n) {
160 EXPECT_EQ(1, n);
161 ASSERT_EQ(2, n);
162}
163
164// Another helper function for testing SCOPED_TRACE.
165void SubWithTrace(int n) {
166 SCOPED_TRACE(testing::Message() << "n = " << n);
167
169}
170
171TEST(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.
182TEST(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.
201TEST(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
208 }
209}
210
211// Tests that SCOPED_TRACE works in a subroutine.
212TEST(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.
220TEST(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.
229TEST(SCOPED_TRACETest, CanBeRepeated) {
230 printf("(expected to fail)\n");
231
232 SCOPED_TRACE("A");
234 << "This failure is expected, and should contain trace point A.";
235
236 SCOPED_TRACE("B");
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
281struct CheckPoints {
282 Notification n1;
283 Notification n2;
284 Notification n3;
285};
286
287static void ThreadWithScopedTrace(CheckPoints* check_points) {
288 {
289 SCOPED_TRACE("Trace B");
291 << "Expected failure #1 (in thread B, only trace B alive).";
292 check_points->n1.Notify();
293 check_points->n2.WaitForNotification();
294
296 << "Expected failure #3 (in thread B, trace A & B both alive).";
297 } // Trace B dies here.
299 << "Expected failure #4 (in thread B, only trace A alive).";
300 check_points->n3.Notify();
301}
302
303TEST(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");
315 << "Expected failure #2 (in thread A, trace A & B both alive).";
316 check_points.n2.Notify();
317 check_points.n3.WaitForNotification();
318
320 << "Expected failure #5 (in thread A, only trace A alive).";
321 } // Trace A dies here.
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).
330TEST(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
335TEST(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.
346void 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.
354 AdHocTest();
355 return RUN_ALL_TESTS();
356}
357
358// Tests non-fatal failures in the fixture constructor.
360 protected:
362 printf("(expecting 5 failures)\n");
363 ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor.";
364 }
365
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
380 ADD_FAILURE() << "Expected failure #3, in the test body.";
381}
382
383// Tests fatal failures in the fixture constructor.
385 protected:
387 printf("(expecting 2 failures)\n");
388 Init();
389 }
390
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
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().
421 protected:
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
441 FAIL() << "Expected failure #2, in the test function.";
442}
443
444// Tests fatal failures in SetUp().
446 protected:
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
466 FAIL() << "UNEXPECTED failure in the test function. "
467 << "We should never get here, as SetUp() failed.";
468}
469
470TEST(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.
477void 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.
484struct 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).
496static 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.
507class 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_;
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
542namespace foo {
543
545};
546
547TEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {}
548TEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {}
549
552
554 TheSecondTestWithThisNameShouldFail) {}
555
556} // namespace foo
557
558namespace bar {
559
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.
565TEST_F(MixedUpTestCaseTest, ThisShouldFail) {}
566TEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {}
567
570
571// Expected to fail. We rely on the golden file to check that Google Test
572// generates the right error message.
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
585
587
588// Expected to fail. We rely on the golden file to check that Google Test
589// generates the right error message.
590TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {}
591
594
596
597// Expected to fail. We rely on the golden file to check that Google Test
598// generates the right error message.
599TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {
600}
601
602// Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE().
604
605// Tests that EXPECT_NONFATAL_FAILURE() can reference global variables.
606TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
607 global_integer = 0;
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).
615TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
616 int m = 0;
617 static int n;
618 n = 1;
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.
626TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
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.
634TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
635 printf("(expecting a failure)\n");
637 }, "");
638}
639
640// Tests that EXPECT_NONFATAL_FAILURE() fails when there are two
641// non-fatal failures.
642TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
643 printf("(expecting a failure)\n");
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.
652TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
653 printf("(expecting a failure)\n");
655 FAIL() << "Expected fatal failure.";
656 }, "");
657}
658
659// Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
660// tested returns.
661TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
662 printf("(expecting a failure)\n");
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.
672TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
673 printf("(expecting a failure)\n");
674 try {
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.
685TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
686 global_integer = 0;
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.
694TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
695 static int n;
696 n = 1;
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.
704TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
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.
712TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
713 printf("(expecting a failure)\n");
715 }, "");
716}
717
718// A helper for generating a fatal failure.
720 FAIL() << "Expected fatal failure.";
721}
722
723// Tests that EXPECT_FATAL_FAILURE() fails when there are two
724// fatal failures.
725TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
726 printf("(expecting a failure)\n");
728 FatalFailure();
729 FatalFailure();
730 }, "");
731}
732
733// Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal
734// failure.
735TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
736 printf("(expecting a failure)\n");
738 ADD_FAILURE() << "Expected non-fatal failure.";
739 }, "");
740}
741
742// Tests that EXPECT_FATAL_FAILURE() fails when the statement being
743// tested returns.
744TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
745 printf("(expecting a failure)\n");
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.
755TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
756 printf("(expecting a failure)\n");
757 try {
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
770 return info.param;
771}
772
773class ParamTest : public testing::TestWithParam<std::string> {
774};
775
776TEST_P(ParamTest, Success) {
777 EXPECT_EQ("a", GetParam());
778}
779
780TEST_P(ParamTest, Failure) {
781 EXPECT_EQ("b", GetParam()) << "Expected failure";
782}
783
785 ParamTest,
786 testing::Values(std::string("a")),
788
789// This #ifdef block tests the output of typed tests.
790#if GTEST_HAS_TYPED_TEST
791
792template <typename T>
793class TypedTest : public testing::Test {
794};
795
796TYPED_TEST_CASE(TypedTest, testing::Types<int>);
797
798TYPED_TEST(TypedTest, Success) {
799 EXPECT_EQ(0, TypeParam());
800}
801
802TYPED_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
811template <typename T>
812class TypedTestP : public testing::Test {
813};
814
815TYPED_TEST_CASE_P(TypedTestP);
816
817TYPED_TEST_P(TypedTestP, Success) {
818 EXPECT_EQ(0U, TypeParam());
819}
820
821TYPED_TEST_P(TypedTestP, Failure) {
822 EXPECT_EQ(1U, TypeParam()) << "Expected failure";
823}
824
825REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure);
826
827typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
828INSTANTIATE_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
837TEST(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
845template <typename T>
846class ATypedDeathTest : public testing::Test {
847};
848
849typedef testing::Types<int, double> NumericTypes;
850TYPED_TEST_CASE(ATypedDeathTest, NumericTypes);
851
852TYPED_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
863template <typename T>
864class ATypeParamDeathTest : public testing::Test {
865};
866
867TYPED_TEST_CASE_P(ATypeParamDeathTest);
868
869TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {
870}
871
872REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst);
873
874INSTANTIATE_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}.
883 public: // Must be public and not protected due to a bug in g++ 3.4.2.
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
897TEST_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
911TEST_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
926class ExpectFailureWithThreadsTest : public ExpectFailureTest {
927 protected:
928 static void AddFailureInOtherThread(FailureMode failure) {
929 ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL);
930 thread.Join();
931 }
932};
933
934TEST_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
941TEST_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
948typedef 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.
952TEST_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
968TEST_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
982TEST_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
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
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.
1029int 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.
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.
1065
1066 return RunAllTests();
1067}
virtual void TearDown()
static void AddFailure(FailureMode failure)
virtual void TearDown()
virtual void SetUp()
Definition gtest.cc:2243
static bool HasFatalFailure()
Definition gtest.cc:2505
virtual void TearDown()
Definition gtest.cc:2249
#define INSTANTIATE_TEST_CASE_P(prefix, test_case_name, generator,...)
#define TEST_P(test_case_name, test_name)
#define GTEST_CHECK_(condition)
#define GTEST_DISALLOW_COPY_AND_ASSIGN_(type)
Definition gtest-port.h:917
#define EXPECT_FATAL_FAILURE(statement, substr)
Definition gtest-spi.h:137
#define EXPECT_NONFATAL_FAILURE(statement, substr)
Definition gtest-spi.h:203
#define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr)
Definition gtest-spi.h:217
#define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr)
Definition gtest-spi.h:154
#define TEST_F(test_fixture, test_name)
Definition gtest.h:2304
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1988
#define FAIL()
Definition gtest.h:1858
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1954
#define ADD_FAILURE_AT(file, line)
Definition gtest.h:1848
#define SCOPED_TRACE(message)
Definition gtest.h:2202
#define SUCCEED()
Definition gtest.h:1867
#define ASSERT_FALSE(condition)
Definition gtest.h:1904
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition gtest.h:2328
#define EXPECT_GE(val1, val2)
Definition gtest.h:1964
#define EXPECT_TRUE(condition)
Definition gtest.h:1895
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
#define ADD_FAILURE()
Definition gtest.h:1844
REGISTER_TYPED_TEST_CASE_P(TypeParamTest, TestA, TestB)
TYPED_TEST(TypedTest, TestA)
TYPED_TEST_CASE(TypedTest, MyTypes)
TYPED_TEST_CASE_P(TypeParamTest)
TYPED_TEST_P(TypeParamTest, TestA)
INSTANTIATE_TYPED_TEST_CASE_P(My, TypeParamTest, MyTypes)
void TryTestSubroutine()
void AdHocTest()
void SubWithTrace(int n)
std::string ParamNameFunc(const testing::TestParamInfo< std::string > &info)
void SubWithoutTrace(int n)
int global_integer
void TestEq1(int x)
void FatalFailure()
int RunAllTests()
char ** argv
LOGGING_API void printf(Category category, const char *format,...)
Definition Logging.cpp:30
FILE * FReopen(const char *path, const char *mode, FILE *stream)
internal::ValueArray1< T1 > Values(T1 v1)
Environment * AddGlobalTestEnvironment(Environment *env)
Definition gtest.h:1391
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition gtest.cc:5787
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1181
Definition lib.h:54
account_query_db::get_accounts_by_authorizers_result results
Definition dtoa.c:306