1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use crate::core::{Join, Matcher};
use num_traits::{self, Float};
use std::fmt;

/// A matcher for `be_close_to` assertions for float numbers.
pub struct BeCloseTo<E> {
    expected: E,
    delta: E,
}

/// Returns a new `BeCloseTo` matcher with default `delta` equal to `0.001`.
pub fn be_close_to<E>(expected: E) -> BeCloseTo<E>
where
    E: Float,
{
    BeCloseTo {
        expected: expected,
        delta: num_traits::cast(0.001).unwrap(),
    }
}

impl<E> BeCloseTo<E> {
    /// Sets new `delta` value.
    pub fn delta(mut self, v: E) -> BeCloseTo<E> {
        self.delta = v;
        self
    }
}

impl<E> Matcher<E, E> for BeCloseTo<E>
where
    E: Float + fmt::Debug,
{
    fn failure_message(&self, join: Join, actual: &E) -> String {
        format!(
            "expected {} be close to <{:?}> ±{:?}, got <{:?}>",
            join, self.expected, self.delta, actual
        )
    }

    fn matches(&self, actual: &E) -> bool {
        if *actual == self.expected {
            true
        } else {
            (self.expected - *actual).abs() - self.delta <= E::zero()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::be_close_to;
    use crate::core::expect;

    #[test]
    fn close_to_one_and_half_failure_message() {
        expect(0.5)
            .to(be_close_to(1.5))
            .assert_eq_message("expected to be close to <1.5> ±0.001, got <0.5>");
    }

    #[test]
    fn to_not_be_close_to_one_and_half_failure_message() {
        expect(1.4991)
            .not_to(be_close_to(1.5))
            .assert_eq_message("expected not to be close to <1.5> ±0.001, got <1.4991>");
    }

    #[test]
    fn close_to_one_and_half_delta_failure_message() {
        expect(0.5)
            .to(be_close_to(1.5).delta(0.1))
            .assert_eq_message("expected to be close to <1.5> ±0.1, got <0.5>");
    }
}