[llvm] r329288 - [Testing/Support]: Better matching of Error failure states

Pavel Labath via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 5 07:32:10 PDT 2018


Author: labath
Date: Thu Apr  5 07:32:10 2018
New Revision: 329288

URL: http://llvm.org/viewvc/llvm-project?rev=329288&view=rev
Log:
[Testing/Support]: Better matching of Error failure states

Summary:
The existing Failed() matcher only allowed asserting that the operation
failed, but it was not possible to verify any details of the returned
error.

This patch adds two new matchers, which make this possible:
- Failed<InfoT>() verifies that the operation failed with a single error
  of a given type.
- Failed<InfoT>(M) additionally check that the contained error info
  object is matched by the nested matcher M.

To make these work, I've changed the implementation of the ErrorHolder
class. Now, instead of just storing the string representation of the
Error, it fetches the ErrorInfo objects and stores then as a list of
shared pointers. This way, ErrorHolder remains copyable, while still
retaining the full information contained in the Error object.

In case the Error object contains two or more errors, the new matchers
will fail to match, instead of trying to match all (or any) of the
individual ErrorInfo objects. This seemed to be the most sensible
behavior for when one wants to match exact error details, but I could be
convinced otherwise...

Reviewers: zturner, lhames

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D44925

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

Modified: llvm/trunk/include/llvm/Testing/Support/Error.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Testing/Support/Error.h?rev=329288&r1=329287&r2=329288&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Testing/Support/Error.h (original)
+++ llvm/trunk/include/llvm/Testing/Support/Error.h Thu Apr  5 07:32:10 2018
@@ -38,7 +38,7 @@ public:
 
   bool MatchAndExplain(const ExpectedHolder<T> &Holder,
                        testing::MatchResultListener *listener) const override {
-    if (!Holder.Success)
+    if (!Holder.Success())
       return false;
 
     bool result = Matcher.MatchAndExplain(*Holder.Exp, listener);
@@ -82,6 +82,53 @@ private:
   M Matcher;
 };
 
+template <typename InfoT>
+class ErrorMatchesMono : public testing::MatcherInterface<const ErrorHolder &> {
+public:
+  explicit ErrorMatchesMono(Optional<testing::Matcher<InfoT>> Matcher)
+      : Matcher(std::move(Matcher)) {}
+
+  bool MatchAndExplain(const ErrorHolder &Holder,
+                       testing::MatchResultListener *listener) const override {
+    if (Holder.Success())
+      return false;
+
+    if (Holder.Infos.size() > 1) {
+      *listener << "multiple errors";
+      return false;
+    }
+
+    auto &Info = *Holder.Infos[0];
+    if (!Info.isA<InfoT>()) {
+      *listener << "Error was not of given type";
+      return false;
+    }
+
+    if (!Matcher)
+      return true;
+
+    return Matcher->MatchAndExplain(static_cast<InfoT &>(Info), listener);
+  }
+
+  void DescribeTo(std::ostream *OS) const override {
+    *OS << "failed with Error of given type";
+    if (Matcher) {
+      *OS << " and the error ";
+      Matcher->DescribeTo(OS);
+    }
+  }
+
+  void DescribeNegationTo(std::ostream *OS) const override {
+    *OS << "succeeded or did not fail with the error of given type";
+    if (Matcher) {
+      *OS << " or the error ";
+      Matcher->DescribeNegationTo(OS);
+    }
+  }
+
+private:
+  Optional<testing::Matcher<InfoT>> Matcher;
+};
 } // namespace detail
 
 #define EXPECT_THAT_ERROR(Err, Matcher)                                        \
@@ -94,8 +141,19 @@ private:
 #define ASSERT_THAT_EXPECTED(Err, Matcher)                                     \
   ASSERT_THAT(llvm::detail::TakeExpected(Err), Matcher)
 
-MATCHER(Succeeded, "") { return arg.Success; }
-MATCHER(Failed, "") { return !arg.Success; }
+MATCHER(Succeeded, "") { return arg.Success(); }
+MATCHER(Failed, "") { return !arg.Success(); }
+
+template <typename InfoT>
+testing::Matcher<const detail::ErrorHolder &> Failed() {
+  return MakeMatcher(new detail::ErrorMatchesMono<InfoT>(None));
+}
+
+template <typename InfoT, typename M>
+testing::Matcher<const detail::ErrorHolder &> Failed(M Matcher) {
+  return MakeMatcher(new detail::ErrorMatchesMono<InfoT>(
+      testing::SafeMatcherCast<InfoT>(Matcher)));
+}
 
 template <typename M>
 detail::ValueMatchesPoly<M> HasValue(M Matcher) {

Modified: llvm/trunk/include/llvm/Testing/Support/SupportHelpers.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Testing/Support/SupportHelpers.h?rev=329288&r1=329287&r2=329288&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Testing/Support/SupportHelpers.h (original)
+++ llvm/trunk/include/llvm/Testing/Support/SupportHelpers.h Thu Apr  5 07:32:10 2018
@@ -17,8 +17,9 @@
 namespace llvm {
 namespace detail {
 struct ErrorHolder {
-  bool Success;
-  std::string Message;
+  std::vector<std::shared_ptr<ErrorInfoBase>> Infos;
+
+  bool Success() const { return Infos.empty(); }
 };
 
 template <typename T> struct ExpectedHolder : public ErrorHolder {
@@ -29,15 +30,22 @@ template <typename T> struct ExpectedHol
 };
 
 inline void PrintTo(const ErrorHolder &Err, std::ostream *Out) {
-  *Out << (Err.Success ? "succeeded" : "failed");
-  if (!Err.Success) {
-    *Out << "  (" << StringRef(Err.Message).trim().str() << ")";
+  raw_os_ostream OS(*Out);
+  OS << (Err.Success() ? "succeeded" : "failed");
+  if (!Err.Success()) {
+    const char *Delim = "  (";
+    for (const auto &Info : Err.Infos) {
+      OS << Delim;
+      Delim = "; ";
+      Info->log(OS);
+    }
+    OS << ")";
   }
 }
 
 template <typename T>
 void PrintTo(const ExpectedHolder<T> &Item, std::ostream *Out) {
-  if (Item.Success) {
+  if (Item.Success()) {
     *Out << "succeeded with value " << ::testing::PrintToString(*Item.Exp);
   } else {
     PrintTo(static_cast<const ErrorHolder &>(Item), Out);

Modified: llvm/trunk/lib/Testing/Support/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Testing/Support/Error.cpp?rev=329288&r1=329287&r2=329288&view=diff
==============================================================================
--- llvm/trunk/lib/Testing/Support/Error.cpp (original)
+++ llvm/trunk/lib/Testing/Support/Error.cpp Thu Apr  5 07:32:10 2018
@@ -14,9 +14,10 @@
 using namespace llvm;
 
 llvm::detail::ErrorHolder llvm::detail::TakeError(llvm::Error Err) {
-  bool Succeeded = !static_cast<bool>(Err);
-  std::string Message;
-  if (!Succeeded)
-    Message = toString(std::move(Err));
-  return {Succeeded, Message};
+  std::vector<std::shared_ptr<ErrorInfoBase>> Infos;
+  handleAllErrors(std::move(Err),
+                  [&Infos](std::unique_ptr<ErrorInfoBase> Info) {
+                    Infos.emplace_back(std::move(Info));
+                  });
+  return {std::move(Infos)};
 }

Modified: llvm/trunk/unittests/Support/ErrorTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/ErrorTest.cpp?rev=329288&r1=329287&r2=329288&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/ErrorTest.cpp (original)
+++ llvm/trunk/unittests/Support/ErrorTest.cpp Thu Apr  5 07:32:10 2018
@@ -726,6 +726,30 @@ TEST(Error, ErrorMatchers) {
   EXPECT_NONFATAL_FAILURE(EXPECT_THAT_ERROR(Error::success(), Failed()),
                           "Expected: failed\n  Actual: succeeded");
 
+  EXPECT_THAT_ERROR(make_error<CustomError>(0), Failed<CustomError>());
+  EXPECT_NONFATAL_FAILURE(
+      EXPECT_THAT_ERROR(Error::success(), Failed<CustomError>()),
+      "Expected: failed with Error of given type\n  Actual: succeeded");
+  EXPECT_NONFATAL_FAILURE(
+      EXPECT_THAT_ERROR(make_error<CustomError>(0), Failed<CustomSubError>()),
+      "Error was not of given type");
+  EXPECT_NONFATAL_FAILURE(
+      EXPECT_THAT_ERROR(
+          joinErrors(make_error<CustomError>(0), make_error<CustomError>(1)),
+          Failed<CustomError>()),
+      "multiple errors");
+
+  EXPECT_THAT_ERROR(
+      make_error<CustomError>(0),
+      Failed<CustomError>(testing::Property(&CustomError::getInfo, 0)));
+  EXPECT_NONFATAL_FAILURE(
+      EXPECT_THAT_ERROR(
+          make_error<CustomError>(0),
+          Failed<CustomError>(testing::Property(&CustomError::getInfo, 1))),
+      "Expected: failed with Error of given type and the error is an object "
+      "whose given property is equal to 1\n"
+      "  Actual: failed  (CustomError { 0})");
+
   EXPECT_THAT_EXPECTED(Expected<int>(0), Succeeded());
   EXPECT_NONFATAL_FAILURE(
       EXPECT_THAT_EXPECTED(Expected<int>(make_error<CustomError>(0)),




More information about the llvm-commits mailing list