Skip to content

Commit d6de171

Browse files
tests: Add test for nan
Signed-off-by: Patrick José Pereira <[email protected]>
1 parent efff77b commit d6de171

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

tests/debug.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ fn value_bool() {
2424

2525
#[test]
2626
fn value_number() {
27+
assert_eq!(format!("{:?}", json!(::std::f64::NAN)), "Number(NaN)");
2728
assert_eq!(format!("{:?}", json!(1)), "Number(1)");
2829
assert_eq!(format!("{:?}", json!(-1)), "Number(-1)");
2930
assert_eq!(format!("{:?}", json!(1.0)), "Number(1.0)");

tests/stream.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ macro_rules! test_stream {
3131

3232
#[test]
3333
fn test_json_stream_newlines() {
34-
let data = "{\"x\":39} {\"x\":40}{\"x\":41}\n{\"x\":42}";
34+
let data = "{\"x\":39} {\"x\":40}{\"x\":41}\n{\"x\":42}{\"x\":nan}";
3535

3636
test_stream!(data, Value, |stream| {
3737
assert_eq!(stream.next().unwrap().unwrap()["x"], 39);
@@ -45,9 +45,11 @@ fn test_json_stream_newlines() {
4545

4646
assert_eq!(stream.next().unwrap().unwrap()["x"], 42);
4747
assert_eq!(stream.byte_offset(), 34);
48+
assert!(stream.next().unwrap().unwrap()["x"].as_f64().unwrap().is_nan());
49+
assert_eq!(stream.byte_offset(), 43);
4850

4951
assert!(stream.next().is_none());
50-
assert_eq!(stream.byte_offset(), 34);
52+
assert_eq!(stream.byte_offset(), 43);
5153
});
5254
}
5355

tests/test.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ fn test_write_f64() {
161161
#[test]
162162
fn test_encode_nonfinite_float_yields_null() {
163163
let v = to_value(::std::f64::NAN).unwrap();
164-
assert!(v.is_null());
164+
assert!(v.is_f64());
165165

166166
let v = to_value(::std::f64::INFINITY).unwrap();
167167
assert!(v.is_null());
168168

169169
let v = to_value(::std::f32::NAN).unwrap();
170-
assert!(v.is_null());
170+
assert!(v.is_f64());
171171

172172
let v = to_value(::std::f32::INFINITY).unwrap();
173173
assert!(v.is_null());
@@ -693,6 +693,17 @@ fn test_parse_null() {
693693
test_parse_ok(vec![("null", ())]);
694694
}
695695

696+
#[test]
697+
fn test_parse_nan() {
698+
test_parse_err::<()>(&[
699+
("n", "EOF while parsing a value at line 1 column 1"),
700+
("na", "expected ident at line 1 column 2"),
701+
("nana", "expected ident at line 1 column 2"),
702+
]);
703+
704+
assert!(from_str::<f64>("nan").unwrap().is_nan());
705+
}
706+
696707
#[test]
697708
fn test_parse_bool() {
698709
test_parse_err::<bool>(&[

0 commit comments

Comments
 (0)