oxedyne/fe2o3/fe2o3_austenite/tests/eval_paginate.rs
5.4 KiB, 1 run
created by r1870400018:63632, 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 | //! Paginated text against the `typst` 0.15.1 oracle at level 4 (U6b-S1): page count, page sizes, and every |
| 2 | //! line's text and baseline, for documents whose text runs across pages -- a paragraph, a deferred float, |
| 3 | //! `pagebreak(to: "odd")`, a breakable block and `columns(2)[..]`. |
| 4 | //! |
| 5 | //! Austenite's side paginates a page at a time, as the pipeline does: each body is placed, its text runs |
| 6 | //! read, and the page dropped. A fixture whose first lines say `// needs: <unit>` depends on a unit not yet |
| 7 | //! merged: while Austenite cannot lay it out it is reported as pending, and once it can it is held to the |
| 8 | //! oracle like any other. |
| 9 | |
| 10 | #![allow(dead_code)] |
| 11 | |
| 12 | #[path = "eval_oracle/mod.rs"] |
| 13 | mod harness; |
| 14 | |
| 15 | use harness::corpus::{ |
| 16 | Expect, |
| 17 | Fixture, |
| 18 | }; |
| 19 | use harness::layout::{ |
| 20 | compare, |
| 21 | APage, |
| 22 | ARun, |
| 23 | }; |
| 24 | use harness::oracle::Oracle; |
| 25 | |
| 26 | use oxedyne_fe2o3_austenite::driver::Nowhere; |
| 27 | use oxedyne_fe2o3_austenite::eval::styles::StyleChain; |
| 28 | use oxedyne_fe2o3_austenite::eval::{ |
| 29 | eval_source, |
| 30 | Engine, |
| 31 | World, |
| 32 | }; |
| 33 | use oxedyne_fe2o3_austenite::flow; |
| 34 | use oxedyne_fe2o3_austenite::page::PlacedKind; |
| 35 | |
| 36 | use oxedyne_fe2o3_core::prelude::*; |
| 37 | |
| 38 | use std::path::{ |
| 39 | Path, |
| 40 | PathBuf, |
| 41 | }; |
| 42 | |
| 43 | const AREA: &str = "paginate"; |
| 44 | |
| 45 | fn dir() -> PathBuf { |
| 46 | Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/eval").join(AREA) |
| 47 | } |
| 48 | |
| 49 | fn fixtures() -> Outcome<Vec<(Fixture, Vec<String>)>> { |
| 50 | let root = dir(); |
| 51 | let mut files: Vec<PathBuf> = match std::fs::read_dir(&root) { |
| 52 | Ok(rd) => rd.filter_map(|e| e.ok().map(|e| e.path())).collect(), |
| 53 | Err(e) => return Err(err!("Cannot read {}: {}", root.display(), e; IO, File, Read)), |
| 54 | }; |
| 55 | files.sort(); |
| 56 | let mut out = Vec::new(); |
| 57 | for path in files { |
| 58 | let name = path.file_name().map(|n| n.to_string_lossy().to_string()).unwrap_or_default(); |
| 59 | if !name.ends_with(".typ") || name.starts_with('_') { |
| 60 | continue; |
| 61 | } |
| 62 | let text = res!(std::fs::read_to_string(&path).map_err(|e| err!("Cannot read {}: {}", path.display(), e; IO, File, Read))); |
| 63 | let needs = text.lines() |
| 64 | .take_while(|l| l.starts_with("//")) |
| 65 | .filter_map(|l| l.strip_prefix("// needs:")) |
| 66 | .flat_map(|l| l.split_whitespace().map(|s| s.to_string()).collect::<Vec<_>>()) |
| 67 | .collect(); |
| 68 | let fx = Fixture { |
| 69 | area: AREA.to_string(), |
| 70 | name: name.trim_end_matches(".typ").to_string(), |
| 71 | path, |
| 72 | root: root.clone(), |
| 73 | text, |
| 74 | levels: [false, false, false, true], |
| 75 | expect: Expect::Accepts, |
| 76 | }; |
| 77 | out.push((fx, needs)); |
| 78 | } |
| 79 | Ok(out) |
| 80 | } |
| 81 | |
| 82 | /// Austenite's pages, read as they stream: each page's text runs are kept, the page itself dropped. |
| 83 | fn austenite(fx: &Fixture) -> std::result::Result<Vec<APage>, String> { |
| 84 | let mut world = World::new(fx.root.clone()); |
| 85 | let id = match world.load(&fx.path) { |
| 86 | Ok(id) => id, |
| 87 | Err(e) => return Err(fmt!("load: {}", e)), |
| 88 | }; |
| 89 | let mut engine = Engine::new(world); |
| 90 | let module = match eval_source(&mut engine, id) { |
| 91 | Ok(m) => m, |
| 92 | Err(e) => return Err(fmt!("eval: {}", last(&e))), |
| 93 | }; |
| 94 | let mut paginator = flow::paginate(&module.content, &StyleChain::root()); |
| 95 | let mut pages = Vec::new(); |
| 96 | loop { |
| 97 | let page = match paginator.next_placed(&mut engine, &mut Nowhere) { |
| 98 | Ok(Some((p, _))) => p, |
| 99 | Ok(None) => break, |
| 100 | Err(e) => return Err(fmt!("flow: {}", last(&e))), |
| 101 | }; |
| 102 | pages.push(APage { |
| 103 | width: page.geom.width.to_pt(), |
| 104 | height: page.geom.height.to_pt(), |
| 105 | runs: page.frame.placed.iter().filter_map(|pl| match &pl.kind { |
| 106 | PlacedKind::Text(st) => Some(ARun { |
| 107 | text: st.source().to_string(), |
| 108 | x0: pl.x.to_pt(), |
| 109 | x1: (pl.x + pl.dims.width).to_pt(), |
| 110 | base: (pl.y + pl.dims.height).to_pt(), |
| 111 | }), |
| 112 | _ => None, |
| 113 | }).collect(), |
| 114 | }); |
| 115 | } |
| 116 | Ok(pages) |
| 117 | } |
| 118 | |
| 119 | fn last(e: &Error<ErrTag>) -> String { |
| 120 | match e.msgs().into_iter().last() { |
| 121 | Some(m) => m, |
| 122 | None => fmt!("{}", e), |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn paginated_text_matches_the_typst_oracle() -> Outcome<()> { |
| 128 | let oracle = match res!(Oracle::find()) { |
| 129 | Some(o) => o, |
| 130 | None => return Ok(()), |
| 131 | }; |
| 132 | let only = std::env::var("EVAL_PAGINATE_FIXTURE").ok(); |
| 133 | let (mut compared, mut pending, mut failures) = (0usize, Vec::new(), Vec::new()); |
| 134 | for (fx, needs) in res!(fixtures()) { |
| 135 | if let Some(o) = &only { |
| 136 | if !fx.name.contains(o.as_str()) { |
| 137 | continue; |
| 138 | } |
| 139 | } |
| 140 | let want = match res!(oracle.layout(&fx)) { |
| 141 | Ok(w) => w, |
| 142 | Err(e) => { |
| 143 | failures.push(fmt!("{}: typst: {}", fx.id(), e)); |
| 144 | continue; |
| 145 | }, |
| 146 | }; |
| 147 | let got = match austenite(&fx) { |
| 148 | Ok(g) => g, |
| 149 | Err(e) => { |
| 150 | if needs.is_empty() { |
| 151 | failures.push(fmt!("{}: {}", fx.id(), e)); |
| 152 | } else { |
| 153 | pending.push(fmt!("{} (needs {}): {}", fx.id(), needs.join(", "), e)); |
| 154 | } |
| 155 | continue; |
| 156 | }, |
| 157 | }; |
| 158 | let mut diffs = Vec::new(); |
| 159 | compare(&want, &got, &mut diffs); |
| 160 | if diffs.is_empty() { |
| 161 | compared += 1; |
| 162 | println!("ok {} ({} pages)", fx.id(), want.len()); |
| 163 | } else { |
| 164 | failures.push(fmt!("{}:\n {}", fx.id(), diffs.join("\n "))); |
| 165 | if std::env::var("EVAL_PAGINATE_DUMP").is_ok() { |
| 166 | for (i, p) in want.iter().enumerate() { |
| 167 | for l in &p.lines { |
| 168 | println!("typst p{} x {:7.2} base {:7.2} {:?}", i + 1, l.x0, l.base, l.text); |
| 169 | } |
| 170 | } |
| 171 | for (i, p) in got.iter().enumerate() { |
| 172 | for r in &p.runs { |
| 173 | println!("austenite p{} x {:7.2} base {:7.2} {:?}", i + 1, r.x0, r.base, r.text); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | for p in &pending { |
| 180 | println!("pending {}", p); |
| 181 | } |
| 182 | for f in &failures { |
| 183 | println!("FAIL {}", f); |
| 184 | } |
| 185 | println!("{} fixtures compared at level 4; {} pending; {} failing", compared, pending.len(), failures.len()); |
| 186 | assert!(failures.is_empty(), "{} fixture(s) differ from typst", failures.len()); |
| 187 | Ok(()) |
| 188 | } |