//===- llvm/Testing/Support/Error.h ---------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef LLVM_TESTING_SUPPORT_ERROR_H #define LLVM_TESTING_SUPPORT_ERROR_H #include "llvm/ADT/Optional.h" #include "llvm/Support/Error.h" #include "llvm/Testing/Support/SupportHelpers.h" #include "gmock/gmock.h" #include namespace llvm { namespace detail { ErrorHolder TakeError(Error Err); template ExpectedHolder TakeExpected(Expected &Exp) { return {TakeError(Exp.takeError()), Exp}; } template ExpectedHolder TakeExpected(Expected &&Exp) { return TakeExpected(Exp); } template class ValueMatchesMono : public testing::MatcherInterface &> { public: explicit ValueMatchesMono(const testing::Matcher &Matcher) : Matcher(Matcher) {} bool MatchAndExplain(const ExpectedHolder &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 Matcher; }; template class ValueMatchesPoly { public: explicit ValueMatchesPoly(const M &Matcher) : Matcher(Matcher) {} template operator testing::Matcher &>() const { return MakeMatcher( new ValueMatchesMono(testing::SafeMatcherCast(Matcher))); } private: M Matcher; }; template class ErrorMatchesMono : public testing::MatcherInterface { public: explicit ErrorMatchesMono(Optional> 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()) { *listener << "Error was not of given type"; return false; } if (!Matcher) return true; return Matcher->MatchAndExplain(static_cast(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> Matcher; }; class ErrorMessageMatches : public testing::MatcherInterface { public: explicit ErrorMessageMatches( testing::Matcher> Matcher) : Matcher(std::move(Matcher)) {} bool MatchAndExplain(const ErrorHolder &Holder, testing::MatchResultListener *listener) const override { std::vector Messages; for (const std::shared_ptr &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> Matcher; }; } // namespace detail #define EXPECT_THAT_ERROR(Err, Matcher) \ EXPECT_THAT(llvm::detail::TakeError(Err), Matcher) #define ASSERT_THAT_ERROR(Err, Matcher) \ ASSERT_THAT(llvm::detail::TakeError(Err), Matcher) #define EXPECT_THAT_EXPECTED(Err, Matcher) \ EXPECT_THAT(llvm::detail::TakeExpected(Err), Matcher) #define ASSERT_THAT_EXPECTED(Err, Matcher) \ ASSERT_THAT(llvm::detail::TakeExpected(Err), Matcher) MATCHER(Succeeded, "") { return arg.Success(); } MATCHER(Failed, "") { return !arg.Success(); } template testing::Matcher Failed() { return MakeMatcher(new detail::ErrorMatchesMono(None)); } template testing::Matcher Failed(M Matcher) { return MakeMatcher(new detail::ErrorMatchesMono( testing::SafeMatcherCast(Matcher))); } template testing::Matcher FailedWithMessage(M... Matcher) { static_assert(sizeof...(M) > 0, ""); return MakeMatcher( new detail::ErrorMessageMatches(testing::ElementsAre(Matcher...))); } template testing::Matcher FailedWithMessageArray(M Matcher) { return MakeMatcher(new detail::ErrorMessageMatches(Matcher)); } template detail::ValueMatchesPoly HasValue(M Matcher) { return detail::ValueMatchesPoly(Matcher); } } // namespace llvm #endif