/
catch_matchers.h
250 lines (195 loc) · 9.05 KB
/
catch_matchers.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Created by Phil Nash on 04/03/2012.
* Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef TWOBLUECUBES_CATCH_MATCHERS_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_MATCHERS_HPP_INCLUDED
#include <catch2/catch_common.h>
#include <string>
#include <tuple>
namespace Catch {
namespace Matchers {
namespace Impl {
template<typename... Matchers> struct MatchAllOf;
template<typename... Matchers> struct MatchAnyOf;
template<typename Matcher> struct MatchNotOf;
template<typename Derived>
class MatcherBaseGeneric {
public:
MatcherBaseGeneric() = default;
MatcherBaseGeneric ( MatcherBaseGeneric const& ) = default;
MatcherBaseGeneric& operator = ( MatcherBaseGeneric const& ) = delete;
std::string toString() const;
template<typename Other>
MatchAllOf<Derived, Other> operator && ( Other const& other ) const;
template<typename Other>
MatchAnyOf<Derived, Other> operator || ( Other const& other ) const;
MatchNotOf<Derived> operator ! () const;
Derived const& derived() const { return static_cast<const Derived &>(*this); }
protected:
mutable std::string m_cachedToString;
};
template<typename Derived>
std::string MatcherBaseGeneric<Derived>::toString() const {
if( m_cachedToString.empty() )
m_cachedToString = derived().describe();
return m_cachedToString;
}
template<typename... Matchers>
using Tuple = std::tuple<typename std::add_lvalue_reference<typename std::add_const<Matchers>::type>::type ...>;
template<typename... Matchers>
struct MatchAllOf : MatcherBaseGeneric<MatchAllOf<Matchers...>> {
MatchAllOf( Matchers const &... matchers ) : m_matchers{matchers...}
{}
template<typename ArgT>
bool match_helper( ArgT && ) const {
return true;
}
template<typename ArgT, typename Matcher, typename... Rest>
bool match_helper( ArgT && arg, Matcher &&matcher, Rest &&... rest ) const {
if (!matcher.match(std::forward<ArgT>(arg))) {
return false;
}
return match_helper(std::forward<ArgT>(arg), std::forward<Rest>(rest)...);
}
template<typename ArgT, size_t... Is>
bool match_impl( ArgT && arg, indexSequence<Is...>) const {
return match_helper(std::forward<ArgT>(arg), std::get<Is>(m_matchers)...);
}
template<typename ArgT>
bool match( ArgT && arg ) const {
return match_impl(std::forward<ArgT>(arg), makeIndexSequence<sizeof...(Matchers)>{});
}
std::string describe_helper() const { return {}; }
template<typename Matcher, typename... Rest>
std::string describe_helper( Matcher &&matcher, Rest &&... rest ) const {
return " and " + matcher.describe() + describe_helper(std::forward<Rest>(rest)...);
}
template<size_t... Is>
std::string describe_impl( indexSequence<Is...>) const {
// remove first and
return describe_helper(std::get<Is>(m_matchers)...).substr(5);
}
std::string describe() const {
std::string description;
description.reserve( 4 + sizeof...(Matchers)*32 );
description += "( " + describe_impl(makeIndexSequence<sizeof...(Matchers)>{}) + " )";
return description;
}
template<typename Other, size_t... I>
MatchAllOf<Matchers..., Other> and_helper(Other const &other, indexSequence<I...>) {
return {std::get<I>(m_matchers)..., other};
}
template<typename Other>
MatchAllOf<Matchers..., Other> operator && ( Other const& other ) {
return and_helper(other, makeIndexSequence<sizeof...(Matchers)>());
}
Tuple<Matchers...> m_matchers;
};
template<typename... Matchers>
struct MatchAnyOf : MatcherBaseGeneric<MatchAnyOf<Matchers...>> {
MatchAnyOf( Matchers const &... matchers ) : m_matchers{matchers...}
{}
template<typename ArgT>
bool match_helper( ArgT && ) const {
return false;
}
template<typename ArgT, typename Matcher, typename... Rest>
bool match_helper( ArgT && arg, Matcher &&matcher, Rest &&... rest ) const {
if (matcher.match(std::forward<ArgT>(arg))) {
return true;
}
return match_helper(std::forward<ArgT>(arg), std::forward<Rest>(rest)...);
}
template<typename ArgT, size_t... Is>
bool match_impl( ArgT && arg, indexSequence<Is...>) const {
return match_helper(std::forward<ArgT>(arg), std::get<Is>(m_matchers)...);
}
template<typename ArgT>
bool match( ArgT && arg ) const {
return match_impl(std::forward<ArgT>(arg), makeIndexSequence<sizeof...(Matchers)>{});
}
std::string describe_helper() const { return {}; }
template<typename Matcher, typename... Rest>
std::string describe_helper( Matcher &&matcher, Rest &&... rest ) const {
return " or " + matcher.describe() + describe_helper(std::forward<Rest>(rest)...);
}
template<size_t... Is>
std::string describe_impl( indexSequence<Is...>) const {
// remove first or
return describe_helper(std::get<Is>(m_matchers)...).substr(4);
}
std::string describe() const {
std::string description;
description.reserve( 4 + sizeof...(Matchers)*32 );
description += "( " + describe_impl(makeIndexSequence<sizeof...(Matchers)>{}) + " )";
return description;
}
template<typename Other, size_t... I>
MatchAnyOf<Matchers..., Other> or_helper(Other const &other, indexSequence<I...>) {
return {std::get<I>(m_matchers)..., other};
}
template<typename Other>
MatchAnyOf<Matchers..., Other> operator || ( Other const& other ) {
return or_helper(other, makeIndexSequence<sizeof...(Matchers)>());
}
Tuple<Matchers...> m_matchers;
};
template<typename Matcher>
struct MatchNotOf : MatcherBaseGeneric<MatchNotOf<Matcher>> {
MatchNotOf( Matcher const& underlyingMatcher ) : m_underlyingMatcher( underlyingMatcher ) {}
template<typename ArgT>
bool match( ArgT && arg ) const {
return !m_underlyingMatcher.match( std::forward<ArgT>(arg) );
}
std::string describe() const {
return "not " + m_underlyingMatcher.toString();
}
Matcher const& m_underlyingMatcher;
};
template<typename Derived>
template<typename Other>
MatchAllOf<Derived, Other> MatcherBaseGeneric<Derived>::operator && ( Other const& other ) const {
return {derived(), other};
}
template<typename Derived>
template<typename Other>
MatchAnyOf<Derived, Other> MatcherBaseGeneric<Derived>::operator || ( Other const& other ) const {
return {derived(), other};
}
template<typename Derived>
MatchNotOf<Derived> MatcherBaseGeneric<Derived>::operator ! () const {
return {derived()};
}
template<typename Derived>
Derived const& operator ! (MatchNotOf<Derived> const& matcher) {
return matcher.m_underlyingMatcher;
}
template<typename ObjectT>
struct MatcherMethod {
virtual bool match( ObjectT const& arg ) const = 0;
};
#if defined(__OBJC__)
// Hack to fix Catch GH issue #1661. Could use id for generic Object support.
// use of const for Object pointers is very uncommon and under ARC it causes some kind of signature mismatch that breaks compilation
template<>
struct MatcherMethod<NSString*> {
virtual bool match( NSString* arg ) const = 0;
};
#endif
// for backward compatibility
template<typename T>
struct MatcherBase : MatcherBaseGeneric<MatcherBase<T>>, MatcherMethod<T> {
virtual ~MatcherBase() = default;
virtual std::string describe() const = 0;
};
} // namespace Impl
} // namespace Matchers
using namespace Matchers;
using Matchers::Impl::MatcherBaseGeneric;
using Matchers::Impl::MatcherBase;
} // namespace Catch
#endif // TWOBLUECUBES_CATCH_MATCHERS_HPP_INCLUDED