oxedyne/fe2o3/fe2o3_austenite/tests/eval_math_pdf.rs
3.2 KiB, 9 runs
created by r1870400018:63604, 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 | //! Maths on the page, from source to Austenite's own PDF (level 4b): every fixture of the `math` area is |
| 2 | //! compiled through the streaming `PdfSink`, and the file is read back by the `pdftotext -bbox-layout` that |
| 3 | //! reads Typst's, so each line's text and where its box and first word sit are compared as the reader of the |
| 4 | //! PDF sees them. The board's level 4 reads Austenite's placed runs from memory; this reads the file. |
| 5 | |
| 6 | #![allow(dead_code)] |
| 7 | |
| 8 | #[path = "eval_oracle/mod.rs"] |
| 9 | mod harness; |
| 10 | |
| 11 | use harness::corpus::{ |
| 12 | self, |
| 13 | Filter, |
| 14 | }; |
| 15 | use harness::oracle::Oracle; |
| 16 | use harness::pdf::{ |
| 17 | pdf_differences, |
| 18 | work_dir, |
| 19 | }; |
| 20 | |
| 21 | use oxedyne_fe2o3_core::prelude::*; |
| 22 | |
| 23 | // Fixtures whose lines differ from Typst's on the file for a reason not yet traced to a root: Typst pushes the |
| 24 | // items of an accent, a matrix's delimiters, a limit's operands, a root's index and a brace's label in an |
| 25 | // order Austenite's frames do not repeat, and a text run's glyphs and spacing are written as one `TJ` by |
| 26 | // Typst and glyph by glyph here, which poppler reads as a word break. Each is a glyph-exact match in |
| 27 | // `eval_math`; the file differs only in the order and grouping of its text. The test requires exactly these |
| 28 | // to differ, so a fix shrinks the list and a new fault is not hidden in it. |
| 29 | const KNOWN: [&str; 9] = [ |
| 30 | "accent_hat", "call_ops", "call_spread", "call_styles", "matrix", "matrix_aug", "root_syntax", |
| 31 | "tall_parens", "variants", |
| 32 | ]; |
| 33 | |
| 34 | #[test] |
| 35 | fn maths_reaches_austenites_own_pdf_as_it_reaches_typsts() -> Outcome<()> { |
| 36 | if res!(Oracle::find()).is_none() { |
| 37 | return Ok(()); |
| 38 | } |
| 39 | let only = std::env::var("EVAL_MATH_PDF_FIXTURE").ok(); |
| 40 | let (mut compared, mut failures) = (0usize, Vec::new()); |
| 41 | let root = res!(work_dir("eval-math-pdf")); |
| 42 | let mut fixtures = res!(corpus::discover(&Filter::only("math"))); |
| 43 | // A directory of variants of the fixtures to take instead, by name. |
| 44 | if let Ok(dir) = std::env::var("EVAL_MATH_PDF_DIR") { |
| 45 | for fx in fixtures.iter_mut() { |
| 46 | fx.path = std::path::Path::new(&dir).join(format!("{}.typ", fx.name)); |
| 47 | } |
| 48 | } |
| 49 | for fx in fixtures { |
| 50 | if let Some(o) = &only { |
| 51 | if !fx.name.contains(o.as_str()) { |
| 52 | continue; |
| 53 | } |
| 54 | } |
| 55 | let diffs = match pdf_differences(&fx.path, &root.join(&fx.name)) { |
| 56 | Ok(d) => d, |
| 57 | Err(e) => vec![fmt!("{}", e)], |
| 58 | }; |
| 59 | if diffs.is_empty() { |
| 60 | compared += 1; |
| 61 | println!("ok {} (pdf)", fx.id()); |
| 62 | } else { |
| 63 | failures.push(fmt!("{}:\n {}", fx.id(), diffs.join("\n "))); |
| 64 | } |
| 65 | } |
| 66 | for f in &failures { |
| 67 | println!("FAIL {}", f); |
| 68 | } |
| 69 | println!("{} maths fixtures agree on austenite's own PDF; {} differ", compared, failures.len()); |
| 70 | if only.is_none() { |
| 71 | let differing: Vec<String> = failures.iter() |
| 72 | .map(|f| f.split(':').next().unwrap_or("").trim_start_matches("math/").to_string()).collect(); |
| 73 | let unexpected: Vec<&String> = differing.iter().filter(|n| !KNOWN.contains(&n.as_str())).collect(); |
| 74 | let mended: Vec<&&str> = KNOWN.iter().filter(|k| !differing.iter().any(|n| n == **k)).collect(); |
| 75 | assert!(unexpected.is_empty(), "fixtures differ from typst on the PDF that are not known to: {:?}", unexpected); |
| 76 | assert!(mended.is_empty(), "fixtures in KNOWN now agree, so take them off the list: {:?}", mended); |
| 77 | assert!(compared >= 88, "only {} maths fixtures agree on the PDF, 88 expected", compared); |
| 78 | } |
| 79 | Ok(()) |
| 80 | } |