fluent_assertions/assertions/
numeric_assertion.rs

1use super::Assertion;
2use num_traits::Zero;
3use std::cmp::PartialOrd;
4use std::fmt::Display;
5
6/// Specific assertions for numeric types
7impl<T> Assertion<T>
8where
9    T: PartialOrd + Display + Zero + Copy,
10{
11    /// Asserts that the value is greater than or equal to the given value
12    pub fn be_greater_than_or_equal_to(&self, other: T) -> &Self {
13        assert!(
14            self.value >= other,
15            "Expected value to be greater than or equal to {}, but got {}",
16            other,
17            self.value
18        );
19        self
20    }
21
22    /// Asserts that the value is greater than the given value
23    pub fn be_greater_than(&self, other: T) -> &Self {
24        assert!(
25            self.value > other,
26            "Expected value to be greater than {}, but got {}",
27            other,
28            self.value
29        );
30        self
31    }
32
33    /// Asserts that the value is less than or equal to the given value
34    pub fn be_less_than_or_equal_to(&self, other: T) -> &Self {
35        assert!(
36            self.value <= other,
37            "Expected value to be less than or equal to {}, but got {}",
38            other,
39            self.value
40        );
41        self
42    }
43
44    /// Asserts that the value is less than the given value
45    pub fn be_less_than(&self, other: T) -> &Self {
46        assert!(
47            self.value < other,
48            "Expected value to be less than {}, but got {}",
49            other,
50            self.value
51        );
52        self
53    }
54
55    /// Asserts that the value is positive
56    pub fn be_positive(&self) -> &Self {
57        assert!(
58            self.value > T::zero(),
59            "Expected positive value, but found {}",
60            self.value
61        );
62        self
63    }
64
65    /// Asserts that the value is negative
66    pub fn be_negative(&self) -> &Self {
67        assert!(
68            self.value < T::zero(),
69            "Expected negative value, but found {}",
70            self.value
71        );
72        self
73    }
74
75    /// Asserts that the value is in the given range
76    pub fn be_in_range(&self, start: T, end: T) -> &Self {
77        assert!(
78            self.value >= start && self.value <= end,
79            "Expected value to be in range {}-{}, but got {}",
80            start,
81            end,
82            self.value
83        );
84        self
85    }
86
87    /// Asserts that the value is not in the given range
88    pub fn not_be_in_range(&self, start: T, end: T) -> &Self {
89        assert!(
90            self.value < start || self.value > end,
91            "Expected value to not be in range {}-{}, but got {}",
92            start,
93            end,
94            self.value
95        );
96        self
97    }
98}
99
100#[cfg(test)]
101mod tests {
102    use crate::assertions::*;
103    use rstest::*;
104
105    #[rstest]
106    #[case(43, 42)]
107    #[case(1, 0)]
108    fn should_be_greater_and_positive(#[case] input: isize, #[case] value: isize) {
109        input.should().be_greater_than(value).be_positive();
110    }
111
112    #[rstest]
113    #[case(42.0, 41.99)]
114    #[case(1.0, 0.9)]
115    fn should_be_greater_f64(#[case] input: f64, #[case] value: f64) {
116        input.should().be_greater_than(value);
117    }
118
119    #[rstest]
120    #[case(-42)]
121    #[case(-3)]
122    fn should_be_negative_i8(#[case] input: i8) {
123        input
124            .should()
125            .be_greater_than(-43)
126            .be_negative()
127            .not_be(32)
128            .be(input);
129    }
130}