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