asserting/order/
mod.rs

1//! Implementation of order assertions.
2
3use crate::assertions::AssertOrder;
4use crate::colored::{mark_missing, mark_unexpected};
5use crate::expectations::{IsAtLeast, IsAtMost, IsGreaterThan, IsLessThan};
6use crate::spec::{DiffFormat, Expectation, Expression, FailingStrategy, Spec};
7use crate::std::fmt::Debug;
8use crate::std::{format, string::String};
9
10impl<S, E, R> AssertOrder<E> for Spec<'_, S, R>
11where
12    S: PartialOrd<E> + Debug,
13    E: Debug,
14    R: FailingStrategy,
15{
16    fn is_less_than(self, expected: E) -> Self {
17        self.expecting(IsLessThan { expected })
18    }
19
20    fn is_greater_than(self, expected: E) -> Self {
21        self.expecting(IsGreaterThan { expected })
22    }
23
24    fn is_at_most(self, expected: E) -> Self {
25        self.expecting(IsAtMost { expected })
26    }
27
28    fn is_at_least(self, expected: E) -> Self {
29        self.expecting(IsAtLeast { expected })
30    }
31}
32
33impl<S, E> Expectation<S> for IsLessThan<E>
34where
35    S: PartialOrd<E> + Debug,
36    E: Debug,
37{
38    fn test(&mut self, subject: &S) -> bool {
39        subject < &self.expected
40    }
41
42    fn message(&self, expression: Expression<'_>, actual: &S, format: &DiffFormat) -> String {
43        let marked_actual = mark_unexpected(actual, format);
44        let marked_expected = mark_missing(&self.expected, format);
45        format!(
46            "expected {expression} is less than {:?}\n   but was: {marked_actual}\n  expected: < {marked_expected}",
47            self.expected,
48        )
49    }
50}
51
52impl<S, E> Expectation<S> for IsAtMost<E>
53where
54    S: PartialOrd<E> + Debug,
55    E: Debug,
56{
57    fn test(&mut self, subject: &S) -> bool {
58        subject <= &self.expected
59    }
60
61    fn message(&self, expression: Expression<'_>, actual: &S, format: &DiffFormat) -> String {
62        let marked_actual = mark_unexpected(actual, format);
63        let marked_expected = mark_missing(&self.expected, format);
64        format!(
65            "expected {expression} is at most {:?}\n   but was: {marked_actual}\n  expected: <= {marked_expected}",
66            self.expected,
67        )
68    }
69}
70
71impl<S, E> Expectation<S> for IsGreaterThan<E>
72where
73    S: PartialOrd<E> + Debug,
74    E: Debug,
75{
76    fn test(&mut self, subject: &S) -> bool {
77        subject > &self.expected
78    }
79
80    fn message(&self, expression: Expression<'_>, actual: &S, format: &DiffFormat) -> String {
81        let marked_actual = mark_unexpected(actual, format);
82        let marked_expected = mark_missing(&self.expected, format);
83        format!(
84            "expected {expression} is greater than {:?}\n   but was: {marked_actual}\n  expected: > {marked_expected}",
85            self.expected,
86        )
87    }
88}
89
90impl<S, E> Expectation<S> for IsAtLeast<E>
91where
92    S: PartialOrd<E> + Debug,
93    E: Debug,
94{
95    fn test(&mut self, subject: &S) -> bool {
96        subject >= &self.expected
97    }
98
99    fn message(&self, expression: Expression<'_>, actual: &S, format: &DiffFormat) -> String {
100        let marked_actual = mark_unexpected(actual, format);
101        let marked_expected = mark_missing(&self.expected, format);
102        format!(
103            "expected {expression} is at least {:?}\n   but was: {marked_actual}\n  expected: >= {marked_expected}",
104            self.expected,
105        )
106    }
107}
108
109#[cfg(test)]
110mod tests;