• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use serde_json::{from_str, Map, Value};
2 
3 #[test]
test_preserve_order()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]
test_append()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 #[cfg(not(no_btreemap_retain))]
39 #[test]
test_retain()40 fn test_retain() {
41     let mut v: Value = from_str(r#"{"b":null,"a":null,"c":null}"#).unwrap();
42     let val = v.as_object_mut().unwrap();
43     val.retain(|k, _| k.as_str() != "b");
44 
45     let keys: Vec<_> = val.keys().collect();
46     assert_eq!(keys, &["a", "c"]);
47 }
48