Remove ThrowsMessageHasSubstr and fix some nits after review
This commit is contained in:
		@@ -4774,15 +4774,15 @@ class ExceptionMatcherImpl {
 | 
				
			|||||||
  ExceptionMatcherImpl(Matcher<const Err&> matcher)
 | 
					  ExceptionMatcherImpl(Matcher<const Err&> matcher)
 | 
				
			||||||
      : matcher_(std::move(matcher)) {}
 | 
					      : matcher_(std::move(matcher)) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  void DescribeTo(::std::ostream* os) const {
 | 
					  void DescribeTo(std::ostream* os) const {
 | 
				
			||||||
    *os << "throws an exception of type " << GetTypeName<Err>();
 | 
					    *os << "throws an exception which is a " << GetTypeName<Err>();
 | 
				
			||||||
    if (matcher_.GetDescriber() != nullptr) {
 | 
					    if (matcher_.GetDescriber() != nullptr) {
 | 
				
			||||||
      *os << " which ";
 | 
					      *os << " which ";
 | 
				
			||||||
      matcher_.DescribeTo(os);
 | 
					      matcher_.DescribeTo(os);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  void DescribeNegationTo(::std::ostream* os) const {
 | 
					  void DescribeNegationTo(std::ostream* os) const {
 | 
				
			||||||
    *os << "not (";
 | 
					    *os << "not (";
 | 
				
			||||||
    DescribeTo(os);
 | 
					    DescribeTo(os);
 | 
				
			||||||
    *os << ")";
 | 
					    *os << ")";
 | 
				
			||||||
@@ -4793,7 +4793,7 @@ class ExceptionMatcherImpl {
 | 
				
			|||||||
    try {
 | 
					    try {
 | 
				
			||||||
      (void)(std::forward<T>(x)());
 | 
					      (void)(std::forward<T>(x)());
 | 
				
			||||||
    } catch (const Err& err) {
 | 
					    } catch (const Err& err) {
 | 
				
			||||||
      *listener << "throws an exception of type " << GetTypeName<Err>();
 | 
					      *listener << "throws an exception which is a " << GetTypeName<Err>();
 | 
				
			||||||
      if (matcher_.GetDescriber() != nullptr) {
 | 
					      if (matcher_.GetDescriber() != nullptr) {
 | 
				
			||||||
        *listener << " ";
 | 
					        *listener << " ";
 | 
				
			||||||
        return matcher_.MatchAndExplain(err, listener);
 | 
					        return matcher_.MatchAndExplain(err, listener);
 | 
				
			||||||
@@ -4826,7 +4826,6 @@ class ExceptionMatcherImpl {
 | 
				
			|||||||
// Throws()
 | 
					// Throws()
 | 
				
			||||||
// Throws(exceptionMatcher)
 | 
					// Throws(exceptionMatcher)
 | 
				
			||||||
// ThrowsMessage(messageMatcher)
 | 
					// ThrowsMessage(messageMatcher)
 | 
				
			||||||
// ThrowsMessageHasSubstr(message)
 | 
					 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
// This matcher accepts a callable and verifies that when invoked, it throws
 | 
					// This matcher accepts a callable and verifies that when invoked, it throws
 | 
				
			||||||
// an exception with the given type and properties.
 | 
					// an exception with the given type and properties.
 | 
				
			||||||
@@ -4843,10 +4842,6 @@ class ExceptionMatcherImpl {
 | 
				
			|||||||
//
 | 
					//
 | 
				
			||||||
//   EXPECT_THAT(
 | 
					//   EXPECT_THAT(
 | 
				
			||||||
//       []() { throw std::runtime_error("message"); },
 | 
					//       []() { throw std::runtime_error("message"); },
 | 
				
			||||||
//       ThrowsMessageHasSubstr<std::runtime_error>("message"));
 | 
					 | 
				
			||||||
//
 | 
					 | 
				
			||||||
//   EXPECT_THAT(
 | 
					 | 
				
			||||||
//       []() { throw std::runtime_error("message"); },
 | 
					 | 
				
			||||||
//       Throws<std::runtime_error>(
 | 
					//       Throws<std::runtime_error>(
 | 
				
			||||||
//           Property(&std::runtime_error::what, HasSubstr("message"))));
 | 
					//           Property(&std::runtime_error::what, HasSubstr("message"))));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -4882,16 +4877,6 @@ ThrowsMessage(const MessageMatcher& messageMatcher) {
 | 
				
			|||||||
          Property("what", &std::exception::what,
 | 
					          Property("what", &std::exception::what,
 | 
				
			||||||
                   MatcherCast<std::string>(messageMatcher))});
 | 
					                   MatcherCast<std::string>(messageMatcher))});
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
template <typename Err, typename Message = std::string>
 | 
					 | 
				
			||||||
PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>>
 | 
					 | 
				
			||||||
ThrowsMessageHasSubstr(const internal::StringLike<Message>& message) {
 | 
					 | 
				
			||||||
  static_assert(
 | 
					 | 
				
			||||||
      std::is_base_of<std::exception, Err>::value,
 | 
					 | 
				
			||||||
      "expected an std::exception-derived class");
 | 
					 | 
				
			||||||
  return MakePolymorphicMatcher(
 | 
					 | 
				
			||||||
      internal::ExceptionMatcherImpl<Err>{
 | 
					 | 
				
			||||||
          Property("what", &std::exception::what, HasSubstr(message))});
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif  // GTEST_HAS_EXCEPTIONS
 | 
					#endif  // GTEST_HAS_EXCEPTIONS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8129,10 +8129,6 @@ TEST(ThrowsTest, Examples) {
 | 
				
			|||||||
      []() { throw std::runtime_error("message"); },
 | 
					      []() { throw std::runtime_error("message"); },
 | 
				
			||||||
      ThrowsMessage<std::runtime_error>(HasSubstr("message")));
 | 
					      ThrowsMessage<std::runtime_error>(HasSubstr("message")));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  EXPECT_THAT(
 | 
					 | 
				
			||||||
      []() { throw std::runtime_error("message"); },
 | 
					 | 
				
			||||||
      ThrowsMessageHasSubstr<std::runtime_error>("message"));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  EXPECT_THAT(
 | 
					  EXPECT_THAT(
 | 
				
			||||||
      []() { throw std::runtime_error("message"); },
 | 
					      []() { throw std::runtime_error("message"); },
 | 
				
			||||||
      Throws<std::runtime_error>(
 | 
					      Throws<std::runtime_error>(
 | 
				
			||||||
@@ -8163,16 +8159,11 @@ TEST(ThrowsTest, CallableExecutedExactlyOnce) {
 | 
				
			|||||||
      ThrowsMessage<std::runtime_error>(HasSubstr("message")));
 | 
					      ThrowsMessage<std::runtime_error>(HasSubstr("message")));
 | 
				
			||||||
  EXPECT_EQ(a, 3u);
 | 
					  EXPECT_EQ(a, 3u);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  EXPECT_THAT(
 | 
					 | 
				
			||||||
      [&a]() { a++; throw std::runtime_error("message"); },
 | 
					 | 
				
			||||||
      ThrowsMessageHasSubstr<std::runtime_error>("message"));
 | 
					 | 
				
			||||||
  EXPECT_EQ(a, 4u);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  EXPECT_THAT(
 | 
					  EXPECT_THAT(
 | 
				
			||||||
      [&a]() { a++; throw std::runtime_error("message"); },
 | 
					      [&a]() { a++; throw std::runtime_error("message"); },
 | 
				
			||||||
      Throws<std::runtime_error>(
 | 
					      Throws<std::runtime_error>(
 | 
				
			||||||
          Property(&std::runtime_error::what, HasSubstr("message"))));
 | 
					          Property(&std::runtime_error::what, HasSubstr("message"))));
 | 
				
			||||||
  EXPECT_EQ(a, 5u);
 | 
					  EXPECT_EQ(a, 4u);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TEST(ThrowsTest, Describe) {
 | 
					TEST(ThrowsTest, Describe) {
 | 
				
			||||||
@@ -8180,7 +8171,7 @@ TEST(ThrowsTest, Describe) {
 | 
				
			|||||||
  std::stringstream ss;
 | 
					  std::stringstream ss;
 | 
				
			||||||
  matcher.DescribeTo(&ss);
 | 
					  matcher.DescribeTo(&ss);
 | 
				
			||||||
  auto explanation = ss.str();
 | 
					  auto explanation = ss.str();
 | 
				
			||||||
  EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error"));
 | 
					  EXPECT_THAT(explanation, HasSubstr("std::runtime_error"));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TEST(ThrowsTest, Success) {
 | 
					TEST(ThrowsTest, Success) {
 | 
				
			||||||
@@ -8189,7 +8180,7 @@ TEST(ThrowsTest, Success) {
 | 
				
			|||||||
  EXPECT_TRUE(
 | 
					  EXPECT_TRUE(
 | 
				
			||||||
      matcher.MatchAndExplain(
 | 
					      matcher.MatchAndExplain(
 | 
				
			||||||
          []() { throw std::runtime_error("error message"); }, &listener));
 | 
					          []() { 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) {
 | 
					TEST(ThrowsTest, FailWrongType) {
 | 
				
			||||||
@@ -8198,8 +8189,8 @@ TEST(ThrowsTest, FailWrongType) {
 | 
				
			|||||||
  EXPECT_FALSE(
 | 
					  EXPECT_FALSE(
 | 
				
			||||||
      matcher.MatchAndExplain(
 | 
					      matcher.MatchAndExplain(
 | 
				
			||||||
          []() { throw std::logic_error("error message"); }, &listener));
 | 
					          []() { throw std::logic_error("error message"); }, &listener));
 | 
				
			||||||
  EXPECT_THAT(listener.str(), testing::HasSubstr("std::logic_error"));
 | 
					  EXPECT_THAT(listener.str(), HasSubstr("std::logic_error"));
 | 
				
			||||||
  EXPECT_THAT(listener.str(), testing::HasSubstr("\"error message\""));
 | 
					  EXPECT_THAT(listener.str(), HasSubstr("\"error message\""));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TEST(ThrowsTest, FailWrongTypeNonStd) {
 | 
					TEST(ThrowsTest, FailWrongTypeNonStd) {
 | 
				
			||||||
@@ -8210,7 +8201,7 @@ TEST(ThrowsTest, FailWrongTypeNonStd) {
 | 
				
			|||||||
          []() { throw 10; }, &listener));
 | 
					          []() { throw 10; }, &listener));
 | 
				
			||||||
  EXPECT_THAT(
 | 
					  EXPECT_THAT(
 | 
				
			||||||
      listener.str(),
 | 
					      listener.str(),
 | 
				
			||||||
      testing::HasSubstr("throws an exception of an unknown type"));
 | 
					      HasSubstr("throws an exception of an unknown type"));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TEST(ThrowsTest, FailNoThrow) {
 | 
					TEST(ThrowsTest, FailNoThrow) {
 | 
				
			||||||
@@ -8221,7 +8212,7 @@ TEST(ThrowsTest, FailNoThrow) {
 | 
				
			|||||||
          []() { (void)0; }, &listener));
 | 
					          []() { (void)0; }, &listener));
 | 
				
			||||||
  EXPECT_THAT(
 | 
					  EXPECT_THAT(
 | 
				
			||||||
      listener.str(),
 | 
					      listener.str(),
 | 
				
			||||||
      testing::HasSubstr("does not throw any exception"));
 | 
					      HasSubstr("does not throw any exception"));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ThrowsPredicateTest: public TestWithParam<Matcher<void (*)()>> {};
 | 
					class ThrowsPredicateTest: public TestWithParam<Matcher<void (*)()>> {};
 | 
				
			||||||
@@ -8231,8 +8222,8 @@ TEST_P(ThrowsPredicateTest, Describe) {
 | 
				
			|||||||
  std::stringstream ss;
 | 
					  std::stringstream ss;
 | 
				
			||||||
  matcher.DescribeTo(&ss);
 | 
					  matcher.DescribeTo(&ss);
 | 
				
			||||||
  auto explanation = ss.str();
 | 
					  auto explanation = ss.str();
 | 
				
			||||||
  EXPECT_THAT(explanation, testing::HasSubstr("std::runtime_error"));
 | 
					  EXPECT_THAT(explanation, HasSubstr("std::runtime_error"));
 | 
				
			||||||
  EXPECT_THAT(explanation, testing::HasSubstr("error message"));
 | 
					  EXPECT_THAT(explanation, HasSubstr("error message"));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TEST_P(ThrowsPredicateTest, Success) {
 | 
					TEST_P(ThrowsPredicateTest, Success) {
 | 
				
			||||||
@@ -8241,7 +8232,7 @@ TEST_P(ThrowsPredicateTest, Success) {
 | 
				
			|||||||
  EXPECT_TRUE(
 | 
					  EXPECT_TRUE(
 | 
				
			||||||
      matcher.MatchAndExplain(
 | 
					      matcher.MatchAndExplain(
 | 
				
			||||||
          []() { throw std::runtime_error("error message"); }, &listener));
 | 
					          []() { 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) {
 | 
					TEST_P(ThrowsPredicateTest, FailWrongType) {
 | 
				
			||||||
@@ -8250,8 +8241,8 @@ TEST_P(ThrowsPredicateTest, FailWrongType) {
 | 
				
			|||||||
  EXPECT_FALSE(
 | 
					  EXPECT_FALSE(
 | 
				
			||||||
      matcher.MatchAndExplain(
 | 
					      matcher.MatchAndExplain(
 | 
				
			||||||
          []() { throw std::logic_error("error message"); }, &listener));
 | 
					          []() { throw std::logic_error("error message"); }, &listener));
 | 
				
			||||||
  EXPECT_THAT(listener.str(), testing::HasSubstr("std::logic_error"));
 | 
					  EXPECT_THAT(listener.str(), HasSubstr("std::logic_error"));
 | 
				
			||||||
  EXPECT_THAT(listener.str(), testing::HasSubstr("\"error message\""));
 | 
					  EXPECT_THAT(listener.str(), HasSubstr("\"error message\""));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TEST_P(ThrowsPredicateTest, FailWrongTypeNonStd) {
 | 
					TEST_P(ThrowsPredicateTest, FailWrongTypeNonStd) {
 | 
				
			||||||
@@ -8262,7 +8253,7 @@ TEST_P(ThrowsPredicateTest, FailWrongTypeNonStd) {
 | 
				
			|||||||
          []() { throw 10; }, &listener));
 | 
					          []() { throw 10; }, &listener));
 | 
				
			||||||
  EXPECT_THAT(
 | 
					  EXPECT_THAT(
 | 
				
			||||||
      listener.str(),
 | 
					      listener.str(),
 | 
				
			||||||
      testing::HasSubstr("throws an exception of an unknown type"));
 | 
					      HasSubstr("throws an exception of an unknown type"));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TEST_P(ThrowsPredicateTest, FailWrongMessage) {
 | 
					TEST_P(ThrowsPredicateTest, FailWrongMessage) {
 | 
				
			||||||
@@ -8271,8 +8262,8 @@ TEST_P(ThrowsPredicateTest, FailWrongMessage) {
 | 
				
			|||||||
  EXPECT_FALSE(
 | 
					  EXPECT_FALSE(
 | 
				
			||||||
      matcher.MatchAndExplain(
 | 
					      matcher.MatchAndExplain(
 | 
				
			||||||
          []() { throw std::runtime_error("wrong message"); }, &listener));
 | 
					          []() { throw std::runtime_error("wrong message"); }, &listener));
 | 
				
			||||||
  EXPECT_THAT(listener.str(), testing::HasSubstr("std::runtime_error"));
 | 
					  EXPECT_THAT(listener.str(), HasSubstr("std::runtime_error"));
 | 
				
			||||||
  EXPECT_THAT(listener.str(), testing::HasSubstr("wrong message"));
 | 
					  EXPECT_THAT(listener.str(), HasSubstr("wrong message"));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
TEST_P(ThrowsPredicateTest, FailNoThrow) {
 | 
					TEST_P(ThrowsPredicateTest, FailNoThrow) {
 | 
				
			||||||
@@ -8283,18 +8274,16 @@ TEST_P(ThrowsPredicateTest, FailNoThrow) {
 | 
				
			|||||||
          []() { (void)0; }, &listener));
 | 
					          []() { (void)0; }, &listener));
 | 
				
			||||||
  EXPECT_THAT(
 | 
					  EXPECT_THAT(
 | 
				
			||||||
      listener.str(),
 | 
					      listener.str(),
 | 
				
			||||||
      testing::HasSubstr("does not throw any exception"));
 | 
					      HasSubstr("does not throw any exception"));
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
INSTANTIATE_TEST_SUITE_P(AllMessagePredicates, ThrowsPredicateTest,
 | 
					INSTANTIATE_TEST_SUITE_P(AllMessagePredicates, ThrowsPredicateTest,
 | 
				
			||||||
    ::testing::Values(
 | 
					    Values(
 | 
				
			||||||
        static_cast<Matcher<void (*)()>>(
 | 
					        static_cast<Matcher<void (*)()>>(
 | 
				
			||||||
            Throws<std::runtime_error>(
 | 
					            Throws<std::runtime_error>(
 | 
				
			||||||
                Property(&std::exception::what, HasSubstr("error message")))),
 | 
					                Property(&std::exception::what, HasSubstr("error message")))),
 | 
				
			||||||
        static_cast<Matcher<void (*)()>>(
 | 
					        static_cast<Matcher<void (*)()>>(
 | 
				
			||||||
            ThrowsMessage<std::runtime_error>(HasSubstr("error message"))),
 | 
					            ThrowsMessage<std::runtime_error>(HasSubstr("error message")))));
 | 
				
			||||||
        static_cast<Matcher<void (*)()>>(
 | 
					 | 
				
			||||||
            ThrowsMessageHasSubstr<std::runtime_error>("error message"))));
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Tests that Throws<E1>(Matcher<E2>{}) compiles even when E2 != const E1&.
 | 
					// Tests that Throws<E1>(Matcher<E2>{}) compiles even when E2 != const E1&.
 | 
				
			||||||
TEST(ThrowsPredicateCompilesTest, ExceptionMatcherAcceptsBroadType) {
 | 
					TEST(ThrowsPredicateCompilesTest, ExceptionMatcherAcceptsBroadType) {
 | 
				
			||||||
@@ -8331,26 +8320,6 @@ TEST(ThrowsPredicateCompilesTest, MessageMatcherAcceptsNonMatcher) {
 | 
				
			|||||||
          []() { throw std::runtime_error("wrong error message"); }));
 | 
					          []() { 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<void (*)()> matcher = ThrowsMessageHasSubstr<std::runtime_error>(
 | 
					 | 
				
			||||||
      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
 | 
					#endif  // GTEST_HAS_EXCEPTIONS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}  // namespace
 | 
					}  // namespace
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user