diff options
author | zhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925> | 2009-09-11 06:59:42 (GMT) |
---|---|---|
committer | zhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925> | 2009-09-11 06:59:42 (GMT) |
commit | b2ee82ebf9b8f1be859d08611b768ae6c0700090 (patch) | |
tree | e9ef0e2d46fce05fcdf325c28b21c0c412567eda /test | |
parent | f6dd67a1550d25518cc37758364834f4f92a3570 (diff) | |
download | googletest-b2ee82ebf9b8f1be859d08611b768ae6c0700090.zip googletest-b2ee82ebf9b8f1be859d08611b768ae6c0700090.tar.gz googletest-b2ee82ebf9b8f1be859d08611b768ae6c0700090.tar.bz2 |
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).
Diffstat (limited to 'test')
-rw-r--r-- | test/gtest-death-test_test.cc | 77 | ||||
-rw-r--r-- | test/gtest-port_test.cc | 7 | ||||
-rw-r--r-- | test/gtest-test-part_test.cc | 8 | ||||
-rw-r--r-- | test/gtest-typed-test_test.cc | 12 | ||||
-rw-r--r-- | test/gtest_filter_unittest_.cc | 10 | ||||
-rw-r--r-- | test/gtest_repeat_test.cc | 6 | ||||
-rw-r--r-- | test/gtest_unittest.cc | 4 |
7 files changed, 90 insertions, 34 deletions
diff --git a/test/gtest-death-test_test.cc b/test/gtest-death-test_test.cc index 16fc7e0..f56f35d 100644 --- a/test/gtest-death-test_test.cc +++ b/test/gtest-death-test_test.cc @@ -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) { diff --git a/test/gtest-port_test.cc b/test/gtest-port_test.cc index d980b7c..9785951 100644 --- a/test/gtest-port_test.cc +++ b/test/gtest-port_test.cc @@ -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"; diff --git a/test/gtest-test-part_test.cc b/test/gtest-test-part_test.cc index 93fe156..fc94f92 100644 --- a/test/gtest-test-part_test.cc +++ b/test/gtest-test-part_test.cc @@ -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 diff --git a/test/gtest-typed-test_test.cc b/test/gtest-typed-test_test.cc index 8e86ac8..e97598f 100644 --- a/test/gtest-typed-test_test.cc +++ b/test/gtest-typed-test_test.cc @@ -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. diff --git a/test/gtest_filter_unittest_.cc b/test/gtest_filter_unittest_.cc index 3cbddcf..325504f 100644 --- a/test/gtest_filter_unittest_.cc +++ b/test/gtest_filter_unittest_.cc @@ -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 diff --git a/test/gtest_repeat_test.cc b/test/gtest_repeat_test.cc index 39a0601..8ec3700 100644 --- a/test/gtest_repeat_test.cc +++ b/test/gtest_repeat_test.cc @@ -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 diff --git a/test/gtest_unittest.cc b/test/gtest_unittest.cc index dcec9da..07e60f5 100644 --- a/test/gtest_unittest.cc +++ b/test/gtest_unittest.cc @@ -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 |