11/* *
22 * @file
3- * @brief Defines the Expectations:: Expectation class and associated functions
3+ * @brief Defines the Expectation class and associated functions
44 */
5+
56#ifndef CPPSPEC_EXPECTATIONS_EXPECTATION_HPP
67#define CPPSPEC_EXPECTATIONS_EXPECTATION_HPP
78#pragma once
2526#include " matchers/throw.hpp"
2627
2728namespace CppSpec {
28- namespace Expectations {
2929
3030/* *
3131 * @brief Wraps the target of an expectation
@@ -72,89 +72,114 @@ class Expectation : public Child {
7272 const bool get_ignore_failure () const { return ignore_failure; }
7373 bool get_ignore_failure () { return ignore_failure; }
7474
75+ /* ******** Modifiers *********/
76+
7577 virtual Expectation ¬_ () = 0;
7678 virtual Expectation &ignore () = 0;
7779
80+ /* ******** Matchers *********/
81+
7882 template <class M >
7983 Result to (M matcher, std::string msg = " " );
8084
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 = " " );
8289 Result to_be_null (std::string msg = " " );
8390 Result to_be_true (std::string msg = " " );
84- Result to_be_false (std::string msg = " " );
8591 Result to_be_truthy (std::string msg = " " );
86- Result to_be_falsy (std::string msg = " " );
92+
8793 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+
8998 template <typename E>
9099 Result to_be_greater_than (E rhs, std::string msg = " " );
91100
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... ----------*/
96108
109+ Result to_end_with (std::string ending, std::string msg = " " );
97110 Result to_fail (std::string msg = " " );
98111 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 = " " );
99118
100119 template <typename U>
101120 Result to_contain (std::initializer_list<U> expected, std::string msg = " " );
102121
103122 template <typename E>
104123 Result to_contain (E expected, std::string msg = " " );
105124
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 = " " );
110127
111128 template <typename E>
112129 Result to_equal (E expected, std::string msg = " " );
113130
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 = " " );
118131 template <typename U>
119132 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 = " " );
123133};
124134
125135/* *
126- * @brief Invert the current matcher.
136+ *
137+ *
138+ * @param matcher
139+ * @param msg
140+ * @tparam A
141+ * @tparam M
142+ *
143+ * @return
127144 */
128145template <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 ());
132155}
133156
134157/* *
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
136164 */
137165template <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);
141171}
142172
143173/* *
144- * @brief Match using the Matchers::Satisfy matcher.
145174 *
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.
149175 *
150- * @return Whether the expectation succeeds or fails.
176+ * @param msg
177+ *
178+ * @return
151179 */
152180template <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);
158183}
159184
160185/* *
@@ -187,46 +212,18 @@ Result Expectation<A>::to_be_true(std::string msg) {
187212}
188213
189214/* *
190- * @brief Match using the Matchers::Be matcher, testing for falsy-ness.
191215 *
192- * @param msg Optional message to give on failure.
193216 *
217+ * @param msg
218+ *
219+ * @tparam A
194220 * @return
195221 */
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-
204222template <typename A>
205223Result Expectation<A>::to_be_truthy(std::string msg) {
206224 return to_satisfy ([](const A &t) { return static_cast <bool >(t); }, msg);
207225}
208226
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-
230227/* *
231228 * @brief Match using the Matchers::BeBetween matcher, with an explicit
232229 * range mode.
@@ -246,6 +243,22 @@ Result Expectation<A>::to_be_between(E min, E max, Matchers::RangeMode mode,
246243 .run (this ->get_formatter ());
247244}
248245
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+
249262/* *
250263 * @brief Match using the Matchers::Include matcher, given an initializer list.
251264 *
@@ -376,16 +389,21 @@ Result Expectation<A>::to_partially_match(std::regex regex, std::string msg) {
376389 .run (this ->get_formatter ());
377390}
378391
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+ */
379401template <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 ());
389407}
390408
391409template <typename A>
@@ -414,13 +432,12 @@ Result Expectation<A>::to_end_with(std::string ending, std::string msg) {
414432template <typename A>
415433template <typename U>
416434Result Expectation<A>::to_end_with(std::initializer_list<U> start_sequence,
417- std::string msg) {
435+ std::string msg) {
418436 return Matchers::StartWith<A, std::initializer_list<U>>(*this , start_sequence)
419437 .set_message (msg)
420438 .run (this ->get_formatter ());
421439}
422440
423-
424441template <typename A>
425442class ExpectationValue : public Expectation <A> {
426443 A value;
@@ -467,15 +484,15 @@ template <typename F>
467484class ExpectationFunc : public Expectation <decltype (std::declval<F>()())> {
468485 static_assert (Util::is_functional<F>::value,
469486 " Error! ExpectationFunc can only contaion lambdas." );
470-
487+
471488 typedef decltype (std::declval<F>()()) block_ret_t;
472489 F block;
473490 std::shared_ptr<block_ret_t > computed = nullptr ;
474491
475492 public:
476493 ExpectationFunc (ExpectationFunc<F> const ©)
477494 : Expectation<block_ret_t >(copy), block(copy.block) {}
478-
495+
479496 /* *
480497 * @brief Create an ExpectationValue using a value.
481498 *
@@ -485,7 +502,7 @@ class ExpectationFunc : public Expectation<decltype(std::declval<F>()())> {
485502 */
486503 ExpectationFunc (ItBase &it, F block)
487504 : Expectation<block_ret_t >(it), block(block) {}
488-
505+
489506 /* *
490507 * @brief Create an Expectation using a function.
491508 *
@@ -539,6 +556,5 @@ Result ExpectationFunc<F>::to_throw(std::string msg) {
539556 return matcher.set_message (msg).run (this ->get_formatter ());
540557}
541558
542- } // namespace Expectations
543559} // namespace CppSpec
544560#endif // CPPSPEC_EXPECTATIONS_EXPECTATION_HPP
0 commit comments