oxedyne/fe2o3/fe2o3_jdat/tests/datmap_strict.rs
2.8 KiB, 5 runs
created by r1870400018:76228, 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 | //! `#[derive(FromDatMap)]` with `#[deny_unknown_keys]`: a key no field reads is refused, named, |
| 2 | //! where without it the key is ignored. A comment in the map is a note, not a key. Every |
| 3 | //! missing required field is named, and no value of the map is shown. |
| 4 | |
| 5 | use oxedyne_fe2o3_core::prelude::*; |
| 6 | use oxedyne_fe2o3_jdat::prelude::*; |
| 7 | |
| 8 | #[derive(Clone, Debug, Default, PartialEq, FromDatMap)] |
| 9 | #[deny_unknown_keys] |
| 10 | struct Strict { |
| 11 | size: String, |
| 12 | #[optional] count: u64, |
| 13 | #[rename(name = "as_named")] |
| 14 | named: String, |
| 15 | } |
| 16 | |
| 17 | #[derive(Clone, Debug, Default, PartialEq, FromDatMap)] |
| 18 | struct Lax { |
| 19 | size: String, |
| 20 | } |
| 21 | |
| 22 | fn map_of(text: &str) -> Outcome<DaticleMap> { |
| 23 | match res!(Dat::decode_string(text)) { |
| 24 | Dat::Map(m) => Ok(m), |
| 25 | d => Err(err!("Not a map: {:?}.", d; Test)), |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | #[test] |
| 30 | fn an_unknown_key_is_refused_by_name() -> Outcome<()> { |
| 31 | let m = res!(map_of(r#"{ "size": "5G", "as_named": "x", "unit_memroy_max": "11G" }"#)); |
| 32 | match Strict::from_datmap(m) { |
| 33 | Ok(v) => Err(err!("Accepted {:?}.", v; Test)), |
| 34 | Err(e) => { |
| 35 | let s = e.plain(); |
| 36 | assert!(s.contains("unit_memroy_max") && s.contains("Strict"), "{}", s); |
| 37 | Ok(()) |
| 38 | }, |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn a_key_under_its_field_name_when_renamed_is_refused() -> Outcome<()> { |
| 44 | let m = res!(map_of(r#"{ "size": "5G", "as_named": "x", "named": "y" }"#)); |
| 45 | match Strict::from_datmap(m) { |
| 46 | Ok(v) => Err(err!("Accepted {:?}.", v; Test)), |
| 47 | Err(e) => { |
| 48 | assert!(e.plain().contains("'named'"), "{}", e.plain()); |
| 49 | Ok(()) |
| 50 | }, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | #[test] |
| 55 | fn known_keys_and_comments_pass() -> Outcome<()> { |
| 56 | let m = res!(map_of("{\n # a comment on its own line\n \"size\": \"5G\",\n \"as_named\": \"x\",\n \"count\": 2,\n}")); |
| 57 | let v = res!(Strict::from_datmap(m)); |
| 58 | assert_eq!(v, Strict { size: "5G".to_string(), count: 2, named: "x".to_string() }); |
| 59 | Ok(()) |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn without_the_attribute_an_unknown_key_is_ignored() -> Outcome<()> { |
| 64 | let m = res!(map_of(r#"{ "size": "5G", "other": 1 }"#)); |
| 65 | let v = res!(Lax::from_datmap(m)); |
| 66 | assert_eq!(v.size, "5G"); |
| 67 | Ok(()) |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn every_missing_field_is_named_and_no_value_shown() -> Outcome<()> { |
| 72 | match Lax::from_datmap(res!(map_of(r#"{ "other": "hunter2" }"#))) { |
| 73 | Ok(v) => return Err(err!("Accepted {:?}.", v; Test)), |
| 74 | Err(e) => assert!(e.plain().contains("field 'size'") && !e.plain().contains("hunter2"), "{}", e.plain()), |
| 75 | } |
| 76 | match Strict::from_datmap(res!(map_of(r#"{ "count": 2 }"#))) { |
| 77 | Ok(v) => Err(err!("Accepted {:?}.", v; Test)), |
| 78 | Err(e) => { |
| 79 | let s = e.plain(); |
| 80 | assert!(s.contains("fields 'size', 'as_named'") && !s.contains("count"), "{}", s); |
| 81 | Ok(()) |
| 82 | }, |
| 83 | } |
| 84 | } |