expect_json/expect/ops/expect_float/
expect_float.rs

1use crate::expect::ops::expect_float::ExpectFloatSubOp;
2use crate::expect_core::expect_op;
3use crate::expect_core::Context;
4use crate::expect_core::ExpectOp;
5use crate::expect_core::ExpectOpResult;
6use crate::JsonType;
7use core::ops::RangeBounds;
8
9#[expect_op(internal, name = "float")]
10#[derive(Debug, Clone, Default, PartialEq)]
11pub struct ExpectFloat {
12    sub_ops: Vec<ExpectFloatSubOp>,
13}
14
15impl ExpectFloat {
16    pub(crate) fn new() -> Self {
17        Self { sub_ops: vec![] }
18    }
19
20    pub fn in_range<R>(mut self, range: R) -> Self
21    where
22        R: RangeBounds<f64>,
23    {
24        let min = range.start_bound().cloned();
25        let max = range.end_bound().cloned();
26
27        self.sub_ops.push(ExpectFloatSubOp::InRange {
28            min: min.into(),
29            max: max.into(),
30        });
31
32        self
33    }
34
35    pub fn outside_range<R>(mut self, range: R) -> Self
36    where
37        R: RangeBounds<f64>,
38    {
39        let min = range.start_bound().cloned();
40        let max = range.end_bound().cloned();
41
42        self.sub_ops.push(ExpectFloatSubOp::OutsideRange {
43            min: min.into(),
44            max: max.into(),
45        });
46
47        self
48    }
49
50    pub fn zero(mut self) -> Self {
51        self.sub_ops.push(ExpectFloatSubOp::Zero);
52        self
53    }
54
55    pub fn not_zero(mut self) -> Self {
56        self.sub_ops.push(ExpectFloatSubOp::NotZero);
57        self
58    }
59
60    pub fn positive(mut self) -> Self {
61        self.sub_ops.push(ExpectFloatSubOp::Positive);
62        self
63    }
64
65    pub fn negative(mut self) -> Self {
66        self.sub_ops.push(ExpectFloatSubOp::Negative);
67        self
68    }
69}
70
71impl ExpectOp for ExpectFloat {
72    fn on_f64(&self, context: &mut Context, received: f64) -> ExpectOpResult<()> {
73        for sub_op in &self.sub_ops {
74            sub_op.on_f64(self, context, received)?;
75        }
76
77        Ok(())
78    }
79
80    fn debug_supported_types(&self) -> &'static [JsonType] {
81        &[JsonType::Float]
82    }
83}
84
85#[cfg(test)]
86mod test_in_range {
87    use crate::expect;
88    use crate::expect_json_eq;
89    use pretty_assertions::assert_eq;
90    use serde_json::json;
91
92    #[test]
93    fn it_should_be_true_for_all_values_in_total_range() {
94        let left = json!(1.0);
95        let right = json!(expect::float().in_range(..));
96        let output = expect_json_eq(&left, &right);
97        assert!(output.is_ok());
98
99        let left = json!(f64::MIN);
100        let right = json!(expect::float().in_range(..));
101        let output = expect_json_eq(&left, &right);
102        assert!(output.is_ok());
103    }
104
105    #[test]
106    fn it_should_be_true_for_all_values_in_partial_range() {
107        let left = json!(0.5);
108        let right = json!(expect::float().in_range(0.0..1.0));
109        let output = expect_json_eq(&left, &right);
110        assert!(output.is_ok());
111    }
112
113    #[test]
114    fn it_should_be_false_for_all_values_out_of_range() {
115        let left = json!(1.0);
116        let right = json!(expect::float().in_range(0.0..1.0));
117
118        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
119        assert_eq!(
120            output,
121            r#"Json expect::float() error at root:
122    float is not in range
123    expected 0.0..1.0
124    received 1.0"#
125        );
126    }
127
128    #[test]
129    fn it_should_be_true_for_value_in_inclusive_range() {
130        let left = json!(1.0);
131        let right = json!(expect::float().in_range(0.0..=1.0));
132
133        let output = expect_json_eq(&left, &right);
134        assert!(output.is_ok());
135    }
136}
137
138#[cfg(test)]
139mod test_outside_range {
140    use crate::expect;
141    use crate::expect_json_eq;
142    use pretty_assertions::assert_eq;
143    use serde_json::json;
144
145    #[test]
146    fn it_should_be_false_for_all_values_in_total_range() {
147        let left = json!(1.0);
148        let right = json!(expect::float().outside_range(..));
149        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
150        assert_eq!(
151            output,
152            r#"Json expect::float() error at root:
153    float is in range
154    expected ..
155    received 1.0"#
156        );
157
158        let left = json!(f64::MIN);
159        let right = json!(expect::float().outside_range(..));
160        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
161        assert_eq!(
162            output,
163            r#"Json expect::float() error at root:
164    float is in range
165    expected ..
166    received -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0"#
167        );
168    }
169
170    #[test]
171    fn it_should_be_false_for_all_values_in_partial_range() {
172        let left = json!(0.5);
173        let right = json!(expect::float().outside_range(0.0..1.0));
174
175        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
176        assert_eq!(
177            output,
178            r#"Json expect::float() error at root:
179    float is in range
180    expected 0.0..1.0
181    received 0.5"#
182        );
183    }
184
185    #[test]
186    fn it_should_be_true_for_all_values_out_of_range() {
187        let left = json!(1.0);
188        let right = json!(expect::float().outside_range(0.0..1.0));
189
190        let output = expect_json_eq(&left, &right);
191        assert!(output.is_ok());
192    }
193
194    #[test]
195    fn it_should_be_false_for_value_in_inclusive_range() {
196        let left = json!(1.0);
197        let right = json!(expect::float().outside_range(0.0..=1.0));
198
199        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
200        assert_eq!(
201            output,
202            r#"Json expect::float() error at root:
203    float is in range
204    expected 0.0..=1.0
205    received 1.0"#
206        );
207    }
208}
209
210#[cfg(test)]
211mod test_zero {
212    use crate::expect;
213    use crate::expect_json_eq;
214    use pretty_assertions::assert_eq;
215    use serde_json::json;
216
217    #[test]
218    fn it_should_be_true_for_zero() {
219        let left = json!(0.0);
220        let right = json!(expect::float().zero());
221
222        let output = expect_json_eq(&left, &right);
223        assert!(output.is_ok());
224    }
225
226    #[test]
227    fn it_should_be_false_for_negative_value() {
228        let left = json!(-1.0);
229        let right = json!(expect::float().zero());
230
231        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
232        assert_eq!(
233            output,
234            r#"Json expect::float() error at root, is not zero:
235    expected 0.0
236    received -1.0"#
237        );
238    }
239
240    #[test]
241    fn it_should_be_false_for_positive_value() {
242        let left = json!(1.0);
243        let right = json!(expect::float().zero());
244
245        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
246        assert_eq!(
247            output,
248            r#"Json expect::float() error at root, is not zero:
249    expected 0.0
250    received 1.0"#
251        );
252    }
253
254    #[test]
255    fn it_should_be_false_for_min() {
256        let left = json!(f64::MIN);
257        let right = json!(expect::float().zero());
258
259        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
260        assert_eq!(
261            output,
262            r#"Json expect::float() error at root, is not zero:
263    expected 0.0
264    received -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0"#
265        );
266    }
267
268    #[test]
269    fn it_should_be_false_for_max() {
270        let left = json!(f64::MAX);
271        let right = json!(expect::float().zero());
272
273        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
274        assert_eq!(
275            output,
276            r#"Json expect::float() error at root, is not zero:
277    expected 0.0
278    received 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0"#
279        );
280    }
281}
282
283#[cfg(test)]
284mod test_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.0);
293        let right = json!(expect::float().not_zero());
294
295        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
296        assert_eq!(
297            output,
298            r#"Json expect::float() error at root, is zero:
299    expected non-zero float
300    received 0.0"#
301        );
302    }
303
304    #[test]
305    fn it_should_be_true_for_negative_value() {
306        let left = json!(-1.0);
307        let right = json!(expect::float().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_positive_value() {
315        let left = json!(1.0);
316        let right = json!(expect::float().not_zero());
317
318        let output = expect_json_eq(&left, &right);
319        assert!(output.is_ok());
320    }
321}
322
323#[cfg(test)]
324mod test_positive {
325    use crate::expect;
326    use crate::expect_json_eq;
327    use pretty_assertions::assert_eq;
328    use serde_json::json;
329
330    #[test]
331    fn it_should_be_false_for_zero() {
332        let left = json!(0.0);
333        let right = json!(expect::float().positive());
334
335        let output = expect_json_eq(&left, &right);
336        assert!(output.is_ok());
337    }
338
339    #[test]
340    fn it_should_be_false_for_negative_value() {
341        let left = json!(-1.0);
342        let right = json!(expect::float().positive());
343
344        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
345        assert_eq!(
346            output,
347            r#"Json expect::float() error at root:
348    float is not positive
349    received -1.0"#
350        );
351    }
352
353    #[test]
354    fn it_should_be_true_for_positive_value() {
355        let left = json!(1.0);
356        let right = json!(expect::float().positive());
357
358        let output = expect_json_eq(&left, &right);
359        assert!(output.is_ok());
360    }
361}
362
363#[cfg(test)]
364mod test_negative {
365    use crate::expect;
366    use crate::expect_json_eq;
367    use pretty_assertions::assert_eq;
368    use serde_json::json;
369
370    #[test]
371    fn it_should_be_false_for_zero() {
372        let left = json!(0.0);
373        let right = json!(expect::float().negative());
374
375        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
376        assert_eq!(
377            output,
378            r#"Json expect::float() error at root:
379    float is not negative
380    received 0.0"#
381        );
382    }
383
384    #[test]
385    fn it_should_be_true_for_negative_value() {
386        let left = json!(-1.0);
387        let right = json!(expect::float().negative());
388
389        let output = expect_json_eq(&left, &right);
390        assert!(output.is_ok());
391    }
392
393    #[test]
394    fn it_should_be_false_for_positive_value() {
395        let left = json!(1.0);
396        let right = json!(expect::float().negative());
397
398        let output = expect_json_eq(&left, &right).unwrap_err().to_string();
399        assert_eq!(
400            output,
401            r#"Json expect::float() error at root:
402    float is not negative
403    received 1.0"#
404        );
405    }
406}