C++Spec 1.0.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
6namespace CppSpec::Matchers {
7
8enum class RangeMode { exclusive, inclusive };
9
10template <typename A, typename E>
11class BeBetween : public MatcherBase<A, E> {
12 E min;
13 E max;
14 RangeMode mode;
15 enum class LtOp { lt, lt_eq } lt_op;
16 enum class GtOp { gt, gt_eq } gt_op;
17
18 public:
19 // BeBetween(Expectation<A> &expectation, E min, E max)
20 // : BaseMatcher<A, E>(expectation), min(min), max(max) {}
21
22 BeBetween(Expectation<A>& expectation, E min, E max, RangeMode mode = RangeMode::inclusive)
23 : MatcherBase<A, E>(expectation), min(min), max(max), mode(mode) {
24 switch (mode) {
25 case RangeMode::inclusive:
26 lt_op = LtOp::lt_eq;
27 gt_op = GtOp::gt_eq;
28 break;
29 case RangeMode::exclusive:
30 lt_op = LtOp::lt;
31 gt_op = GtOp::gt;
32 break;
33 }
34 }
35
36 bool match() override;
37 std::string verb() override { return "be between"; }
38 std::string description() override;
39};
40
41template <typename A, typename E>
42bool BeBetween<A, E>::match() {
43 auto actual = this->actual();
44 bool result1;
45 switch (gt_op) {
46 case GtOp::gt:
47 result1 = actual > min;
48 break;
49 case GtOp::gt_eq:
50 result1 = actual >= min;
51 break;
52 }
53 bool result2;
54 switch (lt_op) {
55 case LtOp::lt:
56 result2 = actual < max;
57 break;
58 case LtOp::lt_eq:
59 result2 = actual <= max;
60 break;
61 }
62 return result1 && result2;
63}
64
65template <typename A, typename E>
67 return std::format("be between {} and {} ({})", min, max, (mode == RangeMode::exclusive ? "exclusive" : "inclusive"));
68}
69
70} // namespace CppSpec::Matchers
Wraps the target of an expectation.
Definition expectation.hpp:47
std::string description() override
Get the description of the Matcher.
Definition be_between.hpp:66
Contains the base class for all Matchers.