Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/eval_realise.rs

6.9 KiB, 9 runs

created by r1870400018:63634, 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//! U4-fix: a symbol realises as text, as Typst's realisation makes it, so a shorthand stays in its paragraph.
2//!
3//! A shorthand (`--`, `---`, `...`, `~`, `-?`), an escape and a symbol value evaluate to `symbol`
4//! elements. Typst turns each into a `text` element before any show rule or grouping runs outside maths
5//! (`typst-realize`, `visit_kind_rules`), so the symbol joins the paragraph and the textual run it stands
6//! in. Realisation that left it a symbol split the paragraph in two at every ` -- ` and dropped the dash.
7//!
8//! - The `realise` corpus (`tests/fixtures/eval/realise/`) holds each kind of symbol inside paragraphs,
9//! headings, tight lists, terms, strong and emphasis, with and without a text show rule that reaches
10//! it. Each fixture is held to the structure of Typst's own realisation, at level 3.
11//! - In maths a symbol stays a symbol and a text or regex rule reaches it one element at a time. That
12//! has no HTML to compare, so the equation of `math_rule.typ` is held to the text Typst sets for it.
13//! Until the evaluator has a maths schema, Austenite's side is the equation's content built by hand.
14
15#![allow(dead_code)]
16
17#[path = "eval_oracle/mod.rs"]
18mod harness;
19
20use harness::corpus::{
21 discover,
22 Expect,
23 Filter,
24 Fixture,
25};
26use harness::oracle::Oracle;
27use harness::Verdict;
28
29use oxedyne_fe2o3_austenite::eval::content::{
30 Content,
31 ElemKind,
32};
33use oxedyne_fe2o3_austenite::eval::realise::{
34 realise,
35 RealiseMode,
36};
37use oxedyne_fe2o3_austenite::eval::select::Selector;
38use oxedyne_fe2o3_austenite::eval::styles::{
39 Recipe,
40 Style,
41 StyleChain,
42 Styles,
43 Transformation,
44};
45use oxedyne_fe2o3_austenite::eval::{
46 Engine,
47 World,
48};
49use oxedyne_fe2o3_austenite::syntax::Span;
50
51use oxedyne_fe2o3_core::prelude::*;
52
53use std::path::PathBuf;
54use std::sync::Arc;
55
56const AREA: &str = "realise";
57
58/// The fixtures the corpus must hold; the count can only go up.
59const FIXTURES: usize = 11;
60
61fn oracle() -> Outcome<Option<Oracle>> {
62 let o = res!(Oracle::find());
63 if o.is_none() {
64 println!("[eval-realise] SKIPPED: EVAL_ORACLE_SKIP=1 and no typst 0.15.x -- nothing was compared");
65 }
66 Ok(o)
67}
68
69#[test]
70fn symbols_in_markup_stay_in_their_paragraph_as_typst_realises_them() -> Outcome<()> {
71 let o = match res!(oracle()) {
72 Some(o) => o,
73 None => return Ok(()),
74 };
75 let all = res!(discover(&Filter::only(AREA)));
76 assert!(all.len() >= FIXTURES, "{} fixtures under tests/fixtures/eval/{}, {} expected", all.len(), AREA, FIXTURES);
77 let limit = harness::timeout();
78 let mut bad = Vec::new();
79 for fx in &all {
80 assert!(fx.wants(3) && !fx.wants(1) && !fx.wants(2) && !fx.wants(4),
81 "{} must ask for level 3 alone", fx.id());
82 let rep = res!(harness::check(fx, &o, limit));
83 if let Some(f) = &rep.fault {
84 bad.push(fmt!("{}: {}", fx.id(), f));
85 continue;
86 }
87 match &rep.levels[2] {
88 Verdict::Pass => (),
89 Verdict::Fail(d) => bad.push(fmt!("{}: {}", fx.id(), d.join("; "))),
90 // A fixture Typst's HTML export cannot compare would pass without being held to anything.
91 other => bad.push(fmt!("{}: level 3 was not compared ({:?})", fx.id(), other)),
92 }
93 }
94 println!("[eval-realise] {} fixtures compared at level 3, {} differ", all.len(), bad.len());
95 assert!(bad.is_empty(), "{} fixture(s) differ from Typst's realisation:\n{}", bad.len(), bad.join("\n"));
96 Ok(())
97}
98
99/// The characters of a page's text, as the PDF holds them, with all whitespace gone.
100fn typst_text(o: &Oracle, fx: &Fixture) -> Outcome<String> {
101 let pages = match res!(o.layout(fx)) {
102 Ok(p) => p,
103 Err(e) => return Err(err!("typst cannot lay {} out: {}", fx.id(), e; Test)),
104 };
105 let mut s = String::new();
106 for p in &pages {
107 for l in &p.lines {
108 s.push_str(&l.text);
109 }
110 }
111 Ok(s.chars().filter(|c| !c.is_whitespace()).collect())
112}
113
114fn show_text(sel: &str, out: &str) -> Style {
115 Style::Recipe(Arc::new(Recipe {
116 selector: Some(Selector::Text(sel.into())),
117 transform: Transformation::Content(Content::text(out)),
118 span: Span::detached(),
119 outside: false,
120 }))
121}
122
123/// The equation of `math_rule.typ`, `$ x + x -> x $`, as maths evaluates it: a character is a symbol and
124/// the arrow shorthand is one, a space is a space. The evaluator has no maths schema on this branch, so
125/// the content is built as the evaluator builds it, and realised in maths under the fixture's two rules.
126fn austenite_math_text() -> Outcome<String> {
127 let sp = || Content::marker(ElemKind::Space, Span::detached());
128 let body = Content::sequence(vec![
129 Content::symbol("x"), sp(), Content::symbol("+"), sp(), Content::symbol("x"), sp(),
130 Content::symbol("\u{2192}"), sp(), Content::symbol("x"),
131 ]);
132 let mut chain = StyleChain::root();
133 for st in [show_text("x", "Z"), show_text("\u{2192}", "R")] {
134 chain = chain.chain(&Styles::from_style(st));
135 }
136 let mut e = Engine::new(World::new(PathBuf::from("/")));
137 let mut s = String::new();
138 for q in res!(realise(&mut e, &body, &chain, RealiseMode::Math)) {
139 if q.tag.is_none() {
140 s.push_str(&q.content.plain_text());
141 }
142 }
143 Ok(s.chars().filter(|c| !c.is_whitespace()).collect())
144}
145
146#[test]
147fn a_text_rule_reaches_the_symbols_of_an_equation() -> Outcome<()> {
148 let o = match res!(oracle()) {
149 Some(o) => o,
150 None => return Ok(()),
151 };
152 let path = harness::corpus::fixtures_dir().join(AREA).join("math_rule.typ");
153 let text = res!(std::fs::read_to_string(&path).map_err(|e| err!("Cannot read {}: {}", path.display(), e; IO, File, Read)));
154 let root: PathBuf = harness::corpus::fixtures_dir().join(AREA);
155 let fx = Fixture {
156 area: AREA.to_string(),
157 name: "math_rule".to_string(),
158 path,
159 root,
160 text,
161 levels: [false, false, false, true],
162 expect: Expect::Accepts,
163 };
164 let want = res!(typst_text(&o, &fx));
165 // Typst sets the replaced `x` and the arrow as `Z` and `R`; nothing of the source is left.
166 assert_eq!(want, "Z+ZRZ", "the oracle's text for the equation");
167 let got = res!(austenite_math_text());
168 assert_eq!(got, want, "Austenite's realised equation against Typst's text");
169 Ok(())
170}
171
172#[test]
173fn a_text_rule_slices_a_symbol_longer_than_a_character() -> Outcome<()> {
174 // Typst's `slice_textual` cuts a symbol as it cuts text, though a symbol is usually one character:
175 // an emoji sequence or a decomposed letter is several, and a rule can match a part of it. No oracle
176 // can be asked, since markup cannot make such a symbol outside an equation; the expectation is
177 // `typst-realize`'s `visit_regex_match`, which keeps the unmatched parts as symbols.
178 let body = Content::symbol("a\u{2192}b");
179 let chain = StyleChain::root().chain(&Styles::from_style(show_text("\u{2192}", "R")));
180 let mut e = Engine::new(World::new(PathBuf::from("/")));
181 let pairs = res!(realise(&mut e, &body, &chain, RealiseMode::Math));
182 let parts: Vec<String> = pairs.iter().filter(|p| p.tag.is_none()).map(|p| p.content.plain_text()).collect();
183 assert_eq!(parts, vec!["a".to_string(), "R".to_string(), "b".to_string()], "the symbol cut around the match");
184 assert!(pairs.iter().filter(|p| p.tag.is_none()).next().map(|p| p.content.is(ElemKind::Symbol)).unwrap_or(false),
185 "the part before the match stays a symbol");
186 Ok(())
187}