mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-26 04:32:44 +01:00
[Error/unittests] Add a FailedWithMessage gtest matcher
Summary: We already have a "Failed" matcher, which can be used to check any property of the Error object. However, most frequently one just wants to check the error message, and while this is possible with the "Failed" matcher, it is also very convoluted (Failed<ErrorInfoBase>(testing::Property(&ErrorInfoBase::message, "the message"))). Now, one can just write: FailedWithMessage("the message"). I expect that most of the usages will remain this simple, but the argument of the matcher is not limited to simple strings -- the argument of the matcher can be any other matcher, so one can write more complicated assertions if needed (FailedWithMessage(ContainsRegex("foo|bar"))). If one wants to match multiple error messages, he can pass multiple arguments to the matcher. If one wants to match the message list as a whole (perhaps to check the message count), I've also included a FailedWithMessageArray matcher, which takes a single matcher receiving a vector of error message strings. Reviewers: sammccall, dblaikie, jhenderson Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D74898
This commit is contained in:
parent
9776683c03
commit
ed1f03baaf
@ -128,6 +128,36 @@ public:
|
||||
private:
|
||||
Optional<testing::Matcher<InfoT &>> Matcher;
|
||||
};
|
||||
|
||||
class ErrorMessageMatches
|
||||
: public testing::MatcherInterface<const ErrorHolder &> {
|
||||
public:
|
||||
explicit ErrorMessageMatches(
|
||||
testing::Matcher<std::vector<std::string>> Matcher)
|
||||
: Matcher(std::move(Matcher)) {}
|
||||
|
||||
bool MatchAndExplain(const ErrorHolder &Holder,
|
||||
testing::MatchResultListener *listener) const override {
|
||||
std::vector<std::string> Messages;
|
||||
for (const std::shared_ptr<ErrorInfoBase> &Info: Holder.Infos)
|
||||
Messages.push_back(Info->message());
|
||||
|
||||
return Matcher.MatchAndExplain(Messages, listener);
|
||||
}
|
||||
|
||||
void DescribeTo(std::ostream *OS) const override {
|
||||
*OS << "failed with Error whose message ";
|
||||
Matcher.DescribeTo(OS);
|
||||
}
|
||||
|
||||
void DescribeNegationTo(std::ostream *OS) const override {
|
||||
*OS << "failed with an Error whose message ";
|
||||
Matcher.DescribeNegationTo(OS);
|
||||
}
|
||||
|
||||
private:
|
||||
testing::Matcher<std::vector<std::string>> Matcher;
|
||||
};
|
||||
} // namespace detail
|
||||
|
||||
#define EXPECT_THAT_ERROR(Err, Matcher) \
|
||||
@ -154,6 +184,18 @@ testing::Matcher<const detail::ErrorHolder &> Failed(M Matcher) {
|
||||
testing::SafeMatcherCast<InfoT &>(Matcher)));
|
||||
}
|
||||
|
||||
template <typename... M>
|
||||
testing::Matcher<const detail::ErrorHolder &> FailedWithMessage(M... Matcher) {
|
||||
static_assert(sizeof...(M) > 0, "");
|
||||
return MakeMatcher(
|
||||
new detail::ErrorMessageMatches(testing::ElementsAre(Matcher...)));
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
testing::Matcher<const detail::ErrorHolder &> FailedWithMessageArray(M Matcher) {
|
||||
return MakeMatcher(new detail::ErrorMessageMatches(Matcher));
|
||||
}
|
||||
|
||||
template <typename M>
|
||||
detail::ValueMatchesPoly<M> HasValue(M Matcher) {
|
||||
return detail::ValueMatchesPoly<M>(Matcher);
|
||||
|
@ -840,6 +840,46 @@ TEST(Error, HasValueMatcher) {
|
||||
" Actual: failed (CustomError {0})");
|
||||
}
|
||||
|
||||
TEST(Error, FailedWithMessageMatcher) {
|
||||
EXPECT_THAT_EXPECTED(Expected<int>(make_error<CustomError>(0)),
|
||||
FailedWithMessage("CustomError {0}"));
|
||||
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_THAT_EXPECTED(Expected<int>(make_error<CustomError>(1)),
|
||||
FailedWithMessage("CustomError {0}")),
|
||||
"Expected: failed with Error whose message has 1 element that is equal "
|
||||
"to \"CustomError {0}\"\n"
|
||||
" Actual: failed (CustomError {1})");
|
||||
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_THAT_EXPECTED(Expected<int>(0),
|
||||
FailedWithMessage("CustomError {0}")),
|
||||
"Expected: failed with Error whose message has 1 element that is equal "
|
||||
"to \"CustomError {0}\"\n"
|
||||
" Actual: succeeded with value 0");
|
||||
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_THAT_EXPECTED(Expected<int>(make_error<CustomError>(0)),
|
||||
FailedWithMessage("CustomError {0}", "CustomError {0}")),
|
||||
"Expected: failed with Error whose message has 2 elements where\n"
|
||||
"element #0 is equal to \"CustomError {0}\",\n"
|
||||
"element #1 is equal to \"CustomError {0}\"\n"
|
||||
" Actual: failed (CustomError {0}), which has 1 element");
|
||||
|
||||
EXPECT_NONFATAL_FAILURE(
|
||||
EXPECT_THAT_EXPECTED(
|
||||
Expected<int>(joinErrors(make_error<CustomError>(0),
|
||||
make_error<CustomError>(0))),
|
||||
FailedWithMessage("CustomError {0}")),
|
||||
"Expected: failed with Error whose message has 1 element that is equal "
|
||||
"to \"CustomError {0}\"\n"
|
||||
" Actual: failed (CustomError {0}; CustomError {0}), which has 2 elements");
|
||||
|
||||
EXPECT_THAT_ERROR(
|
||||
joinErrors(make_error<CustomError>(0), make_error<CustomError>(0)),
|
||||
FailedWithMessageArray(testing::SizeIs(2)));
|
||||
}
|
||||
|
||||
TEST(Error, C_API) {
|
||||
EXPECT_THAT_ERROR(unwrap(wrap(Error::success())), Succeeded())
|
||||
<< "Failed to round-trip Error success value via C API";
|
||||
|
Loading…
Reference in New Issue
Block a user