Merge pull request #2904 from AmatanHead:throw-matchers

PiperOrigin-RevId: 327294137
This commit is contained in:
vslashg
2020-08-23 23:51:02 -04:00
2 changed files with 353 additions and 0 deletions

View File

@@ -4725,6 +4725,175 @@ PolymorphicMatcher<internal::variant_matcher::VariantMatcher<T> > VariantWith(
internal::variant_matcher::VariantMatcher<T>(matcher));
}
#if GTEST_HAS_EXCEPTIONS
// Anything inside the `internal` namespace is internal to the implementation
// and must not be used in user code!
namespace internal {
class WithWhatMatcherImpl {
public:
WithWhatMatcherImpl(Matcher<std::string> matcher)
: matcher_(std::move(matcher)) {}
void DescribeTo(std::ostream* os) const {
*os << "contains .what() that ";
matcher_.DescribeTo(os);
}
void DescribeNegationTo(std::ostream* os) const {
*os << "contains .what() that does not ";
matcher_.DescribeTo(os);
}
template <typename Err>
bool MatchAndExplain(const Err& err, MatchResultListener* listener) const {
*listener << "which contains .what() that ";
return matcher_.MatchAndExplain(err.what(), listener);
}
private:
const Matcher<std::string> matcher_;
};
inline PolymorphicMatcher<WithWhatMatcherImpl> WithWhat(
Matcher<std::string> m) {
return MakePolymorphicMatcher(WithWhatMatcherImpl(std::move(m)));
}
template <typename Err>
class ExceptionMatcherImpl {
class NeverThrown {
public:
const char* what() const noexcept {
return "this exception should never be thrown";
}
};
// If the matchee raises an exception of a wrong type, we'd like to
// catch it and print its message and type. To do that, we add an additional
// catch clause:
//
// try { ... }
// catch (const Err&) { /* an expected exception */ }
// catch (const std::exception&) { /* exception of a wrong type */ }
//
// However, if the `Err` itself is `std::exception`, we'd end up with two
// identical `catch` clauses:
//
// try { ... }
// catch (const std::exception&) { /* an expected exception */ }
// catch (const std::exception&) { /* exception of a wrong type */ }
//
// This can cause a warning or an error in some compilers. To resolve
// the issue, we use a fake error type whenever `Err` is `std::exception`:
//
// try { ... }
// catch (const std::exception&) { /* an expected exception */ }
// catch (const NeverThrown&) { /* exception of a wrong type */ }
using DefaultExceptionType = typename std::conditional<
std::is_same<typename std::remove_cv<
typename std::remove_reference<Err>::type>::type,
std::exception>::value,
const NeverThrown&, const std::exception&>::type;
public:
ExceptionMatcherImpl(Matcher<const Err&> matcher)
: matcher_(std::move(matcher)) {}
void DescribeTo(std::ostream* os) const {
*os << "throws an exception which is a " << GetTypeName<Err>();
*os << " which ";
matcher_.DescribeTo(os);
}
void DescribeNegationTo(std::ostream* os) const {
*os << "throws an exception which is not a " << GetTypeName<Err>();
*os << " which ";
matcher_.DescribeNegationTo(os);
}
template <typename T>
bool MatchAndExplain(T&& x, MatchResultListener* listener) const {
try {
(void)(std::forward<T>(x)());
} catch (const Err& err) {
*listener << "throws an exception which is a " << GetTypeName<Err>();
*listener << " ";
return matcher_.MatchAndExplain(err, listener);
} catch (DefaultExceptionType err) {
#if GTEST_HAS_RTTI
*listener << "throws an exception of type " << GetTypeName(typeid(err));
*listener << " ";
#else
*listener << "throws an std::exception-derived type ";
#endif
*listener << "with description \"" << err.what() << "\"";
return false;
} catch (...) {
*listener << "throws an exception of an unknown type";
return false;
}
*listener << "does not throw any exception";
return false;
}
private:
const Matcher<const Err&> matcher_;
};
} // namespace internal
// Throws()
// Throws(exceptionMatcher)
// ThrowsMessage(messageMatcher)
//
// This matcher accepts a callable and verifies that when invoked, it throws
// an exception with the given type and properties.
//
// Examples:
//
// EXPECT_THAT(
// []() { throw std::runtime_error("message"); },
// Throws<std::runtime_error>());
//
// EXPECT_THAT(
// []() { throw std::runtime_error("message"); },
// ThrowsMessage<std::runtime_error>(HasSubstr("message")));
//
// EXPECT_THAT(
// []() { throw std::runtime_error("message"); },
// Throws<std::runtime_error>(
// Property(&std::runtime_error::what, HasSubstr("message"))));
template <typename Err>
PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws() {
return MakePolymorphicMatcher(
internal::ExceptionMatcherImpl<Err>(A<const Err&>()));
}
template <typename Err, typename ExceptionMatcher>
PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> Throws(
const ExceptionMatcher& exception_matcher) {
// Using matcher cast allows users to pass a matcher of a more broad type.
// For example user may want to pass Matcher<std::exception>
// to Throws<std::runtime_error>, or Matcher<int64> to Throws<int32>.
return MakePolymorphicMatcher(internal::ExceptionMatcherImpl<Err>(
SafeMatcherCast<const Err&>(exception_matcher)));
}
template <typename Err, typename MessageMatcher>
PolymorphicMatcher<internal::ExceptionMatcherImpl<Err>> ThrowsMessage(
MessageMatcher&& message_matcher) {
static_assert(std::is_base_of<std::exception, Err>::value,
"expected an std::exception-derived type");
return Throws<Err>(internal::WithWhat(
MatcherCast<std::string>(std::forward<MessageMatcher>(message_matcher))));
}
#endif // GTEST_HAS_EXCEPTIONS
// These macros allow using matchers to check values in Google Test
// tests. ASSERT_THAT(value, matcher) and EXPECT_THAT(value, matcher)
// succeed if and only if the value matches the matcher. If the assertion