diff options
Diffstat (limited to 'include/llvm/Testing/Support/Error.h')
-rw-r--r-- | include/llvm/Testing/Support/Error.h | 80 |
1 files changed, 59 insertions, 21 deletions
diff --git a/include/llvm/Testing/Support/Error.h b/include/llvm/Testing/Support/Error.h index f23d289266adc..50889b9c66f57 100644 --- a/include/llvm/Testing/Support/Error.h +++ b/include/llvm/Testing/Support/Error.h @@ -22,17 +22,66 @@ namespace detail { ErrorHolder TakeError(Error Err); template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &Exp) { - llvm::detail::ExpectedHolder<T> Result; - auto &EH = static_cast<llvm::detail::ErrorHolder &>(Result); - EH = TakeError(Exp.takeError()); - if (Result.Success) - Result.Value = &(*Exp); - return Result; + return {TakeError(Exp.takeError()), Exp}; } template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &&Exp) { return TakeExpected(Exp); } + +template <typename T> +class ValueMatchesMono + : public testing::MatcherInterface<const ExpectedHolder<T> &> { +public: + explicit ValueMatchesMono(const testing::Matcher<T> &Matcher) + : Matcher(Matcher) {} + + bool MatchAndExplain(const ExpectedHolder<T> &Holder, + testing::MatchResultListener *listener) const override { + if (!Holder.Success) + return false; + + bool result = Matcher.MatchAndExplain(*Holder.Exp, listener); + + if (result) + return result; + *listener << "("; + Matcher.DescribeNegationTo(listener->stream()); + *listener << ")"; + return result; + } + + void DescribeTo(std::ostream *OS) const override { + *OS << "succeeded with value ("; + Matcher.DescribeTo(OS); + *OS << ")"; + } + + void DescribeNegationTo(std::ostream *OS) const override { + *OS << "did not succeed or value ("; + Matcher.DescribeNegationTo(OS); + *OS << ")"; + } + +private: + testing::Matcher<T> Matcher; +}; + +template<typename M> +class ValueMatchesPoly { +public: + explicit ValueMatchesPoly(const M &Matcher) : Matcher(Matcher) {} + + template <typename T> + operator testing::Matcher<const ExpectedHolder<T> &>() const { + return MakeMatcher( + new ValueMatchesMono<T>(testing::SafeMatcherCast<T>(Matcher))); + } + +private: + M Matcher; +}; + } // namespace detail #define EXPECT_THAT_ERROR(Err, Matcher) \ @@ -48,22 +97,11 @@ template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &&Exp) { MATCHER(Succeeded, "") { return arg.Success; } MATCHER(Failed, "") { return !arg.Success; } -MATCHER_P(HasValue, value, - "succeeded with value " + testing::PrintToString(value)) { - if (!arg.Success) { - *result_listener << "operation failed"; - return false; - } - - assert(arg.Value.hasValue()); - if (**arg.Value != value) { - *result_listener << "but \"" + testing::PrintToString(**arg.Value) + - "\" != " + testing::PrintToString(value); - return false; - } - - return true; +template <typename M> +detail::ValueMatchesPoly<M> HasValue(M Matcher) { + return detail::ValueMatchesPoly<M>(Matcher); } + } // namespace llvm #endif |