Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/eval_skeleton.rs

5.6 KiB, 1 run

created by r1870400018:63636, 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//! The walking skeleton, from source to PDF through the evaluator's own pipeline: `compile::assemble_eval`
2//! with the streaming `PdfSink`, then the file Austenite wrote is judged against what `typst` 0.15.1
3//! writes for the same source (streaming addendum, section 5):
4//!
5//! - K1: the file is a PDF of one page whose MediaBox is Typst's.
6//! - K2: `pdftotext` of Austenite's PDF equals Typst's after whitespace normalisation, "Section: Skeleton"
7//! included, which shows the show rule ran.
8//! - K3: level 4b, on Austenite's own PDF: each line's text, and where its box and its first word sit on
9//! the page, lie within a point of Typst's. Both PDFs are read by the same `pdftotext -bbox-layout`, so
10//! the face-dependent offset of a box from its baseline is common to both.
11//! - K4: the strict compile has no diagnostic and no refusal.
12//!
13//! K5, the heap and size, is measured by `tools/heap_probe.mjs` and gated at M4.
14
15#[allow(dead_code)]
16#[path = "eval_oracle/mod.rs"]
17mod harness;
18
19use harness::pdf::{
20 self,
21 k3_differences,
22 lines_of,
23 page_size,
24 text_of,
25 typst_pdf,
26};
27
28use oxedyne_fe2o3_austenite::compile;
29use oxedyne_fe2o3_austenite::emit::sinks::PdfSink;
30use oxedyne_fe2o3_austenite::flow::text::FontStore;
31
32use oxedyne_fe2o3_core::prelude::*;
33
34use std::path::{
35 Path,
36 PathBuf,
37};
38
39fn skeleton() -> PathBuf {
40 Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/eval/skeleton/skeleton.typ")
41}
42
43fn work_dir() -> Outcome<PathBuf> { pdf::work_dir("eval-skeleton") }
44
45#[test]
46fn the_skeleton_is_a_pdf_of_one_a4_page_with_typsts_text_and_lines_and_no_diagnostic() -> Outcome<()> {
47 let src = skeleton();
48 let dir = res!(work_dir());
49 let made = res!(pdf::austenite_pdf(&src));
50 assert_eq!(made.pages, 1, "the skeleton is one page");
51 let (bytes, report) = (made.bytes, made.report);
52 let ours = dir.join("austenite.pdf");
53 let theirs = dir.join("typst.pdf");
54 res!(std::fs::write(&ours, &bytes));
55 res!(typst_pdf(&src, &theirs));
56
57 // K1
58 assert!(bytes.starts_with(b"%PDF-"), "the output is a PDF");
59 let text = String::from_utf8_lossy(&bytes);
60 assert!(text.contains("/Count 1"), "the page tree counts one page");
61 let (mine, want) = (res!(page_size(&ours)), res!(page_size(&theirs)));
62 assert!((mine.0 - want.0).abs() < 0.01 && (mine.1 - want.1).abs() < 0.01, "MediaBox {:?} is not typst's {:?}", mine, want);
63 assert!((mine.0 - 595.276).abs() < 0.01 && (mine.1 - 841.89).abs() < 0.01, "the page is A4, found {:?}", mine);
64 assert!(text.contains("/MediaBox [0 0 595.276 841.89]"), "austenite writes the A4 MediaBox, as typst does");
65
66 // K2
67 let (a, t) = (res!(text_of(&ours)), res!(text_of(&theirs)));
68 assert!(t.contains("Section: Skeleton"), "typst's own text carries the show rule's output: {}", t);
69 assert_eq!(a, t, "pdftotext of austenite's PDF equals typst's");
70
71 // K3
72 let diffs = k3_differences(&res!(lines_of(&theirs)), &res!(lines_of(&ours)));
73 assert!(diffs.is_empty(), "level 4b differences on austenite's own PDF: {:#?}", diffs);
74
75 // K4
76 assert!(report.diagnostics.is_empty(), "the skeleton raises a diagnostic: {:?}", report.diagnostics);
77 assert!(report.strict_failure(&src).is_none(), "the strict compile refuses the skeleton");
78 Ok(())
79}
80
81/// Compiles `source`, written to a file of its own, through the same pipeline.
82fn compile_text(name: &str, source: &str) -> Outcome<(Vec<u8>, compile::Report, PathBuf)> {
83 let dir = res!(work_dir()).join(name);
84 res!(std::fs::create_dir_all(&dir));
85 let src = dir.join("main.typ");
86 res!(std::fs::write(&src, source));
87 let mut sink = res!(PdfSink::new());
88 let done = res!(compile::assemble_eval(&src, &dir, FontStore::default(), &mut sink));
89 res!(done.laid.as_ref().map_err(|e| err!("{} did not lay out: {}", name, e.plain(); Test)));
90 let report = done.report();
91 let out = res!(sink.output().ok_or_else(|| err!("The fixpoint finished with no PDF."; Test)));
92 Ok((out.to_vec(), report, src))
93}
94
95#[test]
96fn a_warning_of_kind_unsupported_refuses_a_strict_compile_and_a_typst_warning_does_not() -> Outcome<()> {
97 // A gradient is drawn in one flat colour: a construct set otherwise than Typst sets it.
98 let (_, report, src) = res!(compile_text("strict-unsupported",
99 "#rect(width: 40pt, height: 10pt, fill: gradient.linear(red, blue))\n"));
100 assert!(report.diagnostics.iter().any(|d| d.kind == compile::DiagnosticKind::Unsupported),
101 "the gradient raises an unsupported warning, found {:?}", report.diagnostics);
102 let refusal = res!(report.strict_failure(&src).ok_or_else(|| err!("strict accepted an unsupported warning"; Test)));
103 assert!(refusal.message.starts_with("strict:"), "the refusal is the strict one: {}", refusal.message);
104 assert!(report.skipped.as_deref().map_or(false, |l| l.starts_with("skipped: ")), "the terse line names it: {:?}", report.skipped);
105
106 // Typst's own warning stands beside the PDF: strict does not refuse it.
107 let dir = res!(work_dir()).join("strict-typst-warning");
108 res!(std::fs::create_dir_all(&dir));
109 res!(std::fs::write(dir.join("t.typ"), "#let doc = 1\n"));
110 let (_, report, src) = res!(compile_text("strict-typst-warning",
111 "#import \"t.typ\": doc as doc\nBody text.\n"));
112 assert!(report.diagnostics.iter().any(|d| d.message.contains("unnecessary import rename")),
113 "Typst's own warning is reported, found {:?}", report.diagnostics);
114 assert!(report.diagnostics.iter().filter(|d| d.message.contains("unnecessary import rename"))
115 .all(|d| d.kind == compile::DiagnosticKind::Lint && d.kind.as_str() == "lint"), "Typst's own warning is a lint");
116 assert!(report.diagnostics.iter().all(|d| !d.kind.refuses_strict()), "none of Typst's own warnings refuses strict");
117 assert!(report.strict_failure(&src).is_none(), "a Typst warning stands beside the PDF");
118 Ok(())
119}