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