oxedyne/fe2o3/fe2o3_sbj/tests/doc_round_trip.rs
12.9 KiB, 7 runs
created by r1870400018:71298, 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 | //! `oxeweb/doc/1` is lossless: `export::to_doc(import::from_doc(d)) == d`, through the canonical |
| 2 | //! bytes, for the page fixtures of `fe2o3_text/tests/html_page.rs` and for a generated corpus that |
| 3 | //! reaches every block and inline variant, nested four deep. |
| 4 | |
| 5 | use oxedyne_fe2o3_sbj::{ |
| 6 | canon, |
| 7 | export, |
| 8 | import, |
| 9 | validate, |
| 10 | SCHEMA_DOC_1, |
| 11 | }; |
| 12 | |
| 13 | use oxedyne_fe2o3_text::doc::{ |
| 14 | html::read, |
| 15 | Align, |
| 16 | Attrs, |
| 17 | Block, |
| 18 | Cell, |
| 19 | Doc, |
| 20 | Inline, |
| 21 | Row, |
| 22 | Term, |
| 23 | }; |
| 24 | |
| 25 | use oxedyne_fe2o3_core::prelude::*; |
| 26 | |
| 27 | /// Imports, validates, encodes, decodes and exports a document, and asserts it came back unchanged. |
| 28 | fn round_trip(d: &Doc) -> Outcome<()> { |
| 29 | let tree = import::from_doc(d, &import::Options::default()); |
| 30 | res!(validate::validate(&tree, SCHEMA_DOC_1)); |
| 31 | let bytes = res!(canon::encode(&tree)); |
| 32 | let back = res!(export::to_doc(&res!(canon::decode(&bytes)))); |
| 33 | if &back != d { |
| 34 | return Err(err!( |
| 35 | "The document did not survive the round trip.\nIn: {:?}\nOut: {:?}", d, back; |
| 36 | Test, Mismatch)); |
| 37 | } |
| 38 | Ok(()) |
| 39 | } |
| 40 | |
| 41 | fn kept(src: &str) -> Outcome<Doc> { |
| 42 | Ok(Doc { blocks: res!(read::parse_with(src, &read::Opts { keep_classed: true })) }) |
| 43 | } |
| 44 | |
| 45 | // ┌───────────────────────────────────────────────────────────────────────────┐ |
| 46 | // │ F1'S FIXTURES │ |
| 47 | // └───────────────────────────────────────────────────────────────────────────┘ |
| 48 | |
| 49 | const FIGURE: &str = "<figure class=\"fig\">\n<img src=\"img/paw.jpg\" alt=\"Dog offering a paw\">\n\ |
| 50 | <figcaption><span class=\"fig-caption\">Hachikō giving a paw.</span> \ |
| 51 | <span class=\"credit\">Unknown author, public domain</span></figcaption>\n</figure>"; |
| 52 | |
| 53 | const TERMS_WRAPPED: &str = "<dl class=\"terms\">\n<div><dt>Jaw joint</dt><dd>The dentary meets the \ |
| 54 | squamosal.</dd></div>\n<div><dt>Middle ear</dt><dd>Three bones.</dd></div>\n</dl>"; |
| 55 | |
| 56 | const TERMS_BARE: &str = "<dl class=\"terms\"><dt>Jaw joint</dt><dd>The dentary meets the squamosal.\ |
| 57 | </dd><dt>Middle ear</dt><dd>Three bones.</dd></dl>"; |
| 58 | |
| 59 | const TABLE_FACTS: &str = "<table>\n<caption>Facts</caption>\n<tbody>\n\ |
| 60 | <tr><th scope=\"row\">Breed</th><td>Akita</td></tr>\n\ |
| 61 | <tr><th scope=\"row\">Sex</th><td>Male</td></tr>\n</tbody>\n</table>"; |
| 62 | |
| 63 | const CLASSED: &str = "<div class=\"table-wrap\"><div><p>Inner.</p></div><p>After.</p></div>\ |
| 64 | <p>A <span class=\"id\">1375772297</span> and <span>bare</span> span.</p>"; |
| 65 | |
| 66 | const ARTICLE: &str = r#" |
| 67 | <h2>Life story</h2> |
| 68 | <p>Hachikō, a creamy-white Akita, was born in mid-November 1923, at a farm in Ōdate, Akita Prefecture, Japan. His father was Ōshinai-yama-gō (<i>'Ōshinai mountain'</i>).</p> |
| 69 | <figure class="fig"> |
| 70 | <img class="diagram" src="img/paw.jpg" width="896" height="654" alt="Dog offering a paw"> |
| 71 | <figcaption><span class="fig-caption">Hachikō giving a paw.</span> <span class="credit">Unknown author, public domain</span></figcaption> |
| 72 | </figure> |
| 73 | <p>Text from <a href="https://en.wikipedia.org/w/index.php?title=Hachik%C5%8D&oldid=1375772297">Hachikō</a>, revision <span class="id">1375772297</span>, CO<sub>2</sub> and m<sup>2</sup>.</p> |
| 74 | <h2>Facts</h2> |
| 75 | <div class="table-wrap"> |
| 76 | <table> |
| 77 | <tbody> |
| 78 | <tr><th scope="row">Breed</th><td>Akita</td></tr> |
| 79 | <tr><th scope="row">Cause of death</th><td>Terminal carcinosarcoma and <i>Dirofilaria immitis</i></td></tr> |
| 80 | </tbody> |
| 81 | </table> |
| 82 | </div> |
| 83 | <div class="table-wrap"> |
| 84 | <table> |
| 85 | <caption>Counts of mammal species</caption> |
| 86 | <thead> |
| 87 | <tr><th scope="col">Source</th><th scope="col" class="num">Year</th><th scope="col" class="num">Species</th></tr> |
| 88 | </thead> |
| 89 | <tbody> |
| 90 | <tr><td><i>Mammal Species of the World</i></td><td class="num">2006</td><td class="num">5,416</td></tr> |
| 91 | <tr><td>IUCN Global Mammal Assessment</td><td class="num">2008</td><td class="num">5,488</td></tr> |
| 92 | </tbody> |
| 93 | </table> |
| 94 | </div> |
| 95 | <h2>Places</h2> |
| 96 | <dl class="terms"> |
| 97 | <div><dt>Ōdate, Akita Prefecture</dt><dd>Hachikō's hometown. A statue stands in front of Ōdate Station.</dd></div> |
| 98 | <div><dt>Shibuya Station, Tokyo</dt><dd>The second statue, unveiled on August 15, 1948, still stands.</dd></div> |
| 99 | </dl> |
| 100 | <blockquote> |
| 101 | <p>A quoted line, with <strong>strength</strong> and <em>emphasis</em>.</p> |
| 102 | </blockquote> |
| 103 | <h2>References</h2> |
| 104 | <ol> |
| 105 | <li>Itoh, Mayumi (2017). <i>Hachikō: Solving Twenty Mysteries about the Most Famous Dog in Japan</i>. pp. 22–23.</li> |
| 106 | <li>“How many species of mammals are there?”. <i>Journal of Mammalogy</i>. 99 (1): 1–14.</li> |
| 107 | </ol> |
| 108 | "#; |
| 109 | |
| 110 | #[test] |
| 111 | fn test_the_page_fixtures_round_trip_00() -> Outcome<()> { |
| 112 | for src in [FIGURE, TERMS_WRAPPED, TERMS_BARE, TABLE_FACTS, CLASSED, ARTICLE] { |
| 113 | let d = res!(kept(src)); |
| 114 | assert!(!d.blocks.is_empty(), "The fixture read as nothing: {}", src); |
| 115 | res!(round_trip(&d)); |
| 116 | } |
| 117 | // The article holds every page construct, so its tree must carry them. |
| 118 | let d = res!(kept(ARTICLE)); |
| 119 | let found = |f: fn(&Block) -> bool| d.blocks.iter().any(|b| match b { |
| 120 | Block::Div { content, .. } => content.iter().any(f) || f(b), |
| 121 | _ => f(b), |
| 122 | }); |
| 123 | assert!(found(|b| matches!(b, Block::Figure { .. })), "No figure in the article."); |
| 124 | assert!(found(|b| matches!(b, Block::Terms { .. })), "No definition list in the article."); |
| 125 | assert!(found(|b| matches!(b, Block::Table { head: Some(_), .. })), "No headed table."); |
| 126 | Ok(()) |
| 127 | } |
| 128 | |
| 129 | // ┌───────────────────────────────────────────────────────────────────────────┐ |
| 130 | // │ GENERATED CORPUS │ |
| 131 | // └───────────────────────────────────────────────────────────────────────────┘ |
| 132 | |
| 133 | /// A small deterministic generator, so a failure names a seed that reproduces it. |
| 134 | struct Gen(u64); |
| 135 | |
| 136 | impl Gen { |
| 137 | fn next(&mut self) -> u64 { |
| 138 | // xorshift64* |
| 139 | self.0 ^= self.0 >> 12; |
| 140 | self.0 ^= self.0 << 25; |
| 141 | self.0 ^= self.0 >> 27; |
| 142 | self.0.wrapping_mul(0x2545_F491_4F6C_DD1D) |
| 143 | } |
| 144 | |
| 145 | fn below(&mut self, n: usize) -> usize { |
| 146 | (self.next() % n as u64) as usize |
| 147 | } |
| 148 | |
| 149 | fn coin(&mut self) -> bool { |
| 150 | self.below(2) == 0 |
| 151 | } |
| 152 | |
| 153 | /// A string already in §3 rule 5's form, the empty one and whitespace among them. |
| 154 | fn s(&mut self) -> String { |
| 155 | const POOL: [&str; 8] = ["", " ", "word", "Ōdate", "a\tb", "two\nlines", "x < y & z", "5,416"]; |
| 156 | POOL[self.below(POOL.len())].to_string() |
| 157 | } |
| 158 | |
| 159 | fn attrs(&mut self) -> Attrs { |
| 160 | let mut a = Attrs::default(); |
| 161 | if self.coin() { |
| 162 | a.id = Some(self.s()); |
| 163 | } |
| 164 | for _ in 0..self.below(3) { |
| 165 | a.classes.push(self.s()); |
| 166 | } |
| 167 | for _ in 0..self.below(3) { |
| 168 | a.pairs.push((self.s(), self.s())); |
| 169 | } |
| 170 | a |
| 171 | } |
| 172 | |
| 173 | fn inlines(&mut self, depth: usize, cov: &mut Coverage) -> Vec<Inline> { |
| 174 | (0..self.below(4)).map(|_| self.inline(depth, cov)).collect() |
| 175 | } |
| 176 | |
| 177 | fn inline(&mut self, depth: usize, cov: &mut Coverage) -> Inline { |
| 178 | // At the floor only the leaves remain. |
| 179 | let n = if depth == 0 { 4 } else { 9 }; |
| 180 | let pick = self.below(n); |
| 181 | cov.inline[pick] = true; |
| 182 | let d = depth.saturating_sub(1); |
| 183 | match pick { |
| 184 | 0 => Inline::Text(self.s()), |
| 185 | 1 => Inline::Image { src: self.s(), alt: self.s(), attrs: self.attrs() }, |
| 186 | 2 => Inline::Code(self.s()), |
| 187 | 3 => Inline::Break, |
| 188 | 4 => Inline::Emph { strong: self.coin(), content: self.inlines(d, cov) }, |
| 189 | 5 => Inline::Link { to: self.s(), content: self.inlines(d, cov) }, |
| 190 | 6 => Inline::Span { attrs: self.attrs(), content: self.inlines(d, cov) }, |
| 191 | 7 => Inline::Sup(self.inlines(d, cov)), |
| 192 | _ => Inline::Sub(self.inlines(d, cov)), |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | fn blocks(&mut self, depth: usize, cov: &mut Coverage) -> Vec<Block> { |
| 197 | (0..self.below(4)).map(|_| self.block(depth, cov)).collect() |
| 198 | } |
| 199 | |
| 200 | fn row(&mut self, depth: usize, cov: &mut Coverage) -> Row { |
| 201 | Row((0..self.below(4)).map(|_| Cell { |
| 202 | content: self.inlines(depth, cov), |
| 203 | header: self.coin(), |
| 204 | }).collect()) |
| 205 | } |
| 206 | |
| 207 | fn block(&mut self, depth: usize, cov: &mut Coverage) -> Block { |
| 208 | let n = if depth == 0 { 5 } else { 11 }; |
| 209 | let pick = self.below(n); |
| 210 | cov.block[pick] = true; |
| 211 | let d = depth.saturating_sub(1); |
| 212 | match pick { |
| 213 | 0 => Block::Heading { level: 1 + self.below(6) as u8, content: self.inlines(d, cov) }, |
| 214 | 1 => Block::Para(self.inlines(depth, cov)), |
| 215 | 2 => Block::Code { lang: if self.coin() { Some(self.s()) } else { None }, text: self.s() }, |
| 216 | 3 => Block::Rule, |
| 217 | 4 => Block::Table { |
| 218 | head: if self.coin() { Some(self.row(d, cov)) } else { None }, |
| 219 | rows: (0..self.below(3)).map(|_| self.row(d, cov)).collect(), |
| 220 | cols: (0..self.below(4)).map(|i| [Align::None, Align::Start, Align::Centre, |
| 221 | Align::End][(i + self.below(4)) % 4]).collect(), |
| 222 | caption: self.inlines(d, cov), |
| 223 | }, |
| 224 | 5 => Block::List { |
| 225 | ordered: self.coin(), |
| 226 | items: (0..self.below(3)).map(|_| self.blocks(d, cov)).collect(), |
| 227 | }, |
| 228 | 6 => Block::Quote(self.blocks(d, cov)), |
| 229 | 7 => Block::Div { attrs: self.attrs(), content: self.blocks(d, cov) }, |
| 230 | 8 => Block::Figure { |
| 231 | attrs: self.attrs(), |
| 232 | content: self.blocks(d, cov), |
| 233 | caption: self.inlines(d, cov), |
| 234 | }, |
| 235 | 9 => Block::Terms { |
| 236 | attrs: self.attrs(), |
| 237 | items: (0..self.below(3)).map(|_| Term { |
| 238 | term: self.inlines(d, cov), |
| 239 | defs: (0..self.below(3)).map(|_| self.blocks(d, cov)).collect(), |
| 240 | }).collect(), |
| 241 | }, |
| 242 | _ => Block::Para(vec![self.inline(depth, cov)]), |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | #[derive(Default)] |
| 248 | struct Coverage { |
| 249 | block: [bool; 11], |
| 250 | inline: [bool; 9], |
| 251 | } |
| 252 | |
| 253 | #[test] |
| 254 | fn test_a_generated_corpus_round_trips_01() -> Outcome<()> { |
| 255 | let mut cov = Coverage::default(); |
| 256 | for seed in 1..=400u64 { |
| 257 | let mut g = Gen(seed.wrapping_mul(0x9E37_79B9_7F4A_7C15)); |
| 258 | let d = Doc { blocks: g.blocks(4, &mut cov) }; |
| 259 | match round_trip(&d) { |
| 260 | Ok(()) => (), |
| 261 | Err(e) => return Err(err!(e, "Seed {} failed.", seed; Test)), |
| 262 | } |
| 263 | } |
| 264 | assert!(cov.block.iter().all(|b| *b), "Not every block variant was generated: {:?}", cov.block); |
| 265 | assert!(cov.inline.iter().all(|b| *b), "Not every inline variant was generated: {:?}", cov.inline); |
| 266 | Ok(()) |
| 267 | } |
| 268 | |
| 269 | /// Every variant at once, nested four deep: a figure in a division in a definition in a quote. |
| 270 | #[test] |
| 271 | fn test_every_variant_nested_four_deep_round_trips_02() -> Outcome<()> { |
| 272 | let leaf = vec![ |
| 273 | Inline::Text("a".to_string()), |
| 274 | Inline::Emph { strong: true, content: vec![Inline::Sup(vec![Inline::Span { |
| 275 | attrs: Attrs { id: Some("s".to_string()), classes: vec!["c".to_string()], |
| 276 | pairs: vec![("k".to_string(), "v".to_string())] }, |
| 277 | content: vec![Inline::Sub(vec![Inline::Code("x".to_string())])], |
| 278 | }])] }, |
| 279 | Inline::Link { to: "b".to_string(), content: vec![Inline::Break] }, |
| 280 | Inline::Image { src: "i.png".to_string(), alt: String::new(), attrs: Attrs::default() }, |
| 281 | Inline::Image { |
| 282 | src: "img/skull.png".to_string(), |
| 283 | alt: "A skull".to_string(), |
| 284 | attrs: Attrs { |
| 285 | id: Some("skull".to_string()), |
| 286 | classes: vec!["diagram".to_string()], |
| 287 | pairs: vec![ |
| 288 | ("width".to_string(), "568".to_string()), |
| 289 | ("height".to_string(), "260".to_string()), |
| 290 | ], |
| 291 | }, |
| 292 | }, |
| 293 | ]; |
| 294 | let table = Block::Table { |
| 295 | head: Some(Row(vec![Cell { content: leaf.clone(), header: true }])), |
| 296 | rows: vec![Row(vec![Cell::new(leaf.clone()), Cell { content: Vec::new(), header: true }]), |
| 297 | Row(Vec::new())], |
| 298 | cols: vec![Align::None, Align::Start, Align::Centre, Align::End], |
| 299 | caption: leaf.clone(), |
| 300 | }; |
| 301 | let d = Doc { blocks: vec![Block::Quote(vec![Block::Terms { |
| 302 | attrs: Attrs::default(), |
| 303 | items: vec![ |
| 304 | Term { term: Vec::new(), defs: Vec::new() }, |
| 305 | Term { term: leaf.clone(), defs: vec![vec![Block::Div { |
| 306 | attrs: Attrs { classes: vec!["box".to_string()], ..Default::default() }, |
| 307 | content: vec![Block::Figure { |
| 308 | attrs: Attrs::default(), |
| 309 | content: vec![table, Block::Rule, Block::List { ordered: true, items: vec![ |
| 310 | Vec::new(), |
| 311 | vec![Block::Code { lang: Some(String::new()), text: String::new() }], |
| 312 | ] }], |
| 313 | caption: leaf.clone(), |
| 314 | }, Block::Heading { level: 6, content: Vec::new() }, Block::Para(Vec::new())], |
| 315 | }], Vec::new()] }, |
| 316 | ], |
| 317 | }])] }; |
| 318 | round_trip(&d) |
| 319 | } |
| 320 | |
| 321 | /// `to_doc` refuses what `from_doc` never writes, by name, rather than guessing a block for it. |
| 322 | #[test] |
| 323 | fn test_a_kind_from_doc_never_writes_is_refused_03() -> Outcome<()> { |
| 324 | use oxedyne_fe2o3_jdat::prelude::*; |
| 325 | use oxedyne_fe2o3_sbj::{ |
| 326 | kinds::NodeKind, |
| 327 | text::ukid, |
| 328 | }; |
| 329 | let mut root = DaticleMap::new(); |
| 330 | root.insert(dat!("title"), dat!("T")); |
| 331 | root.insert(dat!("lang"), dat!("en")); |
| 332 | let mut section = DaticleMap::new(); |
| 333 | section.insert(dat!("title"), dat!("S")); |
| 334 | root.insert(dat!("children"), Dat::List(vec![ |
| 335 | Dat::Usr(ukid(NodeKind::Section), Some(Box::new(Dat::Map(section)))), |
| 336 | ])); |
| 337 | let tree = Dat::Usr(ukid(NodeKind::Doc), Some(Box::new(Dat::Map(root)))); |
| 338 | match export::to_doc(&tree) { |
| 339 | Ok(d) => Err(err!("A section came back as {:?}.", d; Test, Unexpected)), |
| 340 | Err(e) => { |
| 341 | assert!(fmt!("{}", e).contains("section"), "Unhelpful refusal: {}", e); |
| 342 | Ok(()) |
| 343 | }, |
| 344 | } |
| 345 | } |