Skip to content

Commit 67dfe49

Browse files
committed
Add should_be_approx for floats
1 parent 76b506d commit 67dfe49

6 files changed

Lines changed: 88 additions & 0 deletions

File tree

src/impls/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod generic;
22
pub mod iterator;
3+
pub mod numeric;
34
pub mod option;
45
pub mod result;
56
pub mod string;

src/impls/numeric.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use crate::{caller_name::get_caller_name, expected::Expected, ShouldBeApproximatelyEqual};
2+
3+
impl ShouldBeApproximatelyEqual<f32> for f32 {
4+
fn should_be_approx(&self, expected: impl Expected<f32>, tolerance: impl Expected<f32>) {
5+
let other = expected.value();
6+
let tolerance = tolerance.value();
7+
let high = other + tolerance;
8+
let low = other - tolerance;
9+
let caller_name = get_caller_name().unwrap_or("UNKNOWN".to_string());
10+
11+
if *self > high || *self < low {
12+
panic!("{} should be between {:?} and {:?} but was {:?}", caller_name, low, high, self)
13+
}
14+
}
15+
16+
fn should_not_be_approx(&self, expected: impl Expected<f32>, tolerance: impl Expected<f32>) {
17+
let other = expected.value();
18+
let tolerance = tolerance.value();
19+
let high = other + tolerance;
20+
let low = other - tolerance;
21+
let caller_name = get_caller_name().unwrap_or("UNKNOWN".to_string());
22+
23+
if *self <= high && *self >= low {
24+
panic!("{} should not be between {:?} and {:?} but was {:?}", caller_name, low, high, self)
25+
}
26+
}
27+
}
28+
29+
impl ShouldBeApproximatelyEqual<f64> for f64 {
30+
fn should_be_approx(&self, expected: impl Expected<f64>, tolerance: impl Expected<f64>) {
31+
let other = expected.value();
32+
let tolerance = tolerance.value();
33+
let high = other + tolerance;
34+
let low = other - tolerance;
35+
let caller_name = get_caller_name().unwrap_or("UNKNOWN".to_string());
36+
37+
if *self > high || *self < low {
38+
panic!("{} should be between {:?} and {:?} but was {:?}", caller_name, low, high, self)
39+
}
40+
}
41+
42+
fn should_not_be_approx(&self, expected: impl Expected<f64>, tolerance: impl Expected<f64>) {
43+
let other = expected.value();
44+
let tolerance = tolerance.value();
45+
let high = other + tolerance;
46+
let low = other - tolerance;
47+
let caller_name = get_caller_name().unwrap_or("UNKNOWN".to_string());
48+
49+
if *self <= high && *self >= low {
50+
panic!("{} should not be between {:?} and {:?} but was {:?}", caller_name, low, high, self)
51+
}
52+
}
53+
}

src/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod generic;
22
pub mod iterator;
3+
pub mod numeric;
34
pub mod option;
45
pub mod result;
56
pub mod string;

src/tests/numeric.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use crate::ShouldBeApproximatelyEqual;
2+
3+
4+
#[test]
5+
fn success() {
6+
let value = 0.25;
7+
8+
value.should_be_approx(0.0, 0.25);
9+
value.should_be_approx(0.25, 0.25);
10+
value.should_be_approx(0.5, 0.25);
11+
value.should_not_be_approx(0.75, 0.25);
12+
}
13+
14+
15+
#[test]
16+
#[should_panic(expected = "0.25 should be between 0.5 and 1.0 but was 0.25")]
17+
fn should_be_approx_fail() {
18+
0.25.should_be_approx(0.75, 0.25);
19+
}
20+
21+
#[test]
22+
#[should_panic(expected = "0.25 should not be between -0.25 and 0.25 but was 0.25")]
23+
fn should_not_be_approx_fail() {
24+
0.25.should_not_be_approx(0.0, 0.25);
25+
}

src/traits/approx_equal.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use crate::expected::Expected;
2+
3+
pub trait ShouldBeApproximatelyEqual<T: ?Sized> {
4+
fn should_be_approx(&self, expected: impl Expected<T>, tolerance: impl Expected<T>);
5+
fn should_not_be_approx(&self, expected: impl Expected<T>, tolerance: impl Expected<T>);
6+
}

src/traits/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod approx_equal;
12
mod equal;
23
mod empty;
34
mod option;
@@ -6,6 +7,7 @@ mod result;
67
mod same;
78
mod start_end;
89

10+
pub use approx_equal::*;
911
pub use equal::*;
1012
pub use empty::*;
1113
pub use option::*;

0 commit comments

Comments
 (0)