expect_json/expects/ops/expect_float/
expect_float.rs1use crate::expect_op;
2use crate::ops::expect_float::ExpectFloatSubOp;
3use crate::Context;
4use crate::ExpectOp;
5use crate::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 is_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 self
32 }
33
34 pub fn is_zero(mut self) -> Self {
35 self.sub_ops.push(ExpectFloatSubOp::Zero);
36 self
37 }
38
39 pub fn is_not_zero(mut self) -> Self {
40 self.sub_ops.push(ExpectFloatSubOp::NotZero);
41 self
42 }
43
44 pub fn is_positive(mut self) -> Self {
45 self.sub_ops.push(ExpectFloatSubOp::Positive);
46 self
47 }
48
49 pub fn is_negative(mut self) -> Self {
50 self.sub_ops.push(ExpectFloatSubOp::Negative);
51 self
52 }
53}
54
55impl ExpectOp for ExpectFloat {
56 fn on_f64(&self, context: &mut Context, received: f64) -> ExpectOpResult<()> {
57 for sub_op in &self.sub_ops {
58 sub_op.on_f64(self, context, received)?;
59 }
60
61 Ok(())
62 }
63
64 fn supported_types(&self) -> &'static [JsonType] {
65 &[JsonType::Float]
66 }
67}
68
69#[cfg(test)]
70mod test_is_in_range {
71 use crate::expect;
72 use crate::expect_json_eq;
73 use pretty_assertions::assert_eq;
74 use serde_json::json;
75
76 #[test]
77 fn it_should_be_true_for_all_values_in_total_range() {
78 let left = json!(1.0);
79 let right = json!(expect::float().is_in_range(..));
80 let output = expect_json_eq(&left, &right);
81 assert!(output.is_ok());
82
83 let left = json!(f64::MIN);
84 let right = json!(expect::float().is_in_range(..));
85 let output = expect_json_eq(&left, &right);
86 assert!(output.is_ok());
87 }
88
89 #[test]
90 fn it_should_be_true_for_all_values_in_partial_range() {
91 let left = json!(0.5);
92 let right = json!(expect::float().is_in_range(0.0..1.0));
93 let output = expect_json_eq(&left, &right);
94 assert!(output.is_ok());
95 }
96
97 #[test]
98 fn it_should_be_false_for_all_values_out_of_range() {
99 let left = json!(1.0);
100 let right = json!(expect::float().is_in_range(0.0..1.0));
101
102 let output = expect_json_eq(&left, &right).unwrap_err().to_string();
103 assert_eq!(
104 output,
105 r#"Json expect::float() error at root:
106 float is not in range
107 expected 0.0..1.0
108 received 1.0"#
109 );
110 }
111
112 #[test]
113 fn it_should_be_true_for_value_in_inclusive_range() {
114 let left = json!(1.0);
115 let right = json!(expect::float().is_in_range(0.0..=1.0));
116
117 let output = expect_json_eq(&left, &right);
118 assert!(output.is_ok());
119 }
120}
121
122#[cfg(test)]
123mod test_is_zero {
124 use crate::expect;
125 use crate::expect_json_eq;
126 use pretty_assertions::assert_eq;
127 use serde_json::json;
128
129 #[test]
130 fn it_should_be_true_for_zero() {
131 let left = json!(0.0);
132 let right = json!(expect::float().is_zero());
133
134 let output = expect_json_eq(&left, &right);
135 assert!(output.is_ok());
136 }
137
138 #[test]
139 fn it_should_be_false_for_negative_value() {
140 let left = json!(-1.0);
141 let right = json!(expect::float().is_zero());
142
143 let output = expect_json_eq(&left, &right).unwrap_err().to_string();
144 assert_eq!(
145 output,
146 r#"Json expect::float() error at root, is not zero:
147 expected 0.0
148 received -1.0"#
149 );
150 }
151
152 #[test]
153 fn it_should_be_false_for_positive_value() {
154 let left = json!(1.0);
155 let right = json!(expect::float().is_zero());
156
157 let output = expect_json_eq(&left, &right).unwrap_err().to_string();
158 assert_eq!(
159 output,
160 r#"Json expect::float() error at root, is not zero:
161 expected 0.0
162 received 1.0"#
163 );
164 }
165
166 #[test]
167 fn it_should_be_false_for_min() {
168 let left = json!(f64::MIN);
169 let right = json!(expect::float().is_zero());
170
171 let output = expect_json_eq(&left, &right).unwrap_err().to_string();
172 assert_eq!(
173 output,
174 r#"Json expect::float() error at root, is not zero:
175 expected 0.0
176 received -179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0"#
177 );
178 }
179
180 #[test]
181 fn it_should_be_false_for_max() {
182 let left = json!(f64::MAX);
183 let right = json!(expect::float().is_zero());
184
185 let output = expect_json_eq(&left, &right).unwrap_err().to_string();
186 assert_eq!(
187 output,
188 r#"Json expect::float() error at root, is not zero:
189 expected 0.0
190 received 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0"#
191 );
192 }
193}
194
195#[cfg(test)]
196mod test_is_not_zero {
197 use crate::expect;
198 use crate::expect_json_eq;
199 use pretty_assertions::assert_eq;
200 use serde_json::json;
201
202 #[test]
203 fn it_should_be_false_for_zero() {
204 let left = json!(0.0);
205 let right = json!(expect::float().is_not_zero());
206
207 let output = expect_json_eq(&left, &right).unwrap_err().to_string();
208 assert_eq!(
209 output,
210 r#"Json expect::float() error at root, is zero:
211 expected non-zero float
212 received 0.0"#
213 );
214 }
215
216 #[test]
217 fn it_should_be_true_for_negative_value() {
218 let left = json!(-1.0);
219 let right = json!(expect::float().is_not_zero());
220
221 let output = expect_json_eq(&left, &right);
222 assert!(output.is_ok());
223 }
224
225 #[test]
226 fn it_should_be_true_for_positive_value() {
227 let left = json!(1.0);
228 let right = json!(expect::float().is_not_zero());
229
230 let output = expect_json_eq(&left, &right);
231 assert!(output.is_ok());
232 }
233}
234
235#[cfg(test)]
236mod test_is_positive {
237 use crate::expect;
238 use crate::expect_json_eq;
239 use pretty_assertions::assert_eq;
240 use serde_json::json;
241
242 #[test]
243 fn it_should_be_false_for_zero() {
244 let left = json!(0.0);
245 let right = json!(expect::float().is_positive());
246
247 let output = expect_json_eq(&left, &right);
248 assert!(output.is_ok());
249 }
250
251 #[test]
252 fn it_should_be_false_for_negative_value() {
253 let left = json!(-1.0);
254 let right = json!(expect::float().is_positive());
255
256 let output = expect_json_eq(&left, &right).unwrap_err().to_string();
257 assert_eq!(
258 output,
259 r#"Json expect::float() error at root:
260 float is not positive
261 received -1.0"#
262 );
263 }
264
265 #[test]
266 fn it_should_be_true_for_positive_value() {
267 let left = json!(1.0);
268 let right = json!(expect::float().is_positive());
269
270 let output = expect_json_eq(&left, &right);
271 assert!(output.is_ok());
272 }
273}
274
275#[cfg(test)]
276mod test_is_negative {
277 use crate::expect;
278 use crate::expect_json_eq;
279 use pretty_assertions::assert_eq;
280 use serde_json::json;
281
282 #[test]
283 fn it_should_be_false_for_zero() {
284 let left = json!(0.0);
285 let right = json!(expect::float().is_negative());
286
287 let output = expect_json_eq(&left, &right).unwrap_err().to_string();
288 assert_eq!(
289 output,
290 r#"Json expect::float() error at root:
291 float is not negative
292 received 0.0"#
293 );
294 }
295
296 #[test]
297 fn it_should_be_true_for_negative_value() {
298 let left = json!(-1.0);
299 let right = json!(expect::float().is_negative());
300
301 let output = expect_json_eq(&left, &right);
302 assert!(output.is_ok());
303 }
304
305 #[test]
306 fn it_should_be_false_for_positive_value() {
307 let left = json!(1.0);
308 let right = json!(expect::float().is_negative());
309
310 let output = expect_json_eq(&left, &right).unwrap_err().to_string();
311 assert_eq!(
312 output,
313 r#"Json expect::float() error at root:
314 float is not negative
315 received 1.0"#
316 );
317 }
318}