1 | use indoc::indoc; |
2 | use serde_json::{json, Number, Value}; |
3 | |
4 | #[test] |
5 | fn number() { |
6 | assert_eq!(format!("{:?}" , Number::from(1)), "Number(1)" ); |
7 | assert_eq!(format!("{:?}" , Number::from(-1)), "Number(-1)" ); |
8 | assert_eq!( |
9 | format!("{:?}" , Number::from_f64(1.0).unwrap()), |
10 | "Number(1.0)" |
11 | ); |
12 | } |
13 | |
14 | #[test] |
15 | fn value_null() { |
16 | assert_eq!(format!("{:?}" , json!(null)), "Null" ); |
17 | } |
18 | |
19 | #[test] |
20 | fn value_bool() { |
21 | assert_eq!(format!("{:?}" , json!(true)), "Bool(true)" ); |
22 | assert_eq!(format!("{:?}" , json!(false)), "Bool(false)" ); |
23 | } |
24 | |
25 | #[test] |
26 | fn value_number() { |
27 | assert_eq!(format!("{:?}" , json!(1)), "Number(1)" ); |
28 | assert_eq!(format!("{:?}" , json!(-1)), "Number(-1)" ); |
29 | assert_eq!(format!("{:?}" , json!(1.0)), "Number(1.0)" ); |
30 | assert_eq!(Number::from_f64(1.0).unwrap().to_string(), "1.0" ); // not just "1" |
31 | assert_eq!(Number::from_f64(12e40).unwrap().to_string(), "1.2e41" ); |
32 | } |
33 | |
34 | #[test] |
35 | fn value_string() { |
36 | assert_eq!(format!("{:?}" , json!("s" )), "String( \"s \")" ); |
37 | } |
38 | |
39 | #[test] |
40 | fn value_array() { |
41 | assert_eq!(format!("{:?}" , json!([])), "Array []" ); |
42 | } |
43 | |
44 | #[test] |
45 | fn value_object() { |
46 | assert_eq!(format!("{:?}" , json!({})), "Object {}" ); |
47 | } |
48 | |
49 | #[test] |
50 | fn error() { |
51 | let err = serde_json::from_str::<Value>("{0}" ).unwrap_err(); |
52 | let expected = "Error( \"key must be a string \", line: 1, column: 2)" ; |
53 | assert_eq!(format!("{:?}" , err), expected); |
54 | } |
55 | |
56 | #[test] |
57 | fn indented() { |
58 | let j = json!({ |
59 | "Array" : [true], |
60 | "Bool" : true, |
61 | "EmptyArray" : [], |
62 | "EmptyObject" : {}, |
63 | "Null" : null, |
64 | "Number" : 1, |
65 | "String" : "..." , |
66 | }); |
67 | let expected = indoc! {r#" |
68 | Object { |
69 | "Array": Array [ |
70 | Bool(true), |
71 | ], |
72 | "Bool": Bool(true), |
73 | "EmptyArray": Array [], |
74 | "EmptyObject": Object {}, |
75 | "Null": Null, |
76 | "Number": Number(1), |
77 | "String": String("..."), |
78 | }"# |
79 | }; |
80 | assert_eq!(format!("{:#?}" , j), expected); |
81 | } |
82 | |