expect_json/expects/ops/expect_integer/
expect_integer.rs

1use crate::expect_op;
2use crate::ops::expect_integer::ExpectIntegerSubOp;
3use crate::Context;
4use crate::ExpectOp;
5use crate::ExpectOpResult;
6use crate::JsonType;
7use core::ops::RangeBounds;
8
9#[expect_op(internal, name = "integer")]
10#[derive(Debug, Clone, Default, PartialEq)]
11pub struct ExpectInteger {
12    sub_ops: Vec<ExpectIntegerSubOp>,
13}
14
15impl ExpectInteger {
16    pub(crate) fn new() -> Self {
17        Self { sub_ops: vec![] }
18    }
19
20    pub fn is_in_range<R>(mut self, range: R) -> Self
21    where
22        R: RangeBounds<i64>,
23    {
24        let min = range.start_bound().cloned();
25        let max = range.end_bound().cloned();
26
27        self.sub_ops.push(ExpectIntegerSubOp::InRange {
28            min: min.into(),
29            max: max.into(),
30        });
31        self
32    }
33
34    pub fn is_zero(mut self) -> Self {
35        self.sub_ops.push(ExpectIntegerSubOp::Zero);
36        self
37    }
38
39    pub fn is_not_zero(mut self) -> Self {
40        self.sub_ops.push(ExpectIntegerSubOp::NotZero);
41        self
42    }
43
44    pub fn is_positive(mut self) -> Self {
45        self.sub_ops.push(ExpectIntegerSubOp::Positive);
46        self
47    }
48
49    pub fn is_negative(mut self) -> Self {
50        self.sub_ops.push(ExpectIntegerSubOp::Negative);
51        self
52    }
53}
54
55impl ExpectOp for ExpectInteger {
56    fn on_i64(&self, context: &mut Context, received: i64) -> ExpectOpResult<()> {
57        for sub_op in &self.sub_ops {
58            sub_op.on_i64(self, context, received)?;
59        }
60
61        Ok(())
62    }
63
64    fn on_u64(&self, context: &mut Context, received: u64) -> ExpectOpResult<()> {
65        for sub_op in &self.sub_ops {
66            sub_op.on_u64(self, context, received)?;
67        }
68
69        Ok(())
70    }
71
72    fn supported_types(&self) -> &'static [JsonType] {
73        &[JsonType::Integer]
74    }
75}
76
77#[cfg(test)]
78mod test_is_in_range {
79    use crate::expect;
80    use crate::expect_json_eq;
81    use pretty_assertions::assert_eq;
82    use serde_json::json;
83
84    #[test]
85    fn it_should_be_true_for_all_values_in_total_range() {
86        let left = json!(1);
87        let right = json!(expect::integer().is_in_range(..));
88        let output = expect_json_eq(&left, &right);
89        assert!(output.is_ok());
90
91        let left = json!(i64::MIN);
92        let right = json!(expect::integer().is_in_range(..));
93        let output = expect_json_eq(&left, &right);
94        assert!(output.is_ok());
95
96        let left = json!(u64::MAX);
97        let right = json!(expect::integer().is_in_range(..));
98        let output = expect_json_eq(&left, &right);
99        assert!(output.is_ok());
100    }
101
102    #[test]
103    fn it_should_be_true_for_all_values_in_partial_range() {
104        let left = json!(0);
105        let right = json!(expect::integer().is_in_range(-10..10));
106        let output = expect_json_eq(&left, &right);
107        assert!(output.is_ok());
108
109        let left = json!(-10);
110        let right = json!(expect::integer().is_in_range(-10..10));
111        let output = expect_json_eq(&left, &right);
112        assert!(output.is_ok());
113
114        let left = json!(5);
115        let right = json!(expect::integer().is_in_range(-10..10));
116        let output = expect_json_eq(&left, &right);
117        assert!(output.is_ok());
118    }
119
120    #[test]
121    fn it_should_be_false_for_all_values_out_of_range() {
122        let left = json!(1);
123        let right = json!(expect::integer().is_in_range(0..1));
124
125        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
126        assert_eq!(
127            output,
128            r#"Json expect::integer() error at root:
129    integer is not in range
130    expected 0..1
131    received 1"#
132        );
133
134        let left = json!(-11);
135        let right = json!(expect::integer().is_in_range(0..1));
136
137        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
138        assert_eq!(
139            output,
140            r#"Json expect::integer() error at root:
141    integer is not in range
142    expected 0..1
143    received -11"#
144        );
145    }
146
147    #[test]
148    fn it_should_be_true_for_value_in_inclusive_range() {
149        let left = json!(1.0);
150        let right = json!(expect::float().is_in_range(0.0..=1.0));
151
152        let output = expect_json_eq(&left, &right);
153        assert!(output.is_ok());
154    }
155
156    #[test]
157    fn it_should_be_true_for_positive_value_with_negative_min() {
158        let left = json!(5);
159        let right = json!(expect::integer().is_in_range(-10..10));
160
161        let output = expect_json_eq(&left, &right);
162        assert!(output.is_ok());
163    }
164
165    #[test]
166    fn it_should_be_false_for_positive_value_outside_range_with_negative_min() {
167        let left = json!(11);
168        let right = json!(expect::integer().is_in_range(-10..10));
169
170        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
171        assert_eq!(
172            output,
173            r#"Json expect::integer() error at root:
174    integer is not in range
175    expected -10..10
176    received 11"#
177        );
178    }
179
180    #[test]
181    fn it_should_be_false_for_positive_value_outside_range_with_negative_range() {
182        let left = json!(11);
183        let right = json!(expect::integer().is_in_range(-10..-1));
184
185        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
186        assert_eq!(
187            output,
188            r#"Json expect::integer() error at root:
189    integer is not in range
190    expected -10..-1
191    received 11"#
192        );
193    }
194}
195
196#[cfg(test)]
197mod test_is_zero {
198    use crate::expect;
199    use crate::expect_json_eq;
200    use pretty_assertions::assert_eq;
201    use serde_json::json;
202
203    #[test]
204    fn it_should_be_true_for_zero() {
205        let left = json!(0);
206        let right = json!(expect::integer().is_zero());
207
208        let output = expect_json_eq(&left, &right);
209        assert!(output.is_ok());
210    }
211
212    #[test]
213    fn it_should_be_false_for_negative_value() {
214        let left = json!(-1);
215        let right = json!(expect::integer().is_zero());
216
217        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
218        assert_eq!(
219            output,
220            r#"Json expect::integer() error at root, is not zero:
221    expected 0
222    received -1"#
223        );
224    }
225
226    #[test]
227    fn it_should_be_false_for_negative_max() {
228        let left = json!(i64::MIN);
229        let right = json!(expect::integer().is_zero());
230
231        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
232        assert_eq!(
233            output,
234            r#"Json expect::integer() error at root, is not zero:
235    expected 0
236    received -9223372036854775808"#
237        );
238    }
239
240    #[test]
241    fn it_should_be_false_for_positive_value() {
242        let left = json!(1);
243        let right = json!(expect::integer().is_zero());
244
245        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
246        assert_eq!(
247            output,
248            r#"Json expect::integer() error at root, is not zero:
249    expected 0
250    received 1"#
251        );
252    }
253
254    #[test]
255    fn it_should_be_false_for_i64_max() {
256        let left = json!(i64::MAX);
257        let right = json!(expect::integer().is_zero());
258
259        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
260        assert_eq!(
261            output,
262            r#"Json expect::integer() error at root, is not zero:
263    expected 0
264    received 9223372036854775807"#
265        );
266    }
267
268    #[test]
269    fn it_should_be_false_for_u64_max() {
270        let left = json!(u64::MAX);
271        let right = json!(expect::integer().is_zero());
272
273        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
274        assert_eq!(
275            output,
276            r#"Json expect::integer() error at root, is not zero:
277    expected 0
278    received 18446744073709551615"#
279        );
280    }
281}
282
283#[cfg(test)]
284mod test_is_not_zero {
285    use crate::expect;
286    use crate::expect_json_eq;
287    use pretty_assertions::assert_eq;
288    use serde_json::json;
289
290    #[test]
291    fn it_should_be_false_for_zero() {
292        let left = json!(0);
293        let right = json!(expect::integer().is_not_zero());
294
295        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
296        assert_eq!(
297            output,
298            r#"Json expect::integer() error at root, is zero:
299    expected non-zero integer
300    received 0"#
301        );
302    }
303
304    #[test]
305    fn it_should_be_true_for_negative_value() {
306        let left = json!(-1);
307        let right = json!(expect::integer().is_not_zero());
308
309        let output = expect_json_eq(&left, &right);
310        assert!(output.is_ok());
311    }
312
313    #[test]
314    fn it_should_be_true_for_negative_max() {
315        let left = json!(i64::MIN);
316        let right = json!(expect::integer().is_not_zero());
317
318        let output = expect_json_eq(&left, &right);
319        assert!(output.is_ok());
320    }
321
322    #[test]
323    fn it_should_be_true_for_positive_value() {
324        let left = json!(1);
325        let right = json!(expect::integer().is_not_zero());
326
327        let output = expect_json_eq(&left, &right);
328        assert!(output.is_ok());
329    }
330
331    #[test]
332    fn it_should_be_true_for_i64_max() {
333        let left = json!(i64::MAX);
334        let right = json!(expect::integer().is_not_zero());
335
336        let output = expect_json_eq(&left, &right);
337        assert!(output.is_ok());
338    }
339
340    #[test]
341    fn it_should_be_true_for_u64_max() {
342        let left = json!(u64::MAX);
343        let right = json!(expect::integer().is_not_zero());
344
345        let output = expect_json_eq(&left, &right);
346        assert!(output.is_ok());
347    }
348}
349
350#[cfg(test)]
351mod test_is_positive {
352    use crate::expect;
353    use crate::expect_json_eq;
354    use pretty_assertions::assert_eq;
355    use serde_json::json;
356
357    #[test]
358    fn it_should_be_true_for_zero() {
359        let left = json!(0);
360        let right = json!(expect::integer().is_positive());
361
362        let output = expect_json_eq(&left, &right);
363        assert!(output.is_ok());
364    }
365
366    #[test]
367    fn it_should_be_false_for_negative_i64() {
368        let left = json!(-1);
369        let right = json!(expect::integer().is_positive());
370
371        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
372        assert_eq!(
373            output,
374            r#"Json expect::integer() error at root:
375    integer is not positive
376    received -1"#
377        );
378    }
379
380    #[test]
381    fn it_should_be_true_for_positive_i64() {
382        let left = json!(123_i64);
383        let right = json!(expect::integer().is_positive());
384
385        let output = expect_json_eq(&left, &right);
386        assert!(output.is_ok());
387    }
388
389    #[test]
390    fn it_should_be_true_for_positive_u64() {
391        let left = json!(123_u64);
392        let right = json!(expect::integer().is_positive());
393
394        let output = expect_json_eq(&left, &right);
395        assert!(output.is_ok());
396    }
397}
398
399#[cfg(test)]
400mod test_is_negative {
401    use crate::expect;
402    use crate::expect_json_eq;
403    use pretty_assertions::assert_eq;
404    use serde_json::json;
405
406    #[test]
407    fn it_should_be_false_for_zero() {
408        let left = json!(0);
409        let right = json!(expect::integer().is_negative());
410
411        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
412        assert_eq!(
413            output,
414            r#"Json expect::integer() error at root:
415    integer is not negative
416    received 0"#
417        );
418    }
419
420    #[test]
421    fn it_should_be_true_for_negative_i64() {
422        let left = json!(-1);
423        let right = json!(expect::integer().is_negative());
424
425        let output = expect_json_eq(&left, &right);
426        assert!(output.is_ok());
427    }
428
429    #[test]
430    fn it_should_be_false_for_positive_i64() {
431        let left = json!(123_i64);
432        let right = json!(expect::integer().is_negative());
433
434        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
435        assert_eq!(
436            output,
437            r#"Json expect::integer() error at root:
438    integer is not negative
439    received 123"#
440        );
441    }
442
443    #[test]
444    fn it_should_be_false_for_positive_u64() {
445        let left = json!(123_u64);
446        let right = json!(expect::integer().is_negative());
447
448        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
449        assert_eq!(
450            output,
451            r#"Json expect::integer() error at root:
452    integer is not negative
453    received 123"#
454        );
455    }
456}