1 | use serde_json::{from_str, Map, Value}; |
2 | |
3 | #[test] |
4 | fn test_preserve_order() { |
5 | // Sorted order |
6 | #[cfg (not(feature = "preserve_order" ))] |
7 | const EXPECTED: &[&str] = &["a" , "b" , "c" ]; |
8 | |
9 | // Insertion order |
10 | #[cfg (feature = "preserve_order" )] |
11 | const EXPECTED: &[&str] = &["b" , "a" , "c" ]; |
12 | |
13 | let v: Value = from_str(r#"{"b":null,"a":null,"c":null}"# ).unwrap(); |
14 | let keys: Vec<_> = v.as_object().unwrap().keys().collect(); |
15 | assert_eq!(keys, EXPECTED); |
16 | } |
17 | |
18 | #[test] |
19 | fn test_append() { |
20 | // Sorted order |
21 | #[cfg (not(feature = "preserve_order" ))] |
22 | const EXPECTED: &[&str] = &["a" , "b" , "c" ]; |
23 | |
24 | // Insertion order |
25 | #[cfg (feature = "preserve_order" )] |
26 | const EXPECTED: &[&str] = &["b" , "a" , "c" ]; |
27 | |
28 | let mut v: Value = from_str(r#"{"b":null,"a":null,"c":null}"# ).unwrap(); |
29 | let val = v.as_object_mut().unwrap(); |
30 | let mut m = Map::new(); |
31 | m.append(val); |
32 | let keys: Vec<_> = m.keys().collect(); |
33 | |
34 | assert_eq!(keys, EXPECTED); |
35 | assert!(val.is_empty()); |
36 | } |
37 | |
38 | #[test] |
39 | fn test_retain() { |
40 | let mut v: Value = from_str(r#"{"b":null,"a":null,"c":null}"# ).unwrap(); |
41 | let val = v.as_object_mut().unwrap(); |
42 | val.retain(|k, _| k.as_str() != "b" ); |
43 | |
44 | let keys: Vec<_> = val.keys().collect(); |
45 | assert_eq!(keys, &["a" , "c" ]); |
46 | } |
47 | |