diff options
author | durandal <durandal@google.com> | 2018-11-27 20:17:28 (GMT) |
---|---|---|
committer | Gennadiy Civil <misterg@google.com> | 2018-11-29 04:10:17 (GMT) |
commit | b22d23667b6066441e869480e046c170d4bdc6ec (patch) | |
tree | affea0ebb1946dc23895ac6780e0e98e3ccbffc7 /googletest/test | |
parent | 191f9336bc9212b5f5410ab663176f685cafed2a (diff) | |
download | googletest-b22d23667b6066441e869480e046c170d4bdc6ec.zip googletest-b22d23667b6066441e869480e046c170d4bdc6ec.tar.gz googletest-b22d23667b6066441e869480e046c170d4bdc6ec.tar.bz2 |
Googletest export
Accept gmock matchers in EXPECT_EXIT and friends to allow matches other than simple regex matches on death output.
PiperOrigin-RevId: 223035409
Diffstat (limited to 'googletest/test')
-rw-r--r-- | googletest/test/googletest-death-test-test.cc | 78 |
1 files changed, 68 insertions, 10 deletions
diff --git a/googletest/test/googletest-death-test-test.cc b/googletest/test/googletest-death-test-test.cc index f92bb1a..59581d6 100644 --- a/googletest/test/googletest-death-test-test.cc +++ b/googletest/test/googletest-death-test-test.cc @@ -31,6 +31,8 @@ // Tests for death tests. #include "gtest/gtest-death-test.h" + +#include "gmock/gmock.h" #include "gtest/gtest.h" #include "gtest/internal/gtest-filepath.h" @@ -59,6 +61,8 @@ using testing::internal::AlwaysTrue; namespace posix = ::testing::internal::posix; +using testing::HasSubstr; +using testing::Matcher; using testing::Message; using testing::internal::DeathTest; using testing::internal::DeathTestFactory; @@ -97,6 +101,8 @@ class ReplaceDeathTestFactory { } // namespace internal } // namespace testing +namespace { + void DieWithMessage(const ::std::string& message) { fprintf(stderr, "%s", message.c_str()); fflush(stderr); // Make sure the text is printed before the process exits. @@ -452,16 +458,12 @@ TEST_F(TestForDeathTest, MixedStyles) { # if GTEST_HAS_CLONE && GTEST_HAS_PTHREAD -namespace { - bool pthread_flag; void SetPthreadFlag() { pthread_flag = true; } -} // namespace - TEST_F(TestForDeathTest, DoesNotExecuteAtforkHooks) { if (!testing::GTEST_FLAG(death_test_use_fork)) { testing::GTEST_FLAG(death_test_style) = "threadsafe"; @@ -885,7 +887,7 @@ class MockDeathTestFactory : public DeathTestFactory { public: MockDeathTestFactory(); virtual bool Create(const char* statement, - const ::testing::internal::RE* regex, + testing::Matcher<const std::string&> matcher, const char* file, int line, DeathTest** test); // Sets the parameters for subsequent calls to Create. @@ -1000,11 +1002,9 @@ void MockDeathTestFactory::SetParameters(bool create, // Sets test to NULL (if create_ is false) or to the address of a new // MockDeathTest object with parameters taken from the last call // to SetParameters (if create_ is true). Always returns true. -bool MockDeathTestFactory::Create(const char* /*statement*/, - const ::testing::internal::RE* /*regex*/, - const char* /*file*/, - int /*line*/, - DeathTest** test) { +bool MockDeathTestFactory::Create( + const char* /*statement*/, testing::Matcher<const std::string&> /*matcher*/, + const char* /*file*/, int /*line*/, DeathTest** test) { test_deleted_ = false; if (create_) { *test = new MockDeathTest(this, role_, status_, passed_); @@ -1326,8 +1326,60 @@ TEST(InDeathTestChildDeathTest, ReportsDeathTestCorrectlyInThreadSafeStyle) { }, "Inside"); } +void DieWithMessage(const char* message) { + fputs(message, stderr); + fflush(stderr); // Make sure the text is printed before the process exits. + _exit(1); +} + +TEST(MatcherDeathTest, DoesNotBreakBareRegexMatching) { + // googletest tests this, of course; here we ensure that including googlemock + // has not broken it. + EXPECT_DEATH(DieWithMessage("O, I die, Horatio."), "I d[aeiou]e"); +} + +TEST(MatcherDeathTest, MonomorphicMatcherMatches) { + EXPECT_DEATH(DieWithMessage("Behind O, I am slain!"), + Matcher<const std::string&>(HasSubstr("I am slain"))); +} + +TEST(MatcherDeathTest, MonomorphicMatcherDoesNotMatch) { + EXPECT_NONFATAL_FAILURE( + EXPECT_DEATH(DieWithMessage("Behind O, I am slain!"), + Matcher<const std::string&>(HasSubstr("Ow, I am slain"))), + "Expected: has substring \"Ow, I am slain\""); +} + +TEST(MatcherDeathTest, PolymorphicMatcherMatches) { + EXPECT_DEATH(DieWithMessage("The rest is silence."), + HasSubstr("rest is silence")); +} + +TEST(MatcherDeathTest, PolymorphicMatcherDoesNotMatch) { + EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(DieWithMessage("The rest is silence."), + HasSubstr("rest is science")), + "Expected: has substring \"rest is science\""); +} + +TEST(MatcherDeathTest, CompositeMatcherMatches) { + EXPECT_DEATH(DieWithMessage("Et tu, Brute! Then fall, Caesar."), + AllOf(HasSubstr("Et tu"), HasSubstr("fall, Caesar"))); +} + +TEST(MatcherDeathTest, CompositeMatcherDoesNotMatch) { + EXPECT_NONFATAL_FAILURE( + EXPECT_DEATH(DieWithMessage("The rest is silence."), + AnyOf(HasSubstr("Eat two"), HasSubstr("lol Caesar"))), + "Expected: (has substring \"Eat two\") or " + "(has substring \"lol Caesar\")"); +} + +} // namespace + #else // !GTEST_HAS_DEATH_TEST follows +namespace { + using testing::internal::CaptureStderr; using testing::internal::GetCapturedStderr; @@ -1376,8 +1428,12 @@ TEST(ConditionalDeathMacrosTest, AssertDeatDoesNotReturnhIfUnsupported) { EXPECT_EQ(1, n); } +} // namespace + #endif // !GTEST_HAS_DEATH_TEST +namespace { + // 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. @@ -1428,3 +1484,5 @@ TEST(ConditionalDeathMacrosSyntaxDeathTest, SwitchStatement) { TEST(NotADeathTest, Test) { SUCCEED(); } + +} // namespace |