C++Spec 1.1.0
BDD testing for C++
Loading...
Searching...
No Matches
be_between.hpp
Go to the documentation of this file.
1
2#pragma once
3
5#include "util.hpp"
6
7namespace CppSpec::Matchers {
8
9enum class RangeMode { exclusive, inclusive };
10
16template <typename A, typename E>
17class BeBetween : public MatcherBase<A, E> {
18 E min;
19 E max;
20 RangeMode mode;
21 enum class LtOp { lt, lt_eq } lt_op;
22 enum class GtOp { gt, gt_eq } gt_op;
23
24 public:
25 // BeBetween(Expectation<A> &expectation, E min, E max)
26 // : BaseMatcher<A, E>(expectation), min(min), max(max) {}
27
28 BeBetween(Expectation<A>& expectation, E min, E max, RangeMode mode = RangeMode::inclusive)
29 : MatcherBase<A, E>(expectation), min(min), max(max), mode(mode) {
30 switch (mode) {
31 case RangeMode::inclusive:
32 lt_op = LtOp::lt_eq;
33 gt_op = GtOp::gt_eq;
34 break;
35 case RangeMode::exclusive:
36 lt_op = LtOp::lt;
37 gt_op = GtOp::gt;
38 break;
39 }
40 }
41
42 bool match() override;
43 bool negated_match() override;
44 std::string verb() override { return "be between"; }
45 std::string description() override;
46};
47
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;
54}
55
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;
62}
63
64template <typename A, typename E>
66 return std::format("be between {} and {} ({})", min, max, (mode == RangeMode::exclusive ? "exclusive" : "inclusive"));
67}
68
69} // namespace CppSpec::Matchers
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