7namespace CppSpec::Matchers {
9enum class RangeMode { exclusive, inclusive };
16template <
typename A,
typename E>
17class BeBetween :
public MatcherBase<A, E> {
21 enum class LtOp { lt, lt_eq } lt_op;
22 enum class GtOp { gt, gt_eq } gt_op;
28 BeBetween(
Expectation<A>& expectation, E min, E max, RangeMode mode = RangeMode::inclusive)
29 : MatcherBase<A, E>(expectation), min(min), max(max), mode(mode) {
31 case RangeMode::inclusive:
35 case RangeMode::exclusive:
42 bool match()
override;
43 bool negated_match()
override;
44 std::string verb()
override {
return "be between"; }
48template <
typename A,
typename E>
49bool BeBetween<A, E>::match() {
50 const auto& actual = this->actual();
51 const bool above_min = gt_op == GtOp::gt ?
Util::greater(actual, min) : Util::greater_equal(actual, min);
52 const bool below_max = lt_op == LtOp::lt ?
Util::less(actual, max) : Util::less_equal(actual, max);
53 return above_min && below_max;
56template <
typename A,
typename E>
57bool BeBetween<A, E>::negated_match() {
58 const auto& actual = this->actual();
59 const bool below_min = gt_op == GtOp::gt ?
Util::less_equal(actual, min) : Util::less(actual, min);
60 const bool above_max = lt_op == LtOp::lt ?
Util::greater_equal(actual, max) : Util::greater(actual, max);
61 return below_min || above_max;
64template <
typename A,
typename E>
66 return std::format(
"be between {} and {} ({})", min, max, (mode == RangeMode::exclusive ?
"exclusive" :
"inclusive"));
Wraps the target of an expectation.
Definition expectation.hpp:48
std::string description() override
Get the description of the Matcher.
Definition be_between.hpp:65
Contains the base class for all Matchers.
Utility functions and classes.
constexpr bool less(const A &a, const B &b)
a < b, by value when both are integers (std::cmp_less, so a negative signed value is below any unsign...
Definition util.hpp:232
constexpr bool greater_equal(const A &a, const B &b)
a >= b, by value when both are integers (std::cmp_greater_equal), and with >= otherwise.
Definition util.hpp:268
constexpr bool greater(const A &a, const B &b)
a > b, by value when both are integers (std::cmp_greater), and with > otherwise.
Definition util.hpp:256
constexpr bool less_equal(const A &a, const B &b)
a <= b, by value when both are integers (std::cmp_less_equal), and with <= otherwise.
Definition util.hpp:244