easy_assert/
num_assertions.rs

1//! ## Usage
2//!
3//! ``` rust
4//! use easy_assert::{actual, Actual, expected, Expected};
5//! use easy_assert::num_assertions::NumericAssert;
6//!
7//!  NumericAssert::<i32>::assert_that(actual(1))
8//!  .is_equal()
9//!  .to(expected(1));
10//!
11//!  NumericAssert::<i32>::assert_that(actual(-1))
12//!  .is_not_equal()
13//!  .to(expected(1));
14//!
15//!  NumericAssert::<i32>::assert_that(actual(2))
16//!  .is_greater_or_equal()
17//!  .to(expected(1));
18//!
19//!  NumericAssert::<i32>::assert_that(actual(2))
20//!  .is_greater()
21//!  .than(expected(1));
22//!
23//!  NumericAssert::<i32>::assert_that(actual(1))
24//!  .is_less_or_equal()
25//!  .to(expected(1));
26//!
27//!  NumericAssert::<i32>::assert_that(actual(1))
28//!  .is_less()
29//!  .than(expected(3));
30//! ```
31
32use crate::{test_failed, Actual, Expected};
33use num_traits::Num;
34
35use crate::assertions::{Equals, Greater, GreaterOrEqual, Less, LessOrEqual, NotEquals};
36
37pub struct NumericAssert<T>
38where
39    T: Num + PartialOrd,
40{
41    actual: Actual<T>,
42}
43
44impl<T> NumericAssert<T>
45where
46    T: Num + PartialOrd,
47    T: 'static,
48{
49    pub fn assert_that(actual: Actual<T>) -> NumericAssert<T> {
50        NumericAssert { actual }
51    }
52
53    pub fn is_equal(self) -> Box<dyn Equals<T>> {
54        Box::new(self)
55    }
56
57    pub fn is_not_equal(self) -> Box<dyn NotEquals<T>> {
58        Box::new(self)
59    }
60
61    pub fn is_less(self) -> Box<dyn Less<T>> {
62        Box::new(self)
63    }
64
65    pub fn is_less_or_equal(self) -> Box<dyn LessOrEqual<T>> {
66        Box::new(self)
67    }
68
69    pub fn is_greater(self) -> Box<dyn Greater<T>> {
70        Box::new(self)
71    }
72
73    pub fn is_greater_or_equal(self) -> Box<dyn GreaterOrEqual<T>> {
74        Box::new(self)
75    }
76}
77
78impl<T> Equals<T> for NumericAssert<T>
79where
80    T: Num + PartialOrd,
81{
82    fn to(&self, expected: Expected<T>) {
83        if self.actual.ne(&expected) {
84            let error_message = format!(
85                "\n Actual: {} \n not equal to expected \n {} \n",
86                self.actual, expected
87            );
88            test_failed(&error_message);
89        }
90    }
91}
92
93impl<T> NotEquals<T> for NumericAssert<T>
94where
95    T: Num + PartialOrd,
96{
97    fn to(&self, expected: Expected<T>) {
98        if self.actual.eq(&expected) {
99            let error_message = format!(
100                "\n Actual: {} \n is equal to expected \n {} \n",
101                self.actual, expected
102            );
103            test_failed(&error_message);
104        }
105    }
106}
107
108impl<T> Less<T> for NumericAssert<T>
109where
110    T: Num + PartialOrd,
111{
112    fn than(&self, expected: Expected<T>) {
113        if self.actual.value >= expected.value {
114            let error_message = format!(
115                "\n Actual: {} \n is bigger or equal to expected \n {} \n",
116                self.actual, expected
117            );
118            test_failed(&error_message);
119        }
120    }
121}
122
123impl<T> LessOrEqual<T> for NumericAssert<T>
124where
125    T: Num + PartialOrd,
126{
127    fn to(&self, expected: Expected<T>) {
128        if self.actual.value > expected.value {
129            let error_message = format!(
130                "\n Actual: {} \n is bigger to expected \n {} \n",
131                self.actual, expected
132            );
133            test_failed(&error_message);
134        }
135    }
136}
137
138impl<T> Greater<T> for NumericAssert<T>
139where
140    T: Num + PartialOrd,
141{
142    fn than(&self, expected: Expected<T>) {
143        if self.actual.value <= expected.value {
144            let error_message = format!(
145                "\n Actual: {} \n is smaller or equal to expected \n {} \n",
146                self.actual, expected
147            );
148            test_failed(&error_message);
149        }
150    }
151}
152
153impl<T> GreaterOrEqual<T> for NumericAssert<T>
154where
155    T: Num + PartialOrd,
156{
157    fn to(&self, expected: Expected<T>) {
158        if self.actual.value < expected.value {
159            let error_message = format!(
160                "\n Actual: {} \n is smaller to expected \n {} \n",
161                self.actual, expected
162            );
163            test_failed(&error_message);
164        }
165    }
166}