Skip to content

Commit 3b331fa

Browse files
committed
Refactor Expectations out, continuing documenting
1 parent 87ff318 commit 3b331fa

29 files changed

+221
-155
lines changed

docs/syntax/before_after.md

Whitespace-only changes.

docs/syntax/describe.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ Each `describe` is a global instance of the `Description` class, the name of the
1212
1313
!!! important
1414
15-
```
16-
Take note of the `$`. This is used whenever you write a `describe` or a `describe_a`.
17-
```
15+
Take note of the `$`. This is used whenever you write a `describe` or a `describe_a`.
16+
1817
1918
In conventional C++14, after macro-expansion the above snippet would be written as:
2019

docs/syntax/expect.md

Whitespace-only changes.

docs/syntax/it.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# It
2+
`it`s are the examples of the spec. A `Description` holds a group of `it`s, where each `it` has at least one expectation.

docs/syntax/let.md

Whitespace-only changes.

docs/syntax/matchers.md

Whitespace-only changes.

include/class_description.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ Result ClassDescription<T>::run(Formatters::BaseFormatter &printer) {
248248
}
249249

250250
template <class T>
251-
Expectations::ExpectationValue<T> ItCD<T>::is_expected() {
251+
ExpectationValue<T> ItCD<T>::is_expected() {
252252
auto cd = static_cast<ClassDescription<T> *>(this->get_parent());
253-
Expectations::ExpectationValue<T> expectation(*this, cd->subject);
253+
ExpectationValue<T> expectation(*this, cd->subject);
254254
return expectation;
255255
}
256256

include/expectations/expectation.hpp

Lines changed: 101 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
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
@@ -25,7 +26,6 @@
2526
#include "matchers/throw.hpp"
2627

2728
namespace 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 &not_() = 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
*/
128145
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());
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
*/
137165
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);
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
*/
152180
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);
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-
204222
template <typename A>
205223
Result 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+
*/
379401
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());
389407
}
390408

391409
template <typename A>
@@ -414,13 +432,12 @@ Result Expectation<A>::to_end_with(std::string ending, std::string msg) {
414432
template <typename A>
415433
template <typename U>
416434
Result 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-
424441
template <typename A>
425442
class ExpectationValue : public Expectation<A> {
426443
A value;
@@ -467,15 +484,15 @@ template <typename F>
467484
class 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 &copy)
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

include/expectations/handler.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "result.hpp"
88

99
namespace CppSpec {
10-
namespace Expectations {
1110

1211
struct PositiveExpectationHandler {
1312
template <typename A, class Matcher>
@@ -36,6 +35,5 @@ Result NegativeExpectationHandler::handle_matcher(Matcher &matcher) {
3635
: Result::success();
3736
}
3837

39-
} // namespace Expectations
4038
} // namespace CppSpec
4139
#endif // CPPSPEC_EXPECTATIONS_HANDLER_HPP

0 commit comments

Comments
 (0)