5#include <source_location>
12#include "matchers/errors/fail.hpp"
13#include "matchers/errors/have_value.hpp"
14#include "matchers/errors/throw.hpp"
18#include "matchers/numeric/be_within.hpp"
24#ifdef __cpp_lib_expected
25#include "matchers/errors/have_error.hpp"
49 std::source_location location;
52 bool is_positive_ =
true;
57 Expectation() =
default;
58 explicit Expectation(std::source_location location) : location(location) {}
67 explicit Expectation(
ItBase& it, std::source_location location) : it(&it), location(location) {}
73 [[nodiscard]]
ItBase* get_it()
const {
return it; }
74 [[nodiscard]] std::source_location get_location()
const {
return location; }
77 [[nodiscard]]
constexpr bool positive()
const {
return is_positive_; }
78 [[nodiscard]]
constexpr bool ignored()
const {
return ignore_; }
82 virtual Expectation& not_() = 0;
83 virtual Expectation& ignore() = 0;
88 void to(M matcher, std::string msg =
"");
99 void to_be_between(E min, E max, Matchers::RangeMode mode = Matchers::RangeMode::inclusive, std::string msg =
"");
101 template <
typename E>
102 void to_be_greater_than(E rhs, std::string msg =
"");
104 template <
typename E>
105 void to_be_less_than(E rhs, std::string msg =
"");
107 template <
typename E>
112 void to_end_with(std::string ending, std::string msg =
"");
113 void to_fail(std::string msg =
"");
114 void to_fail_with(std::string failure_message, std::string msg =
"");
115 void to_match(std::regex regex, std::string msg =
"");
116 void to_match(std::string str, std::string msg =
"");
117 void to_partially_match(std::regex regex, std::string msg =
"");
118 void to_partially_match(std::string str, std::string msg =
"");
119 void to_satisfy(std::function<
bool(A)> , std::string msg =
"");
120 void to_start_with(std::string start, std::string msg =
"");
122 template <
typename U>
123 void to_contain(std::initializer_list<U> expected, std::string msg =
"");
125 template <
typename E>
128 template <
typename U>
129 void to_end_with(std::initializer_list<U> start, std::string msg =
"");
131 template <
typename E>
134 template <
typename U>
135 void to_start_with(std::initializer_list<U> start, std::string msg =
"");
137 void to_have_value(std::string msg =
"");
138#if __cpp_lib_expected
139 void to_have_error(std::string msg =
"");
156 static_assert(std::is_base_of_v<Matchers::MatcherBase<A, typename M::expected_t>, M>,
157 "Matcher is not a subclass of BaseMatcher.");
160 matcher.set_message(std::move(msg)).run();
173 static_assert(std::is_same_v<A, bool>,
174 ".to_be_false() can only be used on booleans or functions that return booleans");
187 to_satisfy([](
const A& t) {
return !
static_cast<bool>(t); }, msg);
211 static_assert(std::is_same_v<A, bool>,
212 ".to_be_true() can only be used on booleans or functions that return booleans");
227 to_satisfy([](
const A& t) {
return static_cast<bool>(t); }, msg);
248void Expectation<A>::to_be_less_than(E rhs, std::string msg) {
254void Expectation<A>::to_be_greater_than(E rhs, std::string msg) {
255 Matchers::BeGreaterThan<A, E>(*
this, rhs).set_message(std::move(msg)).run();
312 matcher.set_message(std::move(msg));
317void Expectation<A>::to_fail(std::string msg) {
318 static_assert(is_result_v<A>,
".to_fail() must be used on an expression that returns a Result.");
323void Expectation<A>::to_fail_with(std::string failure_message, std::string msg) {
324 static_assert(is_result_v<A>,
".to_fail_with() must be used on an expression that returns a Result.");
325 Matchers::FailWith<A>(*
this, failure_message).set_message(std::move(msg)).run();
329void Expectation<A>::to_match(std::string str, std::string msg) {
334void Expectation<A>::to_match(std::regex regex, std::string msg) {
339void Expectation<A>::to_partially_match(std::string str, std::string msg) {
344void Expectation<A>::to_partially_match(std::regex regex, std::string msg) {
363void Expectation<A>::to_start_with(std::string start, std::string msg) {
369void Expectation<A>::to_start_with(std::initializer_list<U> start_sequence, std::string msg) {
370 Matchers::StartWith<A, std::initializer_list<U>>(*
this, start_sequence).set_message(std::move(msg)).run();
374void Expectation<A>::to_end_with(std::string ending, std::string msg) {
380void Expectation<A>::to_end_with(std::initializer_list<U> start_sequence, std::string msg) {
385void Expectation<A>::to_have_value(std::string msg) {
389#if __cpp_lib_expected
409 explicit ExpectationValue(A value, std::source_location location = std::source_location::current())
419 template <
typename U>
421 : Expectation<A>(it, location), value(std::vector<U>(init_list)) {}
427 this->is_positive_ = not this->is_positive_;
432 this->ignore_ =
true;
437template <Util::is_functional F>
438class ExpectationFunc :
public Expectation<decltype(std::declval<F>()())> {
439 using block_ret_t =
decltype(std::declval<F>()());
441 std::shared_ptr<block_ret_t> computed =
nullptr;
444 ExpectationFunc(ExpectationFunc<F>
const& copy, std::source_location location)
445 : Expectation<block_ret_t>(copy, location), block(copy.block) {}
455 : Expectation<block_ret_t>(it, location), block(block) {}
476 if (computed ==
nullptr) {
477 computed = std::make_shared<block_ret_t>(block());
486 this->is_positive_ = !this->is_positive_;
490 ExpectationFunc& ignore()
override {
491 this->ignore_ =
true;
495 Expectation<
decltype(block())>& casted() {
return static_cast<decltype(block())
>(*this); }
497 template <
typename Ex = std::exception>
498 void to_throw(std::string msg =
"");
501template <Util::is_functional F>
502template <
typename Ex>
503void ExpectationFunc<F>::to_throw(std::string msg) {
Definition expectation.hpp:438
block_ret_t & get_target() &override
Create an Expectation using a function.
Definition expectation.hpp:475
ExpectationFunc(ItBase &it, F block, std::source_location location)
Create an ExpectationValue using a value.
Definition expectation.hpp:454
Definition expectation.hpp:397
A & get_target() &override
Get the target of the expectation.
Definition expectation.hpp:424
ExpectationValue(ItBase &it, std::initializer_list< U > init_list, std::source_location location)
Create an Expectation using an initializer list.
Definition expectation.hpp:420
ExpectationValue(ItBase &it, A value, std::source_location location)
Create an ExpectationValue using a value.
Definition expectation.hpp:408
Wraps the target of an expectation.
Definition expectation.hpp:47
Expectation(ItBase &it, std::source_location location)
Create an Expectation using a value.
Definition expectation.hpp:67
constexpr bool positive() const
Get whether the expectation is normal or negated.
Definition expectation.hpp:77
void to(M matcher, std::string msg="")
Definition expectation.hpp:155
void to_be_false(std::string msg="")
Match using the Matchers::Be matcher, testing for falsy-ness.
Definition expectation.hpp:172
void to_contain(std::initializer_list< U > expected, std::string msg="")
Match using the Matchers::Include matcher, given an initializer list.
Definition expectation.hpp:268
void to_be_falsy(std::string msg="")
Definition expectation.hpp:186
void to_be_true(std::string msg="")
Match using the Matchers::Be matcher, testing for truthy-ness.
Definition expectation.hpp:210
virtual A & get_target() &=0
Get the target of the expectation.
void to_satisfy(std::function< bool(A)>, std::string msg="")
Match using the Matchers::Satisfy matcher.
Definition expectation.hpp:358
void to_be_truthy(std::string msg="")
Definition expectation.hpp:226
void to_equal(E expected, std::string msg="")
Match using the Matchers::Equal matcher.
Definition expectation.hpp:296
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:242
void to_contain(E expected, std::string msg="")
Match using the Matchers::Include matcher.
Definition expectation.hpp:282
Matchers::BeWithinHelper< A, E > to_be_within(E expected, std::string msg="")
Match using the Matchers::BeWithin matcher.
Definition expectation.hpp:310
void to_be_null(std::string msg="")
Match using the Matchers::BeNullptr matcher.
Definition expectation.hpp:198
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