diff options
author | zhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925> | 2009-08-07 06:47:47 (GMT) |
---|---|---|
committer | zhanyong.wan <zhanyong.wan@861a406c-534a-0410-8894-cb66d6ee9925> | 2009-08-07 06:47:47 (GMT) |
commit | ed8500b341c473ecf46acd13951ae5b4e3acc780 (patch) | |
tree | ca41796e67c988e8985cc084e1c6136aa609df39 /test | |
parent | 18c31d64e120919e8e04df3035234b9afe8eb6d9 (diff) | |
download | googletest-ed8500b341c473ecf46acd13951ae5b4e3acc780.zip googletest-ed8500b341c473ecf46acd13951ae5b4e3acc780.tar.gz googletest-ed8500b341c473ecf46acd13951ae5b4e3acc780.tar.bz2 |
Implements EXPECT_DEATH_IF_SUPPORTED (by Vlad Losev); Fixes compatibility with Symbian (by Araceli Checa); Removes GetCapturedStderr()'s dependency on std::string (by Vlad Losev).
Diffstat (limited to 'test')
-rw-r--r-- | test/gtest-death-test_test.cc | 38 | ||||
-rw-r--r-- | test/gtest-port_test.cc | 6 | ||||
-rw-r--r-- | test/gtest_unittest.cc | 44 |
3 files changed, 54 insertions, 34 deletions
diff --git a/test/gtest-death-test_test.cc b/test/gtest-death-test_test.cc index d5f1598..1881139 100644 --- a/test/gtest-death-test_test.cc +++ b/test/gtest-death-test_test.cc @@ -1118,6 +1118,44 @@ TEST(EnvironmentTest, HandleFitsIntoSizeT) { } #endif // GTEST_OS_WINDOWS +// 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(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(;, ""), ""); + EXPECT_FATAL_FAILURE(ASSERT_DEATH_IF_SUPPORTED(;, ""), ""); +} + +#else + +using testing::internal::CaptureStderr; +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 +// the system. +TEST(ConditionalDeathMacrosTest, WarnsWhenDeathTestsNotAvailable) { + // Empty statement will not crash, but that should not trigger a failure + // when death tests are not supported. + CaptureStderr(); + EXPECT_DEATH_IF_SUPPORTED(;, ""); + String 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(;, ""); + output = GetCapturedStderr(); + ASSERT_TRUE(NULL != strstr(output.c_str(), + "Death tests are not supported on this platform")); + ASSERT_TRUE(NULL != strstr(output.c_str(), ";")); +} + #endif // GTEST_HAS_DEATH_TEST // Tests that a test case whose name ends with "DeathTest" works fine diff --git a/test/gtest-port_test.cc b/test/gtest-port_test.cc index 49af8b9..d980b7c 100644 --- a/test/gtest-port_test.cc +++ b/test/gtest-port_test.cc @@ -688,15 +688,11 @@ TEST(RETest, PartialMatchWorks) { #endif // GTEST_USES_POSIX_RE -#if GTEST_HAS_STD_STRING - TEST(CaptureStderrTest, CapturesStdErr) { CaptureStderr(); fprintf(stderr, "abc"); - ASSERT_EQ("abc", GetCapturedStderr()); + ASSERT_STREQ("abc", GetCapturedStderr().c_str()); } -#endif // GTEST_HAS_STD_STRING - } // namespace internal } // namespace testing diff --git a/test/gtest_unittest.cc b/test/gtest_unittest.cc index 4eb098e..2c08720 100644 --- a/test/gtest_unittest.cc +++ b/test/gtest_unittest.cc @@ -78,16 +78,6 @@ TEST(CommandLineFlagsTest, CanBeAccessedInCodeOnceGTestHIsIncluded) { #include <map> #endif -// GTEST_EXPECT_DEATH_IF_SUPPORTED_(statement, regex) expands to a -// real death test if death tests are supported; otherwise it expands -// to empty. -#if GTEST_HAS_DEATH_TEST -#define GTEST_EXPECT_DEATH_IF_SUPPORTED_(statement, regex) \ - EXPECT_DEATH(statement, regex) -#else -#define GTEST_EXPECT_DEATH_IF_SUPPORTED_(statement, regex) -#endif - namespace testing { namespace internal { const char* FormatTimeInMillisAsSeconds(TimeInMillis ms); @@ -630,7 +620,7 @@ TEST(VectorDeathTest, Erase) { Vector<int> a; // Tests erasing from an empty vector. - GTEST_EXPECT_DEATH_IF_SUPPORTED_( + EXPECT_DEATH_IF_SUPPORTED( a.Erase(0), "Invalid Vector index 0: must be in range \\[0, -1\\]\\."); @@ -646,10 +636,10 @@ TEST(VectorDeathTest, Erase) { a1.PushBack(1); a1.PushBack(2); - GTEST_EXPECT_DEATH_IF_SUPPORTED_( + EXPECT_DEATH_IF_SUPPORTED( a1.Erase(3), "Invalid Vector index 3: must be in range \\[0, 2\\]\\."); - GTEST_EXPECT_DEATH_IF_SUPPORTED_( + EXPECT_DEATH_IF_SUPPORTED( a1.Erase(-1), "Invalid Vector index -1: must be in range \\[0, 2\\]\\."); @@ -697,10 +687,10 @@ TEST(ListDeathTest, GetElement) { EXPECT_EQ(0, a.GetElement(0)); EXPECT_EQ(1, a.GetElement(1)); EXPECT_EQ(2, a.GetElement(2)); - GTEST_EXPECT_DEATH_IF_SUPPORTED_( + EXPECT_DEATH_IF_SUPPORTED( a.GetElement(3), "Invalid Vector index 3: must be in range \\[0, 2\\]\\."); - GTEST_EXPECT_DEATH_IF_SUPPORTED_( + EXPECT_DEATH_IF_SUPPORTED( a.GetElement(-1), "Invalid Vector index -1: must be in range \\[0, 2\\]\\."); } @@ -1368,10 +1358,10 @@ typedef TestResultTest TestResultDeathTest; TEST_F(TestResultDeathTest, GetTestPartResult) { CompareTestPartResult(*pr1, r2->GetTestPartResult(0)); CompareTestPartResult(*pr2, r2->GetTestPartResult(1)); - GTEST_EXPECT_DEATH_IF_SUPPORTED_( + EXPECT_DEATH_IF_SUPPORTED( r2->GetTestPartResult(2), "Invalid Vector index 2: must be in range \\[0, 1\\]\\."); - GTEST_EXPECT_DEATH_IF_SUPPORTED_( + EXPECT_DEATH_IF_SUPPORTED( r2->GetTestPartResult(-1), "Invalid Vector index -1: must be in range \\[0, 1\\]\\."); } @@ -1455,10 +1445,10 @@ TEST(TestResultPropertyDeathTest, GetTestProperty) { EXPECT_STREQ("key_3", fetched_property_3.key()); EXPECT_STREQ("3", fetched_property_3.value()); - GTEST_EXPECT_DEATH_IF_SUPPORTED_( + EXPECT_DEATH_IF_SUPPORTED( test_result.GetTestProperty(3), "Invalid Vector index 3: must be in range \\[0, 2\\]\\."); - GTEST_EXPECT_DEATH_IF_SUPPORTED_( + EXPECT_DEATH_IF_SUPPORTED( test_result.GetTestProperty(-1), "Invalid Vector index -1: must be in range \\[0, 2\\]\\."); } @@ -1737,7 +1727,7 @@ TEST(Int32FromEnvOrDieTest, ParsesAndReturnsValidValue) { // if the variable is not an Int32. TEST(Int32FromEnvOrDieDeathTest, AbortsOnFailure) { SetEnv(GTEST_FLAG_PREFIX_UPPER_ "VAR", "xxx"); - GTEST_EXPECT_DEATH_IF_SUPPORTED_( + EXPECT_DEATH_IF_SUPPORTED( Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123), ".*"); } @@ -1746,7 +1736,7 @@ TEST(Int32FromEnvOrDieDeathTest, AbortsOnFailure) { // if the variable cannot be represnted by an Int32. TEST(Int32FromEnvOrDieDeathTest, AbortsOnInt32Overflow) { SetEnv(GTEST_FLAG_PREFIX_UPPER_ "VAR", "1234567891234567891234"); - GTEST_EXPECT_DEATH_IF_SUPPORTED_( + EXPECT_DEATH_IF_SUPPORTED( Int32FromEnvOrDie(GTEST_FLAG_PREFIX_UPPER_ "VAR", 123), ".*"); } @@ -1824,23 +1814,19 @@ typedef ShouldShardTest ShouldShardDeathTest; TEST_F(ShouldShardDeathTest, AbortsWhenShardingEnvVarsAreInvalid) { SetEnv(index_var_, "4"); SetEnv(total_var_, "4"); - GTEST_EXPECT_DEATH_IF_SUPPORTED_(ShouldShard(total_var_, index_var_, false), - ".*"); + EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); SetEnv(index_var_, "4"); SetEnv(total_var_, "-2"); - GTEST_EXPECT_DEATH_IF_SUPPORTED_(ShouldShard(total_var_, index_var_, false), - ".*"); + EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); SetEnv(index_var_, "5"); SetEnv(total_var_, ""); - GTEST_EXPECT_DEATH_IF_SUPPORTED_(ShouldShard(total_var_, index_var_, false), - ".*"); + EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); SetEnv(index_var_, ""); SetEnv(total_var_, "5"); - GTEST_EXPECT_DEATH_IF_SUPPORTED_(ShouldShard(total_var_, index_var_, false), - ".*"); + EXPECT_DEATH_IF_SUPPORTED(ShouldShard(total_var_, index_var_, false), ".*"); } // Tests that ShouldRunTestOnShard is a partition when 5 |