aboutsummaryrefslogtreecommitdiffstats
path: root/gtest/test/gtest-death-test_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'gtest/test/gtest-death-test_test.cc')
-rw-r--r--gtest/test/gtest-death-test_test.cc82
1 files changed, 50 insertions, 32 deletions
diff --git a/gtest/test/gtest-death-test_test.cc b/gtest/test/gtest-death-test_test.cc
index ed5b53b..288c70a 100644
--- a/gtest/test/gtest-death-test_test.cc
+++ b/gtest/test/gtest-death-test_test.cc
@@ -103,21 +103,6 @@ class ReplaceDeathTestFactory {
} // namespace internal
} // namespace testing
-void DieInside(const char* function) {
- fprintf(stderr, "death inside %s().", function);
- fflush(stderr);
- // We call _exit() instead of exit(), as the former is a direct
- // system call and thus safer in the presence of threads. exit()
- // will invoke user-defined exit-hooks, which may do dangerous
- // things that conflict with death tests.
- //
- // Some compilers can recognize that _exit() never returns and issue the
- // 'unreachable code' warning for code following this function, unless
- // fooled by a fake condition.
- if (AlwaysTrue())
- _exit(1);
-}
-
// Tests that death tests work.
class TestForDeathTest : public testing::Test {
@@ -129,12 +114,23 @@ class TestForDeathTest : public testing::Test {
}
// A static member function that's expected to die.
- static void StaticMemberFunction() { DieInside("StaticMemberFunction"); }
+ static void StaticMemberFunction() {
+ fprintf(stderr, "%s", "death inside StaticMemberFunction().");
+ fflush(stderr);
+ // We call _exit() instead of exit(), as the former is a direct
+ // system call and thus safer in the presence of threads. exit()
+ // will invoke user-defined exit-hooks, which may do dangerous
+ // things that conflict with death tests.
+ _exit(1);
+ }
// A method of the test fixture that may die.
void MemberFunction() {
- if (should_die_)
- DieInside("MemberFunction");
+ if (should_die_) {
+ fprintf(stderr, "%s", "death inside MemberFunction().");
+ fflush(stderr);
+ _exit(1);
+ }
}
// True iff MemberFunction() should die.
@@ -149,8 +145,9 @@ class MayDie {
// A member function that may die.
void MemberFunction() const {
- if (should_die_)
- DieInside("MayDie::MemberFunction");
+ if (should_die_) {
+ GTEST_LOG_(FATAL) << "death inside MayDie::MemberFunction().";
+ }
}
private:
@@ -159,24 +156,27 @@ class MayDie {
};
// A global function that's expected to die.
-void GlobalFunction() { DieInside("GlobalFunction"); }
+void GlobalFunction() {
+ GTEST_LOG_(FATAL) << "death inside GlobalFunction().";
+}
// A non-void function that's expected to die.
int NonVoidFunction() {
- DieInside("NonVoidFunction");
+ GTEST_LOG_(FATAL) << "death inside NonVoidFunction().";
return 1;
}
// A unary function that may die.
void DieIf(bool should_die) {
- if (should_die)
- DieInside("DieIf");
+ if (should_die) {
+ GTEST_LOG_(FATAL) << "death inside DieIf().";
+ }
}
// A binary function that may die.
bool DieIfLessThan(int x, int y) {
if (x < y) {
- DieInside("DieIfLessThan");
+ GTEST_LOG_(FATAL) << "death inside DieIfLessThan().";
}
return true;
}
@@ -191,7 +191,7 @@ void DeathTestSubroutine() {
int DieInDebugElse12(int* sideeffect) {
if (sideeffect) *sideeffect = 12;
#ifndef NDEBUG
- DieInside("DieInDebugElse12");
+ GTEST_LOG_(FATAL) << "debug death inside DieInDebugElse12()";
#endif // NDEBUG
return 12;
}
@@ -410,7 +410,7 @@ void SetPthreadFlag() {
} // namespace
-#if GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
+#if GTEST_HAS_CLONE
TEST_F(TestForDeathTest, DoesNotExecuteAtforkHooks) {
if (!testing::GTEST_FLAG(death_test_use_fork)) {
@@ -422,7 +422,7 @@ TEST_F(TestForDeathTest, DoesNotExecuteAtforkHooks) {
}
}
-#endif // GTEST_HAS_CLONE && GTEST_HAS_PTHREAD
+#endif // GTEST_HAS_CLONE
// Tests that a method of another class can be used in a death test.
TEST_F(TestForDeathTest, MethodOfAnotherClass) {
@@ -449,8 +449,10 @@ TEST_F(TestForDeathTest, AcceptsAnythingConvertibleToRE) {
EXPECT_DEATH(GlobalFunction(), regex_str);
#endif // GTEST_HAS_GLOBAL_STRING
+#if GTEST_HAS_STD_STRING
const ::std::string regex_std_str(regex_c_str);
EXPECT_DEATH(GlobalFunction(), regex_std_str);
+#endif // GTEST_HAS_STD_STRING
}
// Tests that a non-void function can be used in a death test.
@@ -657,6 +659,24 @@ static void TestExitMacros() {
EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
ASSERT_EXIT(_exit(42), testing::ExitedWithCode(42), "");
+#if GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW
+ // MinGW (as of MinGW 5.1.6 and MSYS 1.0.11) does not tag crashed
+ // processes with non-zero exit code and does not honor calls to
+ // SetErrorMode(SEM_NOGPFAULTERRORBOX) that are supposed to suppress
+ // error pop-ups.
+ EXPECT_EXIT({
+ testing::GTEST_FLAG(catch_exceptions) = false;
+ *static_cast<int*>(NULL) = 1;
+ }, testing::ExitedWithCode(0xC0000005), "") << "foo";
+
+ EXPECT_NONFATAL_FAILURE({ // NOLINT
+ EXPECT_EXIT({
+ testing::GTEST_FLAG(catch_exceptions) = false;
+ *static_cast<int*>(NULL) = 1;
+ }, testing::ExitedWithCode(0), "") << "This failure is expected.";
+ }, "This failure is expected.");
+#endif // GTEST_OS_WINDOWS && !GTEST_OS_WINDOWS_MINGW
+
#if GTEST_OS_WINDOWS
// Of all signals effects on the process exit code, only those of SIGABRT
// are documented on Windows.
@@ -1111,10 +1131,8 @@ TEST(EnvironmentTest, HandleFitsIntoSizeT) {
// Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED trigger
// failures when death tests are available on the system.
TEST(ConditionalDeathMacrosDeathTest, ExpectsDeathWhenDeathTestsAvailable) {
- EXPECT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestExpectMacro"),
- "death inside CondDeathTestExpectMacro");
- ASSERT_DEATH_IF_SUPPORTED(DieInside("CondDeathTestAssertMacro"),
- "death inside CondDeathTestAssertMacro");
+ EXPECT_DEATH_IF_SUPPORTED(GTEST_CHECK_(false) << "failure", "false.*failure");
+ ASSERT_DEATH_IF_SUPPORTED(GTEST_CHECK_(false) << "failure", "false.*failure");
// Empty statement will not crash, which must trigger a failure.
EXPECT_NONFATAL_FAILURE(EXPECT_DEATH_IF_SUPPORTED(;, ""), "");