1+ #include < algorithm>
12#include < source_location>
23
34#include " cppspec.hpp"
@@ -59,6 +60,20 @@ describe be_within_spec("expect(actual).to_be_within(delta).of(expected)", $ {
5960// }).to_fail_with("expected 5.51 to be within 0.5 of 5.0");
6061// });
6162
63+ it (" reports a failure through expect() when actual is outside the tolerance" , _ {
64+ expect (4.49 ).to_be_within (0.5 ).of (5.0 );
65+ const bool failed = std::ranges::any_of (self.get_results (), [](const Result& r) { return r.is_failure (); });
66+ self.clear_results ();
67+ expect (failed).to_be_true ();
68+ });
69+
70+ it (" passes when expected is zero" , _ {
71+ expect (0.05 ).to_be_within (0.1 ).of (0.0 );
72+ const bool failed = std::ranges::any_of (self.get_results (), [](const Result& r) { return r.is_failure (); });
73+ self.clear_results ();
74+ expect (failed).to_be_false ();
75+ });
76+
6277 it (" provides a description" , _ {
6378 double d = 5.1 ;
6479 ExpectationValue ex (self, d, std::source_location::current ());
@@ -80,8 +95,14 @@ describe be_within_spec("expect(actual).to_be_within(delta).of(expected)", $ {
8095 expect (matcher.run ()).to_fail_with (" expected 20.1 to be within 10% of 10" );
8196 });
8297
98+ it (" scales the tolerance by expected" , _ {
99+ auto ex = ExpectationValue (12.0 , std::source_location::current ());
100+ auto matcher = Matchers::BeWithinHelper (ex, 10.0 ).percent_of (10.0 );
101+ expect (matcher.run ()).to_fail_with (" expected 12 to be within 10% of 10" );
102+ });
103+
83104 it (" provides a description" , _ {
84- auto d = 5.1 ;
105+ auto d = 5.01 ; // within 0.5% of 5: percent_of() now runs the matcher
85106 ExpectationValue<double > ex (self, d, std::source_location::current ());
86107 Matchers::BeWithin matcher = Matchers::BeWithinHelper (ex, 0.5 ).percent_of (5.0 );
87108 expect (matcher.description ()).to_equal (" be within 0.5% of 5" );
0 commit comments