1 // Copyright 2005, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: wan@google.com (Zhanyong Wan)
32 // Tests for death tests.
34 #include "gtest/gtest-death-test.h"
35 #include "gtest/gtest.h"
36 #include "gtest/internal/gtest-filepath.h"
38 using testing::internal::AlwaysFalse
;
39 using testing::internal::AlwaysTrue
;
41 #if GTEST_HAS_DEATH_TEST
44 # include <direct.h> // For chdir().
47 # include <sys/wait.h> // For waitpid.
48 # endif // GTEST_OS_WINDOWS
55 # include <sys/time.h>
56 # endif // GTEST_OS_LINUX
58 # include "gtest/gtest-spi.h"
60 // Indicates that this translation unit is part of Google Test's
61 // implementation. It must come before gtest-internal-inl.h is
62 // included, or there will be a compiler error. This trick is to
63 // prevent a user from accidentally including gtest-internal-inl.h in
65 # define GTEST_IMPLEMENTATION_ 1
66 # include "src/gtest-internal-inl.h"
67 # undef GTEST_IMPLEMENTATION_
69 namespace posix
= ::testing::internal::posix
;
71 using testing::Message
;
72 using testing::internal::DeathTest
;
73 using testing::internal::DeathTestFactory
;
74 using testing::internal::FilePath
;
75 using testing::internal::GetLastErrnoDescription
;
76 using testing::internal::GetUnitTestImpl
;
77 using testing::internal::InDeathTestChild
;
78 using testing::internal::ParseNaturalNumber
;
83 // A helper class whose objects replace the death test factory for a
84 // single UnitTest object during their lifetimes.
85 class ReplaceDeathTestFactory
{
87 explicit ReplaceDeathTestFactory(DeathTestFactory
* new_factory
)
88 : unit_test_impl_(GetUnitTestImpl()) {
89 old_factory_
= unit_test_impl_
->death_test_factory_
.release();
90 unit_test_impl_
->death_test_factory_
.reset(new_factory
);
93 ~ReplaceDeathTestFactory() {
94 unit_test_impl_
->death_test_factory_
.release();
95 unit_test_impl_
->death_test_factory_
.reset(old_factory_
);
98 // Prevents copying ReplaceDeathTestFactory objects.
99 ReplaceDeathTestFactory(const ReplaceDeathTestFactory
&);
100 void operator=(const ReplaceDeathTestFactory
&);
102 UnitTestImpl
* unit_test_impl_
;
103 DeathTestFactory
* old_factory_
;
106 } // namespace internal
107 } // namespace testing
109 void DieWithMessage(const ::std::string
& message
) {
110 fprintf(stderr
, "%s", message
.c_str());
111 fflush(stderr
); // Make sure the text is printed before the process exits.
113 // We call _exit() instead of exit(), as the former is a direct
114 // system call and thus safer in the presence of threads. exit()
115 // will invoke user-defined exit-hooks, which may do dangerous
116 // things that conflict with death tests.
118 // Some compilers can recognize that _exit() never returns and issue the
119 // 'unreachable code' warning for code following this function, unless
120 // fooled by a fake condition.
125 void DieInside(const ::std::string
& function
) {
126 DieWithMessage("death inside " + function
+ "().");
129 // Tests that death tests work.
131 class TestForDeathTest
: public testing::Test
{
133 TestForDeathTest() : original_dir_(FilePath::GetCurrentDir()) {}
135 virtual ~TestForDeathTest() {
136 posix::ChDir(original_dir_
.c_str());
139 // A static member function that's expected to die.
140 static void StaticMemberFunction() { DieInside("StaticMemberFunction"); }
142 // A method of the test fixture that may die.
143 void MemberFunction() {
145 DieInside("MemberFunction");
148 // True iff MemberFunction() should die.
150 const FilePath original_dir_
;
153 // A class with a member function that may die.
156 explicit MayDie(bool should_die
) : should_die_(should_die
) {}
158 // A member function that may die.
159 void MemberFunction() const {
161 DieInside("MayDie::MemberFunction");
165 // True iff MemberFunction() should die.
169 // A global function that's expected to die.
170 void GlobalFunction() { DieInside("GlobalFunction"); }
172 // A non-void function that's expected to die.
173 int NonVoidFunction() {
174 DieInside("NonVoidFunction");
178 // A unary function that may die.
179 void DieIf(bool should_die
) {
184 // A binary function that may die.
185 bool DieIfLessThan(int x
, int y
) {
187 DieInside("DieIfLessThan");
192 // Tests that ASSERT_DEATH can be used outside a TEST, TEST_F, or test fixture.
193 void DeathTestSubroutine() {
194 EXPECT_DEATH(GlobalFunction(), "death.*GlobalFunction");
195 ASSERT_DEATH(GlobalFunction(), "death.*GlobalFunction");
198 // Death in dbg, not opt.
199 int DieInDebugElse12(int* sideeffect
) {
200 if (sideeffect
) *sideeffect
= 12;
204 DieInside("DieInDebugElse12");
211 # if GTEST_OS_WINDOWS
213 // Tests the ExitedWithCode predicate.
214 TEST(ExitStatusPredicateTest
, ExitedWithCode
) {
215 // On Windows, the process's exit code is the same as its exit status,
216 // so the predicate just compares the its input with its parameter.
217 EXPECT_TRUE(testing::ExitedWithCode(0)(0));
218 EXPECT_TRUE(testing::ExitedWithCode(1)(1));
219 EXPECT_TRUE(testing::ExitedWithCode(42)(42));
220 EXPECT_FALSE(testing::ExitedWithCode(0)(1));
221 EXPECT_FALSE(testing::ExitedWithCode(1)(0));
226 // Returns the exit status of a process that calls _exit(2) with a
227 // given exit code. This is a helper function for the
228 // ExitStatusPredicateTest test suite.
229 static int NormalExitStatus(int exit_code
) {
230 pid_t child_pid
= fork();
231 if (child_pid
== 0) {
235 waitpid(child_pid
, &status
, 0);
239 // Returns the exit status of a process that raises a given signal.
240 // If the signal does not cause the process to die, then it returns
241 // instead the exit status of a process that exits normally with exit
242 // code 1. This is a helper function for the ExitStatusPredicateTest
244 static int KilledExitStatus(int signum
) {
245 pid_t child_pid
= fork();
246 if (child_pid
== 0) {
251 waitpid(child_pid
, &status
, 0);
255 // Tests the ExitedWithCode predicate.
256 TEST(ExitStatusPredicateTest
, ExitedWithCode
) {
257 const int status0
= NormalExitStatus(0);
258 const int status1
= NormalExitStatus(1);
259 const int status42
= NormalExitStatus(42);
260 const testing::ExitedWithCode
pred0(0);
261 const testing::ExitedWithCode
pred1(1);
262 const testing::ExitedWithCode
pred42(42);
263 EXPECT_PRED1(pred0
, status0
);
264 EXPECT_PRED1(pred1
, status1
);
265 EXPECT_PRED1(pred42
, status42
);
266 EXPECT_FALSE(pred0(status1
));
267 EXPECT_FALSE(pred42(status0
));
268 EXPECT_FALSE(pred1(status42
));
271 // Tests the KilledBySignal predicate.
272 TEST(ExitStatusPredicateTest
, KilledBySignal
) {
273 const int status_segv
= KilledExitStatus(SIGSEGV
);
274 const int status_kill
= KilledExitStatus(SIGKILL
);
275 const testing::KilledBySignal
pred_segv(SIGSEGV
);
276 const testing::KilledBySignal
pred_kill(SIGKILL
);
277 EXPECT_PRED1(pred_segv
, status_segv
);
278 EXPECT_PRED1(pred_kill
, status_kill
);
279 EXPECT_FALSE(pred_segv(status_kill
));
280 EXPECT_FALSE(pred_kill(status_segv
));
283 # endif // GTEST_OS_WINDOWS
285 // Tests that the death test macros expand to code which may or may not
286 // be followed by operator<<, and that in either case the complete text
287 // comprises only a single C++ statement.
288 TEST_F(TestForDeathTest
, SingleStatement
) {
290 // This would fail if executed; this is a compilation test only
291 ASSERT_DEATH(return, "");
294 EXPECT_DEATH(_exit(1), "");
296 // This empty "else" branch is meant to ensure that EXPECT_DEATH
297 // doesn't expand into an "if" statement without an "else"
301 ASSERT_DEATH(return, "") << "did not die";
306 EXPECT_DEATH(_exit(1), "") << 1 << 2 << 3;
309 void DieWithEmbeddedNul() {
310 fprintf(stderr
, "Hello%cmy null world.\n", '\0');
316 // Tests that EXPECT_DEATH and ASSERT_DEATH work when the error
317 // message has a NUL character in it.
318 TEST_F(TestForDeathTest
, EmbeddedNulInMessage
) {
319 // TODO(wan@google.com): <regex.h> doesn't support matching strings
320 // with embedded NUL characters - find a way to workaround it.
321 EXPECT_DEATH(DieWithEmbeddedNul(), "my null world");
322 ASSERT_DEATH(DieWithEmbeddedNul(), "my null world");
324 # endif // GTEST_USES_PCRE
326 // Tests that death test macros expand to code which interacts well with switch
328 TEST_F(TestForDeathTest
, SwitchStatement
) {
329 // Microsoft compiler usually complains about switch statements without
330 // case labels. We suppress that warning for this test.
331 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4065)
335 ASSERT_DEATH(_exit(1), "") << "exit in default switch handler";
339 EXPECT_DEATH(_exit(1), "") << "exit in switch case";
341 GTEST_DISABLE_MSC_WARNINGS_POP_()
344 // Tests that a static member function can be used in a "fast" style
346 TEST_F(TestForDeathTest
, StaticMemberFunctionFastStyle
) {
347 testing::GTEST_FLAG(death_test_style
) = "fast";
348 ASSERT_DEATH(StaticMemberFunction(), "death.*StaticMember");
351 // Tests that a method of the test fixture can be used in a "fast"
353 TEST_F(TestForDeathTest
, MemberFunctionFastStyle
) {
354 testing::GTEST_FLAG(death_test_style
) = "fast";
356 EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction");
359 void ChangeToRootDir() { posix::ChDir(GTEST_PATH_SEP_
); }
361 // Tests that death tests work even if the current directory has been
363 TEST_F(TestForDeathTest
, FastDeathTestInChangedDir
) {
364 testing::GTEST_FLAG(death_test_style
) = "fast";
367 EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
370 ASSERT_DEATH(_exit(1), "");
374 void SigprofAction(int, siginfo_t
*, void*) { /* no op */ }
376 // Sets SIGPROF action and ITIMER_PROF timer (interval: 1ms).
377 void SetSigprofActionAndTimer() {
378 struct itimerval timer
;
379 timer
.it_interval
.tv_sec
= 0;
380 timer
.it_interval
.tv_usec
= 1;
381 timer
.it_value
= timer
.it_interval
;
382 ASSERT_EQ(0, setitimer(ITIMER_PROF
, &timer
, NULL
));
383 struct sigaction signal_action
;
384 memset(&signal_action
, 0, sizeof(signal_action
));
385 sigemptyset(&signal_action
.sa_mask
);
386 signal_action
.sa_sigaction
= SigprofAction
;
387 signal_action
.sa_flags
= SA_RESTART
| SA_SIGINFO
;
388 ASSERT_EQ(0, sigaction(SIGPROF
, &signal_action
, NULL
));
391 // Disables ITIMER_PROF timer and ignores SIGPROF signal.
392 void DisableSigprofActionAndTimer(struct sigaction
* old_signal_action
) {
393 struct itimerval timer
;
394 timer
.it_interval
.tv_sec
= 0;
395 timer
.it_interval
.tv_usec
= 0;
396 timer
.it_value
= timer
.it_interval
;
397 ASSERT_EQ(0, setitimer(ITIMER_PROF
, &timer
, NULL
));
398 struct sigaction signal_action
;
399 memset(&signal_action
, 0, sizeof(signal_action
));
400 sigemptyset(&signal_action
.sa_mask
);
401 signal_action
.sa_handler
= SIG_IGN
;
402 ASSERT_EQ(0, sigaction(SIGPROF
, &signal_action
, old_signal_action
));
405 // Tests that death tests work when SIGPROF handler and timer are set.
406 TEST_F(TestForDeathTest
, FastSigprofActionSet
) {
407 testing::GTEST_FLAG(death_test_style
) = "fast";
408 SetSigprofActionAndTimer();
409 EXPECT_DEATH(_exit(1), "");
410 struct sigaction old_signal_action
;
411 DisableSigprofActionAndTimer(&old_signal_action
);
412 EXPECT_TRUE(old_signal_action
.sa_sigaction
== SigprofAction
);
415 TEST_F(TestForDeathTest
, ThreadSafeSigprofActionSet
) {
416 testing::GTEST_FLAG(death_test_style
) = "threadsafe";
417 SetSigprofActionAndTimer();
418 EXPECT_DEATH(_exit(1), "");
419 struct sigaction old_signal_action
;
420 DisableSigprofActionAndTimer(&old_signal_action
);
421 EXPECT_TRUE(old_signal_action
.sa_sigaction
== SigprofAction
);
423 # endif // GTEST_OS_LINUX
425 // Repeats a representative sample of death tests in the "threadsafe" style:
427 TEST_F(TestForDeathTest
, StaticMemberFunctionThreadsafeStyle
) {
428 testing::GTEST_FLAG(death_test_style
) = "threadsafe";
429 ASSERT_DEATH(StaticMemberFunction(), "death.*StaticMember");
432 TEST_F(TestForDeathTest
, MemberFunctionThreadsafeStyle
) {
433 testing::GTEST_FLAG(death_test_style
) = "threadsafe";
435 EXPECT_DEATH(MemberFunction(), "inside.*MemberFunction");
438 TEST_F(TestForDeathTest
, ThreadsafeDeathTestInLoop
) {
439 testing::GTEST_FLAG(death_test_style
) = "threadsafe";
441 for (int i
= 0; i
< 3; ++i
)
442 EXPECT_EXIT(_exit(i
), testing::ExitedWithCode(i
), "") << ": i = " << i
;
445 TEST_F(TestForDeathTest
, ThreadsafeDeathTestInChangedDir
) {
446 testing::GTEST_FLAG(death_test_style
) = "threadsafe";
449 EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
452 ASSERT_DEATH(_exit(1), "");
455 TEST_F(TestForDeathTest
, MixedStyles
) {
456 testing::GTEST_FLAG(death_test_style
) = "threadsafe";
457 EXPECT_DEATH(_exit(1), "");
458 testing::GTEST_FLAG(death_test_style
) = "fast";
459 EXPECT_DEATH(_exit(1), "");
462 # if GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
468 void SetPthreadFlag() {
474 TEST_F(TestForDeathTest
, DoesNotExecuteAtforkHooks
) {
475 if (!testing::GTEST_FLAG(death_test_use_fork
)) {
476 testing::GTEST_FLAG(death_test_style
) = "threadsafe";
477 pthread_flag
= false;
478 ASSERT_EQ(0, pthread_atfork(&SetPthreadFlag
, NULL
, NULL
));
479 ASSERT_DEATH(_exit(1), "");
480 ASSERT_FALSE(pthread_flag
);
484 # endif // GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
486 // Tests that a method of another class can be used in a death test.
487 TEST_F(TestForDeathTest
, MethodOfAnotherClass
) {
488 const MayDie
x(true);
489 ASSERT_DEATH(x
.MemberFunction(), "MayDie\\:\\:MemberFunction");
492 // Tests that a global function can be used in a death test.
493 TEST_F(TestForDeathTest
, GlobalFunction
) {
494 EXPECT_DEATH(GlobalFunction(), "GlobalFunction");
497 // Tests that any value convertible to an RE works as a second
498 // argument to EXPECT_DEATH.
499 TEST_F(TestForDeathTest
, AcceptsAnythingConvertibleToRE
) {
500 static const char regex_c_str
[] = "GlobalFunction";
501 EXPECT_DEATH(GlobalFunction(), regex_c_str
);
503 const testing::internal::RE
regex(regex_c_str
);
504 EXPECT_DEATH(GlobalFunction(), regex
);
506 # if GTEST_HAS_GLOBAL_STRING
508 const string
regex_str(regex_c_str
);
509 EXPECT_DEATH(GlobalFunction(), regex_str
);
511 # endif // GTEST_HAS_GLOBAL_STRING
513 # if !GTEST_USES_PCRE
515 const ::std::string
regex_std_str(regex_c_str
);
516 EXPECT_DEATH(GlobalFunction(), regex_std_str
);
518 # endif // !GTEST_USES_PCRE
521 // Tests that a non-void function can be used in a death test.
522 TEST_F(TestForDeathTest
, NonVoidFunction
) {
523 ASSERT_DEATH(NonVoidFunction(), "NonVoidFunction");
526 // Tests that functions that take parameter(s) can be used in a death test.
527 TEST_F(TestForDeathTest
, FunctionWithParameter
) {
528 EXPECT_DEATH(DieIf(true), "DieIf\\(\\)");
529 EXPECT_DEATH(DieIfLessThan(2, 3), "DieIfLessThan");
532 // Tests that ASSERT_DEATH can be used outside a TEST, TEST_F, or test fixture.
533 TEST_F(TestForDeathTest
, OutsideFixture
) {
534 DeathTestSubroutine();
537 // Tests that death tests can be done inside a loop.
538 TEST_F(TestForDeathTest
, InsideLoop
) {
539 for (int i
= 0; i
< 5; i
++) {
540 EXPECT_DEATH(DieIfLessThan(-1, i
), "DieIfLessThan") << "where i == " << i
;
544 // Tests that a compound statement can be used in a death test.
545 TEST_F(TestForDeathTest
, CompoundStatement
) {
546 EXPECT_DEATH({ // NOLINT
554 // Tests that code that doesn't die causes a death test to fail.
555 TEST_F(TestForDeathTest
, DoesNotDie
) {
556 EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(DieIf(false), "DieIf"),
560 // Tests that a death test fails when the error message isn't expected.
561 TEST_F(TestForDeathTest
, ErrorMessageMismatch
) {
562 EXPECT_NONFATAL_FAILURE({ // NOLINT
563 EXPECT_DEATH(DieIf(true), "DieIfLessThan") << "End of death test message.";
564 }, "died but not with expected error");
567 // On exit, *aborted will be true iff the EXPECT_DEATH() statement
568 // aborted the function.
569 void ExpectDeathTestHelper(bool* aborted
) {
571 EXPECT_DEATH(DieIf(false), "DieIf"); // This assertion should fail.
575 // Tests that EXPECT_DEATH doesn't abort the test on failure.
576 TEST_F(TestForDeathTest
, EXPECT_DEATH
) {
578 EXPECT_NONFATAL_FAILURE(ExpectDeathTestHelper(&aborted
),
580 EXPECT_FALSE(aborted
);
583 // Tests that ASSERT_DEATH does abort the test on failure.
584 TEST_F(TestForDeathTest
, ASSERT_DEATH
) {
586 EXPECT_FATAL_FAILURE({ // NOLINT
588 ASSERT_DEATH(DieIf(false), "DieIf"); // This assertion should fail.
591 EXPECT_TRUE(aborted
);
594 // Tests that EXPECT_DEATH evaluates the arguments exactly once.
595 TEST_F(TestForDeathTest
, SingleEvaluation
) {
597 EXPECT_DEATH(DieIf((++x
) == 4), "DieIf");
599 const char* regex
= "DieIf";
600 const char* regex_save
= regex
;
601 EXPECT_DEATH(DieIfLessThan(3, 4), regex
++);
602 EXPECT_EQ(regex_save
+ 1, regex
);
605 // Tests that run-away death tests are reported as failures.
606 TEST_F(TestForDeathTest
, RunawayIsFailure
) {
607 EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(static_cast<void>(0), "Foo"),
611 // Tests that death tests report executing 'return' in the statement as
613 TEST_F(TestForDeathTest
, ReturnIsFailure
) {
614 EXPECT_FATAL_FAILURE(ASSERT_DEATH(return, "Bar"),
615 "illegal return in test statement.");
618 // Tests that EXPECT_DEBUG_DEATH works as expected, that is, you can stream a
619 // message to it, and in debug mode it:
620 // 1. Asserts on death.
621 // 2. Has no side effect.
623 // And in opt mode, it:
624 // 1. Has side effects but does not assert.
625 TEST_F(TestForDeathTest
, TestExpectDebugDeath
) {
628 EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect
), "death.*DieInDebugElse12")
629 << "Must accept a streamed message";
633 // Checks that the assignment occurs in opt mode (sideeffect).
634 EXPECT_EQ(12, sideeffect
);
638 // Checks that the assignment does not occur in dbg mode (no sideeffect).
639 EXPECT_EQ(0, sideeffect
);
644 // Tests that ASSERT_DEBUG_DEATH works as expected, that is, you can stream a
645 // message to it, and in debug mode it:
646 // 1. Asserts on death.
647 // 2. Has no side effect.
649 // And in opt mode, it:
650 // 1. Has side effects but does not assert.
651 TEST_F(TestForDeathTest
, TestAssertDebugDeath
) {
654 ASSERT_DEBUG_DEATH(DieInDebugElse12(&sideeffect
), "death.*DieInDebugElse12")
655 << "Must accept a streamed message";
659 // Checks that the assignment occurs in opt mode (sideeffect).
660 EXPECT_EQ(12, sideeffect
);
664 // Checks that the assignment does not occur in dbg mode (no sideeffect).
665 EXPECT_EQ(0, sideeffect
);
672 void ExpectDebugDeathHelper(bool* aborted
) {
674 EXPECT_DEBUG_DEATH(return, "") << "This is expected to fail.";
678 # if GTEST_OS_WINDOWS
679 TEST(PopUpDeathTest
, DoesNotShowPopUpOnAbort
) {
680 printf("This test should be considered failing if it shows "
681 "any pop-up dialogs.\n");
685 testing::GTEST_FLAG(catch_exceptions
) = false;
689 # endif // GTEST_OS_WINDOWS
691 // Tests that EXPECT_DEBUG_DEATH in debug mode does not abort
693 TEST_F(TestForDeathTest
, ExpectDebugDeathDoesNotAbort
) {
695 EXPECT_NONFATAL_FAILURE(ExpectDebugDeathHelper(&aborted
), "");
696 EXPECT_FALSE(aborted
);
699 void AssertDebugDeathHelper(bool* aborted
) {
701 GTEST_LOG_(INFO
) << "Before ASSERT_DEBUG_DEATH";
702 ASSERT_DEBUG_DEATH(GTEST_LOG_(INFO
) << "In ASSERT_DEBUG_DEATH"; return, "")
703 << "This is expected to fail.";
704 GTEST_LOG_(INFO
) << "After ASSERT_DEBUG_DEATH";
708 // Tests that ASSERT_DEBUG_DEATH in debug mode aborts the function on
710 TEST_F(TestForDeathTest
, AssertDebugDeathAborts
) {
713 EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted
), "");
714 EXPECT_TRUE(aborted
);
717 TEST_F(TestForDeathTest
, AssertDebugDeathAborts2
) {
720 EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted
), "");
721 EXPECT_TRUE(aborted
);
724 TEST_F(TestForDeathTest
, AssertDebugDeathAborts3
) {
727 EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted
), "");
728 EXPECT_TRUE(aborted
);
731 TEST_F(TestForDeathTest
, AssertDebugDeathAborts4
) {
734 EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted
), "");
735 EXPECT_TRUE(aborted
);
738 TEST_F(TestForDeathTest
, AssertDebugDeathAborts5
) {
741 EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted
), "");
742 EXPECT_TRUE(aborted
);
745 TEST_F(TestForDeathTest
, AssertDebugDeathAborts6
) {
748 EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted
), "");
749 EXPECT_TRUE(aborted
);
752 TEST_F(TestForDeathTest
, AssertDebugDeathAborts7
) {
755 EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted
), "");
756 EXPECT_TRUE(aborted
);
759 TEST_F(TestForDeathTest
, AssertDebugDeathAborts8
) {
762 EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted
), "");
763 EXPECT_TRUE(aborted
);
766 TEST_F(TestForDeathTest
, AssertDebugDeathAborts9
) {
769 EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted
), "");
770 EXPECT_TRUE(aborted
);
773 TEST_F(TestForDeathTest
, AssertDebugDeathAborts10
) {
776 EXPECT_FATAL_FAILURE(AssertDebugDeathHelper(&aborted
), "");
777 EXPECT_TRUE(aborted
);
782 // Tests the *_EXIT family of macros, using a variety of predicates.
783 static void TestExitMacros() {
784 EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
785 ASSERT_EXIT(_exit(42), testing::ExitedWithCode(42), "");
787 # if GTEST_OS_WINDOWS
789 // Of all signals effects on the process exit code, only those of SIGABRT
790 // are documented on Windows.
791 // See http://msdn.microsoft.com/en-us/library/dwwzkt4c(VS.71).aspx.
792 EXPECT_EXIT(raise(SIGABRT
), testing::ExitedWithCode(3), "") << "b_ar";
796 EXPECT_EXIT(raise(SIGKILL
), testing::KilledBySignal(SIGKILL
), "") << "foo";
797 ASSERT_EXIT(raise(SIGUSR2
), testing::KilledBySignal(SIGUSR2
), "") << "bar";
799 EXPECT_FATAL_FAILURE({ // NOLINT
800 ASSERT_EXIT(_exit(0), testing::KilledBySignal(SIGSEGV
), "")
801 << "This failure is expected, too.";
802 }, "This failure is expected, too.");
804 # endif // GTEST_OS_WINDOWS
806 EXPECT_NONFATAL_FAILURE({ // NOLINT
807 EXPECT_EXIT(raise(SIGSEGV
), testing::ExitedWithCode(0), "")
808 << "This failure is expected.";
809 }, "This failure is expected.");
812 TEST_F(TestForDeathTest
, ExitMacros
) {
816 TEST_F(TestForDeathTest
, ExitMacrosUsingFork
) {
817 testing::GTEST_FLAG(death_test_use_fork
) = true;
821 TEST_F(TestForDeathTest
, InvalidStyle
) {
822 testing::GTEST_FLAG(death_test_style
) = "rococo";
823 EXPECT_NONFATAL_FAILURE({ // NOLINT
824 EXPECT_DEATH(_exit(0), "") << "This failure is expected.";
825 }, "This failure is expected.");
828 TEST_F(TestForDeathTest
, DeathTestFailedOutput
) {
829 testing::GTEST_FLAG(death_test_style
) = "fast";
830 EXPECT_NONFATAL_FAILURE(
831 EXPECT_DEATH(DieWithMessage("death\n"),
834 "[ DEATH ] death\n");
837 TEST_F(TestForDeathTest
, DeathTestUnexpectedReturnOutput
) {
838 testing::GTEST_FLAG(death_test_style
) = "fast";
839 EXPECT_NONFATAL_FAILURE(
841 fprintf(stderr
, "returning\n");
845 " Result: illegal return in test statement.\n"
847 "[ DEATH ] returning\n");
850 TEST_F(TestForDeathTest
, DeathTestBadExitCodeOutput
) {
851 testing::GTEST_FLAG(death_test_style
) = "fast";
852 EXPECT_NONFATAL_FAILURE(
853 EXPECT_EXIT(DieWithMessage("exiting with rc 1\n"),
854 testing::ExitedWithCode(3),
856 " Result: died but not with expected exit code:\n"
857 " Exited with exit status 1\n"
859 "[ DEATH ] exiting with rc 1\n");
862 TEST_F(TestForDeathTest
, DeathTestMultiLineMatchFail
) {
863 testing::GTEST_FLAG(death_test_style
) = "fast";
864 EXPECT_NONFATAL_FAILURE(
865 EXPECT_DEATH(DieWithMessage("line 1\nline 2\nline 3\n"),
866 "line 1\nxyz\nline 3\n"),
870 "[ DEATH ] line 3\n");
873 TEST_F(TestForDeathTest
, DeathTestMultiLineMatchPass
) {
874 testing::GTEST_FLAG(death_test_style
) = "fast";
875 EXPECT_DEATH(DieWithMessage("line 1\nline 2\nline 3\n"),
876 "line 1\nline 2\nline 3\n");
879 // A DeathTestFactory that returns MockDeathTests.
880 class MockDeathTestFactory
: public DeathTestFactory
{
882 MockDeathTestFactory();
883 virtual bool Create(const char* statement
,
884 const ::testing::internal::RE
* regex
,
885 const char* file
, int line
, DeathTest
** test
);
887 // Sets the parameters for subsequent calls to Create.
888 void SetParameters(bool create
, DeathTest::TestRole role
,
889 int status
, bool passed
);
892 int AssumeRoleCalls() const { return assume_role_calls_
; }
893 int WaitCalls() const { return wait_calls_
; }
894 size_t PassedCalls() const { return passed_args_
.size(); }
895 bool PassedArgument(int n
) const { return passed_args_
[n
]; }
896 size_t AbortCalls() const { return abort_args_
.size(); }
897 DeathTest::AbortReason
AbortArgument(int n
) const {
898 return abort_args_
[n
];
900 bool TestDeleted() const { return test_deleted_
; }
903 friend class MockDeathTest
;
904 // If true, Create will return a MockDeathTest; otherwise it returns
907 // The value a MockDeathTest will return from its AssumeRole method.
908 DeathTest::TestRole role_
;
909 // The value a MockDeathTest will return from its Wait method.
911 // The value a MockDeathTest will return from its Passed method.
914 // Number of times AssumeRole was called.
915 int assume_role_calls_
;
916 // Number of times Wait was called.
918 // The arguments to the calls to Passed since the last call to
920 std::vector
<bool> passed_args_
;
921 // The arguments to the calls to Abort since the last call to
923 std::vector
<DeathTest::AbortReason
> abort_args_
;
924 // True if the last MockDeathTest returned by Create has been
930 // A DeathTest implementation useful in testing. It returns values set
931 // at its creation from its various inherited DeathTest methods, and
932 // reports calls to those methods to its parent MockDeathTestFactory
934 class MockDeathTest
: public DeathTest
{
936 MockDeathTest(MockDeathTestFactory
*parent
,
937 TestRole role
, int status
, bool passed
) :
938 parent_(parent
), role_(role
), status_(status
), passed_(passed
) {
940 virtual ~MockDeathTest() {
941 parent_
->test_deleted_
= true;
943 virtual TestRole
AssumeRole() {
944 ++parent_
->assume_role_calls_
;
948 ++parent_
->wait_calls_
;
951 virtual bool Passed(bool exit_status_ok
) {
952 parent_
->passed_args_
.push_back(exit_status_ok
);
955 virtual void Abort(AbortReason reason
) {
956 parent_
->abort_args_
.push_back(reason
);
960 MockDeathTestFactory
* const parent_
;
961 const TestRole role_
;
967 // MockDeathTestFactory constructor.
968 MockDeathTestFactory::MockDeathTestFactory()
970 role_(DeathTest::OVERSEE_TEST
),
973 assume_role_calls_(0),
980 // Sets the parameters for subsequent calls to Create.
981 void MockDeathTestFactory::SetParameters(bool create
,
982 DeathTest::TestRole role
,
983 int status
, bool passed
) {
989 assume_role_calls_
= 0;
991 passed_args_
.clear();
996 // Sets test to NULL (if create_ is false) or to the address of a new
997 // MockDeathTest object with parameters taken from the last call
998 // to SetParameters (if create_ is true). Always returns true.
999 bool MockDeathTestFactory::Create(const char* /*statement*/,
1000 const ::testing::internal::RE
* /*regex*/,
1001 const char* /*file*/,
1004 test_deleted_
= false;
1006 *test
= new MockDeathTest(this, role_
, status_
, passed_
);
1013 // A test fixture for testing the logic of the GTEST_DEATH_TEST_ macro.
1014 // It installs a MockDeathTestFactory that is used for the duration
1015 // of the test case.
1016 class MacroLogicDeathTest
: public testing::Test
{
1018 static testing::internal::ReplaceDeathTestFactory
* replacer_
;
1019 static MockDeathTestFactory
* factory_
;
1021 static void SetUpTestCase() {
1022 factory_
= new MockDeathTestFactory
;
1023 replacer_
= new testing::internal::ReplaceDeathTestFactory(factory_
);
1026 static void TearDownTestCase() {
1033 // Runs a death test that breaks the rules by returning. Such a death
1034 // test cannot be run directly from a test routine that uses a
1035 // MockDeathTest, or the remainder of the routine will not be executed.
1036 static void RunReturningDeathTest(bool* flag
) {
1037 ASSERT_DEATH({ // NOLINT
1044 testing::internal::ReplaceDeathTestFactory
* MacroLogicDeathTest::replacer_
1046 MockDeathTestFactory
* MacroLogicDeathTest::factory_
= NULL
;
1049 // Test that nothing happens when the factory doesn't return a DeathTest:
1050 TEST_F(MacroLogicDeathTest
, NothingHappens
) {
1052 factory_
->SetParameters(false, DeathTest::OVERSEE_TEST
, 0, true);
1053 EXPECT_DEATH(flag
= true, "");
1055 EXPECT_EQ(0, factory_
->AssumeRoleCalls());
1056 EXPECT_EQ(0, factory_
->WaitCalls());
1057 EXPECT_EQ(0U, factory_
->PassedCalls());
1058 EXPECT_EQ(0U, factory_
->AbortCalls());
1059 EXPECT_FALSE(factory_
->TestDeleted());
1062 // Test that the parent process doesn't run the death test code,
1063 // and that the Passed method returns false when the (simulated)
1064 // child process exits with status 0:
1065 TEST_F(MacroLogicDeathTest
, ChildExitsSuccessfully
) {
1067 factory_
->SetParameters(true, DeathTest::OVERSEE_TEST
, 0, true);
1068 EXPECT_DEATH(flag
= true, "");
1070 EXPECT_EQ(1, factory_
->AssumeRoleCalls());
1071 EXPECT_EQ(1, factory_
->WaitCalls());
1072 ASSERT_EQ(1U, factory_
->PassedCalls());
1073 EXPECT_FALSE(factory_
->PassedArgument(0));
1074 EXPECT_EQ(0U, factory_
->AbortCalls());
1075 EXPECT_TRUE(factory_
->TestDeleted());
1078 // Tests that the Passed method was given the argument "true" when
1079 // the (simulated) child process exits with status 1:
1080 TEST_F(MacroLogicDeathTest
, ChildExitsUnsuccessfully
) {
1082 factory_
->SetParameters(true, DeathTest::OVERSEE_TEST
, 1, true);
1083 EXPECT_DEATH(flag
= true, "");
1085 EXPECT_EQ(1, factory_
->AssumeRoleCalls());
1086 EXPECT_EQ(1, factory_
->WaitCalls());
1087 ASSERT_EQ(1U, factory_
->PassedCalls());
1088 EXPECT_TRUE(factory_
->PassedArgument(0));
1089 EXPECT_EQ(0U, factory_
->AbortCalls());
1090 EXPECT_TRUE(factory_
->TestDeleted());
1093 // Tests that the (simulated) child process executes the death test
1094 // code, and is aborted with the correct AbortReason if it
1095 // executes a return statement.
1096 TEST_F(MacroLogicDeathTest
, ChildPerformsReturn
) {
1098 factory_
->SetParameters(true, DeathTest::EXECUTE_TEST
, 0, true);
1099 RunReturningDeathTest(&flag
);
1101 EXPECT_EQ(1, factory_
->AssumeRoleCalls());
1102 EXPECT_EQ(0, factory_
->WaitCalls());
1103 EXPECT_EQ(0U, factory_
->PassedCalls());
1104 EXPECT_EQ(1U, factory_
->AbortCalls());
1105 EXPECT_EQ(DeathTest::TEST_ENCOUNTERED_RETURN_STATEMENT
,
1106 factory_
->AbortArgument(0));
1107 EXPECT_TRUE(factory_
->TestDeleted());
1110 // Tests that the (simulated) child process is aborted with the
1111 // correct AbortReason if it does not die.
1112 TEST_F(MacroLogicDeathTest
, ChildDoesNotDie
) {
1114 factory_
->SetParameters(true, DeathTest::EXECUTE_TEST
, 0, true);
1115 EXPECT_DEATH(flag
= true, "");
1117 EXPECT_EQ(1, factory_
->AssumeRoleCalls());
1118 EXPECT_EQ(0, factory_
->WaitCalls());
1119 EXPECT_EQ(0U, factory_
->PassedCalls());
1120 // This time there are two calls to Abort: one since the test didn't
1121 // die, and another from the ReturnSentinel when it's destroyed. The
1122 // sentinel normally isn't destroyed if a test doesn't die, since
1123 // _exit(2) is called in that case by ForkingDeathTest, but not by
1124 // our MockDeathTest.
1125 ASSERT_EQ(2U, factory_
->AbortCalls());
1126 EXPECT_EQ(DeathTest::TEST_DID_NOT_DIE
,
1127 factory_
->AbortArgument(0));
1128 EXPECT_EQ(DeathTest::TEST_ENCOUNTERED_RETURN_STATEMENT
,
1129 factory_
->AbortArgument(1));
1130 EXPECT_TRUE(factory_
->TestDeleted());
1133 // Tests that a successful death test does not register a successful
1135 TEST(SuccessRegistrationDeathTest
, NoSuccessPart
) {
1136 EXPECT_DEATH(_exit(1), "");
1137 EXPECT_EQ(0, GetUnitTestImpl()->current_test_result()->total_part_count());
1140 TEST(StreamingAssertionsDeathTest
, DeathTest
) {
1141 EXPECT_DEATH(_exit(1), "") << "unexpected failure";
1142 ASSERT_DEATH(_exit(1), "") << "unexpected failure";
1143 EXPECT_NONFATAL_FAILURE({ // NOLINT
1144 EXPECT_DEATH(_exit(0), "") << "expected failure";
1145 }, "expected failure");
1146 EXPECT_FATAL_FAILURE({ // NOLINT
1147 ASSERT_DEATH(_exit(0), "") << "expected failure";
1148 }, "expected failure");
1151 // Tests that GetLastErrnoDescription returns an empty string when the
1152 // last error is 0 and non-empty string when it is non-zero.
1153 TEST(GetLastErrnoDescription
, GetLastErrnoDescriptionWorks
) {
1155 EXPECT_STRNE("", GetLastErrnoDescription().c_str());
1157 EXPECT_STREQ("", GetLastErrnoDescription().c_str());
1160 # if GTEST_OS_WINDOWS
1161 TEST(AutoHandleTest
, AutoHandleWorks
) {
1162 HANDLE handle
= ::CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1163 ASSERT_NE(INVALID_HANDLE_VALUE
, handle
);
1165 // Tests that the AutoHandle is correctly initialized with a handle.
1166 testing::internal::AutoHandle
auto_handle(handle
);
1167 EXPECT_EQ(handle
, auto_handle
.Get());
1169 // Tests that Reset assigns INVALID_HANDLE_VALUE.
1170 // Note that this cannot verify whether the original handle is closed.
1171 auto_handle
.Reset();
1172 EXPECT_EQ(INVALID_HANDLE_VALUE
, auto_handle
.Get());
1174 // Tests that Reset assigns the new handle.
1175 // Note that this cannot verify whether the original handle is closed.
1176 handle
= ::CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
1177 ASSERT_NE(INVALID_HANDLE_VALUE
, handle
);
1178 auto_handle
.Reset(handle
);
1179 EXPECT_EQ(handle
, auto_handle
.Get());
1181 // Tests that AutoHandle contains INVALID_HANDLE_VALUE by default.
1182 testing::internal::AutoHandle auto_handle2
;
1183 EXPECT_EQ(INVALID_HANDLE_VALUE
, auto_handle2
.Get());
1185 # endif // GTEST_OS_WINDOWS
1187 # if GTEST_OS_WINDOWS
1188 typedef unsigned __int64 BiggestParsable
;
1189 typedef signed __int64 BiggestSignedParsable
;
1191 typedef unsigned long long BiggestParsable
;
1192 typedef signed long long BiggestSignedParsable
;
1193 # endif // GTEST_OS_WINDOWS
1195 // We cannot use std::numeric_limits<T>::max() as it clashes with the
1196 // max() macro defined by <windows.h>.
1197 const BiggestParsable kBiggestParsableMax
= ULLONG_MAX
;
1198 const BiggestSignedParsable kBiggestSignedParsableMax
= LLONG_MAX
;
1200 TEST(ParseNaturalNumberTest
, RejectsInvalidFormat
) {
1201 BiggestParsable result
= 0;
1203 // Rejects non-numbers.
1204 EXPECT_FALSE(ParseNaturalNumber("non-number string", &result
));
1206 // Rejects numbers with whitespace prefix.
1207 EXPECT_FALSE(ParseNaturalNumber(" 123", &result
));
1209 // Rejects negative numbers.
1210 EXPECT_FALSE(ParseNaturalNumber("-123", &result
));
1212 // Rejects numbers starting with a plus sign.
1213 EXPECT_FALSE(ParseNaturalNumber("+123", &result
));
1217 TEST(ParseNaturalNumberTest
, RejectsOverflownNumbers
) {
1218 BiggestParsable result
= 0;
1220 EXPECT_FALSE(ParseNaturalNumber("99999999999999999999999", &result
));
1222 signed char char_result
= 0;
1223 EXPECT_FALSE(ParseNaturalNumber("200", &char_result
));
1227 TEST(ParseNaturalNumberTest
, AcceptsValidNumbers
) {
1228 BiggestParsable result
= 0;
1231 ASSERT_TRUE(ParseNaturalNumber("123", &result
));
1232 EXPECT_EQ(123U, result
);
1234 // Check 0 as an edge case.
1236 ASSERT_TRUE(ParseNaturalNumber("0", &result
));
1237 EXPECT_EQ(0U, result
);
1240 ASSERT_TRUE(ParseNaturalNumber("00000", &result
));
1241 EXPECT_EQ(0U, result
);
1244 TEST(ParseNaturalNumberTest
, AcceptsTypeLimits
) {
1246 msg
<< kBiggestParsableMax
;
1248 BiggestParsable result
= 0;
1249 EXPECT_TRUE(ParseNaturalNumber(msg
.GetString(), &result
));
1250 EXPECT_EQ(kBiggestParsableMax
, result
);
1253 msg2
<< kBiggestSignedParsableMax
;
1255 BiggestSignedParsable signed_result
= 0;
1256 EXPECT_TRUE(ParseNaturalNumber(msg2
.GetString(), &signed_result
));
1257 EXPECT_EQ(kBiggestSignedParsableMax
, signed_result
);
1263 EXPECT_TRUE(ParseNaturalNumber(msg3
.GetString(), &int_result
));
1264 EXPECT_EQ(INT_MAX
, int_result
);
1269 unsigned int uint_result
= 0;
1270 EXPECT_TRUE(ParseNaturalNumber(msg4
.GetString(), &uint_result
));
1271 EXPECT_EQ(UINT_MAX
, uint_result
);
1274 TEST(ParseNaturalNumberTest
, WorksForShorterIntegers
) {
1275 short short_result
= 0;
1276 ASSERT_TRUE(ParseNaturalNumber("123", &short_result
));
1277 EXPECT_EQ(123, short_result
);
1279 signed char char_result
= 0;
1280 ASSERT_TRUE(ParseNaturalNumber("123", &char_result
));
1281 EXPECT_EQ(123, char_result
);
1284 # if GTEST_OS_WINDOWS
1285 TEST(EnvironmentTest
, HandleFitsIntoSizeT
) {
1286 // TODO(vladl@google.com): Remove this test after this condition is verified
1287 // in a static assertion in gtest-death-test.cc in the function
1288 // GetStatusFileDescriptor.
1289 ASSERT_TRUE(sizeof(HANDLE
) <= sizeof(size_t));
1291 # endif // GTEST_OS_WINDOWS
1293 // Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED trigger
1294 // failures when death tests are available on the system.
1295 TEST(ConditionalDeathMacrosDeathTest
, ExpectsDeathWhenDeathTestsAvailable
) {
1296 EXPECT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestExpectMacro"),
1297 "death inside CondDeathTestExpectMacro");
1298 ASSERT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestAssertMacro"),
1299 "death inside CondDeathTestAssertMacro");
1301 // Empty statement will not crash, which must trigger a failure.
1302 EXPECT_NONFATAL_FAILURE(EXPECT_DEATH_IF_SUPPORTED(;, ""), "");
1303 EXPECT_FATAL_FAILURE(ASSERT_DEATH_IF_SUPPORTED(;, ""), "");
1306 TEST(InDeathTestChildDeathTest
, ReportsDeathTestCorrectlyInFastStyle
) {
1307 testing::GTEST_FLAG(death_test_style
) = "fast";
1308 EXPECT_FALSE(InDeathTestChild());
1310 fprintf(stderr
, InDeathTestChild() ? "Inside" : "Outside");
1316 TEST(InDeathTestChildDeathTest
, ReportsDeathTestCorrectlyInThreadSafeStyle
) {
1317 testing::GTEST_FLAG(death_test_style
) = "threadsafe";
1318 EXPECT_FALSE(InDeathTestChild());
1320 fprintf(stderr
, InDeathTestChild() ? "Inside" : "Outside");
1326 #else // !GTEST_HAS_DEATH_TEST follows
1328 using testing::internal::CaptureStderr
;
1329 using testing::internal::GetCapturedStderr
;
1331 // Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED are still
1332 // defined but do not trigger failures when death tests are not available on
1334 TEST(ConditionalDeathMacrosTest
, WarnsWhenDeathTestsNotAvailable
) {
1335 // Empty statement will not crash, but that should not trigger a failure
1336 // when death tests are not supported.
1338 EXPECT_DEATH_IF_SUPPORTED(;, "");
1339 std::string output
= GetCapturedStderr();
1340 ASSERT_TRUE(NULL
!= strstr(output
.c_str(),
1341 "Death tests are not supported on this platform"));
1342 ASSERT_TRUE(NULL
!= strstr(output
.c_str(), ";"));
1344 // The streamed message should not be printed as there is no test failure.
1346 EXPECT_DEATH_IF_SUPPORTED(;, "") << "streamed message";
1347 output
= GetCapturedStderr();
1348 ASSERT_TRUE(NULL
== strstr(output
.c_str(), "streamed message"));
1351 ASSERT_DEATH_IF_SUPPORTED(;, ""); // NOLINT
1352 output
= GetCapturedStderr();
1353 ASSERT_TRUE(NULL
!= strstr(output
.c_str(),
1354 "Death tests are not supported on this platform"));
1355 ASSERT_TRUE(NULL
!= strstr(output
.c_str(), ";"));
1358 ASSERT_DEATH_IF_SUPPORTED(;, "") << "streamed message"; // NOLINT
1359 output
= GetCapturedStderr();
1360 ASSERT_TRUE(NULL
== strstr(output
.c_str(), "streamed message"));
1363 void FuncWithAssert(int* n
) {
1364 ASSERT_DEATH_IF_SUPPORTED(return;, "");
1368 // Tests that ASSERT_DEATH_IF_SUPPORTED does not return from the current
1369 // function (as ASSERT_DEATH does) if death tests are not supported.
1370 TEST(ConditionalDeathMacrosTest
, AssertDeatDoesNotReturnhIfUnsupported
) {
1376 #endif // !GTEST_HAS_DEATH_TEST
1378 // Tests that the death test macros expand to code which may or may not
1379 // be followed by operator<<, and that in either case the complete text
1380 // comprises only a single C++ statement.
1382 // The syntax should work whether death tests are available or not.
1383 TEST(ConditionalDeathMacrosSyntaxDeathTest
, SingleStatement
) {
1385 // This would fail if executed; this is a compilation test only
1386 ASSERT_DEATH_IF_SUPPORTED(return, "");
1389 EXPECT_DEATH_IF_SUPPORTED(_exit(1), "");
1391 // This empty "else" branch is meant to ensure that EXPECT_DEATH
1392 // doesn't expand into an "if" statement without an "else"
1396 ASSERT_DEATH_IF_SUPPORTED(return, "") << "did not die";
1401 EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << 1 << 2 << 3;
1404 // Tests that conditional death test macros expand to code which interacts
1405 // well with switch statements.
1406 TEST(ConditionalDeathMacrosSyntaxDeathTest
, SwitchStatement
) {
1407 // Microsoft compiler usually complains about switch statements without
1408 // case labels. We suppress that warning for this test.
1409 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4065)
1413 ASSERT_DEATH_IF_SUPPORTED(_exit(1), "")
1414 << "exit in default switch handler";
1418 EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << "exit in switch case";
1420 GTEST_DISABLE_MSC_WARNINGS_POP_()
1423 // Tests that a test case whose name ends with "DeathTest" works fine
1425 TEST(NotADeathTest
, Test
) {