1use crate::assertions::AssertOrder;
4use crate::colored::{mark_missing, mark_unexpected};
5use crate::expectations::{
6 is_after, is_at_least, is_at_most, is_before, is_between, is_greater_than, is_less_than,
7 IsAfter, IsAtLeast, IsAtMost, IsBefore, IsBetween, IsGreaterThan, IsLessThan,
8};
9use crate::spec::{DiffFormat, Expectation, Expression, FailingStrategy, Invertible, Spec};
10use crate::std::fmt::Debug;
11use crate::std::{format, string::String};
12
13impl<S, E, R> AssertOrder<E> for Spec<'_, S, R>
14where
15 S: PartialOrd<E> + Debug,
16 E: Debug,
17 R: FailingStrategy,
18{
19 fn is_less_than(self, expected: E) -> Self {
20 self.expecting(is_less_than(expected))
21 }
22
23 fn is_greater_than(self, expected: E) -> Self {
24 self.expecting(is_greater_than(expected))
25 }
26
27 fn is_at_most(self, expected: E) -> Self {
28 self.expecting(is_at_most(expected))
29 }
30
31 fn is_at_least(self, expected: E) -> Self {
32 self.expecting(is_at_least(expected))
33 }
34
35 fn is_before(self, expected: E) -> Self {
36 self.expecting(is_before(expected))
37 }
38
39 fn is_after(self, expected: E) -> Self {
40 self.expecting(is_after(expected))
41 }
42
43 fn is_between(self, min: E, max: E) -> Self {
44 self.expecting(is_between(min, max))
45 }
46}
47
48impl<S, E> Expectation<S> for IsLessThan<E>
49where
50 S: PartialOrd<E> + Debug,
51 E: Debug,
52{
53 fn test(&mut self, subject: &S) -> bool {
54 subject < &self.expected
55 }
56
57 fn message(
58 &self,
59 expression: &Expression<'_>,
60 actual: &S,
61 inverted: bool,
62 format: &DiffFormat,
63 ) -> String {
64 let (not, cmp) = if inverted { ("not ", ">=") } else { ("", "<") };
65 let marked_actual = mark_unexpected(actual, format);
66 let marked_expected = mark_missing(&self.expected, format);
67 format!(
68 "expected {expression} to be {not}less than {:?}\n but was: {marked_actual}\n expected: {cmp} {marked_expected}",
69 self.expected,
70 )
71 }
72}
73
74impl<E> Invertible for IsLessThan<E> {}
75
76impl<S, E> Expectation<S> for IsAtMost<E>
77where
78 S: PartialOrd<E> + Debug,
79 E: Debug,
80{
81 fn test(&mut self, subject: &S) -> bool {
82 subject <= &self.expected
83 }
84
85 fn message(
86 &self,
87 expression: &Expression<'_>,
88 actual: &S,
89 inverted: bool,
90 format: &DiffFormat,
91 ) -> String {
92 let (not, cmp) = if inverted { ("not ", ">") } else { ("", "<=") };
93 let marked_actual = mark_unexpected(actual, format);
94 let marked_expected = mark_missing(&self.expected, format);
95 format!(
96 "expected {expression} to be {not}at most {:?}\n but was: {marked_actual}\n expected: {cmp} {marked_expected}",
97 self.expected,
98 )
99 }
100}
101
102impl<E> Invertible for IsAtMost<E> {}
103
104impl<S, E> Expectation<S> for IsGreaterThan<E>
105where
106 S: PartialOrd<E> + Debug,
107 E: Debug,
108{
109 fn test(&mut self, subject: &S) -> bool {
110 subject > &self.expected
111 }
112
113 fn message(
114 &self,
115 expression: &Expression<'_>,
116 actual: &S,
117 inverted: bool,
118 format: &DiffFormat,
119 ) -> String {
120 let (not, cmp) = if inverted { ("not ", "<=") } else { ("", ">") };
121 let marked_actual = mark_unexpected(actual, format);
122 let marked_expected = mark_missing(&self.expected, format);
123 format!(
124 "expected {expression} to be {not}greater than {:?}\n but was: {marked_actual}\n expected: {cmp} {marked_expected}",
125 self.expected,
126 )
127 }
128}
129
130impl<E> Invertible for IsGreaterThan<E> {}
131
132impl<S, E> Expectation<S> for IsAtLeast<E>
133where
134 S: PartialOrd<E> + Debug,
135 E: Debug,
136{
137 fn test(&mut self, subject: &S) -> bool {
138 subject >= &self.expected
139 }
140
141 fn message(
142 &self,
143 expression: &Expression<'_>,
144 actual: &S,
145 inverted: bool,
146 format: &DiffFormat,
147 ) -> String {
148 let (not, cmp) = if inverted { ("not ", "<") } else { ("", ">=") };
149 let marked_actual = mark_unexpected(actual, format);
150 let marked_expected = mark_missing(&self.expected, format);
151 format!(
152 "expected {expression} to be {not}at least {:?}\n but was: {marked_actual}\n expected: {cmp} {marked_expected}",
153 self.expected,
154 )
155 }
156}
157
158impl<E> Invertible for IsAtLeast<E> {}
159
160impl<S, E> Expectation<S> for IsBefore<E>
161where
162 S: PartialOrd<E> + Debug,
163 E: Debug,
164{
165 fn test(&mut self, subject: &S) -> bool {
166 subject < &self.expected
167 }
168
169 fn message(
170 &self,
171 expression: &Expression<'_>,
172 actual: &S,
173 inverted: bool,
174 format: &DiffFormat,
175 ) -> String {
176 let (not, cmp) = if inverted { ("not ", ">=") } else { ("", "<") };
177 let marked_actual = mark_unexpected(actual, format);
178 let marked_expected = mark_missing(&self.expected, format);
179 format!(
180 "expected {expression} to be {not}before {:?}\n but was: {marked_actual}\n expected: {cmp} {marked_expected}",
181 self.expected,
182 )
183 }
184}
185
186impl<E> Invertible for IsBefore<E> {}
187
188impl<S, E> Expectation<S> for IsAfter<E>
189where
190 S: PartialOrd<E> + Debug,
191 E: Debug,
192{
193 fn test(&mut self, subject: &S) -> bool {
194 subject > &self.expected
195 }
196
197 fn message(
198 &self,
199 expression: &Expression<'_>,
200 actual: &S,
201 inverted: bool,
202 format: &DiffFormat,
203 ) -> String {
204 let (not, cmp) = if inverted { ("not ", "<=") } else { ("", ">") };
205 let marked_actual = mark_unexpected(actual, format);
206 let marked_expected = mark_missing(&self.expected, format);
207 format!(
208 "expected {expression} to be {not}after {:?}\n but was: {marked_actual}\n expected: {cmp} {marked_expected}",
209 self.expected,
210 )
211 }
212}
213
214impl<E> Invertible for IsAfter<E> {}
215
216impl<S, E> Expectation<S> for IsBetween<E>
217where
218 S: PartialOrd<E> + Debug,
219 E: Debug,
220{
221 fn test(&mut self, subject: &S) -> bool {
222 subject >= &self.min && subject <= &self.max
223 }
224
225 fn message(
226 &self,
227 expression: &Expression<'_>,
228 actual: &S,
229 inverted: bool,
230 format: &DiffFormat,
231 ) -> String {
232 let (not, cmp) = if inverted {
233 ("not ", "> x or x >")
234 } else {
235 ("", "<= x <=")
236 };
237 let marked_actual = mark_unexpected(actual, format);
238 let marked_start = if (actual < &self.min) || inverted {
239 mark_missing(&self.min, format)
240 } else {
241 format!("{:?}", &self.min)
242 };
243 let marked_end = if (actual > &self.max) || inverted {
244 mark_missing(&self.max, format)
245 } else {
246 format!("{:?}", &self.max)
247 };
248 format!(
249 "expected {expression} to be {not}between {:?} and {:?}\n but was: {marked_actual}\n expected: {marked_start} {cmp} {marked_end}",
250 self.min, self.max
251 )
252 }
253}
254
255impl<E> Invertible for IsBetween<E> {}
256
257#[cfg(test)]
258mod tests;