4
4
#include < vector>
5
5
#include " runnable.hpp"
6
6
#include " matchers/basematcher.hpp"
7
+ #include " matchers/be.hpp"
7
8
#include " matchers/be_between.hpp"
9
+ #include " matchers/be_within.hpp"
8
10
#include " matchers/include.hpp"
9
11
#include " matchers/equal.hpp"
10
- #include " matchers/be_within.hpp"
11
12
12
13
namespace Expectations {
13
14
@@ -17,10 +18,10 @@ namespace Expectations {
17
18
* expect(something)
18
19
* expect([] -> auto { return something })
19
20
*
20
- * // used with `to` matchers
21
+ * // used with a matcher
21
22
* expect(actual).to_equal(target)
22
23
*
23
- * // used with `not` and matchers
24
+ * // used with `not` and a matcher
24
25
* expect(actual).not_().to_equal(target)
25
26
* @endcode
26
27
*
@@ -31,32 +32,64 @@ template <class A>
31
32
class Expectation : public Child {
32
33
A target;
33
34
std::function<A(void )> block;
34
- bool has_block = false ;
35
- bool is_positive = true ;
35
+ bool has_block = false ; // Is the target a lambda?
36
+ bool is_positive = true ; // Have we been negated?
36
37
37
38
template <typename E>
38
39
bool setup_and_run (Matchers::BaseMatcher<A, E> &matcher, std::string msg);
39
40
40
41
public:
42
+ /* *
43
+ * @brief Create an Expectation using a value.
44
+ *
45
+ * @param value The target to test, an explicit value.
46
+ *
47
+ * @return The constructed Expectation.
48
+ */
41
49
Expectation (A &value) : target(value) {}
42
50
51
+ /* *
52
+ * @brief Create an Expectation using a function.
53
+ *
54
+ * This does not simply contain the return value of the given
55
+ * lambda, but instead wraps the thunk, delaying execution until it is time
56
+ * to perform the match.
57
+ *
58
+ * @param block A function that returns some value to test against.
59
+ *
60
+ * @return The constructed Expectation.
61
+ */
43
62
Expectation (std::function<A(void )> block) : block(block), has_block(true ) {}
44
63
64
+ /* *
65
+ * @brief Create an Expectation using an initializer list.
66
+ *
67
+ * @param init_list The initializer list to match against.
68
+ *
69
+ * @return The constructed Expectation.
70
+ */
45
71
template <typename U>
46
72
Expectation (std::initializer_list<U> init_list)
47
73
: target(std::vector<U>(init_list)) {}
48
74
49
- A &get_target ();
50
- bool get_sign ();
75
+ /* * @brief Get the target of the expectation. */
76
+ A &get_target () { return target; }
77
+
78
+ /* * @brief Get whether the expectation is normal or negated. */
79
+ bool get_sign () { return is_positive; }
51
80
52
81
Expectation ¬_ ();
53
- Expectation &to ();
82
+
83
+ bool to_be (std::function<bool (A)>, std::string msg = "");
84
+ bool to_be_null (std::string msg = " " );
85
+ bool to_be_true (std::string msg = " " );
86
+ bool to_be_false (std::string msg = " " );
54
87
55
88
template <typename U>
56
89
bool to_include (std::initializer_list<U> expected, std::string msg = " " );
57
90
58
- template <typename U >
59
- bool to_include (U expected, std::string msg = " " );
91
+ template <typename E >
92
+ bool to_include (E expected, std::string msg = " " );
60
93
61
94
template <typename E>
62
95
bool to_be_between (E min, E max, Matchers::RangeMode mode,
@@ -72,18 +105,8 @@ class Expectation : public Child {
72
105
bool to_be_within (E expected, std::string msg = " " );
73
106
};
74
107
75
- template <typename A>
76
- A &Expectation<A>::get_target() {
77
- return target;
78
- }
79
-
80
- template <typename A>
81
- bool Expectation<A>::get_sign() {
82
- return is_positive;
83
- }
84
-
85
108
/* *
86
- * @brief Inverts the current matcher
109
+ * @brief Invert the current matcher.
87
110
*/
88
111
template <typename A>
89
112
Expectation<A> &Expectation<A>::not_() {
@@ -106,6 +129,69 @@ bool Expectation<A>::setup_and_run(Matchers::BaseMatcher<A, E> &matcher,
106
129
return matcher (msg);
107
130
}
108
131
132
+ /* *
133
+ * @brief Match using the Matchers::Be matcher.
134
+ *
135
+ * @param test The function to use to test the output of the
136
+ * expectation expression.
137
+ * @param msg Optional message to give on failure.
138
+ *
139
+ * @return Whether the expectation succeeds or fails.
140
+ */ template <typename A>
141
+ bool Expectation<A>::to_be(std::function<bool (A)> test, std::string msg) {
142
+ Matchers::Be<A> matcher (*this , test);
143
+ return setup_and_run (matcher, msg);
144
+ }
145
+
146
+ /* *
147
+ * @brief Match using the Matchers::BeNullptr matcher.
148
+ *
149
+ * @param msg Optional message to give on failure.
150
+ *
151
+ * @return
152
+ */
153
+ template <typename A>
154
+ bool Expectation<A>::to_be_null(std::string msg) {
155
+ Matchers::BeNullptr<A> matcher (*this );
156
+ return setup_and_run (matcher, msg);
157
+ }
158
+
159
+ /* *
160
+ * @brief Match using the Matchers::Be matcher, testing for truthy-ness.
161
+ *
162
+ * @param msg Optional message to give on failure.
163
+ *
164
+ * @return
165
+ */
166
+ template <typename A>
167
+ bool Expectation<A>::to_be_true(std::string msg) {
168
+ Matchers::Be<A> matcher (*this , [](A t) { return static_cast <bool >(t); });
169
+ return setup_and_run (matcher, msg);
170
+ }
171
+
172
+ /* *
173
+ * @brief Match using the Matchers::Be matcher, testing for falsy-ness.
174
+ *
175
+ * @param msg Optional message to give on failure.
176
+ *
177
+ * @return
178
+ */
179
+ template <typename A>
180
+ bool Expectation<A>::to_be_false(std::string msg) {
181
+ Matchers::Be<A> matcher (*this , [](A t) { return not static_cast <bool >(t); });
182
+ return setup_and_run (matcher, msg);
183
+ }
184
+
185
+ /* *
186
+ * @brief Match using the Matchers::BeBetween matcher.
187
+ *
188
+ * @param min
189
+ * @param max
190
+ * @param mode
191
+ * @param msg Optional message to give on failure.
192
+ *
193
+ * @return
194
+ */
109
195
template <typename A>
110
196
template <typename E>
111
197
bool Expectation<A>::to_be_between(E min, E max, Matchers::RangeMode mode,
@@ -114,12 +200,30 @@ bool Expectation<A>::to_be_between(E min, E max, Matchers::RangeMode mode,
114
200
return setup_and_run (matcher, msg);
115
201
}
116
202
203
+ /* *
204
+ * @brief Match using the Matchers::BeBetween matcher, with an explicit
205
+ * range mode.
206
+ *
207
+ * @param min
208
+ * @param max
209
+ * @param msg Optional message to give on failure.
210
+ *
211
+ * @return
212
+ */
117
213
template <typename A>
118
214
template <typename E>
119
215
bool Expectation<A>::to_be_between(E min, E max, std::string msg) {
120
216
return this ->to_be_between (min, max, Matchers::RangeMode::inclusive, msg);
121
217
}
122
218
219
+ /* *
220
+ * @brief Match using the Matchers::Include matcher, given an initializer list.
221
+ *
222
+ * @param expected
223
+ * @param msg Optional message to give on failure.
224
+ *
225
+ * @return
226
+ */
123
227
template <typename A>
124
228
template <typename U>
125
229
bool Expectation<A>::to_include(std::initializer_list<U> expected,
@@ -128,20 +232,44 @@ bool Expectation<A>::to_include(std::initializer_list<U> expected,
128
232
return setup_and_run (matcher, msg);
129
233
}
130
234
235
+ /* *
236
+ * @brief Match using the Matchers::Include matcher.
237
+ *
238
+ * @param expected
239
+ * @param msg Optional message to give on failure.
240
+ *
241
+ * @return
242
+ */
131
243
template <typename A>
132
- template <typename U >
133
- bool Expectation<A>::to_include(U expected, std::string msg) {
134
- Matchers::Include<A, U, U > matcher (*this , expected);
244
+ template <typename E >
245
+ bool Expectation<A>::to_include(E expected, std::string msg) {
246
+ Matchers::Include<A, E, E > matcher (*this , expected);
135
247
return setup_and_run (matcher, msg);
136
248
}
137
249
250
+ /* *
251
+ * @brief Match using the Matchers::Equal matcher.
252
+ *
253
+ * @param expected
254
+ * @param msg Optional message to give on failure.
255
+ *
256
+ * @return
257
+ */
138
258
template <typename A>
139
259
template <typename E>
140
260
bool Expectation<A>::to_equal(E expected, std::string msg) {
141
261
Matchers::Equal<A, E> matcher (*this , expected);
142
262
return setup_and_run (matcher, msg);
143
263
}
144
264
265
+ /* *
266
+ * @brief Match using the Matchers::BeWithin matcher.
267
+ *
268
+ * @param expected
269
+ * @param msg Optional message to give on failure.
270
+ *
271
+ * @return
272
+ */
145
273
template <typename A>
146
274
template <typename E>
147
275
bool Expectation<A>::to_be_within(E expected, std::string msg) {
0 commit comments