C++Spec 1.0.0
BDD testing for C++
Loading...
Searching...
No Matches
be_within.hpp
1#pragma once
2
4
5namespace CppSpec::Matchers {
6template <typename A, typename E>
7class BeWithin;
8
9template <typename A, typename E>
10class BeWithinHelper {
11 Expectation<A>& expectation;
12 E tolerance;
13 std::string msg;
14
15 public:
16 BeWithinHelper(Expectation<A>& expectation, E tolerance) : expectation(expectation), tolerance(tolerance) {}
17
19 BeWithin<A, E> percent_of(E expected);
20 void set_message(const std::string& msg) { this->msg = msg; }
21 std::string get_message() { return this->msg; }
22};
23
24template <typename A, typename E>
25class BeWithin : public MatcherBase<A, E> {
26 E tolerance;
27 std::string unit;
28
29 public:
30 BeWithin(Expectation<A>& expectation, E tolerance, E value, std::string_view unit)
31 : MatcherBase<A, E>(expectation, value), tolerance{tolerance}, unit{unit} {}
32
33 bool match() override;
34
35 std::string failure_message() override;
36 std::string failure_message_when_negated() override;
37 std::string description() override;
38 std::string verb() override { return "be within"; }
39};
40
41template <typename A, typename E>
42BeWithin<A, E> BeWithinHelper<A, E>::of(E expected) {
43 auto matcher = BeWithin<A, E>(expectation, tolerance, expected, ""); // No unit specified
44 matcher.set_message(msg);
45 return matcher;
46}
47
48template <typename A, typename E>
49BeWithin<A, E> BeWithinHelper<A, E>::percent_of(E expected) {
50 auto matcher = BeWithin<A, E>(expectation, tolerance, expected, "%"); // Percent unit specified
51 matcher.set_message(msg);
52 return matcher;
53}
54
55template <typename A, typename E>
56bool BeWithin<A, E>::match() {
57 if (!this->expected()) {
58 return false;
59 }
60 return std::abs(this->actual() - this->expected()) <= this->tolerance;
61}
62
63template <typename A, typename E>
65 return std::format("expected {} to {}", this->actual(), description());
66}
67
68template <typename A, typename E>
70 return std::format("expected {} not to {}", this->actual(), description());
71}
72
73template <typename A, typename E>
75 return std::format("be within {}{} of {}", this->tolerance, this->unit, this->expected());
76}
77
78} // namespace CppSpec::Matchers
Wraps the target of an expectation.
Definition expectation.hpp:47
Definition be_within.hpp:25
std::string description() override
Get the description of the Matcher.
Definition be_within.hpp:74
std::string failure_message() override
Get message to give on match failure.
Definition be_within.hpp:64
std::string failure_message_when_negated() override
Get message to give on match failure when negated.
Definition be_within.hpp:69
Definition have_error.hpp:9
Contains the base class for all Matchers.