C++Spec 1.1.0
BDD testing for C++
Loading...
Searching...
No Matches
expectation.hpp
1#pragma once
2
3#include <exception>
4#include <optional>
5#include <regex>
6#include <source_location>
7#include <string>
8#include <vector>
9
11#include "matchers/contain.hpp"
12#include "matchers/equal.hpp"
13#include "matchers/errors/fail.hpp"
14#include "matchers/errors/have_value.hpp"
15#include "matchers/errors/throw.hpp"
19#include "matchers/numeric/be_within.hpp"
20#include "matchers/satisfy.hpp"
24
25#ifdef __cpp_lib_expected
26#include "matchers/errors/have_error.hpp"
27#endif
28
29namespace CppSpec {
30
47template <class A>
48class Expectation {
49 ItBase* it = nullptr;
50 std::source_location location;
51
52 protected:
53 bool is_positive_ = true;
54 // Have we been negated?
55 bool ignore_ = false;
56
57 public:
58 Expectation() = default;
59 explicit Expectation(std::source_location location) : location(location) {}
60
68 explicit Expectation(ItBase& it, std::source_location location) : it(&it), location(location) {}
69
71 // virtual const A &get_target() const & { return target; }
72 virtual A& get_target() & = 0;
73
74 [[nodiscard]] ItBase* get_it() const { return it; }
75 [[nodiscard]] std::source_location get_location() const { return location; }
76
78 [[nodiscard]] constexpr bool positive() const { return is_positive_; }
79 [[nodiscard]] constexpr bool ignored() const { return ignore_; }
80
81 /********* Modifiers *********/
82
83 virtual Expectation& not_() = 0;
84 virtual Expectation& ignore() = 0;
85
86 /********* Matchers *********/
87
88 template <class M>
89 void to(M matcher, std::string msg = "");
90
91 /*-------- to be... ----------*/
92
93 void to_be_false(std::string msg = "");
94 void to_be_falsy(std::string msg = "");
95 void to_be_null(std::string msg = "");
96 void to_be_true(std::string msg = "");
97 void to_be_truthy(std::string msg = "");
98
99 template <typename E>
100 void to_be_between(E min, E max, Matchers::RangeMode mode = Matchers::RangeMode::inclusive, std::string msg = "");
101
102 template <typename E>
103 void to_be_greater_than(E rhs, std::string msg = "");
104
105 template <typename E>
106 void to_be_less_than(E rhs, std::string msg = "");
107
108 template <typename E>
109 Matchers::BeWithinHelper<A, E> to_be_within(E expected, std::string msg = "");
110
111 /*-------- to... ----------*/
112
113 template <Util::is_string_like T>
115 void to_end_with(T ending, std::string msg = "");
116 void to_fail(std::string msg = "");
117 void to_fail_with(std::string failure_message, std::string msg = "");
118 void to_match(std::regex regex, std::string msg = "");
119 template <Util::is_string_like T>
120 void to_match(T pattern, std::string msg = "");
121 void to_partially_match(std::regex regex, std::string msg = "");
122 template <Util::is_string_like T>
123 void to_partially_match(T pattern, std::string msg = "");
124 template <typename F>
125 requires std::invocable<F, A> && std::convertible_to<std::invoke_result_t<F, A>, bool>
126 void to_satisfy(F test, std::string msg = "");
127 template <Util::is_string_like T>
129 void to_start_with(T start, std::string msg = "");
130
131 template <typename U>
132 void to_contain(std::initializer_list<U> expected, std::string msg = "");
133
134 template <typename E>
135 void to_contain(E expected, std::string msg = "");
136
137 template <typename U>
138 void to_end_with(std::initializer_list<U> start, std::string msg = "");
139
140 template <typename E>
141 void to_equal(E expected, std::string msg = "");
142
143 template <typename U>
144 void to_start_with(std::initializer_list<U> start, std::string msg = "");
145
146 void to_have_value(std::string msg = "");
147#if __cpp_lib_expected
148 void to_have_error(std::string msg = "");
149#endif
150};
151
162template <typename A>
163template <class M>
164void Expectation<A>::to(M matcher, std::string msg) {
165 static_assert(std::is_base_of_v<Matchers::MatcherBase<A, typename M::expected_t>, M>,
166 "Matcher is not a subclass of BaseMatcher.");
167 // auto base_matcher = static_cast<Matchers::BaseMatcher<A,typename
168 // M::expected_t>>(matcher);
169 matcher.set_message(std::move(msg)).run();
170}
171
180template <typename A>
181void Expectation<A>::to_be_false(std::string msg) {
182 static_assert(std::is_same_v<A, bool>,
183 ".to_be_false() can only be used on booleans or functions that return booleans");
184 to_equal(false, msg);
185}
186
194template <typename A>
195void Expectation<A>::to_be_falsy(std::string msg) {
196 to_satisfy([](const A& t) { return !static_cast<bool>(t); }, msg);
197}
198
206template <typename A>
207void Expectation<A>::to_be_null(std::string msg) {
208 Matchers::BeNullptr<A>(*this).set_message(std::move(msg)).run();
209}
210
218template <typename A>
219void Expectation<A>::to_be_true(std::string msg) {
220 static_assert(std::is_same_v<A, bool>,
221 ".to_be_true() can only be used on booleans or functions that return booleans");
222 // return to_be([](A t) { return static_cast<bool>(t); }, msg);
223 to_equal(true, msg);
224}
225
234template <typename A>
235void Expectation<A>::to_be_truthy(std::string msg) {
236 to_satisfy([](const A& t) { return static_cast<bool>(t); }, msg);
237}
238
249template <typename A>
250template <typename E>
251void Expectation<A>::to_be_between(E min, E max, Matchers::RangeMode mode, std::string msg) {
252 Matchers::BeBetween<A, E>(*this, min, max, mode).set_message(std::move(msg)).run();
253}
254
255template <typename A>
256template <typename E>
257void Expectation<A>::to_be_less_than(E rhs, std::string msg) {
258 Matchers::BeLessThan<A, E>(*this, rhs).set_message(std::move(msg)).run();
259}
260
261template <typename A>
262template <typename E>
263void Expectation<A>::to_be_greater_than(E rhs, std::string msg) {
264 Matchers::BeGreaterThan<A, E>(*this, rhs).set_message(std::move(msg)).run();
265}
266
275template <typename A>
276template <typename U>
277void Expectation<A>::to_contain(std::initializer_list<U> expected, std::string msg) {
278 Matchers::Contain<A, std::vector<U>, U>(*this, expected).set_message(std::move(msg)).run();
279}
280
289template <typename A>
290template <typename E>
291void Expectation<A>::to_contain(E expected, std::string msg) {
292 Matchers::Contain<A, E, E>(*this, expected).set_message(std::move(msg)).run();
293}
294
303template <typename A>
304template <typename E>
305void Expectation<A>::to_equal(E expected, std::string msg) {
306 Matchers::Equal<A, E>(*this, expected).set_message(std::move(msg)).run();
307}
308
317template <typename A>
318template <typename E>
320 Matchers::BeWithinHelper<A, E> matcher(*this, expected);
321 matcher.set_message(std::move(msg));
322 return matcher;
323}
324
325template <typename A>
326void Expectation<A>::to_fail(std::string msg) {
327 static_assert(is_result_v<A>, ".to_fail() must be used on an expression that returns a Result.");
328 Matchers::Fail<Result>(*this).set_message(std::move(msg)).run();
329}
330
331template <typename A>
332void Expectation<A>::to_fail_with(std::string failure_message, std::string msg) {
333 static_assert(is_result_v<A>, ".to_fail_with() must be used on an expression that returns a Result.");
334 Matchers::FailWith<A>(*this, failure_message).set_message(std::move(msg)).run();
335}
336
343template <typename A>
344template <Util::is_string_like T>
345void Expectation<A>::to_match(T pattern, std::string msg) {
346 Matchers::Match<A>(*this, pattern).set_message(std::move(msg)).run();
347}
348
349template <typename A>
350void Expectation<A>::to_match(std::regex regex, std::string msg) {
351 Matchers::Match<A>(*this, regex).set_message(std::move(msg)).run();
352}
353
360template <typename A>
361template <Util::is_string_like T>
362void Expectation<A>::to_partially_match(T pattern, std::string msg) {
363 Matchers::MatchPartial<A>(*this, pattern).set_message(std::move(msg)).run();
364}
365
366template <typename A>
367void Expectation<A>::to_partially_match(std::regex regex, std::string msg) {
368 Matchers::MatchPartial<A>(*this, regex).set_message(std::move(msg)).run();
369}
370
380template <typename A>
381template <typename F>
382 requires std::invocable<F, A> && std::convertible_to<std::invoke_result_t<F, A>, bool>
383void Expectation<A>::to_satisfy(F test, std::string msg) {
384 Matchers::Satisfy<A>(*this, std::function<bool(A)>(std::move(test))).set_message(std::move(msg)).run();
385}
386
393template <typename A>
394template <Util::is_string_like T>
396void Expectation<A>::to_start_with(T start, std::string msg) {
397 Matchers::StartWith<A, T>(*this, start).set_message(std::move(msg)).run();
398}
399
400template <typename A>
401template <typename U>
402void Expectation<A>::to_start_with(std::initializer_list<U> start_sequence, std::string msg) {
403 Matchers::StartWith<A, std::initializer_list<U>>(*this, start_sequence).set_message(std::move(msg)).run();
404}
405
412template <typename A>
413template <Util::is_string_like T>
414 requires Util::compares_start_and_end<A, T>
415void Expectation<A>::to_end_with(T ending, std::string msg) {
416 Matchers::EndWith<A, T>(*this, ending).set_message(std::move(msg)).run();
417}
418
419template <typename A>
420template <typename U>
421void Expectation<A>::to_end_with(std::initializer_list<U> end_sequence, std::string msg) {
422 Matchers::EndWith<A, std::initializer_list<U>>(*this, end_sequence).set_message(std::move(msg)).run();
423}
424
425template <typename A>
426void Expectation<A>::to_have_value(std::string msg) {
427 Matchers::HaveValue<A>(*this).set_message(std::move(msg)).run();
428}
429
430#if __cpp_lib_expected
431template <typename A>
432void Expectation<A>::to_have_error(std::string msg) {
433 Matchers::HaveError<A>(*this).set_message(std::move(msg)).run();
434}
435#endif
436
437template <typename A>
438class ExpectationValue : public Expectation<A> {
439 A value;
440
441 public:
449 ExpectationValue(ItBase& it, A value, std::source_location location) : Expectation<A>(it, location), value(value) {}
450 explicit ExpectationValue(A value, std::source_location location = std::source_location::current())
451 : Expectation<A>(location), value(value) {}
452
460 template <typename U>
461 ExpectationValue(ItBase& it, std::initializer_list<U> init_list, std::source_location location)
462 : Expectation<A>(it, location), value(std::vector<U>(init_list)) {}
463
465 A& get_target() & override { return value; }
466
467 ExpectationValue& not_() override {
468 this->is_positive_ = not this->is_positive_;
469 return *this;
470 }
471
472 ExpectationValue& ignore() override {
473 this->ignore_ = true;
474 return *this;
475 }
476};
477
478template <Util::is_functional F>
479class ExpectationFunc : public Expectation<decltype(std::declval<F>()())> {
480 using block_ret_t = decltype(std::declval<F>()());
481 F block;
482 std::optional<block_ret_t> computed = std::nullopt;
483
484 public:
485 ExpectationFunc(ExpectationFunc<F> const& copy, std::source_location location)
486 : Expectation<block_ret_t>(copy, location), block(copy.block) {}
487
495 ExpectationFunc(ItBase& it, F block, std::source_location location)
496 : Expectation<block_ret_t>(it, location), block(block) {}
497
509 // TODO: create a "lazy" parameter for differentiating between delayed and
510 // immediate execution
511 // ExpectationFunc(BaseIt &it, std::function<A()> block)
512 // : Expectation(it), block(block()){}; // block(block), has_block(true)
513 // {}
514
516 block_ret_t& get_target() & override {
517 if (!computed.has_value()) {
518 computed.emplace(block());
519 }
520 return *computed;
521 }
522
523 // auto get_target() & override -> decltype(std::declval<A>()()) & { return
524 // Expectation<A>::get_target()(); }
525
526 ExpectationFunc& not_() override {
527 this->is_positive_ = !this->is_positive_;
528 return *this;
529 }
530
531 ExpectationFunc& ignore() override {
532 this->ignore_ = true;
533 return *this;
534 }
535
536 Expectation<decltype(block())>& casted() { return static_cast<decltype(block())>(*this); }
537
538 template <typename Ex = std::exception>
539 void to_throw(std::string msg = "");
540};
541
542template <Util::is_functional F>
543template <typename Ex>
544void ExpectationFunc<F>::to_throw(std::string msg) {
545 Matchers::Throw<decltype(this->block.operator()()), Ex>(*this).set_message(std::move(msg)).run();
546}
547
548} // namespace CppSpec
Definition expectation.hpp:479
block_ret_t & get_target() &override
Create an Expectation using a function.
Definition expectation.hpp:516
ExpectationFunc(ItBase &it, F block, std::source_location location)
Create an ExpectationValue using a value.
Definition expectation.hpp:495
Definition expectation.hpp:438
A & get_target() &override
Get the target of the expectation.
Definition expectation.hpp:465
ExpectationValue(ItBase &it, std::initializer_list< U > init_list, std::source_location location)
Create an Expectation using an initializer list.
Definition expectation.hpp:461
ExpectationValue(ItBase &it, A value, std::source_location location)
Create an ExpectationValue using a value.
Definition expectation.hpp:449
Wraps the target of an expectation.
Definition expectation.hpp:48
Expectation(ItBase &it, std::source_location location)
Create an Expectation using a value.
Definition expectation.hpp:68
constexpr bool positive() const
Get whether the expectation is normal or negated.
Definition expectation.hpp:78
void to(M matcher, std::string msg="")
Definition expectation.hpp:164
void to_be_false(std::string msg="")
Match using the Matchers::Be matcher, testing for falsy-ness.
Definition expectation.hpp:181
void to_start_with(T start, std::string msg="")
Match using the Matchers::StartWith matcher, with the start given as text.
Definition expectation.hpp:396
void to_contain(std::initializer_list< U > expected, std::string msg="")
Match using the Matchers::Include matcher, given an initializer list.
Definition expectation.hpp:277
void to_be_falsy(std::string msg="")
Definition expectation.hpp:195
void to_satisfy(F test, std::string msg="")
Match using the Matchers::Satisfy matcher.
Definition expectation.hpp:383
void to_be_true(std::string msg="")
Match using the Matchers::Be matcher, testing for truthy-ness.
Definition expectation.hpp:219
virtual A & get_target() &=0
Get the target of the expectation.
void to_end_with(T ending, std::string msg="")
Match using the Matchers::EndWith matcher, with the ending given as text.
Definition expectation.hpp:415
void to_be_truthy(std::string msg="")
Definition expectation.hpp:235
void to_equal(E expected, std::string msg="")
Match using the Matchers::Equal matcher.
Definition expectation.hpp:305
void to_be_between(E min, E max, Matchers::RangeMode mode=Matchers::RangeMode::inclusive, std::string msg="")
Match using the Matchers::BeBetween matcher, with an explicit range mode.
Definition expectation.hpp:251
Matchers::BeWithinHelper< A, E > to_be_within(E expected, std::string msg="")
Match using the Matchers::BeWithin matcher.
Definition expectation.hpp:319
void to_be_null(std::string msg="")
Match using the Matchers::BeNullptr matcher.
Definition expectation.hpp:207
Base class for it expressions.
Definition it_base.hpp:32
Matches a value between two bounds, inclusive or exclusive, compared by value when both sides are int...
Definition be_between.hpp:17
Matches a value below the expected one: actual < expected, compared by value when both are integers (...
Definition be_less_than.hpp:16
Definition be_nullptr.hpp:10
Definition be_within.hpp:10
Definition contain.hpp:131
The end with matcher.
Definition end_with.hpp:24
The equal matcher.
Definition equal.hpp:17
Definition fail.hpp:8
Definition have_error.hpp:14
The partially match matcher: a regular expression matches somewhere in the actual text.
Definition match.hpp:92
The match matcher: the whole of the actual text matches a regular expression.
Definition match.hpp:44
Result run()
Run the Matcher object.
Definition matcher_base.hpp:217
virtual MatcherBase & set_message(std::string message)
Set a custom failure message.
Definition matcher_base.hpp:169
Definition satisfy.hpp:21
The start with matcher.
Definition start_with.hpp:24
Definition throw.hpp:9
Checks whether to_start_with and to_end_with compare an actual value of type A with an expected one o...
Definition util.hpp:211