LLVM  7.0.0svn
Error.h
Go to the documentation of this file.
1 //===- llvm/Testing/Support/Error.h ---------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLVM_TESTING_SUPPORT_ERROR_H
11 #define LLVM_TESTING_SUPPORT_ERROR_H
12 
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/Support/Error.h"
16 
17 #include "gmock/gmock.h"
18 #include <ostream>
19 
20 namespace llvm {
21 namespace detail {
22 ErrorHolder TakeError(Error Err);
23 
24 template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &Exp) {
25  return {TakeError(Exp.takeError()), Exp};
26 }
27 
28 template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &&Exp) {
29  return TakeExpected(Exp);
30 }
31 
32 template <typename T>
34  : public testing::MatcherInterface<const ExpectedHolder<T> &> {
35 public:
36  explicit ValueMatchesMono(const testing::Matcher<T> &Matcher)
37  : Matcher(Matcher) {}
38 
39  bool MatchAndExplain(const ExpectedHolder<T> &Holder,
40  testing::MatchResultListener *listener) const override {
41  if (!Holder.Success)
42  return false;
43 
44  bool result = Matcher.MatchAndExplain(*Holder.Exp, listener);
45 
46  if (result)
47  return result;
48  *listener << "(";
49  Matcher.DescribeNegationTo(listener->stream());
50  *listener << ")";
51  return result;
52  }
53 
54  void DescribeTo(std::ostream *OS) const override {
55  *OS << "succeeded with value (";
56  Matcher.DescribeTo(OS);
57  *OS << ")";
58  }
59 
60  void DescribeNegationTo(std::ostream *OS) const override {
61  *OS << "did not succeed or value (";
62  Matcher.DescribeNegationTo(OS);
63  *OS << ")";
64  }
65 
66 private:
67  testing::Matcher<T> Matcher;
68 };
69 
70 template<typename M>
72 public:
73  explicit ValueMatchesPoly(const M &Matcher) : Matcher(Matcher) {}
74 
75  template <typename T>
76  operator testing::Matcher<const ExpectedHolder<T> &>() const {
77  return MakeMatcher(
78  new ValueMatchesMono<T>(testing::SafeMatcherCast<T>(Matcher)));
79  }
80 
81 private:
82  M Matcher;
83 };
84 
85 } // namespace detail
86 
87 #define EXPECT_THAT_ERROR(Err, Matcher) \
88  EXPECT_THAT(llvm::detail::TakeError(Err), Matcher)
89 #define ASSERT_THAT_ERROR(Err, Matcher) \
90  ASSERT_THAT(llvm::detail::TakeError(Err), Matcher)
91 
92 #define EXPECT_THAT_EXPECTED(Err, Matcher) \
93  EXPECT_THAT(llvm::detail::TakeExpected(Err), Matcher)
94 #define ASSERT_THAT_EXPECTED(Err, Matcher) \
95  ASSERT_THAT(llvm::detail::TakeExpected(Err), Matcher)
96 
97 MATCHER(Succeeded, "") { return arg.Success; }
98 MATCHER(Failed, "") { return !arg.Success; }
99 
100 template <typename M>
102  return detail::ValueMatchesPoly<M>(Matcher);
103 }
104 
105 } // namespace llvm
106 
107 #endif
ValueMatchesPoly(const M &Matcher)
Definition: Error.h:73
ErrorHolder TakeError(Error Err)
Definition: Error.cpp:16
void DescribeNegationTo(std::ostream *OS) const override
Definition: Error.h:60
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
ExpectedHolder< T > TakeExpected(Expected< T > &Exp)
Definition: Error.h:24
detail::ValueMatchesPoly< M > HasValue(M Matcher)
Definition: Error.h:101
Error takeError()
Take ownership of the stored error.
Definition: Error.h:537
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
void DescribeTo(std::ostream *OS) const override
Definition: Error.h:54
bool MatchAndExplain(const ExpectedHolder< T > &Holder, testing::MatchResultListener *listener) const override
Definition: Error.h:39
MATCHER(Succeeded, "")
Definition: Error.h:97
ValueMatchesMono(const testing::Matcher< T > &Matcher)
Definition: Error.h:36