LLVM: include/llvm/Testing/Support/Error.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9#ifndef LLVM_TESTING_SUPPORT_ERROR_H

10#define LLVM_TESTING_SUPPORT_ERROR_H

11

14

15#include "gmock/gmock.h"

16#include

17

18namespace llvm {

21

23 return {TakeError(Exp.takeError()), Exp};

24}

25

29

30template

32 : public testing::MatcherInterface<const ExpectedHolder &> {

33public:

36

38 testing::MatchResultListener *listener) const override {

40 return false;

41

42 bool result = Matcher.MatchAndExplain(*Holder.Exp, listener);

43

44 if (result || !listener->IsInterested())

45 return result;

46 *listener << "(";

47 Matcher.DescribeNegationTo(listener->stream());

48 *listener << ")";

49 return result;

50 }

51

52 void DescribeTo(std::ostream *OS) const override {

53 *OS << "succeeded with value (";

54 Matcher.DescribeTo(OS);

55 *OS << ")";

56 }

57

59 *OS << "did not succeed or value (";

60 Matcher.DescribeNegationTo(OS);

61 *OS << ")";

62 }

63

64private:

65 testing::Matcher Matcher;

66};

67

68template

70public:

72

73 template

74 operator testing::Matcher<const ExpectedHolder &>() const {

75 return MakeMatcher(

77 }

78

79private:

80 M Matcher;

81};

82

83template

84class ErrorMatchesMono : public testing::MatcherInterface<const ErrorHolder &> {

85public:

88

90 testing::MatchResultListener *listener) const override {

92 return false;

93

94 if (Holder.Infos.size() > 1) {

95 *listener << "multiple errors";

96 return false;

97 }

98

100 if (Info.isA()) {

101 *listener << "Error was not of given type";

102 return false;

103 }

104

105 if (!Matcher)

106 return true;

107

108 return Matcher->MatchAndExplain(static_cast<InfoT &>(Info), listener);

109 }

110

111 void DescribeTo(std::ostream *OS) const override {

112 *OS << "failed with Error of given type";

113 if (Matcher) {

114 *OS << " and the error ";

115 Matcher->DescribeTo(OS);

116 }

117 }

118

120 *OS << "succeeded or did not fail with the error of given type";

121 if (Matcher) {

122 *OS << " or the error ";

123 Matcher->DescribeNegationTo(OS);

124 }

125 }

126

127private:

128 std::optional<testing::Matcher<InfoT &>> Matcher;

129};

130

132 : public testing::MatcherInterface<const ErrorHolder &> {

133public:

135 testing::Matcher<std::vectorstd::string> Matcher)

136 : Matcher(std::move(Matcher)) {}

137

139 testing::MatchResultListener *listener) const override {

140 std::vectorstd::string Messages;

141 Messages.reserve(Holder.Infos.size());

142 for (const std::shared_ptr &Info : Holder.Infos)

143 Messages.push_back(Info->message());

144

145 return Matcher.MatchAndExplain(Messages, listener);

146 }

147

148 void DescribeTo(std::ostream *OS) const override {

149 *OS << "failed with Error whose message ";

150 Matcher.DescribeTo(OS);

151 }

152

154 *OS << "failed with an Error whose message ";

155 Matcher.DescribeNegationTo(OS);

156 }

157

158private:

159 testing::Matcher<std::vectorstd::string> Matcher;

160};

161}

162

163#define EXPECT_THAT_ERROR(Err, Matcher) \

164 EXPECT_THAT(llvm::detail::TakeError(Err), Matcher)

165#define ASSERT_THAT_ERROR(Err, Matcher) \

166 ASSERT_THAT(llvm::detail::TakeError(Err), Matcher)

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189#define EXPECT_THAT_EXPECTED(Err, Matcher) \

190 EXPECT_THAT(llvm::detail::TakeExpected(Err), Matcher)

191#define ASSERT_THAT_EXPECTED(Err, Matcher) \

192 ASSERT_THAT(llvm::detail::TakeExpected(Err), Matcher)

193

194MATCHER(Succeeded, "") { return arg.Success(); }

196

197template

198testing::Matcher<const detail::ErrorHolder &> Failed() {

200}

201

202template <typename InfoT, typename M>

203testing::Matcher<const detail::ErrorHolder &> Failed(M Matcher) {

205 testing::SafeMatcherCast<InfoT &>(Matcher)));

206}

207

208template <typename... M>

209testing::Matcher<const detail::ErrorHolder &> FailedWithMessage(M... Matcher) {

210 static_assert(sizeof...(M) > 0);

211 return MakeMatcher(

213}

214

215template

219

220template

224

225}

226

227#endif

Analysis containing CSE Info

Tagged union holding either a T or a Error.

ErrorMatchesMono(std::optional< testing::Matcher< InfoT & > > Matcher)

Definition Error.h:86

void DescribeNegationTo(std::ostream *OS) const override

Definition Error.h:119

bool MatchAndExplain(const ErrorHolder &Holder, testing::MatchResultListener *listener) const override

Definition Error.h:89

void DescribeTo(std::ostream *OS) const override

Definition Error.h:111

ErrorMessageMatches(testing::Matcher< std::vector< std::string > > Matcher)

Definition Error.h:134

void DescribeTo(std::ostream *OS) const override

Definition Error.h:148

bool MatchAndExplain(const ErrorHolder &Holder, testing::MatchResultListener *listener) const override

Definition Error.h:138

void DescribeNegationTo(std::ostream *OS) const override

Definition Error.h:153

bool MatchAndExplain(const ExpectedHolder< T > &Holder, testing::MatchResultListener *listener) const override

Definition Error.h:37

void DescribeNegationTo(std::ostream *OS) const override

Definition Error.h:58

void DescribeTo(std::ostream *OS) const override

Definition Error.h:52

ValueMatchesMono(const testing::Matcher< T > &Matcher)

Definition Error.h:34

ValueMatchesPoly(const M &Matcher)

Definition Error.h:71

A self-contained host- and target-independent arbitrary-precision floating-point software implementat...

ExpectedHolder< T > TakeExpected(Expected< T > &Exp)

Definition Error.h:22

ErrorHolder TakeError(Error Err)

This is an optimization pass for GlobalISel generic memory operations.

testing::Matcher< const detail::ErrorHolder & > FailedWithMessageArray(M Matcher)

Definition Error.h:216

testing::Matcher< const detail::ErrorHolder & > Failed()

Definition Error.h:198

testing::Matcher< const detail::ErrorHolder & > FailedWithMessage(M... Matcher)

Definition Error.h:209

detail::ValueMatchesPoly< M > HasValue(M Matcher)

Definition Error.h:221

OutputIt move(R &&Range, OutputIt Out)

Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.

MATCHER(Succeeded, "")

Definition Error.h:194

Implement std::hash so that hash_code can be used in STL containers.

std::vector< std::shared_ptr< ErrorInfoBase > > Infos