[llvm] b55c58a - [Error/unittests] Add a FailedWithMessage gtest matcher

Pavel Labath via llvm-commits llvm-commits at lists.llvm.org
Fri Feb 21 06:29:56 PST 2020


Author: Pavel Labath
Date: 2020-02-21T15:29:48+01:00
New Revision: b55c58a2d569f2d92333b05c1a7a00114d75e0a6

URL: https://github.com/llvm/llvm-project/commit/b55c58a2d569f2d92333b05c1a7a00114d75e0a6
DIFF: https://github.com/llvm/llvm-project/commit/b55c58a2d569f2d92333b05c1a7a00114d75e0a6.diff

LOG: [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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Testing/Support/Error.h b/llvm/include/llvm/Testing/Support/Error.h
index 85328f26440b..cd5b79cd6bfb 100644
--- a/llvm/include/llvm/Testing/Support/Error.h
+++ b/llvm/include/llvm/Testing/Support/Error.h
@@ -128,6 +128,36 @@ class ErrorMatchesMono : public testing::MatcherInterface<const ErrorHolder &> {
 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);

diff  --git a/llvm/unittests/Support/ErrorTest.cpp b/llvm/unittests/Support/ErrorTest.cpp
index 8b8a621ab50d..37289e1b2356 100644
--- a/llvm/unittests/Support/ErrorTest.cpp
+++ b/llvm/unittests/Support/ErrorTest.cpp
@@ -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";


        


More information about the llvm-commits mailing list