Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_jdat/tests/map_path.rs

2.8 KiB, 1 run

created by r1870400018:68741, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! Setting a value at a path into nested maps, the way `austenite --set colour.space=rgb` does.
2
3use oxedyne_fe2o3_jdat::prelude::*;
4
5use std::collections::BTreeMap;
6
7fn s(k: &str) -> Dat { Dat::Str(k.to_string()) }
8
9fn found<'a>(d: &'a Dat, path: &[&str]) -> Option<&'a Dat> {
10 let keys = Dat::List(path.iter().map(|k| s(k)).collect());
11 d.find(&keys).expect("a find over a map")
12}
13
14#[test]
15fn a_dotted_path_makes_the_levels_it_lacks() {
16 let mut d = Dat::Map(BTreeMap::new());
17 let was = d.map_put_dotted("colour.space", s("rgb")).expect("a set");
18 assert_eq!(was, None);
19 assert_eq!(found(&d, &["colour", "space"]), Some(&s("rgb")));
20}
21
22#[test]
23fn a_set_replaces_the_value_and_returns_it_and_keeps_its_siblings() {
24 let mut d = Dat::Map(BTreeMap::new());
25 d.map_put_dotted("a.b", Dat::U8(1)).expect("a set");
26 d.map_put_dotted("a.c", Dat::U8(2)).expect("a set");
27 let was = d.map_put_dotted("a.b", Dat::U8(3)).expect("a set");
28 assert_eq!(was, Some(Dat::U8(1)));
29 assert_eq!(found(&d, &["a", "b"]), Some(&Dat::U8(3)));
30 assert_eq!(found(&d, &["a", "c"]), Some(&Dat::U8(2)));
31}
32
33#[test]
34fn a_single_key_is_a_plain_put() {
35 let mut d = Dat::Map(BTreeMap::new());
36 d.map_put_dotted("strict", Dat::Bool(true)).expect("a set");
37 assert_eq!(found(&d, &["strict"]), Some(&Dat::Bool(true)));
38}
39
40#[test]
41fn a_path_through_a_value_that_is_not_a_map_is_refused_and_changes_nothing() {
42 let mut d = Dat::Map(BTreeMap::new());
43 d.map_put_dotted("strict", Dat::Bool(true)).expect("a set");
44 let before = d.clone();
45 let e = d.map_put_dotted("strict.deeper", Dat::U8(1));
46 assert!(e.is_err(), "a bool is no map");
47 assert_eq!(d, before);
48}
49
50#[test]
51fn an_empty_key_is_refused() {
52 for bad in ["", ".a", "a.", "a..b"] {
53 let mut d = Dat::Map(BTreeMap::new());
54 assert!(d.map_put_dotted(bad, Dat::U8(1)).is_err(), "'{}' has an empty key", bad);
55 }
56}
57
58#[test]
59fn a_value_that_is_no_map_takes_no_path() {
60 let mut d = Dat::Bool(true);
61 assert!(d.map_put_dotted("a", Dat::U8(1)).is_err());
62}
63
64#[test]
65fn an_ordered_map_keeps_its_order_and_gains_an_ordered_child() {
66 let mut d = Dat::OrdMap(BTreeMap::new());
67 d.map_put_dotted("a.b", Dat::U8(1)).expect("a set");
68 d.map_put_dotted("z", Dat::U8(9)).expect("a set");
69 d.map_put_dotted("a.b", Dat::U8(2)).expect("a set");
70 match found(&d, &["a"]) {
71 Some(Dat::OrdMap(_)) => (),
72 other => panic!("the child of an ordered map is ordered, got {:?}", other),
73 }
74 assert_eq!(found(&d, &["a", "b"]), Some(&Dat::U8(2)));
75 assert_eq!(found(&d, &["z"]), Some(&Dat::U8(9)));
76}
77
78#[test]
79fn the_mutable_getter_changes_a_value_in_place() {
80 let mut d = Dat::Map(BTreeMap::new());
81 d.map_put_dotted("k", Dat::U8(1)).expect("a set");
82 *d.map_get_mut(&s("k")).expect("a lookup").expect("present") = Dat::U8(7);
83 assert_eq!(found(&d, &["k"]), Some(&Dat::U8(7)));
84 assert!(d.map_get_mut(&s("absent")).expect("a lookup").is_none());
85}