diff --git a/googlemock/include/gmock/gmock-matchers.h b/googlemock/include/gmock/gmock-matchers.h index 969d9202..59ef2f3d 100644 --- a/googlemock/include/gmock/gmock-matchers.h +++ b/googlemock/include/gmock/gmock-matchers.h @@ -4774,15 +4774,15 @@ class ExceptionMatcherImpl { ExceptionMatcherImpl(Matcher matcher) : matcher_(std::move(matcher)) {} - void DescribeTo(::std::ostream* os) const { - *os << "throws an exception of type " << GetTypeName(); + void DescribeTo(std::ostream* os) const { + *os << "throws an exception which is a " << GetTypeName(); if (matcher_.GetDescriber() != nullptr) { *os << " which "; matcher_.DescribeTo(os); } } - void DescribeNegationTo(::std::ostream* os) const { + void DescribeNegationTo(std::ostream* os) const { *os << "not ("; DescribeTo(os); *os << ")"; @@ -4793,7 +4793,7 @@ class ExceptionMatcherImpl { try { (void)(std::forward(x)()); } catch (const Err& err) { - *listener << "throws an exception of type " << GetTypeName(); + *listener << "throws an exception which is a " << GetTypeName(); if (matcher_.GetDescriber() != nullptr) { *listener << " "; return matcher_.MatchAndExplain(err, listener); @@ -4826,7 +4826,6 @@ class ExceptionMatcherImpl { // Throws() // Throws(exceptionMatcher) // ThrowsMessage(messageMatcher) -// ThrowsMessageHasSubstr(message) // // This matcher accepts a callable and verifies that when invoked, it throws // an exception with the given type and properties. @@ -4843,10 +4842,6 @@ class ExceptionMatcherImpl { // // EXPECT_THAT( // []() { throw std::runtime_error("message"); }, -// ThrowsMessageHasSubstr("message")); -// -// EXPECT_THAT( -// []() { throw std::runtime_error("message"); }, // Throws( // Property(&std::runtime_error::what, HasSubstr("message")))); @@ -4882,16 +4877,6 @@ ThrowsMessage(const MessageMatcher& messageMatcher) { Property("what", &std::exception::what, MatcherCast(messageMatcher))}); } -template -PolymorphicMatcher> -ThrowsMessageHasSubstr(const internal::StringLike& message) { - static_assert( - std::is_base_of::value, - "expected an std::exception-derived class"); - return MakePolymorphicMatcher( - internal::ExceptionMatcherImpl{ - Property("what", &std::exception::what, HasSubstr(message))}); -} #endif // GTEST_HAS_EXCEPTIONS diff --git a/googlemock/test/gmock-matchers_test.cc b/googlemock/test/gmock-matchers_test.cc index a8b39b8f..af3b02c1 100644 --- a/googlemock/test/gmock-matchers_test.cc +++ b/googlemock/test/gmock-matchers_test.cc @@ -8129,10 +8129,6 @@ TEST(ThrowsTest, Examples) { []() { throw std::runtime_error("message"); }, ThrowsMessage(HasSubstr("message"))); - EXPECT_THAT( - []() { throw std::runtime_error("message"); }, - ThrowsMessageHasSubstr("message")); - EXPECT_THAT( []() { throw std::runtime_error("message"); }, Throws( @@ -8163,16 +8159,11 @@ TEST(ThrowsTest, CallableExecutedExactlyOnce) { ThrowsMessage(HasSubstr("message"))); EXPECT_EQ(a, 3u); - EXPECT_THAT( - [&a]() { a++; throw std::runtime_error("message"); }, - ThrowsMessageHasSubstr("message")); - EXPECT_EQ(a, 4u); - EXPECT_THAT( [&a]() { a++; throw std::runtime_error("message"); }, Throws( Property(&std::runtime_error::what, HasSubstr("message")))); - EXPECT_EQ(a, 5u); + EXPECT_EQ(a, 4u); } TEST(ThrowsTest, Describe) { @@ -8180,7 +8171,7 @@ TEST(ThrowsTest, Describe) { std::stringstream ss; matcher.DescribeTo(&ss); auto explanation = ss.str(); - EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); + EXPECT_THAT(explanation, HasSubstr("std::runtime_error")); } TEST(ThrowsTest, Success) { @@ -8189,7 +8180,7 @@ TEST(ThrowsTest, Success) { EXPECT_TRUE( matcher.MatchAndExplain( []() { throw std::runtime_error("error message"); }, &listener)); - EXPECT_THAT(listener.str(), testing::HasSubstr("std::runtime_error")); + EXPECT_THAT(listener.str(), HasSubstr("std::runtime_error")); } TEST(ThrowsTest, FailWrongType) { @@ -8198,8 +8189,8 @@ TEST(ThrowsTest, FailWrongType) { EXPECT_FALSE( matcher.MatchAndExplain( []() { throw std::logic_error("error message"); }, &listener)); - EXPECT_THAT(listener.str(), testing::HasSubstr("std::logic_error")); - EXPECT_THAT(listener.str(), testing::HasSubstr("\"error message\"")); + EXPECT_THAT(listener.str(), HasSubstr("std::logic_error")); + EXPECT_THAT(listener.str(), HasSubstr("\"error message\"")); } TEST(ThrowsTest, FailWrongTypeNonStd) { @@ -8210,7 +8201,7 @@ TEST(ThrowsTest, FailWrongTypeNonStd) { []() { throw 10; }, &listener)); EXPECT_THAT( listener.str(), - testing::HasSubstr("throws an exception of an unknown type")); + HasSubstr("throws an exception of an unknown type")); } TEST(ThrowsTest, FailNoThrow) { @@ -8221,7 +8212,7 @@ TEST(ThrowsTest, FailNoThrow) { []() { (void)0; }, &listener)); EXPECT_THAT( listener.str(), - testing::HasSubstr("does not throw any exception")); + HasSubstr("does not throw any exception")); } class ThrowsPredicateTest: public TestWithParam> {}; @@ -8231,8 +8222,8 @@ TEST_P(ThrowsPredicateTest, Describe) { std::stringstream ss; matcher.DescribeTo(&ss); auto explanation = ss.str(); - EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error")); - EXPECT_THAT(explanation, testing::HasSubstr("error message")); + EXPECT_THAT(explanation, HasSubstr("std::runtime_error")); + EXPECT_THAT(explanation, HasSubstr("error message")); } TEST_P(ThrowsPredicateTest, Success) { @@ -8241,7 +8232,7 @@ TEST_P(ThrowsPredicateTest, Success) { EXPECT_TRUE( matcher.MatchAndExplain( []() { throw std::runtime_error("error message"); }, &listener)); - EXPECT_THAT(listener.str(), testing::HasSubstr("std::runtime_error")); + EXPECT_THAT(listener.str(), HasSubstr("std::runtime_error")); } TEST_P(ThrowsPredicateTest, FailWrongType) { @@ -8250,8 +8241,8 @@ TEST_P(ThrowsPredicateTest, FailWrongType) { EXPECT_FALSE( matcher.MatchAndExplain( []() { throw std::logic_error("error message"); }, &listener)); - EXPECT_THAT(listener.str(), testing::HasSubstr("std::logic_error")); - EXPECT_THAT(listener.str(), testing::HasSubstr("\"error message\"")); + EXPECT_THAT(listener.str(), HasSubstr("std::logic_error")); + EXPECT_THAT(listener.str(), HasSubstr("\"error message\"")); } TEST_P(ThrowsPredicateTest, FailWrongTypeNonStd) { @@ -8262,7 +8253,7 @@ TEST_P(ThrowsPredicateTest, FailWrongTypeNonStd) { []() { throw 10; }, &listener)); EXPECT_THAT( listener.str(), - testing::HasSubstr("throws an exception of an unknown type")); + HasSubstr("throws an exception of an unknown type")); } TEST_P(ThrowsPredicateTest, FailWrongMessage) { @@ -8271,8 +8262,8 @@ TEST_P(ThrowsPredicateTest, FailWrongMessage) { EXPECT_FALSE( matcher.MatchAndExplain( []() { throw std::runtime_error("wrong message"); }, &listener)); - EXPECT_THAT(listener.str(), testing::HasSubstr("std::runtime_error")); - EXPECT_THAT(listener.str(), testing::HasSubstr("wrong message")); + EXPECT_THAT(listener.str(), HasSubstr("std::runtime_error")); + EXPECT_THAT(listener.str(), HasSubstr("wrong message")); } TEST_P(ThrowsPredicateTest, FailNoThrow) { @@ -8283,18 +8274,16 @@ TEST_P(ThrowsPredicateTest, FailNoThrow) { []() { (void)0; }, &listener)); EXPECT_THAT( listener.str(), - testing::HasSubstr("does not throw any exception")); + HasSubstr("does not throw any exception")); } INSTANTIATE_TEST_SUITE_P(AllMessagePredicates, ThrowsPredicateTest, - ::testing::Values( + Values( static_cast>( Throws( Property(&std::exception::what, HasSubstr("error message")))), static_cast>( - ThrowsMessage(HasSubstr("error message"))), - static_cast>( - ThrowsMessageHasSubstr("error message")))); + ThrowsMessage(HasSubstr("error message"))))); // Tests that Throws(Matcher{}) compiles even when E2 != const E1&. TEST(ThrowsPredicateCompilesTest, ExceptionMatcherAcceptsBroadType) { @@ -8331,26 +8320,6 @@ TEST(ThrowsPredicateCompilesTest, MessageMatcherAcceptsNonMatcher) { []() { throw std::runtime_error("wrong error message"); })); } -// Tests that ThrowsMessageHasSubstr accepts types that're -// explicitly-convertible to std::string. -TEST(ThrowsPredicateCompilesTest, StringLikeMessage) { - struct SomeCustomString { - std::string inner; - - // Note: explicit conversion. - explicit operator std::string() const { return inner; } - }; - - Matcher matcher = ThrowsMessageHasSubstr( - SomeCustomString{"error message"}); - EXPECT_TRUE( - matcher.Matches( - []() { throw std::runtime_error("error message"); })); - EXPECT_FALSE( - matcher.Matches( - []() { throw std::runtime_error("wrong message"); })); -} - #endif // GTEST_HAS_EXCEPTIONS } // namespace