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 void to_end_with(std::string ending, std::string msg =
"");
114 void to_fail(std::string msg =
"");
115 void to_fail_with(std::string failure_message, std::string msg =
"");
116 void to_match(std::regex regex, std::string msg =
"");
117 void to_match(std::string str, std::string msg =
"");
118 void to_partially_match(std::regex regex, std::string msg =
"");
119 void to_partially_match(std::string str, std::string msg =
"");
120 template <
typename F>
121 requires std::invocable<F, A> && std::convertible_to<std::invoke_result_t<F, A>,
bool>
123 void to_start_with(std::string start, std::string msg =
"");
125 template <
typename U>
126 void to_contain(std::initializer_list<U> expected, std::string msg =
"");
128 template <
typename E>
129 void to_contain(E expected, std::string msg =
"");
131 template <
typename U>
132 void to_end_with(std::initializer_list<U> start, std::string msg =
"");
134 template <
typename E>
137 template <
typename U>
138 void to_start_with(std::initializer_list<U> start, std::string msg =
"");
140 void to_have_value(std::string msg =
"");
141#if __cpp_lib_expected
142 void to_have_error(std::string msg =
"");
159 static_assert(std::is_base_of_v<Matchers::MatcherBase<A, typename M::expected_t>, M>,
160 "Matcher is not a subclass of BaseMatcher.");
163 matcher.set_message(std::move(msg)).run();
176 static_assert(std::is_same_v<A, bool>,
177 ".to_be_false() can only be used on booleans or functions that return booleans");
190 to_satisfy([](
const A& t) {
return !
static_cast<bool>(t); }, msg);
214 static_assert(std::is_same_v<A, bool>,
215 ".to_be_true() can only be used on booleans or functions that return booleans");
230 to_satisfy([](
const A& t) {
return static_cast<bool>(t); }, msg);
251void Expectation<A>::to_be_less_than(E rhs, std::string msg) {
257void Expectation<A>::to_be_greater_than(E rhs, std::string msg) {
258 Matchers::BeGreaterThan<A, E>(*
this, rhs).set_message(std::move(msg)).run();
315 matcher.set_message(std::move(msg));
320void Expectation<A>::to_fail(std::string msg) {
321 static_assert(is_result_v<A>,
".to_fail() must be used on an expression that returns a Result.");
326void Expectation<A>::to_fail_with(std::string failure_message, std::string msg) {
327 static_assert(is_result_v<A>,
".to_fail_with() must be used on an expression that returns a Result.");
328 Matchers::FailWith<A>(*
this, failure_message).set_message(std::move(msg)).run();
332void Expectation<A>::to_match(std::string str, std::string msg) {
337void Expectation<A>::to_match(std::regex regex, std::string msg) {
342void Expectation<A>::to_partially_match(std::string str, std::string msg) {
347void Expectation<A>::to_partially_match(std::regex regex, std::string msg) {
362 requires std::invocable<F, A> && std::convertible_to<std::invoke_result_t<F, A>,
bool>
368void Expectation<A>::to_start_with(std::string start, std::string msg) {
374void Expectation<A>::to_start_with(std::initializer_list<U> start_sequence, std::string msg) {
375 Matchers::StartWith<A, std::initializer_list<U>>(*
this, start_sequence).set_message(std::move(msg)).run();
379void Expectation<A>::to_end_with(std::string ending, std::string msg) {
385void Expectation<A>::to_end_with(std::initializer_list<U> end_sequence, std::string msg) {
390void Expectation<A>::to_have_value(std::string msg) {
394#if __cpp_lib_expected
414 explicit ExpectationValue(A value, std::source_location location = std::source_location::current())
424 template <
typename U>
426 : Expectation<A>(it, location), value(std::vector<U>(init_list)) {}
432 this->is_positive_ = not this->is_positive_;
437 this->ignore_ =
true;
442template <Util::is_functional F>
443class ExpectationFunc :
public Expectation<decltype(std::declval<F>()())> {
444 using block_ret_t =
decltype(std::declval<F>()());
446 std::optional<block_ret_t> computed = std::nullopt;
449 ExpectationFunc(ExpectationFunc<F>
const& copy, std::source_location location)
450 : Expectation<block_ret_t>(copy, location), block(copy.block) {}
460 : Expectation<block_ret_t>(it, location), block(block) {}
481 if (!computed.has_value()) {
482 computed.emplace(block());
491 this->is_positive_ = !this->is_positive_;
495 ExpectationFunc& ignore()
override {
496 this->ignore_ =
true;
500 Expectation<
decltype(block())>& casted() {
return static_cast<decltype(block())
>(*this); }
502 template <
typename Ex = std::exception>
503 void to_throw(std::string msg =
"");
506template <Util::is_functional F>
507template <
typename Ex>
508void ExpectationFunc<F>::to_throw(std::string msg) {
Definition expectation.hpp:443
block_ret_t & get_target() &override
Create an Expectation using a function.
Definition expectation.hpp:480
ExpectationFunc(ItBase &it, F block, std::source_location location)
Create an ExpectationValue using a value.
Definition expectation.hpp:459
Definition expectation.hpp:402
A & get_target() &override
Get the target of the expectation.
Definition expectation.hpp:429
ExpectationValue(ItBase &it, std::initializer_list< U > init_list, std::source_location location)
Create an Expectation using an initializer list.
Definition expectation.hpp:425
ExpectationValue(ItBase &it, A value, std::source_location location)
Create an ExpectationValue using a value.
Definition expectation.hpp:413
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:158
void to_be_false(std::string msg="")
Match using the Matchers::Be matcher, testing for falsy-ness.
Definition expectation.hpp:175
void to_contain(std::initializer_list< U > expected, std::string msg="")
Match using the Matchers::Include matcher, given an initializer list.
Definition expectation.hpp:271
void to_be_falsy(std::string msg="")
Definition expectation.hpp:189
void to_satisfy(F test, std::string msg="")
Match using the Matchers::Satisfy matcher.
Definition expectation.hpp:363
void to_be_true(std::string msg="")
Match using the Matchers::Be matcher, testing for truthy-ness.
Definition expectation.hpp:213
virtual A & get_target() &=0
Get the target of the expectation.
void to_be_truthy(std::string msg="")
Definition expectation.hpp:229
void to_equal(E expected, std::string msg="")
Match using the Matchers::Equal matcher.
Definition expectation.hpp:299
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:245
Matchers::BeWithinHelper< A, E > to_be_within(E expected, std::string msg="")
Match using the Matchers::BeWithin matcher.
Definition expectation.hpp:313
void to_be_null(std::string msg="")
Match using the Matchers::BeNullptr matcher.
Definition expectation.hpp:201
Base class for it expressions.
Definition it_base.hpp:32
Definition be_between.hpp:11
Definition be_less_than.hpp:10
Definition be_nullptr.hpp:10
Definition be_within.hpp:10
Definition contain.hpp:61
Definition end_with.hpp:12
The equal matcher.
Definition equal.hpp:17
Definition have_error.hpp:14
Definition have_value.hpp:14
Result run()
Run the Matcher object.
Definition matcher_base.hpp:154
virtual MatcherBase & set_message(std::string message)
Set a custom failure message.
Definition matcher_base.hpp:101
Definition satisfy.hpp:21
Definition start_with.hpp:12