[Error/unittests] Add a FailedWithMessage gtest matcher
authorPavel Labath <pavel@labath.sk>
Thu, 20 Feb 2020 14:06:01 +0000 (15:06 +0100)
committerPavel Labath <pavel@labath.sk>
Fri, 21 Feb 2020 14:29:48 +0000 (15:29 +0100)
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

llvm/include/llvm/Testing/Support/Error.h
llvm/unittests/Support/ErrorTest.cpp

index 85328f26440ba65e1ec20c91e00e5bccbc49297a..cd5b79cd6bfb0eda80030c3467b903c420aaa1a6 100644 (file)
@@ -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);
index 8b8a621ab50db6181a7b35ba13362be9c77dd5bf..37289e1b23562407364c3417ae30b193e9b2c69a 100644 (file)
@@ -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";