oxedyne/fe2o3/fe2o3_jdat/tests/notes.rs
1.9 KiB, 1 run
created by r1870400018:75873, 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 | //! A jdat file's comments, seen through by `Dat::without_notes`. |
| 2 | |
| 3 | use oxedyne_fe2o3_jdat::prelude::*; |
| 4 | |
| 5 | use oxedyne_fe2o3_core::prelude::*; |
| 6 | |
| 7 | /// **A list with comments between its rows decodes as the rows alone.** The decoder keeps a |
| 8 | /// comment on a line of its own as a list entry, so a reader matching each row's shape would |
| 9 | /// otherwise meet a note where it expects a row. |
| 10 | #[test] |
| 11 | fn test_list_with_interleaved_comments_decodes() -> Outcome<()> { |
| 12 | let text = "{\n\ |
| 13 | # Before the key.\n\ |
| 14 | \"rows\": [\n\ |
| 15 | [\"a\", 1],\n\ |
| 16 | # Between rows,\n\ |
| 17 | # over two lines.\n\ |
| 18 | [\"b\", 2], # After a row, past its comma, so alone.\n\ |
| 19 | [\"c\", 3]\n\ |
| 20 | ],\n\ |
| 21 | \"n\": 4 # On a value.\n\ |
| 22 | }"; |
| 23 | let dat = res!(Dat::decode_string(text)); |
| 24 | let rows = match dat.map_get(&Dat::Str(fmt!("rows"))) { |
| 25 | Ok(Some(Dat::List(l))) => l.clone(), |
| 26 | other => return Err(err!("rows is {:?}", other; Test, Unexpected)), |
| 27 | }; |
| 28 | assert_eq!(rows.iter().filter(|d| d.is_lone_note()).count(), 3, |
| 29 | "the decoder no longer keeps a lone comment as an entry: {:?}", rows); |
| 30 | |
| 31 | let bare = dat.without_notes(); |
| 32 | let mut want = DaticleMap::new(); |
| 33 | want.insert(Dat::Str(fmt!("rows")), Dat::List(vec![ |
| 34 | Dat::List(vec![Dat::Str(fmt!("a")), Dat::U8(1)]), |
| 35 | Dat::List(vec![Dat::Str(fmt!("b")), Dat::U8(2)]), |
| 36 | Dat::List(vec![Dat::Str(fmt!("c")), Dat::U8(3)]), |
| 37 | ])); |
| 38 | want.insert(Dat::Str(fmt!("n")), Dat::U8(4)); |
| 39 | assert_eq!(bare, Dat::Map(want)); |
| 40 | Ok(()) |
| 41 | } |
| 42 | |
| 43 | /// **Notes leave data that holds none unchanged, and an empty entry is not a note.** |
| 44 | #[test] |
| 45 | fn test_without_notes_keeps_plain_data() -> Outcome<()> { |
| 46 | let dat = res!(Dat::decode_string("[1, [2, 3], {\"k\": \"v\"}]")); |
| 47 | assert_eq!(dat.clone().without_notes(), dat); |
| 48 | assert!(!Dat::is_note_entry(&Dat::Empty, &Dat::Empty)); |
| 49 | assert!(!Dat::Empty.is_lone_note()); |
| 50 | Ok(()) |
| 51 | } |