6#include <source_location>
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"
25#ifdef __cpp_lib_expected
26#include "matchers/errors/have_error.hpp"
50 std::source_location location;
53 bool is_positive_ =
true;
58 Expectation() =
default;
59 explicit Expectation(std::source_location location) : location(location) {}
68 explicit Expectation(
ItBase& it, std::source_location location) : it(&it), location(location) {}
74 [[nodiscard]]
ItBase* get_it()
const {
return it; }
75 [[nodiscard]] std::source_location get_location()
const {
return location; }
78 [[nodiscard]]
constexpr bool positive()
const {
return is_positive_; }
79 [[nodiscard]]
constexpr bool ignored()
const {
return ignore_; }
83 virtual Expectation& not_() = 0;
84 virtual Expectation& ignore() = 0;
89 void to(M matcher, std::string msg =
"");
100 void to_be_between(E min, E max, Matchers::RangeMode mode = Matchers::RangeMode::inclusive, std::string msg =
"");
102 template <
typename E>
103 void to_be_greater_than(E rhs, std::string msg =
"");
105 template <
typename E>
106 void to_be_less_than(E rhs, std::string msg =
"");
108 template <
typename E>
113 template <Util::is_
string_like T>
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>
127 template <Util::is_
string_like T>
131 template <
typename U>
132 void to_contain(std::initializer_list<U> expected, std::string msg =
"");
134 template <
typename E>
135 void to_contain(E expected, std::string msg =
"");
137 template <
typename U>
138 void to_end_with(std::initializer_list<U> start, std::string msg =
"");
140 template <
typename E>
143 template <
typename U>
144 void to_start_with(std::initializer_list<U> start, std::string msg =
"");
146 void to_have_value(std::string msg =
"");
147#if __cpp_lib_expected
148 void to_have_error(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.");
169 matcher.set_message(std::move(msg)).run();
182 static_assert(std::is_same_v<A, bool>,
183 ".to_be_false() can only be used on booleans or functions that return booleans");
196 to_satisfy([](
const A& t) {
return !
static_cast<bool>(t); }, 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");
236 to_satisfy([](
const A& t) {
return static_cast<bool>(t); }, msg);
257void Expectation<A>::to_be_less_than(E rhs, std::string msg) {
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();
321 matcher.set_message(std::move(msg));
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.");
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();
344template <Util::is_
string_like T>
345void Expectation<A>::to_match(T pattern, std::string msg) {
350void Expectation<A>::to_match(std::regex regex, std::string msg) {
361template <Util::is_
string_like T>
362void Expectation<A>::to_partially_match(T pattern, std::string msg) {
367void Expectation<A>::to_partially_match(std::regex regex, std::string msg) {
382 requires std::invocable<F, A> && std::convertible_to<std::invoke_result_t<F, A>,
bool>
394template <Util::is_
string_like T>
413template <Util::is_
string_like T>
414 requires Util::compares_start_and_end<A, T>
426void Expectation<A>::to_have_value(std::string msg) {
427 Matchers::HaveValue<A>(*this).set_message(std::move(msg)).run();
430#if __cpp_lib_expected
450 explicit ExpectationValue(A value, std::source_location location = std::source_location::current())
460 template <
typename U>
462 : Expectation<A>(it, location), value(std::vector<U>(init_list)) {}
468 this->is_positive_ = not this->is_positive_;
473 this->ignore_ =
true;
478template <Util::is_functional F>
479class ExpectationFunc :
public Expectation<decltype(std::declval<F>()())> {
480 using block_ret_t =
decltype(std::declval<F>()());
482 std::optional<block_ret_t> computed = std::nullopt;
485 ExpectationFunc(ExpectationFunc<F>
const& copy, std::source_location location)
486 : Expectation<block_ret_t>(copy, location), block(copy.block) {}
496 : Expectation<block_ret_t>(it, location), block(block) {}
517 if (!computed.has_value()) {
518 computed.emplace(block());
527 this->is_positive_ = !this->is_positive_;
531 ExpectationFunc& ignore()
override {
532 this->ignore_ =
true;
536 Expectation<
decltype(block())>& casted() {
return static_cast<decltype(block())
>(*this); }
538 template <
typename Ex = std::exception>
539 void to_throw(std::string msg =
"");
542template <Util::is_functional F>
543template <
typename Ex>
544void ExpectationFunc<F>::to_throw(std::string msg) {
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 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
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