1 //===- llvm/Testing/Support/Error.h ---------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_TESTING_SUPPORT_ERROR_H
11 #define LLVM_TESTING_SUPPORT_ERROR_H
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/Support/Error.h"
15 #include "llvm/Testing/Support/SupportHelpers.h"
17 #include "gmock/gmock.h"
22 ErrorHolder TakeError(Error Err);
24 template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &Exp) {
25 return {TakeError(Exp.takeError()), Exp};
28 template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &&Exp) {
29 return TakeExpected(Exp);
33 class ValueMatchesMono
34 : public testing::MatcherInterface<const ExpectedHolder<T> &> {
36 explicit ValueMatchesMono(const testing::Matcher<T> &Matcher)
39 bool MatchAndExplain(const ExpectedHolder<T> &Holder,
40 testing::MatchResultListener *listener) const override {
41 if (!Holder.Success())
44 bool result = Matcher.MatchAndExplain(*Holder.Exp, listener);
49 Matcher.DescribeNegationTo(listener->stream());
54 void DescribeTo(std::ostream *OS) const override {
55 *OS << "succeeded with value (";
56 Matcher.DescribeTo(OS);
60 void DescribeNegationTo(std::ostream *OS) const override {
61 *OS << "did not succeed or value (";
62 Matcher.DescribeNegationTo(OS);
67 testing::Matcher<T> Matcher;
71 class ValueMatchesPoly {
73 explicit ValueMatchesPoly(const M &Matcher) : Matcher(Matcher) {}
76 operator testing::Matcher<const ExpectedHolder<T> &>() const {
78 new ValueMatchesMono<T>(testing::SafeMatcherCast<T>(Matcher)));
85 template <typename InfoT>
86 class ErrorMatchesMono : public testing::MatcherInterface<const ErrorHolder &> {
88 explicit ErrorMatchesMono(Optional<testing::Matcher<InfoT &>> Matcher)
89 : Matcher(std::move(Matcher)) {}
91 bool MatchAndExplain(const ErrorHolder &Holder,
92 testing::MatchResultListener *listener) const override {
96 if (Holder.Infos.size() > 1) {
97 *listener << "multiple errors";
101 auto &Info = *Holder.Infos[0];
102 if (!Info.isA<InfoT>()) {
103 *listener << "Error was not of given type";
110 return Matcher->MatchAndExplain(static_cast<InfoT &>(Info), listener);
113 void DescribeTo(std::ostream *OS) const override {
114 *OS << "failed with Error of given type";
116 *OS << " and the error ";
117 Matcher->DescribeTo(OS);
121 void DescribeNegationTo(std::ostream *OS) const override {
122 *OS << "succeeded or did not fail with the error of given type";
124 *OS << " or the error ";
125 Matcher->DescribeNegationTo(OS);
130 Optional<testing::Matcher<InfoT &>> Matcher;
132 } // namespace detail
134 #define EXPECT_THAT_ERROR(Err, Matcher) \
135 EXPECT_THAT(llvm::detail::TakeError(Err), Matcher)
136 #define ASSERT_THAT_ERROR(Err, Matcher) \
137 ASSERT_THAT(llvm::detail::TakeError(Err), Matcher)
139 #define EXPECT_THAT_EXPECTED(Err, Matcher) \
140 EXPECT_THAT(llvm::detail::TakeExpected(Err), Matcher)
141 #define ASSERT_THAT_EXPECTED(Err, Matcher) \
142 ASSERT_THAT(llvm::detail::TakeExpected(Err), Matcher)
144 MATCHER(Succeeded, "") { return arg.Success(); }
145 MATCHER(Failed, "") { return !arg.Success(); }
147 template <typename InfoT>
148 testing::Matcher<const detail::ErrorHolder &> Failed() {
149 return MakeMatcher(new detail::ErrorMatchesMono<InfoT>(None));
152 template <typename InfoT, typename M>
153 testing::Matcher<const detail::ErrorHolder &> Failed(M Matcher) {
154 return MakeMatcher(new detail::ErrorMatchesMono<InfoT>(
155 testing::SafeMatcherCast<InfoT &>(Matcher)));
158 template <typename M>
159 detail::ValueMatchesPoly<M> HasValue(M Matcher) {
160 return detail::ValueMatchesPoly<M>(Matcher);