1
1
/* *
2
2
* @file
3
- * @brief Defines the Expectations:: Expectation class and associated functions
3
+ * @brief Defines the Expectation class and associated functions
4
4
*/
5
+
5
6
#ifndef CPPSPEC_EXPECTATIONS_EXPECTATION_HPP
6
7
#define CPPSPEC_EXPECTATIONS_EXPECTATION_HPP
7
8
#pragma once
25
26
#include " matchers/throw.hpp"
26
27
27
28
namespace CppSpec {
28
- namespace Expectations {
29
29
30
30
/* *
31
31
* @brief Wraps the target of an expectation
@@ -72,89 +72,114 @@ class Expectation : public Child {
72
72
const bool get_ignore_failure () const { return ignore_failure; }
73
73
bool get_ignore_failure () { return ignore_failure; }
74
74
75
+ /* ******** Modifiers *********/
76
+
75
77
virtual Expectation ¬_ () = 0;
76
78
virtual Expectation &ignore () = 0;
77
79
80
+ /* ******** Matchers *********/
81
+
78
82
template <class M >
79
83
Result to (M matcher, std::string msg = " " );
80
84
81
- Result to_satisfy (std::function<bool (A)>, std::string msg = "");
85
+ /* -------- to be... ----------*/
86
+
87
+ Result to_be_false (std::string msg = " " );
88
+ Result to_be_falsy (std::string msg = " " );
82
89
Result to_be_null (std::string msg = " " );
83
90
Result to_be_true (std::string msg = " " );
84
- Result to_be_false (std::string msg = " " );
85
91
Result to_be_truthy (std::string msg = " " );
86
- Result to_be_falsy (std::string msg = " " );
92
+
87
93
template <typename E>
88
- Result to_be_less_than (E rhs, std::string msg = " " );
94
+ Result to_be_between (
95
+ E min, E max, Matchers::RangeMode mode = Matchers::RangeMode::inclusive,
96
+ std::string msg = " " );
97
+
89
98
template <typename E>
90
99
Result to_be_greater_than (E rhs, std::string msg = " " );
91
100
92
- Result to_match (std::string str, std::string msg = " " );
93
- Result to_match (std::regex regex, std::string msg = " " );
94
- Result to_partially_match (std::string str, std::string msg = " " );
95
- Result to_partially_match (std::regex regex, std::string msg = " " );
101
+ template <typename E>
102
+ Result to_be_less_than (E rhs, std::string msg = " " );
103
+
104
+ template <typename E>
105
+ Matchers::BeWithin<A, E> to_be_within (E expected, std::string msg = " " );
106
+
107
+ /* -------- to... ----------*/
96
108
109
+ Result to_end_with (std::string ending, std::string msg = " " );
97
110
Result to_fail (std::string msg = " " );
98
111
Result to_fail_with (std::string failure_message, std::string msg = " " );
112
+ Result to_match (std::regex regex, std::string msg = " " );
113
+ Result to_match (std::string str, std::string msg = " " );
114
+ Result to_partially_match (std::regex regex, std::string msg = " " );
115
+ Result to_partially_match (std::string str, std::string msg = " " );
116
+ Result to_satisfy (std::function<bool (A)>, std::string msg = "");
117
+ Result to_start_with (std::string start, std::string msg = " " );
99
118
100
119
template <typename U>
101
120
Result to_contain (std::initializer_list<U> expected, std::string msg = " " );
102
121
103
122
template <typename E>
104
123
Result to_contain (E expected, std::string msg = " " );
105
124
106
- template <typename E>
107
- Result to_be_between (
108
- E min, E max, Matchers::RangeMode mode = Matchers::RangeMode::inclusive,
109
- std::string msg = " " );
125
+ template <typename U>
126
+ Result to_end_with (std::initializer_list<U> start, std::string msg = " " );
110
127
111
128
template <typename E>
112
129
Result to_equal (E expected, std::string msg = " " );
113
130
114
- template <typename E>
115
- Matchers::BeWithin<A, E> to_be_within (E expected, std::string msg = " " );
116
-
117
- Result to_start_with (std::string start, std::string msg = " " );
118
131
template <typename U>
119
132
Result to_start_with (std::initializer_list<U> start, std::string msg = " " );
120
- Result to_end_with (std::string ending, std::string msg = " " );
121
- template <typename U>
122
- Result to_end_with (std::initializer_list<U> start, std::string msg = " " );
123
133
};
124
134
125
135
/* *
126
- * @brief Invert the current matcher.
136
+ *
137
+ *
138
+ * @param matcher
139
+ * @param msg
140
+ * @tparam A
141
+ * @tparam M
142
+ *
143
+ * @return
127
144
*/
128
145
template <typename A>
129
- Expectation<A> &Expectation<A>::not_() {
130
- this ->is_positive = not this ->is_positive ;
131
- return *this ;
146
+ template <class M >
147
+ Result Expectation<A>::to(M matcher, std::string msg) {
148
+ static_assert (
149
+ std::is_base_of<Matchers::MatcherBase<A, typename M::expected_t >,
150
+ M>::value,
151
+ " Matcher is not a subclass of BaseMatcher." );
152
+ // auto base_matcher = static_cast<Matchers::BaseMatcher<A,typename
153
+ // M::expected_t>>(matcher);
154
+ return matcher.set_message (msg).run (this ->get_formatter ());
132
155
}
133
156
134
157
/* *
135
- * @brief Set the flag to ignore match failure.
158
+ * @brief Match using the Matchers::Be matcher, testing for falsy-ness.
159
+ *
160
+ * @param msg Optional message to give on failure.
161
+ * @tparam A
162
+ *
163
+ * @return
136
164
*/
137
165
template <typename A>
138
- Expectation<A> &Expectation<A>::ignore() {
139
- this ->ignore_failure = true ;
140
- return *this ;
166
+ Result Expectation<A>::to_be_false(std::string msg) {
167
+ static_assert (std::is_same<A, bool >::value,
168
+ " Error! to_be_false can only be used on booleans or functions "
169
+ " that return booleans" );
170
+ return to_equal (false , msg);
141
171
}
142
172
143
173
/* *
144
- * @brief Match using the Matchers::Satisfy matcher.
145
174
*
146
- * @param test The function to use to test the output of the
147
- * expectation expression.
148
- * @param msg Optional message to give on failure.
149
175
*
150
- * @return Whether the expectation succeeds or fails.
176
+ * @param msg
177
+ *
178
+ * @return
151
179
*/
152
180
template <typename A>
153
- Result Expectation<A>::to_satisfy(std::function<bool (A)> test,
154
- std::string msg) {
155
- return Matchers::Satisfy<A>(*this , test)
156
- .set_message (msg)
157
- .run (this ->get_formatter ());
181
+ Result Expectation<A>::to_be_falsy(std::string msg) {
182
+ return to_satisfy ([](const A &t) { return !static_cast <bool >(t); }, msg);
158
183
}
159
184
160
185
/* *
@@ -187,46 +212,18 @@ Result Expectation<A>::to_be_true(std::string msg) {
187
212
}
188
213
189
214
/* *
190
- * @brief Match using the Matchers::Be matcher, testing for falsy-ness.
191
215
*
192
- * @param msg Optional message to give on failure.
193
216
*
217
+ * @param msg
218
+ *
219
+ * @tparam A
194
220
* @return
195
221
*/
196
- template <typename A>
197
- Result Expectation<A>::to_be_false(std::string msg) {
198
- static_assert (std::is_same<A, bool >::value,
199
- " Error! to_be_false can only be used on booleans or functions "
200
- " that return booleans" );
201
- return to_equal (false , msg);
202
- }
203
-
204
222
template <typename A>
205
223
Result Expectation<A>::to_be_truthy(std::string msg) {
206
224
return to_satisfy ([](const A &t) { return static_cast <bool >(t); }, msg);
207
225
}
208
226
209
- template <typename A>
210
- Result Expectation<A>::to_be_falsy(std::string msg) {
211
- return to_satisfy ([](const A &t) { return !static_cast <bool >(t); }, msg);
212
- }
213
-
214
- template <typename A>
215
- template <typename E>
216
- Result Expectation<A>::to_be_less_than(E rhs, std::string msg) {
217
- return Matchers::BeLessThan<A, E>(*this , rhs)
218
- .set_message (msg)
219
- .run (this ->get_formatter ());
220
- }
221
-
222
- template <typename A>
223
- template <typename E>
224
- Result Expectation<A>::to_be_greater_than(E rhs, std::string msg) {
225
- return Matchers::BeGreaterThan<A, E>(*this , rhs)
226
- .set_message (msg)
227
- .run (this ->get_formatter ());
228
- }
229
-
230
227
/* *
231
228
* @brief Match using the Matchers::BeBetween matcher, with an explicit
232
229
* range mode.
@@ -246,6 +243,22 @@ Result Expectation<A>::to_be_between(E min, E max, Matchers::RangeMode mode,
246
243
.run (this ->get_formatter ());
247
244
}
248
245
246
+ template <typename A>
247
+ template <typename E>
248
+ Result Expectation<A>::to_be_less_than(E rhs, std::string msg) {
249
+ return Matchers::BeLessThan<A, E>(*this , rhs)
250
+ .set_message (msg)
251
+ .run (this ->get_formatter ());
252
+ }
253
+
254
+ template <typename A>
255
+ template <typename E>
256
+ Result Expectation<A>::to_be_greater_than(E rhs, std::string msg) {
257
+ return Matchers::BeGreaterThan<A, E>(*this , rhs)
258
+ .set_message (msg)
259
+ .run (this ->get_formatter ());
260
+ }
261
+
249
262
/* *
250
263
* @brief Match using the Matchers::Include matcher, given an initializer list.
251
264
*
@@ -376,16 +389,21 @@ Result Expectation<A>::to_partially_match(std::regex regex, std::string msg) {
376
389
.run (this ->get_formatter ());
377
390
}
378
391
392
+ /* *
393
+ * @brief Match using the Matchers::Satisfy matcher.
394
+ *
395
+ * @param test The function to use to test the output of the
396
+ * expectation expression.
397
+ * @param msg Optional message to give on failure.
398
+ *
399
+ * @return Whether the expectation succeeds or fails.
400
+ */
379
401
template <typename A>
380
- template <class M >
381
- Result Expectation<A>::to(M matcher, std::string msg) {
382
- static_assert (
383
- std::is_base_of<Matchers::MatcherBase<A, typename M::expected_t >,
384
- M>::value,
385
- " Matcher is not a subclass of BaseMatcher." );
386
- // auto base_matcher = static_cast<Matchers::BaseMatcher<A,typename
387
- // M::expected_t>>(matcher);
388
- return matcher.set_message (msg).run (this ->get_formatter ());
402
+ Result Expectation<A>::to_satisfy(std::function<bool (A)> test,
403
+ std::string msg) {
404
+ return Matchers::Satisfy<A>(*this , test)
405
+ .set_message (msg)
406
+ .run (this ->get_formatter ());
389
407
}
390
408
391
409
template <typename A>
@@ -414,13 +432,12 @@ Result Expectation<A>::to_end_with(std::string ending, std::string msg) {
414
432
template <typename A>
415
433
template <typename U>
416
434
Result Expectation<A>::to_end_with(std::initializer_list<U> start_sequence,
417
- std::string msg) {
435
+ std::string msg) {
418
436
return Matchers::StartWith<A, std::initializer_list<U>>(*this , start_sequence)
419
437
.set_message (msg)
420
438
.run (this ->get_formatter ());
421
439
}
422
440
423
-
424
441
template <typename A>
425
442
class ExpectationValue : public Expectation <A> {
426
443
A value;
@@ -467,15 +484,15 @@ template <typename F>
467
484
class ExpectationFunc : public Expectation <decltype (std::declval<F>()())> {
468
485
static_assert (Util::is_functional<F>::value,
469
486
" Error! ExpectationFunc can only contaion lambdas." );
470
-
487
+
471
488
typedef decltype (std::declval<F>()()) block_ret_t;
472
489
F block;
473
490
std::shared_ptr<block_ret_t > computed = nullptr ;
474
491
475
492
public:
476
493
ExpectationFunc (ExpectationFunc<F> const ©)
477
494
: Expectation<block_ret_t >(copy), block(copy.block) {}
478
-
495
+
479
496
/* *
480
497
* @brief Create an ExpectationValue using a value.
481
498
*
@@ -485,7 +502,7 @@ class ExpectationFunc : public Expectation<decltype(std::declval<F>()())> {
485
502
*/
486
503
ExpectationFunc (ItBase &it, F block)
487
504
: Expectation<block_ret_t >(it), block(block) {}
488
-
505
+
489
506
/* *
490
507
* @brief Create an Expectation using a function.
491
508
*
@@ -539,6 +556,5 @@ Result ExpectationFunc<F>::to_throw(std::string msg) {
539
556
return matcher.set_message (msg).run (this ->get_formatter ());
540
557
}
541
558
542
- } // namespace Expectations
543
559
} // namespace CppSpec
544
560
#endif // CPPSPEC_EXPECTATIONS_EXPECTATION_HPP
0 commit comments