Improves EXPECT_DEATH_IF_SUPPORTED to allow streaming of messages and enforcing the validity of arguments (by Vlad Losev); adds samples for the event listener API (by Vlad Losev); simplifies the tests using EXPECT_DEATH_IF_SUPPORTED (by Zhanyong Wan).

This commit is contained in:
zhanyong.wan
2009-09-11 06:59:42 +00:00
parent f6dd67a155
commit b2ee82ebf9
12 changed files with 497 additions and 41 deletions

View File

@@ -1136,7 +1136,7 @@ using testing::internal::GetCapturedStderr;
using testing::internal::String;
// Tests that EXPECT_DEATH_IF_SUPPORTED/ASSERT_DEATH_IF_SUPPORTED are still
// defined but do not rigger failures when death tests are not available on
// defined but do not trigger failures when death tests are not available on
// the system.
TEST(ConditionalDeathMacrosTest, WarnsWhenDeathTestsNotAvailable) {
// Empty statement will not crash, but that should not trigger a failure
@@ -1148,16 +1148,89 @@ TEST(ConditionalDeathMacrosTest, WarnsWhenDeathTestsNotAvailable) {
"Death tests are not supported on this platform"));
ASSERT_TRUE(NULL != strstr(output.c_str(), ";"));
// The streamed message should not be printed as there is no test failure.
CaptureStderr();
ASSERT_DEATH_IF_SUPPORTED(;, "");
EXPECT_DEATH_IF_SUPPORTED(;, "") << "streamed message";
output = GetCapturedStderr();
ASSERT_TRUE(NULL == strstr(output.c_str(), "streamed message"));
CaptureStderr();
ASSERT_DEATH_IF_SUPPORTED(;, ""); // NOLINT
output = GetCapturedStderr();
ASSERT_TRUE(NULL != strstr(output.c_str(),
"Death tests are not supported on this platform"));
ASSERT_TRUE(NULL != strstr(output.c_str(), ";"));
CaptureStderr();
ASSERT_DEATH_IF_SUPPORTED(;, "") << "streamed message"; // NOLINT
output = GetCapturedStderr();
ASSERT_TRUE(NULL == strstr(output.c_str(), "streamed message"));
}
void FuncWithAssert(int* n) {
ASSERT_DEATH_IF_SUPPORTED(return;, "");
(*n)++;
}
// Tests that ASSERT_DEATH_IF_SUPPORTED does not return from the current
// function (as ASSERT_DEATH does) if death tests are not supported.
TEST(ConditionalDeathMacrosTest, AssertDeatDoesNotReturnhIfUnsupported) {
int n = 0;
FuncWithAssert(&n);
EXPECT_EQ(1, n);
}
#endif // GTEST_HAS_DEATH_TEST
// Tests that the death test macros expand to code which may or may not
// be followed by operator<<, and that in either case the complete text
// comprises only a single C++ statement.
//
// The syntax should work whether death tests are available or not.
TEST(ConditionalDeathMacrosSyntaxDeathTest, SingleStatement) {
if (false)
// This would fail if executed; this is a compilation test only
ASSERT_DEATH_IF_SUPPORTED(return, "");
if (true)
EXPECT_DEATH_IF_SUPPORTED(_exit(1), "");
else
// This empty "else" branch is meant to ensure that EXPECT_DEATH
// doesn't expand into an "if" statement without an "else"
; // NOLINT
if (false)
ASSERT_DEATH_IF_SUPPORTED(return, "") << "did not die";
if (false)
; // NOLINT
else
EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << 1 << 2 << 3;
}
// Tests that conditional death test macros expand to code which interacts
// well with switch statements.
TEST(ConditionalDeathMacrosSyntaxDeathTest, SwitchStatement) {
// Microsoft compiler usually complains about switch statements without
// case labels. We suppress that warning for this test.
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4065)
#endif // _MSC_VER
switch (0)
default:
ASSERT_DEATH_IF_SUPPORTED(_exit(1), "")
<< "exit in default switch handler";
switch (0)
case 0:
EXPECT_DEATH_IF_SUPPORTED(_exit(1), "") << "exit in switch case";
#ifdef _MSC_VER
#pragma warning(pop)
#endif // _MSC_VER
}
// Tests that a test case whose name ends with "DeathTest" works fine
// on Windows.
TEST(NotADeathTest, Test) {

View File

@@ -133,8 +133,6 @@ TEST(GetThreadCountTest, ReturnsZeroWhenUnableToCountThreads) {
}
#endif // GTEST_OS_MAC
#if GTEST_HAS_DEATH_TEST
TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
const bool a_false_condition = false;
const char regex[] =
@@ -145,9 +143,12 @@ TEST(GtestCheckDeathTest, DiesWithCorrectOutputOnFailure) {
#endif // _MSC_VER
".*a_false_condition.*Extra info.*";
EXPECT_DEATH(GTEST_CHECK_(a_false_condition) << "Extra info", regex);
EXPECT_DEATH_IF_SUPPORTED(GTEST_CHECK_(a_false_condition) << "Extra info",
regex);
}
#if GTEST_HAS_DEATH_TEST
TEST(GtestCheckDeathTest, LivesSilentlyOnSuccess) {
EXPECT_EXIT({
GTEST_CHECK_(true) << "Extra info";

View File

@@ -146,8 +146,6 @@ TEST_F(TestPartResultArrayTest, ContainsGivenResultsAfterTwoAppends) {
EXPECT_STREQ("Failure 2", results.GetTestPartResult(1).message());
}
#if GTEST_HAS_DEATH_TEST
typedef TestPartResultArrayTest TestPartResultArrayDeathTest;
// Tests that the program dies when GetTestPartResult() is called with
@@ -156,12 +154,10 @@ TEST_F(TestPartResultArrayDeathTest, DiesWhenIndexIsOutOfBound) {
TestPartResultArray results;
results.Append(r1_);
EXPECT_DEATH(results.GetTestPartResult(-1), "");
EXPECT_DEATH(results.GetTestPartResult(1), "");
EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(-1), "");
EXPECT_DEATH_IF_SUPPORTED(results.GetTestPartResult(1), "");
}
#endif // GTEST_HAS_DEATH_TEST
// TODO(mheule@google.com): Add a test for the class HasNewFatalFailureHelper.
} // namespace

View File

@@ -198,24 +198,22 @@ TEST_F(TypedTestCasePStateTest, IgnoresOrderAndSpaces) {
state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
}
#if GTEST_HAS_DEATH_TEST
typedef TypedTestCasePStateTest TypedTestCasePStateDeathTest;
TEST_F(TypedTestCasePStateDeathTest, DetectsDuplicates) {
EXPECT_DEATH(
EXPECT_DEATH_IF_SUPPORTED(
state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, A, C"),
"foo\\.cc.1.?: Test A is listed more than once\\.");
}
TEST_F(TypedTestCasePStateDeathTest, DetectsExtraTest) {
EXPECT_DEATH(
EXPECT_DEATH_IF_SUPPORTED(
state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C, D"),
"foo\\.cc.1.?: No test named D can be found in this test case\\.");
}
TEST_F(TypedTestCasePStateDeathTest, DetectsMissedTest) {
EXPECT_DEATH(
EXPECT_DEATH_IF_SUPPORTED(
state_.VerifyRegisteredTestNames("foo.cc", 1, "A, C"),
"foo\\.cc.1.?: You forgot to list test B\\.");
}
@@ -224,14 +222,12 @@ TEST_F(TypedTestCasePStateDeathTest, DetectsMissedTest) {
// a run-time error if the test case has been registered.
TEST_F(TypedTestCasePStateDeathTest, DetectsTestAfterRegistration) {
state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C");
EXPECT_DEATH(
EXPECT_DEATH_IF_SUPPORTED(
state_.AddTestName("foo.cc", 2, "FooTest", "D"),
"foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_CASE_P"
"\\(FooTest, \\.\\.\\.\\)\\.");
}
#endif // GTEST_HAS_DEATH_TEST
// Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
// and SetUp()/TearDown() work correctly in type-parameterized tests.

View File

@@ -92,19 +92,13 @@ TEST(BazTest, DISABLED_TestC) {
// Test case HasDeathTest
TEST(HasDeathTest, Test1) {
#if GTEST_HAS_DEATH_TEST
EXPECT_DEATH({exit(1);},
".*");
#endif // GTEST_HAS_DEATH_TEST
EXPECT_DEATH_IF_SUPPORTED(exit(1), ".*");
}
// We need at least two death tests to make sure that the all death tests
// aren't on the first shard.
TEST(HasDeathTest, Test2) {
#if GTEST_HAS_DEATH_TEST
EXPECT_DEATH({exit(1);},
".*");
#endif // GTEST_HAS_DEATH_TEST
EXPECT_DEATH_IF_SUPPORTED(exit(1), ".*");
}
// Test case FoobarTest

View File

@@ -112,13 +112,11 @@ int g_death_test_count = 0;
TEST(BarDeathTest, ThreadSafeAndFast) {
g_death_test_count++;
#if GTEST_HAS_DEATH_TEST
GTEST_FLAG(death_test_style) = "threadsafe";
EXPECT_DEATH(abort(), "");
EXPECT_DEATH_IF_SUPPORTED(abort(), "");
GTEST_FLAG(death_test_style) = "fast";
EXPECT_DEATH(abort(), "");
#endif // GTEST_HAS_DEATH_TEST
EXPECT_DEATH_IF_SUPPORTED(abort(), "");
}
#if GTEST_HAS_PARAM_TEST

View File

@@ -6345,16 +6345,14 @@ TEST(EventListenerTest, SuppressEventForwarding) {
EXPECT_EQ(0, on_start_counter);
}
#if GTEST_HAS_DEATH_TEST
// Tests that events generated by Google Test are not forwarded in
// death test subprocesses.
TEST(EventListenerDeathTest, EventsNotForwardedInDeathTestSubprecesses) {
EXPECT_DEATH({ // NOLINT
EXPECT_DEATH_IF_SUPPORTED({
GTEST_CHECK_(EventListenersAccessor::EventForwardingEnabled(
*GetUnitTestImpl()->listeners())) << "expected failure";},
"expected failure");
}
#endif // GTEST_HAS_DEATH_TEST
// Tests that a listener installed via SetDefaultResultPrinter() starts
// receiving events and is returned via default_result_printer() and that