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