1b89a7cc2SEnji Cooper // Copyright 2005, Google Inc.
2b89a7cc2SEnji Cooper // All rights reserved.
3b89a7cc2SEnji Cooper //
4b89a7cc2SEnji Cooper // Redistribution and use in source and binary forms, with or without
5b89a7cc2SEnji Cooper // modification, are permitted provided that the following conditions are
6b89a7cc2SEnji Cooper // met:
7b89a7cc2SEnji Cooper //
8b89a7cc2SEnji Cooper //     * Redistributions of source code must retain the above copyright
9b89a7cc2SEnji Cooper // notice, this list of conditions and the following disclaimer.
10b89a7cc2SEnji Cooper //     * Redistributions in binary form must reproduce the above
11b89a7cc2SEnji Cooper // copyright notice, this list of conditions and the following disclaimer
12b89a7cc2SEnji Cooper // in the documentation and/or other materials provided with the
13b89a7cc2SEnji Cooper // distribution.
14b89a7cc2SEnji Cooper //     * Neither the name of Google Inc. nor the names of its
15b89a7cc2SEnji Cooper // contributors may be used to endorse or promote products derived from
16b89a7cc2SEnji Cooper // this software without specific prior written permission.
17b89a7cc2SEnji Cooper //
18b89a7cc2SEnji Cooper // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19b89a7cc2SEnji Cooper // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20b89a7cc2SEnji Cooper // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21b89a7cc2SEnji Cooper // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22b89a7cc2SEnji Cooper // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23b89a7cc2SEnji Cooper // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24b89a7cc2SEnji Cooper // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25b89a7cc2SEnji Cooper // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26b89a7cc2SEnji Cooper // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27b89a7cc2SEnji Cooper // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28b89a7cc2SEnji Cooper // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29b89a7cc2SEnji Cooper //
30b89a7cc2SEnji Cooper // The purpose of this file is to generate Google Test output under
31b89a7cc2SEnji Cooper // various conditions.  The output will then be verified by
32b89a7cc2SEnji Cooper // googletest-output-test.py to ensure that Google Test generates the
33b89a7cc2SEnji Cooper // desired messages.  Therefore, most tests in this file are MEANT TO
34b89a7cc2SEnji Cooper // FAIL.
35b89a7cc2SEnji Cooper 
3628f6c2f2SEnji Cooper #include <stdlib.h>
3728f6c2f2SEnji Cooper 
3828f6c2f2SEnji Cooper #include <algorithm>
3928f6c2f2SEnji Cooper #include <string>
4028f6c2f2SEnji Cooper 
41b89a7cc2SEnji Cooper #include "gtest/gtest-spi.h"
42b89a7cc2SEnji Cooper #include "gtest/gtest.h"
43b89a7cc2SEnji Cooper #include "src/gtest-internal-inl.h"
44b89a7cc2SEnji Cooper 
45b89a7cc2SEnji Cooper GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */)
46b89a7cc2SEnji Cooper 
4728f6c2f2SEnji Cooper #ifdef GTEST_IS_THREADSAFE
48b89a7cc2SEnji Cooper using testing::ScopedFakeTestPartResultReporter;
49b89a7cc2SEnji Cooper using testing::TestPartResultArray;
50b89a7cc2SEnji Cooper 
51b89a7cc2SEnji Cooper using testing::internal::Notification;
52b89a7cc2SEnji Cooper using testing::internal::ThreadWithParam;
53b89a7cc2SEnji Cooper #endif
54b89a7cc2SEnji Cooper 
55b89a7cc2SEnji Cooper namespace posix = ::testing::internal::posix;
56b89a7cc2SEnji Cooper 
57b89a7cc2SEnji Cooper // Tests catching fatal failures.
58b89a7cc2SEnji Cooper 
59b89a7cc2SEnji Cooper // A subroutine used by the following test.
TestEq1(int x)6028f6c2f2SEnji Cooper void TestEq1(int x) { ASSERT_EQ(1, x); }
61b89a7cc2SEnji Cooper 
62b89a7cc2SEnji Cooper // This function calls a test subroutine, catches the fatal failure it
63b89a7cc2SEnji Cooper // generates, and then returns early.
TryTestSubroutine()64b89a7cc2SEnji Cooper void TryTestSubroutine() {
65b89a7cc2SEnji Cooper   // Calls a subrountine that yields a fatal failure.
66b89a7cc2SEnji Cooper   TestEq1(2);
67b89a7cc2SEnji Cooper 
68b89a7cc2SEnji Cooper   // Catches the fatal failure and aborts the test.
69b89a7cc2SEnji Cooper   //
70b89a7cc2SEnji Cooper   // The testing::Test:: prefix is necessary when calling
71b89a7cc2SEnji Cooper   // HasFatalFailure() outside of a TEST, TEST_F, or test fixture.
72b89a7cc2SEnji Cooper   if (testing::Test::HasFatalFailure()) return;
73b89a7cc2SEnji Cooper 
74b89a7cc2SEnji Cooper   // If we get here, something is wrong.
75b89a7cc2SEnji Cooper   FAIL() << "This should never be reached.";
76b89a7cc2SEnji Cooper }
77b89a7cc2SEnji Cooper 
TEST(PassingTest,PassingTest1)7828f6c2f2SEnji Cooper TEST(PassingTest, PassingTest1) {}
79b89a7cc2SEnji Cooper 
TEST(PassingTest,PassingTest2)8028f6c2f2SEnji Cooper TEST(PassingTest, PassingTest2) {}
81b89a7cc2SEnji Cooper 
82b89a7cc2SEnji Cooper // Tests that parameters of failing parameterized tests are printed in the
83b89a7cc2SEnji Cooper // failing test summary.
84b89a7cc2SEnji Cooper class FailingParamTest : public testing::TestWithParam<int> {};
85b89a7cc2SEnji Cooper 
TEST_P(FailingParamTest,Fails)8628f6c2f2SEnji Cooper TEST_P(FailingParamTest, Fails) { EXPECT_EQ(1, GetParam()); }
87b89a7cc2SEnji Cooper 
88b89a7cc2SEnji Cooper // This generates a test which will fail. Google Test is expected to print
89b89a7cc2SEnji Cooper // its parameter when it outputs the list of all failed tests.
9028f6c2f2SEnji Cooper INSTANTIATE_TEST_SUITE_P(PrintingFailingParams, FailingParamTest,
91b89a7cc2SEnji Cooper                          testing::Values(2));
92b89a7cc2SEnji Cooper 
9328f6c2f2SEnji Cooper // Tests that an empty value for the test suite basename yields just
9428f6c2f2SEnji Cooper // the test name without any prior /
9528f6c2f2SEnji Cooper class EmptyBasenameParamInst : public testing::TestWithParam<int> {};
9628f6c2f2SEnji Cooper 
TEST_P(EmptyBasenameParamInst,Passes)9728f6c2f2SEnji Cooper TEST_P(EmptyBasenameParamInst, Passes) { EXPECT_EQ(1, GetParam()); }
9828f6c2f2SEnji Cooper 
9928f6c2f2SEnji Cooper INSTANTIATE_TEST_SUITE_P(, EmptyBasenameParamInst, testing::Values(1));
10028f6c2f2SEnji Cooper 
101b89a7cc2SEnji Cooper static const char kGoldenString[] = "\"Line\0 1\"\nLine 2";
102b89a7cc2SEnji Cooper 
TEST(NonfatalFailureTest,EscapesStringOperands)103b89a7cc2SEnji Cooper TEST(NonfatalFailureTest, EscapesStringOperands) {
104b89a7cc2SEnji Cooper   std::string actual = "actual \"string\"";
105b89a7cc2SEnji Cooper   EXPECT_EQ(kGoldenString, actual);
106b89a7cc2SEnji Cooper 
107b89a7cc2SEnji Cooper   const char* golden = kGoldenString;
108b89a7cc2SEnji Cooper   EXPECT_EQ(golden, actual);
109b89a7cc2SEnji Cooper }
110b89a7cc2SEnji Cooper 
TEST(NonfatalFailureTest,DiffForLongStrings)111b89a7cc2SEnji Cooper TEST(NonfatalFailureTest, DiffForLongStrings) {
112b89a7cc2SEnji Cooper   std::string golden_str(kGoldenString, sizeof(kGoldenString) - 1);
113b89a7cc2SEnji Cooper   EXPECT_EQ(golden_str, "Line 2");
114b89a7cc2SEnji Cooper }
115b89a7cc2SEnji Cooper 
116b89a7cc2SEnji Cooper // Tests catching a fatal failure in a subroutine.
TEST(FatalFailureTest,FatalFailureInSubroutine)117b89a7cc2SEnji Cooper TEST(FatalFailureTest, FatalFailureInSubroutine) {
118b89a7cc2SEnji Cooper   printf("(expecting a failure that x should be 1)\n");
119b89a7cc2SEnji Cooper 
120b89a7cc2SEnji Cooper   TryTestSubroutine();
121b89a7cc2SEnji Cooper }
122b89a7cc2SEnji Cooper 
123b89a7cc2SEnji Cooper // Tests catching a fatal failure in a nested subroutine.
TEST(FatalFailureTest,FatalFailureInNestedSubroutine)124b89a7cc2SEnji Cooper TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
125b89a7cc2SEnji Cooper   printf("(expecting a failure that x should be 1)\n");
126b89a7cc2SEnji Cooper 
127b89a7cc2SEnji Cooper   // Calls a subrountine that yields a fatal failure.
128b89a7cc2SEnji Cooper   TryTestSubroutine();
129b89a7cc2SEnji Cooper 
130b89a7cc2SEnji Cooper   // Catches the fatal failure and aborts the test.
131b89a7cc2SEnji Cooper   //
132b89a7cc2SEnji Cooper   // When calling HasFatalFailure() inside a TEST, TEST_F, or test
133b89a7cc2SEnji Cooper   // fixture, the testing::Test:: prefix is not needed.
134b89a7cc2SEnji Cooper   if (HasFatalFailure()) return;
135b89a7cc2SEnji Cooper 
136b89a7cc2SEnji Cooper   // If we get here, something is wrong.
137b89a7cc2SEnji Cooper   FAIL() << "This should never be reached.";
138b89a7cc2SEnji Cooper }
139b89a7cc2SEnji Cooper 
140b89a7cc2SEnji Cooper // Tests HasFatalFailure() after a failed EXPECT check.
TEST(FatalFailureTest,NonfatalFailureInSubroutine)141b89a7cc2SEnji Cooper TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
142b89a7cc2SEnji Cooper   printf("(expecting a failure on false)\n");
143b89a7cc2SEnji Cooper   EXPECT_TRUE(false);               // Generates a nonfatal failure
144b89a7cc2SEnji Cooper   ASSERT_FALSE(HasFatalFailure());  // This should succeed.
145b89a7cc2SEnji Cooper }
146b89a7cc2SEnji Cooper 
147b89a7cc2SEnji Cooper // Tests interleaving user logging and Google Test assertions.
TEST(LoggingTest,InterleavingLoggingAndAssertions)148b89a7cc2SEnji Cooper TEST(LoggingTest, InterleavingLoggingAndAssertions) {
14928f6c2f2SEnji Cooper   static const int a[4] = {3, 9, 2, 6};
150b89a7cc2SEnji Cooper 
151b89a7cc2SEnji Cooper   printf("(expecting 2 failures on (3) >= (a[i]))\n");
152b89a7cc2SEnji Cooper   for (int i = 0; i < static_cast<int>(sizeof(a) / sizeof(*a)); i++) {
153b89a7cc2SEnji Cooper     printf("i == %d\n", i);
154b89a7cc2SEnji Cooper     EXPECT_GE(3, a[i]);
155b89a7cc2SEnji Cooper   }
156b89a7cc2SEnji Cooper }
157b89a7cc2SEnji Cooper 
158b89a7cc2SEnji Cooper // Tests the SCOPED_TRACE macro.
159b89a7cc2SEnji Cooper 
160b89a7cc2SEnji Cooper // A helper function for testing SCOPED_TRACE.
SubWithoutTrace(int n)161b89a7cc2SEnji Cooper void SubWithoutTrace(int n) {
162b89a7cc2SEnji Cooper   EXPECT_EQ(1, n);
163b89a7cc2SEnji Cooper   ASSERT_EQ(2, n);
164b89a7cc2SEnji Cooper }
165b89a7cc2SEnji Cooper 
166b89a7cc2SEnji Cooper // Another helper function for testing SCOPED_TRACE.
SubWithTrace(int n)167b89a7cc2SEnji Cooper void SubWithTrace(int n) {
168b89a7cc2SEnji Cooper   SCOPED_TRACE(testing::Message() << "n = " << n);
169b89a7cc2SEnji Cooper 
170b89a7cc2SEnji Cooper   SubWithoutTrace(n);
171b89a7cc2SEnji Cooper }
172b89a7cc2SEnji Cooper 
TEST(SCOPED_TRACETest,AcceptedValues)173b89a7cc2SEnji Cooper TEST(SCOPED_TRACETest, AcceptedValues) {
174b89a7cc2SEnji Cooper   SCOPED_TRACE("literal string");
175b89a7cc2SEnji Cooper   SCOPED_TRACE(std::string("std::string"));
176b89a7cc2SEnji Cooper   SCOPED_TRACE(1337);  // streamable type
17728f6c2f2SEnji Cooper   const char* null_value = nullptr;
178b89a7cc2SEnji Cooper   SCOPED_TRACE(null_value);
179b89a7cc2SEnji Cooper 
180b89a7cc2SEnji Cooper   ADD_FAILURE() << "Just checking that all these values work fine.";
181b89a7cc2SEnji Cooper }
182b89a7cc2SEnji Cooper 
183b89a7cc2SEnji Cooper // Tests that SCOPED_TRACE() obeys lexical scopes.
TEST(SCOPED_TRACETest,ObeysScopes)184b89a7cc2SEnji Cooper TEST(SCOPED_TRACETest, ObeysScopes) {
185b89a7cc2SEnji Cooper   printf("(expected to fail)\n");
186b89a7cc2SEnji Cooper 
187b89a7cc2SEnji Cooper   // There should be no trace before SCOPED_TRACE() is invoked.
188b89a7cc2SEnji Cooper   ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
189b89a7cc2SEnji Cooper 
190b89a7cc2SEnji Cooper   {
191b89a7cc2SEnji Cooper     SCOPED_TRACE("Expected trace");
192b89a7cc2SEnji Cooper     // After SCOPED_TRACE(), a failure in the current scope should contain
193b89a7cc2SEnji Cooper     // the trace.
194b89a7cc2SEnji Cooper     ADD_FAILURE() << "This failure is expected, and should have a trace.";
195b89a7cc2SEnji Cooper   }
196b89a7cc2SEnji Cooper 
197b89a7cc2SEnji Cooper   // Once the control leaves the scope of the SCOPED_TRACE(), there
198b89a7cc2SEnji Cooper   // should be no trace again.
199b89a7cc2SEnji Cooper   ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
200b89a7cc2SEnji Cooper }
201b89a7cc2SEnji Cooper 
202b89a7cc2SEnji Cooper // Tests that SCOPED_TRACE works inside a loop.
TEST(SCOPED_TRACETest,WorksInLoop)203b89a7cc2SEnji Cooper TEST(SCOPED_TRACETest, WorksInLoop) {
204b89a7cc2SEnji Cooper   printf("(expected to fail)\n");
205b89a7cc2SEnji Cooper 
206b89a7cc2SEnji Cooper   for (int i = 1; i <= 2; i++) {
207b89a7cc2SEnji Cooper     SCOPED_TRACE(testing::Message() << "i = " << i);
208b89a7cc2SEnji Cooper 
209b89a7cc2SEnji Cooper     SubWithoutTrace(i);
210b89a7cc2SEnji Cooper   }
211b89a7cc2SEnji Cooper }
212b89a7cc2SEnji Cooper 
213b89a7cc2SEnji Cooper // Tests that SCOPED_TRACE works in a subroutine.
TEST(SCOPED_TRACETest,WorksInSubroutine)214b89a7cc2SEnji Cooper TEST(SCOPED_TRACETest, WorksInSubroutine) {
215b89a7cc2SEnji Cooper   printf("(expected to fail)\n");
216b89a7cc2SEnji Cooper 
217b89a7cc2SEnji Cooper   SubWithTrace(1);
218b89a7cc2SEnji Cooper   SubWithTrace(2);
219b89a7cc2SEnji Cooper }
220b89a7cc2SEnji Cooper 
221b89a7cc2SEnji Cooper // Tests that SCOPED_TRACE can be nested.
TEST(SCOPED_TRACETest,CanBeNested)222b89a7cc2SEnji Cooper TEST(SCOPED_TRACETest, CanBeNested) {
223b89a7cc2SEnji Cooper   printf("(expected to fail)\n");
224b89a7cc2SEnji Cooper 
225b89a7cc2SEnji Cooper   SCOPED_TRACE("");  // A trace without a message.
226b89a7cc2SEnji Cooper 
227b89a7cc2SEnji Cooper   SubWithTrace(2);
228b89a7cc2SEnji Cooper }
229b89a7cc2SEnji Cooper 
230b89a7cc2SEnji Cooper // Tests that multiple SCOPED_TRACEs can be used in the same scope.
TEST(SCOPED_TRACETest,CanBeRepeated)231b89a7cc2SEnji Cooper TEST(SCOPED_TRACETest, CanBeRepeated) {
232b89a7cc2SEnji Cooper   printf("(expected to fail)\n");
233b89a7cc2SEnji Cooper 
234b89a7cc2SEnji Cooper   SCOPED_TRACE("A");
235b89a7cc2SEnji Cooper   ADD_FAILURE()
236b89a7cc2SEnji Cooper       << "This failure is expected, and should contain trace point A.";
237b89a7cc2SEnji Cooper 
238b89a7cc2SEnji Cooper   SCOPED_TRACE("B");
239b89a7cc2SEnji Cooper   ADD_FAILURE()
240b89a7cc2SEnji Cooper       << "This failure is expected, and should contain trace point A and B.";
241b89a7cc2SEnji Cooper 
242b89a7cc2SEnji Cooper   {
243b89a7cc2SEnji Cooper     SCOPED_TRACE("C");
244b89a7cc2SEnji Cooper     ADD_FAILURE() << "This failure is expected, and should "
245b89a7cc2SEnji Cooper                   << "contain trace point A, B, and C.";
246b89a7cc2SEnji Cooper   }
247b89a7cc2SEnji Cooper 
248b89a7cc2SEnji Cooper   SCOPED_TRACE("D");
249b89a7cc2SEnji Cooper   ADD_FAILURE() << "This failure is expected, and should "
250b89a7cc2SEnji Cooper                 << "contain trace point A, B, and D.";
251b89a7cc2SEnji Cooper }
252b89a7cc2SEnji Cooper 
25328f6c2f2SEnji Cooper #ifdef GTEST_IS_THREADSAFE
254b89a7cc2SEnji Cooper // Tests that SCOPED_TRACE()s can be used concurrently from multiple
255b89a7cc2SEnji Cooper // threads.  Namely, an assertion should be affected by
256b89a7cc2SEnji Cooper // SCOPED_TRACE()s in its own thread only.
257b89a7cc2SEnji Cooper 
258b89a7cc2SEnji Cooper // Here's the sequence of actions that happen in the test:
259b89a7cc2SEnji Cooper //
260b89a7cc2SEnji Cooper //   Thread A (main)                | Thread B (spawned)
261b89a7cc2SEnji Cooper //   ===============================|================================
262b89a7cc2SEnji Cooper //   spawns thread B                |
263b89a7cc2SEnji Cooper //   -------------------------------+--------------------------------
264b89a7cc2SEnji Cooper //   waits for n1                   | SCOPED_TRACE("Trace B");
265b89a7cc2SEnji Cooper //                                  | generates failure #1
266b89a7cc2SEnji Cooper //                                  | notifies n1
267b89a7cc2SEnji Cooper //   -------------------------------+--------------------------------
268b89a7cc2SEnji Cooper //   SCOPED_TRACE("Trace A");       | waits for n2
269b89a7cc2SEnji Cooper //   generates failure #2           |
270b89a7cc2SEnji Cooper //   notifies n2                    |
271b89a7cc2SEnji Cooper //   -------------------------------|--------------------------------
272b89a7cc2SEnji Cooper //   waits for n3                   | generates failure #3
273b89a7cc2SEnji Cooper //                                  | trace B dies
274b89a7cc2SEnji Cooper //                                  | generates failure #4
275b89a7cc2SEnji Cooper //                                  | notifies n3
276b89a7cc2SEnji Cooper //   -------------------------------|--------------------------------
277b89a7cc2SEnji Cooper //   generates failure #5           | finishes
278b89a7cc2SEnji Cooper //   trace A dies                   |
279b89a7cc2SEnji Cooper //   generates failure #6           |
280b89a7cc2SEnji Cooper //   -------------------------------|--------------------------------
281b89a7cc2SEnji Cooper //   waits for thread B to finish   |
282b89a7cc2SEnji Cooper 
283b89a7cc2SEnji Cooper struct CheckPoints {
284b89a7cc2SEnji Cooper   Notification n1;
285b89a7cc2SEnji Cooper   Notification n2;
286b89a7cc2SEnji Cooper   Notification n3;
287b89a7cc2SEnji Cooper };
288b89a7cc2SEnji Cooper 
ThreadWithScopedTrace(CheckPoints * check_points)289b89a7cc2SEnji Cooper static void ThreadWithScopedTrace(CheckPoints* check_points) {
290b89a7cc2SEnji Cooper   {
291b89a7cc2SEnji Cooper     SCOPED_TRACE("Trace B");
29228f6c2f2SEnji Cooper     ADD_FAILURE() << "Expected failure #1 (in thread B, only trace B alive).";
293b89a7cc2SEnji Cooper     check_points->n1.Notify();
294b89a7cc2SEnji Cooper     check_points->n2.WaitForNotification();
295b89a7cc2SEnji Cooper 
296b89a7cc2SEnji Cooper     ADD_FAILURE()
297b89a7cc2SEnji Cooper         << "Expected failure #3 (in thread B, trace A & B both alive).";
298b89a7cc2SEnji Cooper   }  // Trace B dies here.
29928f6c2f2SEnji Cooper   ADD_FAILURE() << "Expected failure #4 (in thread B, only trace A alive).";
300b89a7cc2SEnji Cooper   check_points->n3.Notify();
301b89a7cc2SEnji Cooper }
302b89a7cc2SEnji Cooper 
TEST(SCOPED_TRACETest,WorksConcurrently)303b89a7cc2SEnji Cooper TEST(SCOPED_TRACETest, WorksConcurrently) {
304b89a7cc2SEnji Cooper   printf("(expecting 6 failures)\n");
305b89a7cc2SEnji Cooper 
306b89a7cc2SEnji Cooper   CheckPoints check_points;
30728f6c2f2SEnji Cooper   ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace, &check_points,
30828f6c2f2SEnji Cooper                                        nullptr);
309b89a7cc2SEnji Cooper   check_points.n1.WaitForNotification();
310b89a7cc2SEnji Cooper 
311b89a7cc2SEnji Cooper   {
312b89a7cc2SEnji Cooper     SCOPED_TRACE("Trace A");
313b89a7cc2SEnji Cooper     ADD_FAILURE()
314b89a7cc2SEnji Cooper         << "Expected failure #2 (in thread A, trace A & B both alive).";
315b89a7cc2SEnji Cooper     check_points.n2.Notify();
316b89a7cc2SEnji Cooper     check_points.n3.WaitForNotification();
317b89a7cc2SEnji Cooper 
31828f6c2f2SEnji Cooper     ADD_FAILURE() << "Expected failure #5 (in thread A, only trace A alive).";
319b89a7cc2SEnji Cooper   }  // Trace A dies here.
32028f6c2f2SEnji Cooper   ADD_FAILURE() << "Expected failure #6 (in thread A, no trace alive).";
321b89a7cc2SEnji Cooper   thread.Join();
322b89a7cc2SEnji Cooper }
323b89a7cc2SEnji Cooper #endif  // GTEST_IS_THREADSAFE
324b89a7cc2SEnji Cooper 
325b89a7cc2SEnji Cooper // Tests basic functionality of the ScopedTrace utility (most of its features
326b89a7cc2SEnji Cooper // are already tested in SCOPED_TRACETest).
TEST(ScopedTraceTest,WithExplicitFileAndLine)327b89a7cc2SEnji Cooper TEST(ScopedTraceTest, WithExplicitFileAndLine) {
328b89a7cc2SEnji Cooper   testing::ScopedTrace trace("explicit_file.cc", 123, "expected trace message");
329b89a7cc2SEnji Cooper   ADD_FAILURE() << "Check that the trace is attached to a particular location.";
330b89a7cc2SEnji Cooper }
331b89a7cc2SEnji Cooper 
TEST(DisabledTestsWarningTest,DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning)332b89a7cc2SEnji Cooper TEST(DisabledTestsWarningTest,
333b89a7cc2SEnji Cooper      DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
334b89a7cc2SEnji Cooper   // This test body is intentionally empty.  Its sole purpose is for
335b89a7cc2SEnji Cooper   // verifying that the --gtest_also_run_disabled_tests flag
336b89a7cc2SEnji Cooper   // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of
337b89a7cc2SEnji Cooper   // the test output.
338b89a7cc2SEnji Cooper }
339b89a7cc2SEnji Cooper 
340b89a7cc2SEnji Cooper // Tests using assertions outside of TEST and TEST_F.
341b89a7cc2SEnji Cooper //
342b89a7cc2SEnji Cooper // This function creates two failures intentionally.
AdHocTest()343b89a7cc2SEnji Cooper void AdHocTest() {
344b89a7cc2SEnji Cooper   printf("The non-test part of the code is expected to have 2 failures.\n\n");
345b89a7cc2SEnji Cooper   EXPECT_TRUE(false);
346b89a7cc2SEnji Cooper   EXPECT_EQ(2, 3);
347b89a7cc2SEnji Cooper }
348b89a7cc2SEnji Cooper 
349b89a7cc2SEnji Cooper // Runs all TESTs, all TEST_Fs, and the ad hoc test.
RunAllTests()350b89a7cc2SEnji Cooper int RunAllTests() {
351b89a7cc2SEnji Cooper   AdHocTest();
352b89a7cc2SEnji Cooper   return RUN_ALL_TESTS();
353b89a7cc2SEnji Cooper }
354b89a7cc2SEnji Cooper 
355b89a7cc2SEnji Cooper // Tests non-fatal failures in the fixture constructor.
356b89a7cc2SEnji Cooper class NonFatalFailureInFixtureConstructorTest : public testing::Test {
357b89a7cc2SEnji Cooper  protected:
NonFatalFailureInFixtureConstructorTest()358b89a7cc2SEnji Cooper   NonFatalFailureInFixtureConstructorTest() {
359b89a7cc2SEnji Cooper     printf("(expecting 5 failures)\n");
360b89a7cc2SEnji Cooper     ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor.";
361b89a7cc2SEnji Cooper   }
362b89a7cc2SEnji Cooper 
~NonFatalFailureInFixtureConstructorTest()36328f6c2f2SEnji Cooper   ~NonFatalFailureInFixtureConstructorTest() override {
364b89a7cc2SEnji Cooper     ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor.";
365b89a7cc2SEnji Cooper   }
366b89a7cc2SEnji Cooper 
SetUp()36728f6c2f2SEnji Cooper   void SetUp() override { ADD_FAILURE() << "Expected failure #2, in SetUp()."; }
368b89a7cc2SEnji Cooper 
TearDown()36928f6c2f2SEnji Cooper   void TearDown() override {
370b89a7cc2SEnji Cooper     ADD_FAILURE() << "Expected failure #4, in TearDown.";
371b89a7cc2SEnji Cooper   }
372b89a7cc2SEnji Cooper };
373b89a7cc2SEnji Cooper 
TEST_F(NonFatalFailureInFixtureConstructorTest,FailureInConstructor)374b89a7cc2SEnji Cooper TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) {
375b89a7cc2SEnji Cooper   ADD_FAILURE() << "Expected failure #3, in the test body.";
376b89a7cc2SEnji Cooper }
377b89a7cc2SEnji Cooper 
378b89a7cc2SEnji Cooper // Tests fatal failures in the fixture constructor.
379b89a7cc2SEnji Cooper class FatalFailureInFixtureConstructorTest : public testing::Test {
380b89a7cc2SEnji Cooper  protected:
FatalFailureInFixtureConstructorTest()381b89a7cc2SEnji Cooper   FatalFailureInFixtureConstructorTest() {
382b89a7cc2SEnji Cooper     printf("(expecting 2 failures)\n");
383b89a7cc2SEnji Cooper     Init();
384b89a7cc2SEnji Cooper   }
385b89a7cc2SEnji Cooper 
~FatalFailureInFixtureConstructorTest()38628f6c2f2SEnji Cooper   ~FatalFailureInFixtureConstructorTest() override {
387b89a7cc2SEnji Cooper     ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor.";
388b89a7cc2SEnji Cooper   }
389b89a7cc2SEnji Cooper 
SetUp()39028f6c2f2SEnji Cooper   void SetUp() override {
391b89a7cc2SEnji Cooper     ADD_FAILURE() << "UNEXPECTED failure in SetUp().  "
392b89a7cc2SEnji Cooper                   << "We should never get here, as the test fixture c'tor "
393b89a7cc2SEnji Cooper                   << "had a fatal failure.";
394b89a7cc2SEnji Cooper   }
395b89a7cc2SEnji Cooper 
TearDown()39628f6c2f2SEnji Cooper   void TearDown() override {
397b89a7cc2SEnji Cooper     ADD_FAILURE() << "UNEXPECTED failure in TearDown().  "
398b89a7cc2SEnji Cooper                   << "We should never get here, as the test fixture c'tor "
399b89a7cc2SEnji Cooper                   << "had a fatal failure.";
400b89a7cc2SEnji Cooper   }
401b89a7cc2SEnji Cooper 
402b89a7cc2SEnji Cooper  private:
Init()40328f6c2f2SEnji Cooper   void Init() { FAIL() << "Expected failure #1, in the test fixture c'tor."; }
404b89a7cc2SEnji Cooper };
405b89a7cc2SEnji Cooper 
TEST_F(FatalFailureInFixtureConstructorTest,FailureInConstructor)406b89a7cc2SEnji Cooper TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) {
407b89a7cc2SEnji Cooper   ADD_FAILURE() << "UNEXPECTED failure in the test body.  "
408b89a7cc2SEnji Cooper                 << "We should never get here, as the test fixture c'tor "
409b89a7cc2SEnji Cooper                 << "had a fatal failure.";
410b89a7cc2SEnji Cooper }
411b89a7cc2SEnji Cooper 
412b89a7cc2SEnji Cooper // Tests non-fatal failures in SetUp().
413b89a7cc2SEnji Cooper class NonFatalFailureInSetUpTest : public testing::Test {
414b89a7cc2SEnji Cooper  protected:
~NonFatalFailureInSetUpTest()41528f6c2f2SEnji Cooper   ~NonFatalFailureInSetUpTest() override { Deinit(); }
416b89a7cc2SEnji Cooper 
SetUp()41728f6c2f2SEnji Cooper   void SetUp() override {
418b89a7cc2SEnji Cooper     printf("(expecting 4 failures)\n");
419b89a7cc2SEnji Cooper     ADD_FAILURE() << "Expected failure #1, in SetUp().";
420b89a7cc2SEnji Cooper   }
421b89a7cc2SEnji Cooper 
TearDown()42228f6c2f2SEnji Cooper   void TearDown() override { FAIL() << "Expected failure #3, in TearDown()."; }
42328f6c2f2SEnji Cooper 
424b89a7cc2SEnji Cooper  private:
Deinit()42528f6c2f2SEnji Cooper   void Deinit() { FAIL() << "Expected failure #4, in the test fixture d'tor."; }
426b89a7cc2SEnji Cooper };
427b89a7cc2SEnji Cooper 
TEST_F(NonFatalFailureInSetUpTest,FailureInSetUp)428b89a7cc2SEnji Cooper TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) {
429b89a7cc2SEnji Cooper   FAIL() << "Expected failure #2, in the test function.";
430b89a7cc2SEnji Cooper }
431b89a7cc2SEnji Cooper 
432b89a7cc2SEnji Cooper // Tests fatal failures in SetUp().
433b89a7cc2SEnji Cooper class FatalFailureInSetUpTest : public testing::Test {
434b89a7cc2SEnji Cooper  protected:
~FatalFailureInSetUpTest()43528f6c2f2SEnji Cooper   ~FatalFailureInSetUpTest() override { Deinit(); }
436b89a7cc2SEnji Cooper 
SetUp()43728f6c2f2SEnji Cooper   void SetUp() override {
438b89a7cc2SEnji Cooper     printf("(expecting 3 failures)\n");
439b89a7cc2SEnji Cooper     FAIL() << "Expected failure #1, in SetUp().";
440b89a7cc2SEnji Cooper   }
441b89a7cc2SEnji Cooper 
TearDown()44228f6c2f2SEnji Cooper   void TearDown() override { FAIL() << "Expected failure #2, in TearDown()."; }
44328f6c2f2SEnji Cooper 
444b89a7cc2SEnji Cooper  private:
Deinit()44528f6c2f2SEnji Cooper   void Deinit() { FAIL() << "Expected failure #3, in the test fixture d'tor."; }
446b89a7cc2SEnji Cooper };
447b89a7cc2SEnji Cooper 
TEST_F(FatalFailureInSetUpTest,FailureInSetUp)448b89a7cc2SEnji Cooper TEST_F(FatalFailureInSetUpTest, FailureInSetUp) {
449b89a7cc2SEnji Cooper   FAIL() << "UNEXPECTED failure in the test function.  "
450b89a7cc2SEnji Cooper          << "We should never get here, as SetUp() failed.";
451b89a7cc2SEnji Cooper }
452b89a7cc2SEnji Cooper 
TEST(AddFailureAtTest,MessageContainsSpecifiedFileAndLineNumber)453b89a7cc2SEnji Cooper TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
45428f6c2f2SEnji Cooper   ADD_FAILURE_AT("foo.cc", 42) << "Expected nonfatal failure in foo.cc";
455b89a7cc2SEnji Cooper }
456b89a7cc2SEnji Cooper 
TEST(GtestFailAtTest,MessageContainsSpecifiedFileAndLineNumber)45728f6c2f2SEnji Cooper TEST(GtestFailAtTest, MessageContainsSpecifiedFileAndLineNumber) {
45828f6c2f2SEnji Cooper   GTEST_FAIL_AT("foo.cc", 42) << "Expected fatal failure in foo.cc";
459b89a7cc2SEnji Cooper }
460b89a7cc2SEnji Cooper 
46128f6c2f2SEnji Cooper // The MixedUpTestSuiteTest test case verifies that Google Test will fail a
462b89a7cc2SEnji Cooper // test if it uses a different fixture class than what other tests in
463b89a7cc2SEnji Cooper // the same test case use.  It deliberately contains two fixture
464b89a7cc2SEnji Cooper // classes with the same name but defined in different namespaces.
465b89a7cc2SEnji Cooper 
46628f6c2f2SEnji Cooper // The MixedUpTestSuiteWithSameTestNameTest test case verifies that
467b89a7cc2SEnji Cooper // when the user defines two tests with the same test case name AND
468b89a7cc2SEnji Cooper // same test name (but in different namespaces), the second test will
469b89a7cc2SEnji Cooper // fail.
470b89a7cc2SEnji Cooper 
471b89a7cc2SEnji Cooper namespace foo {
472b89a7cc2SEnji Cooper 
47328f6c2f2SEnji Cooper class MixedUpTestSuiteTest : public testing::Test {};
474b89a7cc2SEnji Cooper 
TEST_F(MixedUpTestSuiteTest,FirstTestFromNamespaceFoo)47528f6c2f2SEnji Cooper TEST_F(MixedUpTestSuiteTest, FirstTestFromNamespaceFoo) {}
TEST_F(MixedUpTestSuiteTest,SecondTestFromNamespaceFoo)47628f6c2f2SEnji Cooper TEST_F(MixedUpTestSuiteTest, SecondTestFromNamespaceFoo) {}
477b89a7cc2SEnji Cooper 
47828f6c2f2SEnji Cooper class MixedUpTestSuiteWithSameTestNameTest : public testing::Test {};
479b89a7cc2SEnji Cooper 
TEST_F(MixedUpTestSuiteWithSameTestNameTest,TheSecondTestWithThisNameShouldFail)48028f6c2f2SEnji Cooper TEST_F(MixedUpTestSuiteWithSameTestNameTest,
481b89a7cc2SEnji Cooper        TheSecondTestWithThisNameShouldFail) {}
482b89a7cc2SEnji Cooper 
483b89a7cc2SEnji Cooper }  // namespace foo
484b89a7cc2SEnji Cooper 
485b89a7cc2SEnji Cooper namespace bar {
486b89a7cc2SEnji Cooper 
48728f6c2f2SEnji Cooper class MixedUpTestSuiteTest : public testing::Test {};
488b89a7cc2SEnji Cooper 
489b89a7cc2SEnji Cooper // The following two tests are expected to fail.  We rely on the
490b89a7cc2SEnji Cooper // golden file to check that Google Test generates the right error message.
TEST_F(MixedUpTestSuiteTest,ThisShouldFail)49128f6c2f2SEnji Cooper TEST_F(MixedUpTestSuiteTest, ThisShouldFail) {}
TEST_F(MixedUpTestSuiteTest,ThisShouldFailToo)49228f6c2f2SEnji Cooper TEST_F(MixedUpTestSuiteTest, ThisShouldFailToo) {}
493b89a7cc2SEnji Cooper 
49428f6c2f2SEnji Cooper class MixedUpTestSuiteWithSameTestNameTest : public testing::Test {};
495b89a7cc2SEnji Cooper 
496b89a7cc2SEnji Cooper // Expected to fail.  We rely on the golden file to check that Google Test
497b89a7cc2SEnji Cooper // generates the right error message.
TEST_F(MixedUpTestSuiteWithSameTestNameTest,TheSecondTestWithThisNameShouldFail)49828f6c2f2SEnji Cooper TEST_F(MixedUpTestSuiteWithSameTestNameTest,
499b89a7cc2SEnji Cooper        TheSecondTestWithThisNameShouldFail) {}
500b89a7cc2SEnji Cooper 
501b89a7cc2SEnji Cooper }  // namespace bar
502b89a7cc2SEnji Cooper 
503b89a7cc2SEnji Cooper // The following two test cases verify that Google Test catches the user
504b89a7cc2SEnji Cooper // error of mixing TEST and TEST_F in the same test case.  The first
505b89a7cc2SEnji Cooper // test case checks the scenario where TEST_F appears before TEST, and
506b89a7cc2SEnji Cooper // the second one checks where TEST appears before TEST_F.
507b89a7cc2SEnji Cooper 
50828f6c2f2SEnji Cooper class TEST_F_before_TEST_in_same_test_case : public testing::Test {};
509b89a7cc2SEnji Cooper 
TEST_F(TEST_F_before_TEST_in_same_test_case,DefinedUsingTEST_F)510b89a7cc2SEnji Cooper TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {}
511b89a7cc2SEnji Cooper 
512b89a7cc2SEnji Cooper // Expected to fail.  We rely on the golden file to check that Google Test
513b89a7cc2SEnji Cooper // generates the right error message.
TEST(TEST_F_before_TEST_in_same_test_case,DefinedUsingTESTAndShouldFail)514b89a7cc2SEnji Cooper TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {}
515b89a7cc2SEnji Cooper 
51628f6c2f2SEnji Cooper class TEST_before_TEST_F_in_same_test_case : public testing::Test {};
517b89a7cc2SEnji Cooper 
TEST(TEST_before_TEST_F_in_same_test_case,DefinedUsingTEST)518b89a7cc2SEnji Cooper TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {}
519b89a7cc2SEnji Cooper 
520b89a7cc2SEnji Cooper // Expected to fail.  We rely on the golden file to check that Google Test
521b89a7cc2SEnji Cooper // generates the right error message.
TEST_F(TEST_before_TEST_F_in_same_test_case,DefinedUsingTEST_FAndShouldFail)52228f6c2f2SEnji Cooper TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {}
523b89a7cc2SEnji Cooper 
524b89a7cc2SEnji Cooper // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE().
525b89a7cc2SEnji Cooper int global_integer = 0;
526b89a7cc2SEnji Cooper 
527b89a7cc2SEnji Cooper // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables.
TEST(ExpectNonfatalFailureTest,CanReferenceGlobalVariables)528b89a7cc2SEnji Cooper TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
529b89a7cc2SEnji Cooper   global_integer = 0;
53028f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
53128f6c2f2SEnji Cooper       { EXPECT_EQ(1, global_integer) << "Expected non-fatal failure."; },
53228f6c2f2SEnji Cooper       "Expected non-fatal failure.");
533b89a7cc2SEnji Cooper }
534b89a7cc2SEnji Cooper 
535b89a7cc2SEnji Cooper // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables
536b89a7cc2SEnji Cooper // (static or not).
TEST(ExpectNonfatalFailureTest,CanReferenceLocalVariables)537b89a7cc2SEnji Cooper TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
538b89a7cc2SEnji Cooper   int m = 0;
539b89a7cc2SEnji Cooper   static int n;
540b89a7cc2SEnji Cooper   n = 1;
54128f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE({ EXPECT_EQ(m, n) << "Expected non-fatal failure."; },
54228f6c2f2SEnji Cooper                           "Expected non-fatal failure.");
543b89a7cc2SEnji Cooper }
544b89a7cc2SEnji Cooper 
545b89a7cc2SEnji Cooper // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly
546b89a7cc2SEnji Cooper // one non-fatal failure and no fatal failure.
TEST(ExpectNonfatalFailureTest,SucceedsWhenThereIsOneNonfatalFailure)547b89a7cc2SEnji Cooper TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
54828f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE({ ADD_FAILURE() << "Expected non-fatal failure."; },
54928f6c2f2SEnji Cooper                           "Expected non-fatal failure.");
550b89a7cc2SEnji Cooper }
551b89a7cc2SEnji Cooper 
552b89a7cc2SEnji Cooper // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no
553b89a7cc2SEnji Cooper // non-fatal failure.
TEST(ExpectNonfatalFailureTest,FailsWhenThereIsNoNonfatalFailure)554b89a7cc2SEnji Cooper TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
555b89a7cc2SEnji Cooper   printf("(expecting a failure)\n");
55628f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE({}, "");
557b89a7cc2SEnji Cooper }
558b89a7cc2SEnji Cooper 
559b89a7cc2SEnji Cooper // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two
560b89a7cc2SEnji Cooper // non-fatal failures.
TEST(ExpectNonfatalFailureTest,FailsWhenThereAreTwoNonfatalFailures)561b89a7cc2SEnji Cooper TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
562b89a7cc2SEnji Cooper   printf("(expecting a failure)\n");
56328f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE(
56428f6c2f2SEnji Cooper       {
565b89a7cc2SEnji Cooper         ADD_FAILURE() << "Expected non-fatal failure 1.";
566b89a7cc2SEnji Cooper         ADD_FAILURE() << "Expected non-fatal failure 2.";
56728f6c2f2SEnji Cooper       },
56828f6c2f2SEnji Cooper       "");
569b89a7cc2SEnji Cooper }
570b89a7cc2SEnji Cooper 
571b89a7cc2SEnji Cooper // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal
572b89a7cc2SEnji Cooper // failure.
TEST(ExpectNonfatalFailureTest,FailsWhenThereIsOneFatalFailure)573b89a7cc2SEnji Cooper TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
574b89a7cc2SEnji Cooper   printf("(expecting a failure)\n");
57528f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE({ FAIL() << "Expected fatal failure."; }, "");
576b89a7cc2SEnji Cooper }
577b89a7cc2SEnji Cooper 
578b89a7cc2SEnji Cooper // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
579b89a7cc2SEnji Cooper // tested returns.
TEST(ExpectNonfatalFailureTest,FailsWhenStatementReturns)580b89a7cc2SEnji Cooper TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
581b89a7cc2SEnji Cooper   printf("(expecting a failure)\n");
58228f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE({ return; }, "");
583b89a7cc2SEnji Cooper }
584b89a7cc2SEnji Cooper 
585b89a7cc2SEnji Cooper #if GTEST_HAS_EXCEPTIONS
586b89a7cc2SEnji Cooper 
587b89a7cc2SEnji Cooper // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
588b89a7cc2SEnji Cooper // tested throws.
TEST(ExpectNonfatalFailureTest,FailsWhenStatementThrows)589b89a7cc2SEnji Cooper TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
590b89a7cc2SEnji Cooper   printf("(expecting a failure)\n");
591b89a7cc2SEnji Cooper   try {
59228f6c2f2SEnji Cooper     EXPECT_NONFATAL_FAILURE({ throw 0; }, "");
593b89a7cc2SEnji Cooper   } catch (int) {  // NOLINT
594b89a7cc2SEnji Cooper   }
595b89a7cc2SEnji Cooper }
596b89a7cc2SEnji Cooper 
597b89a7cc2SEnji Cooper #endif  // GTEST_HAS_EXCEPTIONS
598b89a7cc2SEnji Cooper 
599b89a7cc2SEnji Cooper // Tests that EXPECT_FATAL_FAILURE() can reference global variables.
TEST(ExpectFatalFailureTest,CanReferenceGlobalVariables)600b89a7cc2SEnji Cooper TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
601b89a7cc2SEnji Cooper   global_integer = 0;
60228f6c2f2SEnji Cooper   EXPECT_FATAL_FAILURE(
60328f6c2f2SEnji Cooper       { ASSERT_EQ(1, global_integer) << "Expected fatal failure."; },
60428f6c2f2SEnji Cooper       "Expected fatal failure.");
605b89a7cc2SEnji Cooper }
606b89a7cc2SEnji Cooper 
607b89a7cc2SEnji Cooper // Tests that EXPECT_FATAL_FAILURE() can reference local static
608b89a7cc2SEnji Cooper // variables.
TEST(ExpectFatalFailureTest,CanReferenceLocalStaticVariables)609b89a7cc2SEnji Cooper TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
610b89a7cc2SEnji Cooper   static int n;
611b89a7cc2SEnji Cooper   n = 1;
61228f6c2f2SEnji Cooper   EXPECT_FATAL_FAILURE({ ASSERT_EQ(0, n) << "Expected fatal failure."; },
61328f6c2f2SEnji Cooper                        "Expected fatal failure.");
614b89a7cc2SEnji Cooper }
615b89a7cc2SEnji Cooper 
616b89a7cc2SEnji Cooper // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly
617b89a7cc2SEnji Cooper // one fatal failure and no non-fatal failure.
TEST(ExpectFatalFailureTest,SucceedsWhenThereIsOneFatalFailure)618b89a7cc2SEnji Cooper TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
61928f6c2f2SEnji Cooper   EXPECT_FATAL_FAILURE({ FAIL() << "Expected fatal failure."; },
62028f6c2f2SEnji Cooper                        "Expected fatal failure.");
621b89a7cc2SEnji Cooper }
622b89a7cc2SEnji Cooper 
623b89a7cc2SEnji Cooper // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal
624b89a7cc2SEnji Cooper // failure.
TEST(ExpectFatalFailureTest,FailsWhenThereIsNoFatalFailure)625b89a7cc2SEnji Cooper TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
626b89a7cc2SEnji Cooper   printf("(expecting a failure)\n");
62728f6c2f2SEnji Cooper   EXPECT_FATAL_FAILURE({}, "");
628b89a7cc2SEnji Cooper }
629b89a7cc2SEnji Cooper 
630b89a7cc2SEnji Cooper // A helper for generating a fatal failure.
FatalFailure()63128f6c2f2SEnji Cooper void FatalFailure() { FAIL() << "Expected fatal failure."; }
632b89a7cc2SEnji Cooper 
633b89a7cc2SEnji Cooper // Tests that EXPECT_FATAL_FAILURE() fails when there are two
634b89a7cc2SEnji Cooper // fatal failures.
TEST(ExpectFatalFailureTest,FailsWhenThereAreTwoFatalFailures)635b89a7cc2SEnji Cooper TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
636b89a7cc2SEnji Cooper   printf("(expecting a failure)\n");
63728f6c2f2SEnji Cooper   EXPECT_FATAL_FAILURE(
63828f6c2f2SEnji Cooper       {
639b89a7cc2SEnji Cooper         FatalFailure();
640b89a7cc2SEnji Cooper         FatalFailure();
64128f6c2f2SEnji Cooper       },
64228f6c2f2SEnji Cooper       "");
643b89a7cc2SEnji Cooper }
644b89a7cc2SEnji Cooper 
645b89a7cc2SEnji Cooper // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal
646b89a7cc2SEnji Cooper // failure.
TEST(ExpectFatalFailureTest,FailsWhenThereIsOneNonfatalFailure)647b89a7cc2SEnji Cooper TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
648b89a7cc2SEnji Cooper   printf("(expecting a failure)\n");
64928f6c2f2SEnji Cooper   EXPECT_FATAL_FAILURE({ ADD_FAILURE() << "Expected non-fatal failure."; }, "");
650b89a7cc2SEnji Cooper }
651b89a7cc2SEnji Cooper 
652b89a7cc2SEnji Cooper // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
653b89a7cc2SEnji Cooper // tested returns.
TEST(ExpectFatalFailureTest,FailsWhenStatementReturns)654b89a7cc2SEnji Cooper TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
655b89a7cc2SEnji Cooper   printf("(expecting a failure)\n");
65628f6c2f2SEnji Cooper   EXPECT_FATAL_FAILURE({ return; }, "");
657b89a7cc2SEnji Cooper }
658b89a7cc2SEnji Cooper 
659b89a7cc2SEnji Cooper #if GTEST_HAS_EXCEPTIONS
660b89a7cc2SEnji Cooper 
661b89a7cc2SEnji Cooper // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
662b89a7cc2SEnji Cooper // tested throws.
TEST(ExpectFatalFailureTest,FailsWhenStatementThrows)663b89a7cc2SEnji Cooper TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
664b89a7cc2SEnji Cooper   printf("(expecting a failure)\n");
665b89a7cc2SEnji Cooper   try {
66628f6c2f2SEnji Cooper     EXPECT_FATAL_FAILURE({ throw 0; }, "");
667b89a7cc2SEnji Cooper   } catch (int) {  // NOLINT
668b89a7cc2SEnji Cooper   }
669b89a7cc2SEnji Cooper }
670b89a7cc2SEnji Cooper 
671b89a7cc2SEnji Cooper #endif  // GTEST_HAS_EXCEPTIONS
672b89a7cc2SEnji Cooper 
673b89a7cc2SEnji Cooper // This #ifdef block tests the output of value-parameterized tests.
674b89a7cc2SEnji Cooper 
ParamNameFunc(const testing::TestParamInfo<std::string> & info)675b89a7cc2SEnji Cooper std::string ParamNameFunc(const testing::TestParamInfo<std::string>& info) {
676b89a7cc2SEnji Cooper   return info.param;
677b89a7cc2SEnji Cooper }
678b89a7cc2SEnji Cooper 
67928f6c2f2SEnji Cooper class ParamTest : public testing::TestWithParam<std::string> {};
680b89a7cc2SEnji Cooper 
TEST_P(ParamTest,Success)68128f6c2f2SEnji Cooper TEST_P(ParamTest, Success) { EXPECT_EQ("a", GetParam()); }
682b89a7cc2SEnji Cooper 
TEST_P(ParamTest,Failure)68328f6c2f2SEnji Cooper TEST_P(ParamTest, Failure) { EXPECT_EQ("b", GetParam()) << "Expected failure"; }
684b89a7cc2SEnji Cooper 
68528f6c2f2SEnji Cooper INSTANTIATE_TEST_SUITE_P(PrintingStrings, ParamTest,
68628f6c2f2SEnji Cooper                          testing::Values(std::string("a")), ParamNameFunc);
687b89a7cc2SEnji Cooper 
68828f6c2f2SEnji Cooper // The case where a suite has INSTANTIATE_TEST_SUITE_P but not TEST_P.
68928f6c2f2SEnji Cooper using NoTests = ParamTest;
69028f6c2f2SEnji Cooper INSTANTIATE_TEST_SUITE_P(ThisIsOdd, NoTests, ::testing::Values("Hello"));
69128f6c2f2SEnji Cooper 
69228f6c2f2SEnji Cooper // fails under kErrorOnUninstantiatedParameterizedTest=true
69328f6c2f2SEnji Cooper class DetectNotInstantiatedTest : public testing::TestWithParam<int> {};
TEST_P(DetectNotInstantiatedTest,Used)69428f6c2f2SEnji Cooper TEST_P(DetectNotInstantiatedTest, Used) {}
69528f6c2f2SEnji Cooper 
69628f6c2f2SEnji Cooper // This would make the test failure from the above go away.
69728f6c2f2SEnji Cooper // INSTANTIATE_TEST_SUITE_P(Fix, DetectNotInstantiatedTest, testing::Values(1));
698b89a7cc2SEnji Cooper 
699b89a7cc2SEnji Cooper template <typename T>
70028f6c2f2SEnji Cooper class TypedTest : public testing::Test {};
701b89a7cc2SEnji Cooper 
70228f6c2f2SEnji Cooper TYPED_TEST_SUITE(TypedTest, testing::Types<int>);
703b89a7cc2SEnji Cooper 
TYPED_TEST(TypedTest,Success)70428f6c2f2SEnji Cooper TYPED_TEST(TypedTest, Success) { EXPECT_EQ(0, TypeParam()); }
705b89a7cc2SEnji Cooper 
TYPED_TEST(TypedTest,Failure)706b89a7cc2SEnji Cooper TYPED_TEST(TypedTest, Failure) {
707b89a7cc2SEnji Cooper   EXPECT_EQ(1, TypeParam()) << "Expected failure";
708b89a7cc2SEnji Cooper }
709b89a7cc2SEnji Cooper 
710b89a7cc2SEnji Cooper typedef testing::Types<char, int> TypesForTestWithNames;
711b89a7cc2SEnji Cooper 
712b89a7cc2SEnji Cooper template <typename T>
713b89a7cc2SEnji Cooper class TypedTestWithNames : public testing::Test {};
714b89a7cc2SEnji Cooper 
715b89a7cc2SEnji Cooper class TypedTestNames {
716b89a7cc2SEnji Cooper  public:
717b89a7cc2SEnji Cooper   template <typename T>
GetName(int i)718b89a7cc2SEnji Cooper   static std::string GetName(int i) {
71928f6c2f2SEnji Cooper     if (std::is_same<T, char>::value)
720b89a7cc2SEnji Cooper       return std::string("char") + ::testing::PrintToString(i);
72128f6c2f2SEnji Cooper     if (std::is_same<T, int>::value)
722b89a7cc2SEnji Cooper       return std::string("int") + ::testing::PrintToString(i);
723b89a7cc2SEnji Cooper   }
724b89a7cc2SEnji Cooper };
725b89a7cc2SEnji Cooper 
72628f6c2f2SEnji Cooper TYPED_TEST_SUITE(TypedTestWithNames, TypesForTestWithNames, TypedTestNames);
727b89a7cc2SEnji Cooper 
TYPED_TEST(TypedTestWithNames,Success)728b89a7cc2SEnji Cooper TYPED_TEST(TypedTestWithNames, Success) {}
729b89a7cc2SEnji Cooper 
TYPED_TEST(TypedTestWithNames,Failure)730b89a7cc2SEnji Cooper TYPED_TEST(TypedTestWithNames, Failure) { FAIL(); }
731b89a7cc2SEnji Cooper 
732b89a7cc2SEnji Cooper template <typename T>
73328f6c2f2SEnji Cooper class TypedTestP : public testing::Test {};
734b89a7cc2SEnji Cooper 
73528f6c2f2SEnji Cooper TYPED_TEST_SUITE_P(TypedTestP);
736b89a7cc2SEnji Cooper 
TYPED_TEST_P(TypedTestP,Success)73728f6c2f2SEnji Cooper TYPED_TEST_P(TypedTestP, Success) { EXPECT_EQ(0U, TypeParam()); }
738b89a7cc2SEnji Cooper 
TYPED_TEST_P(TypedTestP,Failure)739b89a7cc2SEnji Cooper TYPED_TEST_P(TypedTestP, Failure) {
740b89a7cc2SEnji Cooper   EXPECT_EQ(1U, TypeParam()) << "Expected failure";
741b89a7cc2SEnji Cooper }
742b89a7cc2SEnji Cooper 
74328f6c2f2SEnji Cooper REGISTER_TYPED_TEST_SUITE_P(TypedTestP, Success, Failure);
744b89a7cc2SEnji Cooper 
745b89a7cc2SEnji Cooper typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
74628f6c2f2SEnji Cooper INSTANTIATE_TYPED_TEST_SUITE_P(Unsigned, TypedTestP, UnsignedTypes);
747b89a7cc2SEnji Cooper 
748b89a7cc2SEnji Cooper class TypedTestPNames {
749b89a7cc2SEnji Cooper  public:
750b89a7cc2SEnji Cooper   template <typename T>
GetName(int i)751b89a7cc2SEnji Cooper   static std::string GetName(int i) {
75228f6c2f2SEnji Cooper     if (std::is_same<T, unsigned char>::value) {
753b89a7cc2SEnji Cooper       return std::string("unsignedChar") + ::testing::PrintToString(i);
754b89a7cc2SEnji Cooper     }
75528f6c2f2SEnji Cooper     if (std::is_same<T, unsigned int>::value) {
756b89a7cc2SEnji Cooper       return std::string("unsignedInt") + ::testing::PrintToString(i);
757b89a7cc2SEnji Cooper     }
758b89a7cc2SEnji Cooper   }
759b89a7cc2SEnji Cooper };
760b89a7cc2SEnji Cooper 
76128f6c2f2SEnji Cooper INSTANTIATE_TYPED_TEST_SUITE_P(UnsignedCustomName, TypedTestP, UnsignedTypes,
762b89a7cc2SEnji Cooper                                TypedTestPNames);
763b89a7cc2SEnji Cooper 
76428f6c2f2SEnji Cooper template <typename T>
76528f6c2f2SEnji Cooper class DetectNotInstantiatedTypesTest : public testing::Test {};
76628f6c2f2SEnji Cooper TYPED_TEST_SUITE_P(DetectNotInstantiatedTypesTest);
TYPED_TEST_P(DetectNotInstantiatedTypesTest,Used)76728f6c2f2SEnji Cooper TYPED_TEST_P(DetectNotInstantiatedTypesTest, Used) {
76828f6c2f2SEnji Cooper   TypeParam instantiate;
76928f6c2f2SEnji Cooper   (void)instantiate;
77028f6c2f2SEnji Cooper }
77128f6c2f2SEnji Cooper REGISTER_TYPED_TEST_SUITE_P(DetectNotInstantiatedTypesTest, Used);
772b89a7cc2SEnji Cooper 
77328f6c2f2SEnji Cooper // kErrorOnUninstantiatedTypeParameterizedTest=true would make the above fail.
77428f6c2f2SEnji Cooper // Adding the following would make that test failure go away.
77528f6c2f2SEnji Cooper //
77628f6c2f2SEnji Cooper // typedef ::testing::Types<char, int, unsigned int> MyTypes;
77728f6c2f2SEnji Cooper // INSTANTIATE_TYPED_TEST_SUITE_P(All, DetectNotInstantiatedTypesTest, MyTypes);
77828f6c2f2SEnji Cooper 
77928f6c2f2SEnji Cooper #ifdef GTEST_HAS_DEATH_TEST
780b89a7cc2SEnji Cooper 
781b89a7cc2SEnji Cooper // We rely on the golden file to verify that tests whose test case
782b89a7cc2SEnji Cooper // name ends with DeathTest are run first.
783b89a7cc2SEnji Cooper 
TEST(ADeathTest,ShouldRunFirst)78428f6c2f2SEnji Cooper TEST(ADeathTest, ShouldRunFirst) {}
785b89a7cc2SEnji Cooper 
786b89a7cc2SEnji Cooper // We rely on the golden file to verify that typed tests whose test
787b89a7cc2SEnji Cooper // case name ends with DeathTest are run first.
788b89a7cc2SEnji Cooper 
789b89a7cc2SEnji Cooper template <typename T>
79028f6c2f2SEnji Cooper class ATypedDeathTest : public testing::Test {};
791b89a7cc2SEnji Cooper 
792b89a7cc2SEnji Cooper typedef testing::Types<int, double> NumericTypes;
79328f6c2f2SEnji Cooper TYPED_TEST_SUITE(ATypedDeathTest, NumericTypes);
794b89a7cc2SEnji Cooper 
TYPED_TEST(ATypedDeathTest,ShouldRunFirst)79528f6c2f2SEnji Cooper TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {}
796b89a7cc2SEnji Cooper 
797b89a7cc2SEnji Cooper // We rely on the golden file to verify that type-parameterized tests
798b89a7cc2SEnji Cooper // whose test case name ends with DeathTest are run first.
799b89a7cc2SEnji Cooper 
800b89a7cc2SEnji Cooper template <typename T>
80128f6c2f2SEnji Cooper class ATypeParamDeathTest : public testing::Test {};
802b89a7cc2SEnji Cooper 
80328f6c2f2SEnji Cooper TYPED_TEST_SUITE_P(ATypeParamDeathTest);
804b89a7cc2SEnji Cooper 
TYPED_TEST_P(ATypeParamDeathTest,ShouldRunFirst)80528f6c2f2SEnji Cooper TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {}
806b89a7cc2SEnji Cooper 
80728f6c2f2SEnji Cooper REGISTER_TYPED_TEST_SUITE_P(ATypeParamDeathTest, ShouldRunFirst);
808b89a7cc2SEnji Cooper 
80928f6c2f2SEnji Cooper INSTANTIATE_TYPED_TEST_SUITE_P(My, ATypeParamDeathTest, NumericTypes);
810b89a7cc2SEnji Cooper 
811b89a7cc2SEnji Cooper #endif  // GTEST_HAS_DEATH_TEST
812b89a7cc2SEnji Cooper 
813b89a7cc2SEnji Cooper // Tests various failure conditions of
814b89a7cc2SEnji Cooper // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}.
815b89a7cc2SEnji Cooper class ExpectFailureTest : public testing::Test {
816b89a7cc2SEnji Cooper  public:  // Must be public and not protected due to a bug in g++ 3.4.2.
81728f6c2f2SEnji Cooper   enum FailureMode { FATAL_FAILURE, NONFATAL_FAILURE };
AddFailure(FailureMode failure)818b89a7cc2SEnji Cooper   static void AddFailure(FailureMode failure) {
819b89a7cc2SEnji Cooper     if (failure == FATAL_FAILURE) {
820b89a7cc2SEnji Cooper       FAIL() << "Expected fatal failure.";
821b89a7cc2SEnji Cooper     } else {
822b89a7cc2SEnji Cooper       ADD_FAILURE() << "Expected non-fatal failure.";
823b89a7cc2SEnji Cooper     }
824b89a7cc2SEnji Cooper   }
825b89a7cc2SEnji Cooper };
826b89a7cc2SEnji Cooper 
TEST_F(ExpectFailureTest,ExpectFatalFailure)827b89a7cc2SEnji Cooper TEST_F(ExpectFailureTest, ExpectFatalFailure) {
828b89a7cc2SEnji Cooper   // Expected fatal failure, but succeeds.
829b89a7cc2SEnji Cooper   printf("(expecting 1 failure)\n");
830b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure.");
831b89a7cc2SEnji Cooper   // Expected fatal failure, but got a non-fatal failure.
832b89a7cc2SEnji Cooper   printf("(expecting 1 failure)\n");
83328f6c2f2SEnji Cooper   EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE),
83428f6c2f2SEnji Cooper                        "Expected non-fatal "
835b89a7cc2SEnji Cooper                        "failure.");
836b89a7cc2SEnji Cooper   // Wrong message.
837b89a7cc2SEnji Cooper   printf("(expecting 1 failure)\n");
83828f6c2f2SEnji Cooper   EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE),
83928f6c2f2SEnji Cooper                        "Some other fatal failure "
840b89a7cc2SEnji Cooper                        "expected.");
841b89a7cc2SEnji Cooper }
842b89a7cc2SEnji Cooper 
TEST_F(ExpectFailureTest,ExpectNonFatalFailure)843b89a7cc2SEnji Cooper TEST_F(ExpectFailureTest, ExpectNonFatalFailure) {
844b89a7cc2SEnji Cooper   // Expected non-fatal failure, but succeeds.
845b89a7cc2SEnji Cooper   printf("(expecting 1 failure)\n");
846b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure.");
847b89a7cc2SEnji Cooper   // Expected non-fatal failure, but got a fatal failure.
848b89a7cc2SEnji Cooper   printf("(expecting 1 failure)\n");
849b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure.");
850b89a7cc2SEnji Cooper   // Wrong message.
851b89a7cc2SEnji Cooper   printf("(expecting 1 failure)\n");
85228f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE),
85328f6c2f2SEnji Cooper                           "Some other non-fatal "
854b89a7cc2SEnji Cooper                           "failure.");
855b89a7cc2SEnji Cooper }
856b89a7cc2SEnji Cooper 
85728f6c2f2SEnji Cooper #ifdef GTEST_IS_THREADSAFE
858b89a7cc2SEnji Cooper 
859b89a7cc2SEnji Cooper class ExpectFailureWithThreadsTest : public ExpectFailureTest {
860b89a7cc2SEnji Cooper  protected:
AddFailureInOtherThread(FailureMode failure)861b89a7cc2SEnji Cooper   static void AddFailureInOtherThread(FailureMode failure) {
86228f6c2f2SEnji Cooper     ThreadWithParam<FailureMode> thread(&AddFailure, failure, nullptr);
863b89a7cc2SEnji Cooper     thread.Join();
864b89a7cc2SEnji Cooper   }
865b89a7cc2SEnji Cooper };
866b89a7cc2SEnji Cooper 
TEST_F(ExpectFailureWithThreadsTest,ExpectFatalFailure)867b89a7cc2SEnji Cooper TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
868b89a7cc2SEnji Cooper   // We only intercept the current thread.
869b89a7cc2SEnji Cooper   printf("(expecting 2 failures)\n");
870b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE),
871b89a7cc2SEnji Cooper                        "Expected fatal failure.");
872b89a7cc2SEnji Cooper }
873b89a7cc2SEnji Cooper 
TEST_F(ExpectFailureWithThreadsTest,ExpectNonFatalFailure)874b89a7cc2SEnji Cooper TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
875b89a7cc2SEnji Cooper   // We only intercept the current thread.
876b89a7cc2SEnji Cooper   printf("(expecting 2 failures)\n");
877b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE),
878b89a7cc2SEnji Cooper                           "Expected non-fatal failure.");
879b89a7cc2SEnji Cooper }
880b89a7cc2SEnji Cooper 
881b89a7cc2SEnji Cooper typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
882b89a7cc2SEnji Cooper 
883b89a7cc2SEnji Cooper // Tests that the ScopedFakeTestPartResultReporter only catches failures from
884b89a7cc2SEnji Cooper // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD.
TEST_F(ScopedFakeTestPartResultReporterTest,InterceptOnlyCurrentThread)885b89a7cc2SEnji Cooper TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
886b89a7cc2SEnji Cooper   printf("(expecting 2 failures)\n");
887b89a7cc2SEnji Cooper   TestPartResultArray results;
888b89a7cc2SEnji Cooper   {
889b89a7cc2SEnji Cooper     ScopedFakeTestPartResultReporter reporter(
890b89a7cc2SEnji Cooper         ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
891b89a7cc2SEnji Cooper         &results);
892b89a7cc2SEnji Cooper     AddFailureInOtherThread(FATAL_FAILURE);
893b89a7cc2SEnji Cooper     AddFailureInOtherThread(NONFATAL_FAILURE);
894b89a7cc2SEnji Cooper   }
895b89a7cc2SEnji Cooper   // The two failures should not have been intercepted.
896b89a7cc2SEnji Cooper   EXPECT_EQ(0, results.size()) << "This shouldn't fail.";
897b89a7cc2SEnji Cooper }
898b89a7cc2SEnji Cooper 
899b89a7cc2SEnji Cooper #endif  // GTEST_IS_THREADSAFE
900b89a7cc2SEnji Cooper 
TEST_F(ExpectFailureTest,ExpectFatalFailureOnAllThreads)901b89a7cc2SEnji Cooper TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) {
902b89a7cc2SEnji Cooper   // Expected fatal failure, but succeeds.
903b89a7cc2SEnji Cooper   printf("(expecting 1 failure)\n");
904b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure.");
905b89a7cc2SEnji Cooper   // Expected fatal failure, but got a non-fatal failure.
906b89a7cc2SEnji Cooper   printf("(expecting 1 failure)\n");
907b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
908b89a7cc2SEnji Cooper                                       "Expected non-fatal failure.");
909b89a7cc2SEnji Cooper   // Wrong message.
910b89a7cc2SEnji Cooper   printf("(expecting 1 failure)\n");
911b89a7cc2SEnji Cooper   EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
912b89a7cc2SEnji Cooper                                       "Some other fatal failure expected.");
913b89a7cc2SEnji Cooper }
914b89a7cc2SEnji Cooper 
TEST_F(ExpectFailureTest,ExpectNonFatalFailureOnAllThreads)915b89a7cc2SEnji Cooper TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) {
916b89a7cc2SEnji Cooper   // Expected non-fatal failure, but succeeds.
917b89a7cc2SEnji Cooper   printf("(expecting 1 failure)\n");
91828f6c2f2SEnji Cooper   EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(),
91928f6c2f2SEnji Cooper                                          "Expected non-fatal "
920b89a7cc2SEnji Cooper                                          "failure.");
921b89a7cc2SEnji Cooper   // Expected non-fatal failure, but got a fatal failure.
922b89a7cc2SEnji Cooper   printf("(expecting 1 failure)\n");
923b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
924b89a7cc2SEnji Cooper                                          "Expected fatal failure.");
925b89a7cc2SEnji Cooper   // Wrong message.
926b89a7cc2SEnji Cooper   printf("(expecting 1 failure)\n");
927b89a7cc2SEnji Cooper   EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
928b89a7cc2SEnji Cooper                                          "Some other non-fatal failure.");
929b89a7cc2SEnji Cooper }
930b89a7cc2SEnji Cooper 
93128f6c2f2SEnji Cooper class DynamicFixture : public testing::Test {
93228f6c2f2SEnji Cooper  protected:
DynamicFixture()93328f6c2f2SEnji Cooper   DynamicFixture() { printf("DynamicFixture()\n"); }
~DynamicFixture()93428f6c2f2SEnji Cooper   ~DynamicFixture() override { printf("~DynamicFixture()\n"); }
SetUp()93528f6c2f2SEnji Cooper   void SetUp() override { printf("DynamicFixture::SetUp\n"); }
TearDown()93628f6c2f2SEnji Cooper   void TearDown() override { printf("DynamicFixture::TearDown\n"); }
93728f6c2f2SEnji Cooper 
SetUpTestSuite()93828f6c2f2SEnji Cooper   static void SetUpTestSuite() { printf("DynamicFixture::SetUpTestSuite\n"); }
TearDownTestSuite()93928f6c2f2SEnji Cooper   static void TearDownTestSuite() {
94028f6c2f2SEnji Cooper     printf("DynamicFixture::TearDownTestSuite\n");
94128f6c2f2SEnji Cooper   }
94228f6c2f2SEnji Cooper };
94328f6c2f2SEnji Cooper 
94428f6c2f2SEnji Cooper template <bool Pass>
94528f6c2f2SEnji Cooper class DynamicTest : public DynamicFixture {
94628f6c2f2SEnji Cooper  public:
TestBody()94728f6c2f2SEnji Cooper   void TestBody() override { EXPECT_TRUE(Pass); }
94828f6c2f2SEnji Cooper };
94928f6c2f2SEnji Cooper 
95028f6c2f2SEnji Cooper auto dynamic_test = (
95128f6c2f2SEnji Cooper     // Register two tests with the same fixture correctly.
95228f6c2f2SEnji Cooper     testing::RegisterTest(
95328f6c2f2SEnji Cooper         "DynamicFixture", "DynamicTestPass", nullptr, nullptr, __FILE__,
__anon752cb04b0102() 95428f6c2f2SEnji Cooper         __LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }),
95528f6c2f2SEnji Cooper     testing::RegisterTest(
95628f6c2f2SEnji Cooper         "DynamicFixture", "DynamicTestFail", nullptr, nullptr, __FILE__,
__anon752cb04b0202() 95728f6c2f2SEnji Cooper         __LINE__, []() -> DynamicFixture* { return new DynamicTest<false>; }),
95828f6c2f2SEnji Cooper 
95928f6c2f2SEnji Cooper     // Register the same fixture with another name. That's fine.
96028f6c2f2SEnji Cooper     testing::RegisterTest(
96128f6c2f2SEnji Cooper         "DynamicFixtureAnotherName", "DynamicTestPass", nullptr, nullptr,
96228f6c2f2SEnji Cooper         __FILE__, __LINE__,
__anon752cb04b0302() 96328f6c2f2SEnji Cooper         []() -> DynamicFixture* { return new DynamicTest<true>; }),
96428f6c2f2SEnji Cooper 
96528f6c2f2SEnji Cooper     // Register two tests with the same fixture incorrectly.
96628f6c2f2SEnji Cooper     testing::RegisterTest(
96728f6c2f2SEnji Cooper         "BadDynamicFixture1", "FixtureBase", nullptr, nullptr, __FILE__,
__anon752cb04b0402() 96828f6c2f2SEnji Cooper         __LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }),
96928f6c2f2SEnji Cooper     testing::RegisterTest(
97028f6c2f2SEnji Cooper         "BadDynamicFixture1", "TestBase", nullptr, nullptr, __FILE__, __LINE__,
__anon752cb04b0502() 97128f6c2f2SEnji Cooper         []() -> testing::Test* { return new DynamicTest<true>; }),
97228f6c2f2SEnji Cooper 
97328f6c2f2SEnji Cooper     // Register two tests with the same fixture incorrectly by omitting the
97428f6c2f2SEnji Cooper     // return type.
97528f6c2f2SEnji Cooper     testing::RegisterTest(
97628f6c2f2SEnji Cooper         "BadDynamicFixture2", "FixtureBase", nullptr, nullptr, __FILE__,
__anon752cb04b0602() 97728f6c2f2SEnji Cooper         __LINE__, []() -> DynamicFixture* { return new DynamicTest<true>; }),
97828f6c2f2SEnji Cooper     testing::RegisterTest("BadDynamicFixture2", "Derived", nullptr, nullptr,
97928f6c2f2SEnji Cooper                           __FILE__, __LINE__,
__anon752cb04b0702() 98028f6c2f2SEnji Cooper                           []() { return new DynamicTest<true>; }));
981b89a7cc2SEnji Cooper 
982b89a7cc2SEnji Cooper // Two test environments for testing testing::AddGlobalTestEnvironment().
983b89a7cc2SEnji Cooper 
984b89a7cc2SEnji Cooper class FooEnvironment : public testing::Environment {
985b89a7cc2SEnji Cooper  public:
SetUp()98628f6c2f2SEnji Cooper   void SetUp() override { printf("%s", "FooEnvironment::SetUp() called.\n"); }
987b89a7cc2SEnji Cooper 
TearDown()98828f6c2f2SEnji Cooper   void TearDown() override {
989b89a7cc2SEnji Cooper     printf("%s", "FooEnvironment::TearDown() called.\n");
990b89a7cc2SEnji Cooper     FAIL() << "Expected fatal failure.";
991b89a7cc2SEnji Cooper   }
992b89a7cc2SEnji Cooper };
993b89a7cc2SEnji Cooper 
994b89a7cc2SEnji Cooper class BarEnvironment : public testing::Environment {
995b89a7cc2SEnji Cooper  public:
SetUp()99628f6c2f2SEnji Cooper   void SetUp() override { printf("%s", "BarEnvironment::SetUp() called.\n"); }
997b89a7cc2SEnji Cooper 
TearDown()99828f6c2f2SEnji Cooper   void TearDown() override {
999b89a7cc2SEnji Cooper     printf("%s", "BarEnvironment::TearDown() called.\n");
1000b89a7cc2SEnji Cooper     ADD_FAILURE() << "Expected non-fatal failure.";
1001b89a7cc2SEnji Cooper   }
1002b89a7cc2SEnji Cooper };
1003b89a7cc2SEnji Cooper 
100428f6c2f2SEnji Cooper class TestSuiteThatFailsToSetUp : public testing::Test {
100528f6c2f2SEnji Cooper  public:
SetUpTestSuite()100628f6c2f2SEnji Cooper   static void SetUpTestSuite() { EXPECT_TRUE(false); }
100728f6c2f2SEnji Cooper };
TEST_F(TestSuiteThatFailsToSetUp,ShouldNotRun)100828f6c2f2SEnji Cooper TEST_F(TestSuiteThatFailsToSetUp, ShouldNotRun) { std::abort(); }
100928f6c2f2SEnji Cooper 
101028f6c2f2SEnji Cooper class TestSuiteThatSkipsInSetUp : public testing::Test {
101128f6c2f2SEnji Cooper  public:
SetUpTestSuite()101228f6c2f2SEnji Cooper   static void SetUpTestSuite() { GTEST_SKIP() << "Skip entire test suite"; }
101328f6c2f2SEnji Cooper };
TEST_F(TestSuiteThatSkipsInSetUp,ShouldNotRun)101428f6c2f2SEnji Cooper TEST_F(TestSuiteThatSkipsInSetUp, ShouldNotRun) { std::abort(); }
101528f6c2f2SEnji Cooper 
1016b89a7cc2SEnji Cooper // The main function.
1017b89a7cc2SEnji Cooper //
1018b89a7cc2SEnji Cooper // The idea is to use Google Test to run all the tests we have defined (some
1019b89a7cc2SEnji Cooper // of them are intended to fail), and then compare the test results
1020b89a7cc2SEnji Cooper // with the "golden" file.
main(int argc,char ** argv)1021b89a7cc2SEnji Cooper int main(int argc, char** argv) {
102228f6c2f2SEnji Cooper   GTEST_FLAG_SET(print_time, false);
1023b89a7cc2SEnji Cooper 
1024b89a7cc2SEnji Cooper   // We just run the tests, knowing some of them are intended to fail.
1025b89a7cc2SEnji Cooper   // We will use a separate Python script to compare the output of
1026b89a7cc2SEnji Cooper   // this program with the golden file.
1027b89a7cc2SEnji Cooper 
1028b89a7cc2SEnji Cooper   // It's hard to test InitGoogleTest() directly, as it has many
102928f6c2f2SEnji Cooper   // global side effects.  The following line serves as a test
1030b89a7cc2SEnji Cooper   // for it.
1031b89a7cc2SEnji Cooper   testing::InitGoogleTest(&argc, argv);
1032b89a7cc2SEnji Cooper   bool internal_skip_environment_and_ad_hoc_tests =
1033b89a7cc2SEnji Cooper       std::count(argv, argv + argc,
1034b89a7cc2SEnji Cooper                  std::string("internal_skip_environment_and_ad_hoc_tests")) > 0;
1035b89a7cc2SEnji Cooper 
103628f6c2f2SEnji Cooper #ifdef GTEST_HAS_DEATH_TEST
103728f6c2f2SEnji Cooper   if (!GTEST_FLAG_GET(internal_run_death_test).empty()) {
1038b89a7cc2SEnji Cooper     // Skip the usual output capturing if we're running as the child
1039b89a7cc2SEnji Cooper     // process of an threadsafe-style death test.
104028f6c2f2SEnji Cooper #if defined(GTEST_OS_WINDOWS)
1041b89a7cc2SEnji Cooper     posix::FReopen("nul:", "w", stdout);
1042b89a7cc2SEnji Cooper #else
1043b89a7cc2SEnji Cooper     posix::FReopen("/dev/null", "w", stdout);
1044b89a7cc2SEnji Cooper #endif  // GTEST_OS_WINDOWS
1045b89a7cc2SEnji Cooper     return RUN_ALL_TESTS();
1046b89a7cc2SEnji Cooper   }
1047b89a7cc2SEnji Cooper #endif  // GTEST_HAS_DEATH_TEST
1048b89a7cc2SEnji Cooper 
104928f6c2f2SEnji Cooper   if (internal_skip_environment_and_ad_hoc_tests) return RUN_ALL_TESTS();
1050b89a7cc2SEnji Cooper 
1051b89a7cc2SEnji Cooper   // Registers two global test environments.
1052b89a7cc2SEnji Cooper   // The golden file verifies that they are set up in the order they
1053b89a7cc2SEnji Cooper   // are registered, and torn down in the reverse order.
1054b89a7cc2SEnji Cooper   testing::AddGlobalTestEnvironment(new FooEnvironment);
1055b89a7cc2SEnji Cooper   testing::AddGlobalTestEnvironment(new BarEnvironment);
1056b89a7cc2SEnji Cooper   GTEST_DISABLE_MSC_WARNINGS_POP_()  //  4127
1057b89a7cc2SEnji Cooper   return RunAllTests();
1058b89a7cc2SEnji Cooper }
1059