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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
use crate::ExpectationBuilder;
use crate::expectations::predicate::PredicateExpectation;
use std::fmt::Debug;
use std::ops::RangeBounds;
/// Extension trait for ordering expectations
pub trait OrderExpectations<'e, T> {
/// Expect the value to be less than another value
/// ```
/// # use rxpect::expect;
/// # use rxpect::expectations::OrderExpectations;
///
/// let a = "abc";
/// let b = "def";
/// expect(a).to_be_less_than(b);
/// ```
/// asserts that `a.lt(b)` is true
fn to_be_less_than(self, value: T) -> Self;
/// Expect the value to be less than or equal to another value
/// ```
/// # use rxpect::expect;
/// # use rxpect::expectations::OrderExpectations;
///
/// let a = "abc";
/// let b = "abc";
/// let c = "def";
/// expect(a).to_be_less_than_or_equal(b);
/// expect(a).to_be_less_than_or_equal(c);
/// ```
/// asserts that `a.le(b)` is true
fn to_be_less_than_or_equal(self, value: T) -> Self;
/// Expect the value to be greater than another value
/// ```
/// # use rxpect::expect;
/// # use rxpect::expectations::OrderExpectations;
///
/// let a = "def";
/// let b = "abc";
/// expect(a).to_be_greater_than(b);
/// ```
/// asserts that `a.gt(b)` is true
fn to_be_greater_than(self, value: T) -> Self;
/// Expect the value to be greater than or equal to another value
/// ```
/// # use rxpect::expect;
/// # use rxpect::expectations::OrderExpectations;
///
/// let a = "def";
/// let b = "def";
/// let c = "abc";
/// expect(a).to_be_greater_than_or_equal(b);
/// expect(a).to_be_greater_than_or_equal(c);
/// ```
/// asserts that `a.ge(b)` is true
fn to_be_greater_than_or_equal(self, value: T) -> Self;
/// Expect the value to be inside a range
/// ```
/// # use rxpect::expect;
/// # use rxpect::expectations::OrderExpectations;
///
/// let a = 5;
/// let range = 1..10;
/// expect(a).to_be_inside(range);
/// ```
/// asserts that `a` is inside `range`
///
/// It works with inclusive ranges as well
/// ```
/// # use rxpect::expect;
/// # use rxpect::expectations::OrderExpectations;
///
/// let a = 10;
/// let range = 1..=10;
/// expect(a).to_be_inside(range);
/// ```
fn to_be_inside<R: RangeBounds<T> + Debug + 'e>(self, range: R) -> Self;
}
impl<'e, T, B> OrderExpectations<'e, T> for B
where
T: PartialOrd + Debug + 'e,
B: ExpectationBuilder<'e, Value = T>,
{
fn to_be_less_than(self, value: T) -> Self {
self.to_pass(PredicateExpectation::new(
value,
|a: &T, b: &T| a.lt(b),
|a: &T, b: &T| format!("Expectation failed (a < b)\na: `{:?}`\nb: `{:?}`", a, b),
))
}
fn to_be_less_than_or_equal(self, value: T) -> Self {
self.to_pass(PredicateExpectation::new(
value,
|a: &T, b: &T| a.le(b),
|a: &T, b: &T| format!("Expectation failed (a ≤ b)\na: `{:?}`\nb: `{:?}`", a, b),
))
}
fn to_be_greater_than(self, value: T) -> Self {
self.to_pass(PredicateExpectation::new(
value,
|a: &T, b: &T| a.gt(b),
|a: &T, b: &T| format!("Expectation failed (a > b)\na: `{:?}`\nb: `{:?}`", a, b),
))
}
fn to_be_greater_than_or_equal(self, value: T) -> Self {
self.to_pass(PredicateExpectation::new(
value,
|a: &T, b: &T| a.ge(b),
|a: &T, b: &T| format!("Expectation failed (a ≥ b)\na: `{:?}`\nb: `{:?}`", a, b),
))
}
fn to_be_inside<R: RangeBounds<T> + Debug + 'e>(self, range: R) -> Self {
self.to_pass(PredicateExpectation::new(
range,
|value: &T, range: &R| range.contains(value),
|value: &T, range: &R| {
format!(
"Expectation failed (value ∈ range)\nvalue:`{:?}`\nrange:`{:?}`",
value, range
)
},
))
}
}
#[cfg(test)]
mod tests {
use super::OrderExpectations;
use crate::expect;
use rstest::rstest;
use std::fmt::Debug;
use std::ops::Range;
use std::ops::RangeBounds;
#[test]
pub fn that_greater_than_accepts_lesser_value() {
// Given a value that implements PartialOrd
let value = 2;
// Expect the to_be_greater_than expectation to pass with a lesser value
expect(value).to_be_greater_than(1);
}
#[test]
#[should_panic]
pub fn that_greater_than_does_not_accept_equal_value() {
// Given a value that implements PartialOrd
let value = 2;
// Expect the to_be_greater_than expectation to not pass with the same value
expect(value).to_be_greater_than(2);
}
#[test]
#[should_panic]
pub fn that_greater_than_does_not_accept_greater_value() {
// Given a value that implements PartialOrd
let value = 2;
// Expect the to_be_greater_than expectation to not pass with a greater value
expect(value).to_be_greater_than(3);
}
#[test]
pub fn that_less_than_accepts_greater_value() {
// Given a value that implements PartialOrd
let value = 1;
// Expect the to_be_less_than expectation to pass with a greater value
expect(value).to_be_less_than(2);
}
#[test]
#[should_panic]
pub fn that_less_than_does_not_accept_equal_value() {
// Given a value that implements PartialOrd
let value = 2;
// Expect the to_be_less_than expectation to not pass with the same value
expect(value).to_be_less_than(2);
}
#[test]
#[should_panic]
pub fn that_less_than_does_not_accept_lesser_value() {
// Given a value that implements PartialOrd
let value = 3;
// Expect the to_be_less_than expectation to not pass with a lesser value
expect(value).to_be_less_than(2);
}
#[test]
pub fn that_less_than_or_equal_accepts_greater_value() {
// Given a value that implements PartialOrd
let value = 1;
// Expect the to_be_less_than_or_equal expectation to pass with a greater value
expect(value).to_be_less_than_or_equal(2);
}
#[test]
pub fn that_less_than_or_equal_accepts_equal_value() {
// Given a value that implements PartialOrd
let value = 2;
// Expect the to_be_less_than_or_equal expectation to pass with the same value
expect(value).to_be_less_than_or_equal(2);
}
#[test]
#[should_panic]
pub fn that_less_than_or_equal_does_not_accept_lesser_value() {
// Given a value that implements PartialOrd
let value = 3;
// Expect the to_be_less_than_or_equal expectation to not pass with a lesser value
expect(value).to_be_less_than_or_equal(2);
}
#[test]
pub fn that_greater_than_or_equal_accepts_lesser_value() {
// Given a value that implements PartialOrd
let value = 3;
// Expect the to_be_greater_than_or_equal expectation to pass with a lesser value
expect(value).to_be_greater_than_or_equal(2);
}
#[test]
pub fn that_greater_than_or_equal_accepts_equal_value() {
// Given a value that implements PartialOrd
let value = 2;
// Expect the to_be_greater_than_or_equal expectation to pass with the same value
expect(value).to_be_greater_than_or_equal(2);
}
#[test]
#[should_panic]
pub fn that_greater_than_or_equal_does_not_accept_greater_value() {
// Given a value that implements PartialOrd
let value = 1;
// Expect the to_be_greater_than_or_equal expectation to not pass with a greater value
expect(value).to_be_greater_than_or_equal(2);
}
#[rstest]
#[case(1..=5)]
#[case(5..10)]
pub fn that_inside_does_accept_value_inside_range(
#[case] range: impl RangeBounds<u32> + Debug,
) {
// Given a value that implements PartialOrd
let value = 5;
// Expect the to_be_inside expectation to not pass with a range that does not include it
expect(value).to_be_inside(range);
}
#[rstest]
#[case(1..5)]
#[case(6..10)]
#[should_panic]
pub fn that_inside_does_not_accept_value_outside_range(#[case] range: Range<u32>) {
// Given a value that implements PartialOrd
let value = 5;
// Expect the to_be_inside expectation to not pass with a range that does not include it
expect(value).to_be_inside(range);
}
}