Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/eval_model.rs

27.3 KiB, 7 runs

created by r1870400018:63606, 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//! U5: the model elements against the `typst` 0.15.1 oracle. Four comparisons, none against Austenite's
2//! own output:
3//!
4//! * construction -- each line of `construct.txt` is evaluated by `typst eval` and by Austenite in code
5//! mode; the element's fields (as JSON, in order) or the first error must agree;
6//! * materialised and synthesised fields -- for each `fields_*.typ`, `query(kind).map(e => e.fields())`
7//! against Austenite's elements after preparation (location, unset fields from the style chain and
8//! the schema's defaults, then `model::synthesise`), over two passes so a reference sees its target;
9//! * local names -- each language's supplements and titles against what Typst synthesises;
10//! * bibliographies -- IEEE and Chicago author-date entries and citations against Typst's HTML export.
11//!
12//! Preparation is done here the way realisation's `prepare` does it -- location, fields from the chain,
13//! then `content::synthesise` -- over the evaluated content, without show rules, which the fixtures do
14//! not use and which run after preparation in any case.
15//! A case needing another unit is marked `%%[U3]` and checked from the moment that unit is present.
16//! `EVAL_ORACLE_SKIP=1` skips explicitly; a missing `typst` otherwise fails.
17
18#[path = "eval_oracle/json.rs"]
19#[allow(dead_code)]
20mod json;
21
22use json::J;
23
24use oxedyne_fe2o3_austenite::bib::{
25 Bibliography,
26 CiteForm,
27 CiteStyle,
28};
29use oxedyne_fe2o3_austenite::eval::content::{
30 Content,
31 ElemKind,
32 Family,
33 FieldId,
34};
35use oxedyne_fe2o3_austenite::eval::eval::{
36 eval_string,
37 EvalMode,
38};
39use oxedyne_fe2o3_austenite::eval::intro::{
40 Builder,
41 CounterKey,
42};
43use oxedyne_fe2o3_austenite::eval::lib::foundations::repr;
44use oxedyne_fe2o3_austenite::eval::lib::model::local::local_name;
45use oxedyne_fe2o3_austenite::eval::scope::Scope;
46use oxedyne_fe2o3_austenite::eval::select::Selector;
47use oxedyne_fe2o3_austenite::eval::styles::StyleChain;
48use oxedyne_fe2o3_austenite::eval::value::{
49 Alignment,
50 HAlign,
51 Length,
52 VAlign,
53 Value,
54};
55use oxedyne_fe2o3_austenite::eval::{
56 eval_source,
57 Engine,
58 World,
59};
60use oxedyne_fe2o3_austenite::ir::Sp;
61use oxedyne_fe2o3_austenite::ledger::Position;
62use oxedyne_fe2o3_austenite::syntax::Span;
63
64use oxedyne_fe2o3_core::prelude::*;
65
66use std::path::{
67 Path,
68 PathBuf,
69};
70use std::process::Command;
71use std::sync::Arc;
72
73// Oracle plumbing
74
75fn dir() -> PathBuf { Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/eval/model") }
76
77fn skip() -> bool { std::env::var("EVAL_ORACLE_SKIP").map(|v| v == "1").unwrap_or(false) }
78
79/// Runs `typst` under a 3G memory cap when systemd can scope it; environment expansion is off so a `$`
80/// in an argument reaches typst.
81fn typst(args: &[&str]) -> Outcome<(bool, String, String)> {
82 let mut capped = vec!["--user", "--scope", "--quiet", "--expand-environment=no", "-p", "MemoryMax=3G",
83 "--slice=claude-rc.slice", "typst"];
84 capped.extend_from_slice(args);
85 let out = match Command::new("systemd-run").args(&capped).output() {
86 Ok(o) if o.status.code() != Some(127) && !String::from_utf8_lossy(&o.stderr).contains("Failed to") => o,
87 _ => res!(Command::new("typst").args(args).output().map_err(|e| err!(
88 "The typst oracle could not run ({}); set EVAL_ORACLE_SKIP=1 to skip explicitly.", e; IO, Missing))),
89 };
90 Ok((out.status.success(), String::from_utf8_lossy(&out.stdout).to_string(),
91 String::from_utf8_lossy(&out.stderr).to_string()))
92}
93
94fn first_error(stderr: &str) -> String {
95 stderr.lines().find_map(|l| l.strip_prefix("error: ")).unwrap_or(stderr.trim()).to_string()
96}
97
98// Values as `typst eval` serialises them
99
100fn num(f: f64) -> String {
101 let r = (f * 100.0).round() / 100.0;
102 let s = fmt!("{:.2}", r);
103 let s = s.trim_end_matches('0').trim_end_matches('.').to_string();
104 if s == "-0" { "0".to_string() } else { s }
105}
106
107fn length(l: &Length) -> String {
108 match (l.abs != 0.0, l.em != 0.0) {
109 (true, true) => fmt!("{}pt + {}em", num(l.abs), num(l.em)),
110 (false, true) => fmt!("{}em", num(l.em)),
111 _ => fmt!("{}pt", num(l.abs)),
112 }
113}
114
115fn alignment(a: &Alignment) -> String {
116 let x = a.x.map(|x| match x {
117 HAlign::Start => "start",
118 HAlign::Left => "left",
119 HAlign::Center => "center",
120 HAlign::Right => "right",
121 HAlign::End => "end",
122 });
123 let y = a.y.map(|y| match y {
124 VAlign::Top => "top",
125 VAlign::Horizon => "horizon",
126 VAlign::Bottom => "bottom",
127 });
128 match (x, y) {
129 (Some(x), Some(y)) => fmt!("{} + {}", x, y),
130 (Some(x), None) => x.to_string(),
131 (None, Some(y)) => y.to_string(),
132 (None, None) => "start".to_string(),
133 }
134}
135
136fn selector(s: &Selector) -> String {
137 match s {
138 Selector::Elem(k, None) => k.path().to_string(),
139 Selector::Elem(k, Some(w)) => {
140 let parts: Vec<String> = w.iter().map(|(id, v)| {
141 let name = k.field_spec(*id).map(|s| s.name).unwrap_or("?");
142 fmt!("{}: {}", name, leaf(v))
143 }).collect();
144 fmt!("{}.where({})", k.path(), parts.join(", "))
145 }
146 Selector::Label(l) => fmt!("<{}>", l.as_str()),
147 _ => "selector(..)".to_string(),
148 }
149}
150
151/// The `repr` of a value JSON cannot hold, for the kinds a model field holds; anything else through
152/// U3's `repr`, whose stub makes such a case pend rather than pass.
153fn leaf(v: &Value) -> String {
154 match v {
155 Value::Auto => "auto".to_string(),
156 Value::None => "none".to_string(),
157 Value::Int(i) => i.to_string(),
158 Value::Str(s) => fmt!("\"{}\"", s),
159 Value::Length(l) => length(l),
160 Value::Ratio(r) => fmt!("{}%", num(r.0 * 100.0)),
161 Value::Relative(r) => fmt!("{}% + {}", num(r.rel.0 * 100.0), length(&r.abs)),
162 Value::Fraction(f) => fmt!("{}fr", num(f.0)),
163 Value::Alignment(a) => alignment(a),
164 Value::Label(l) => fmt!("<{}>", l.as_str()),
165 Value::Func(f) => f.name().unwrap_or("(..) => ..").to_string(),
166 Value::Selector(s) => selector(s),
167 Value::Counter(c) => match &c.key {
168 CounterKey::Page => "counter(page)".to_string(),
169 CounterKey::Str(s) => fmt!("counter(\"{}\")", s),
170 CounterKey::Selector(s) => fmt!("counter({})", selector(s)),
171 },
172 other => repr(other),
173 }
174}
175
176fn to_json(v: &Value) -> J {
177 match v {
178 Value::None => J::Null,
179 Value::Bool(b) => J::Bool(*b),
180 Value::Int(i) => J::Int(*i),
181 Value::Float(f) => J::Float(*f),
182 Value::Str(s) => J::Str(s.to_string()),
183 Value::Array(a) => J::Arr(a.iter().map(to_json).collect()),
184 Value::Dict(d) => J::Obj(d.iter().map(|(k, x)| (k.to_string(), to_json(x))).collect()),
185 Value::Content(c) => content_json(c),
186 other => J::Str(leaf(other)),
187 }
188}
189
190fn content_json(c: &Content) -> J {
191 let mut kv: Vec<(String, J)> = Vec::new();
192 match c {
193 Content::Sequence(s) => {
194 kv.push(("func".to_string(), J::str("sequence")));
195 kv.push(("children".to_string(), J::Arr(s.children.iter().map(content_json).collect())));
196 }
197 Content::Styled(s) => {
198 kv.push(("func".to_string(), J::str("styled")));
199 kv.push(("child".to_string(), content_json(&s.child)));
200 kv.push(("styles".to_string(), J::str("styles(..)")));
201 }
202 Content::Elem(e) => {
203 kv.push(("func".to_string(), J::str(e.kind.name())));
204 let mut fields: Vec<&(FieldId, Value)> = e.fields.iter().collect();
205 fields.sort_by_key(|(id, _)| id.0);
206 for (id, v) in fields {
207 let spec = e.kind.field_spec(*id);
208 if spec.map(|s| s.internal).unwrap_or(false) {
209 continue;
210 }
211 let name = spec.map(|s| s.name).unwrap_or("?");
212 kv.push((name.to_string(), to_json(v)));
213 }
214 }
215 }
216 if let Some(l) = c.label() {
217 kv.push(("label".to_string(), J::Str(fmt!("<{}>", l.as_str()))));
218 }
219 J::Obj(kv)
220}
221
222/// A raw line's body is compared by its text: Typst highlights it, which Austenite does not yet.
223fn loosen(j: &J) -> J {
224 match j {
225 J::Obj(kv) => {
226 let raw_line = j.get("func").and_then(|f| f.as_str()) == Some("line") && j.get("count").is_some();
227 J::Obj(kv.iter().map(|(k, v)| {
228 if raw_line && k == "body" {
229 (k.clone(), J::Str(plain(v)))
230 } else {
231 (k.clone(), loosen(v))
232 }
233 }).collect())
234 }
235 J::Arr(a) => J::Arr(a.iter().map(loosen).collect()),
236 other => other.clone(),
237 }
238}
239
240fn plain(j: &J) -> String {
241 match j {
242 J::Obj(_) => match (j.get("text"), j.get("children"), j.get("child")) {
243 (Some(J::Str(t)), _, _) => t.clone(),
244 (_, Some(J::Arr(a)), _) => a.iter().map(plain).collect(),
245 (_, _, Some(c)) => plain(c),
246 _ => String::new(),
247 },
248 _ => String::new(),
249 }
250}
251
252/// Is the U3 library present (its `repr` no longer a stub)?
253fn have_u3() -> bool { repr(&Value::Int(1)) == "1" }
254
255/// Does the rendering hold a value only U3's `repr` can print? Its stub prints `<type>`.
256fn pends_on_u3(j: &J) -> bool {
257 let r = j.render();
258 !have_u3() && ["<color>", "<stroke>", "<angle>", "<datetime>", "<regex>", "<symbol>", "<bytes>", "<version>",
259 "<float>", "<dictionary>", "<array>", "<function>"].iter().any(|t| r.contains(t))
260}
261
262// Construction
263
264#[test]
265fn construction_matches_the_typst_oracle() -> Outcome<()> {
266 if skip() {
267 println!("EVAL_ORACLE_SKIP=1: oracle comparison skipped");
268 return Ok(());
269 }
270 let text = res!(std::fs::read_to_string(dir().join("construct.txt")).map_err(|e| err!("{}", e; IO)));
271 let mut checked = 0;
272 let mut pending = Vec::new();
273 let mut failures = Vec::new();
274 for line in text.lines() {
275 let line = line.trim();
276 if line.is_empty() || line.starts_with(";;") {
277 continue;
278 }
279 if let Some(c) = line.strip_prefix("%%[CSL] ") {
280 pending.push(c.to_string());
281 continue;
282 }
283 let code = match line.strip_prefix("%%[U3] ") {
284 Some(c) if !have_u3() => {
285 pending.push(c.to_string());
286 continue;
287 }
288 Some(c) => c,
289 None => line,
290 };
291 let (ok, out, err) = res!(typst(&["eval", "--", code]));
292 let mut engine = Engine::new(World::new(PathBuf::from("/")));
293 let got = eval_string(&mut engine, code, EvalMode::Code, Scope::new(), Span::detached());
294 checked += 1;
295 match (ok, got) {
296 (true, Ok(v)) => {
297 let want = match json::parse(out.trim()) {
298 Ok(j) => loosen(&j),
299 Err(_) => {
300 failures.push(fmt!("{}\n unparsable oracle output {}", code, out.trim()));
301 continue;
302 }
303 };
304 let have = loosen(&to_json(&v));
305 if pends_on_u3(&have) {
306 pending.push(code.to_string());
307 continue;
308 }
309 if want.render() != have.render() {
310 failures.push(fmt!("{}\n typst {}\n austenite {}", code, want.render(), have.render()));
311 }
312 }
313 (false, Err(_)) => {
314 let want = first_error(&err);
315 let have = engine.diags.iter().find(|d| d.is_error()).map(|d| d.message.clone()).unwrap_or_default();
316 if want != have {
317 failures.push(fmt!("{}\n typst error {}\n austenite error {}", code, want, have));
318 }
319 }
320 (true, Err(e)) => failures.push(fmt!("{}\n typst accepts it, austenite: {}", code, e)),
321 (false, Ok(v)) => failures.push(fmt!("{}\n typst refuses it ({}), austenite gives {}", code,
322 first_error(&err), to_json(&v).render())),
323 }
324 }
325 println!("construction: {} checked, {} pending (U3 or CSL), {} failed", checked, pending.len(), failures.len());
326 assert!(checked > 120, "the construction corpus shrank to {}", checked);
327 assert!(failures.is_empty(), "{} case(s) differ from typst:\n{}", failures.len(), failures.join("\n"));
328 Ok(())
329}
330
331// Preparation and synthesis
332
333/// Evaluates a fixture file into its content.
334fn load(path: &Path) -> Outcome<(Engine, Content)> {
335 let text = res!(std::fs::read_to_string(path).map_err(|e| err!("{}", e; IO)));
336 let mut world = World::new(dir());
337 let id = res!(world.add_source(path.to_path_buf(), text));
338 let mut engine = Engine::new(world);
339 let module = res!(eval_source(&mut engine, id));
340 Ok((engine, module.content.clone()))
341}
342
343/// The model elements Typst 0.15 locates, which `ElemKind::locatable` should report (see the U5 report);
344/// until it does, the walk locates them itself so their queries can be compared.
345const TYPST_LOCATABLE: &[ElemKind] = &[ElemKind::Par, ElemKind::Strong, ElemKind::Emph, ElemKind::Raw,
346 ElemKind::Title, ElemKind::List, ElemKind::Enum, ElemKind::Terms, ElemKind::Link, ElemKind::Ref,
347 ElemKind::FootnoteEntry, ElemKind::FigureCaption, ElemKind::OutlineEntry, ElemKind::Quote];
348
349fn located(k: ElemKind) -> bool { k.locatable() || TYPST_LOCATABLE.contains(&k) }
350
351/// Realisation's preparation: a location for a locatable or labelled element, unset settable fields
352/// from the chain, then the family's synthesis (which also materialises the computed defaults).
353fn prepare(engine: &mut Engine, c: &Content, chain: &StyleChain) -> Outcome<Content> {
354 let mut out = c.clone();
355 // Read before `edit` forgets the cell.
356 let fp = c.fingerprint();
357 if let Content::Elem(e) = &mut out {
358 let e = e.edit();
359 if e.location.is_none() && (located(e.kind) || e.label.is_some()) {
360 e.location = Some(engine.locator.locate(e.kind, fp));
361 }
362 for (i, spec) in e.kind.fields().iter().enumerate() {
363 let id = FieldId(i as u8);
364 if !spec.settable || e.fields.iter().any(|(f, _)| *f == id) {
365 continue;
366 }
367 if let Some(v) = res!(chain.get(e.kind, id)) {
368 e.fields.push((id, v));
369 }
370 }
371 e.prepared = true;
372 }
373 if out.kind().map(|k| k.family() == Family::Model).unwrap_or(false) {
374 res!(oxedyne_fe2o3_austenite::eval::content::synthesise(engine, &mut out, chain));
375 }
376 Ok(out)
377}
378
379/// Walks the content as realisation would reach it, preparing each element and keeping the located
380/// ones in document order. Show rules are not applied: the fixtures have none, and preparation happens
381/// before any.
382fn walk(engine: &mut Engine, c: &Content, chain: &StyleChain, sink: &mut Vec<Content>) -> Outcome<()> {
383 match c {
384 Content::Sequence(s) => {
385 for x in &s.children {
386 res!(walk(engine, x, chain, sink));
387 }
388 }
389 Content::Styled(s) => {
390 let inner = chain.chain(&s.styles);
391 res!(walk(engine, &s.child, &inner, sink));
392 }
393 Content::Elem(_) => {
394 let prepared = res!(prepare(engine, c, chain));
395 if prepared.location().is_some() {
396 sink.push(prepared.clone());
397 }
398 if let Content::Elem(e) = c {
399 for (_, v) in &e.fields {
400 res!(walk_value(engine, v, chain, sink));
401 }
402 }
403 }
404 }
405 Ok(())
406}
407
408fn walk_value(engine: &mut Engine, v: &Value, chain: &StyleChain, sink: &mut Vec<Content>) -> Outcome<()> {
409 match v {
410 Value::Content(c) => walk(engine, c, chain, sink),
411 Value::Array(a) => {
412 for x in a.iter() {
413 res!(walk_value(engine, x, chain, sink));
414 }
415 Ok(())
416 }
417 _ => Ok(()),
418 }
419}
420
421/// Two passes: the second sees the first's elements, as the fixpoint's second pass does.
422fn realised(engine: &mut Engine, content: &Content) -> Outcome<Vec<Content>> {
423 let mut first = Vec::new();
424 res!(walk(engine, content, &StyleChain::root(), &mut first));
425 let mut builder = Builder::new();
426 for (i, e) in first.iter().enumerate() {
427 builder.record(e, Position::new(1, Sp::ZERO, Sp(i as i32)), None);
428 }
429 builder.page(1, &Value::None);
430 engine.intro = Arc::new(builder.finish());
431 engine.locator.reset();
432 let mut second = Vec::new();
433 res!(walk(engine, content, &StyleChain::root(), &mut second));
434 Ok(second)
435}
436
437fn kind_named(name: &str) -> Option<ElemKind> { ElemKind::ALL.iter().copied().find(|k| k.path() == name) }
438
439#[test]
440fn materialised_fields_match_the_typst_oracle() -> Outcome<()> {
441 if skip() {
442 println!("EVAL_ORACLE_SKIP=1: oracle comparison skipped");
443 return Ok(());
444 }
445 let mut files: Vec<PathBuf> = res!(std::fs::read_dir(dir()).map_err(|e| err!("{}", e; IO)))
446 .filter_map(|e| e.ok().map(|e| e.path()))
447 .filter(|p| p.file_name().and_then(|n| n.to_str()).map(|n| n.starts_with("fields_")).unwrap_or(false))
448 .collect();
449 files.sort();
450 let mut failures = Vec::new();
451 let mut compared = 0;
452 for f in &files {
453 let text = res!(std::fs::read_to_string(f).map_err(|e| err!("{}", e; IO)));
454 if text.lines().any(|l| l.trim() == "// needs: U3") && !have_u3() {
455 println!("{}: pending on U3", f.display());
456 continue;
457 }
458 let kinds: Vec<String> = text.lines().next().and_then(|l| l.strip_prefix("// query: "))
459 .map(|q| q.split_whitespace().map(|s| s.to_string()).collect()).unwrap_or_default();
460 let (mut engine, content) = match load(f) {
461 Ok(x) => x,
462 Err(e) => {
463 failures.push(fmt!("{}: austenite cannot evaluate it: {}", f.display(), e));
464 continue;
465 }
466 };
467 let elems = match realised(&mut engine, &content) {
468 Ok(x) => x,
469 Err(e) => {
470 let d: Vec<String> = engine.diags.iter().map(|d| d.message.clone()).collect();
471 failures.push(fmt!("{}: preparation failed: {} {:?}", f.display(), e, d));
472 continue;
473 }
474 };
475 for k in &kinds {
476 let kind = match kind_named(k) {
477 Some(x) => x,
478 None => return Err(err!("unknown kind {} in {}", k, f.display(); Input, Invalid)),
479 };
480 let fpath = f.to_string_lossy().to_string();
481 let (ok, out, err) = res!(typst(&["eval", "--in", &fpath, &fmt!("query({}).map(e => e.fields())", k)]));
482 if !ok {
483 failures.push(fmt!("{} {}: typst failed: {}", f.display(), k, first_error(&err)));
484 continue;
485 }
486 let want = loosen(&res!(json::parse(out.trim())));
487 let have = loosen(&J::Arr(elems.iter().filter(|e| e.is(kind)).map(|e| {
488 // `fields()` has no `func` key; it is the element's own dictionary.
489 match content_json(e) {
490 J::Obj(kv) => J::Obj(kv.into_iter().filter(|(k, _)| k != "func").collect()),
491 other => other,
492 }
493 }).collect()));
494 compared += 1;
495 if want.render() != have.render() {
496 let mut d = Vec::new();
497 json::diff(k, &want, &have, &mut d);
498 failures.push(fmt!("{} {}:\n typst {}\n austenite {}\n {}", f.display(), k,
499 json::clip(&want.render()), json::clip(&have.render()), d.join("\n ")));
500 }
501 }
502 }
503 println!("fields: {} queries compared over {} documents, {} failed", compared, files.len(), failures.len());
504 assert!(failures.is_empty(), "{} query(ies) differ from typst:\n{}", failures.len(), failures.join("\n"));
505 assert!(compared >= 9, "only {} queries were compared", compared);
506 Ok(())
507}
508
509// Local names
510
511const LANGS: &[(&str, Option<&str>)] = &[
512 ("en", None), ("de", None), ("fr", None), ("fr", Some("CA")), ("es", None), ("it", None), ("pt", None),
513 ("pt", Some("PT")), ("nl", None), ("sv", None), ("da", None), ("nb", None), ("nn", None), ("fi", None),
514 ("pl", None), ("cs", None), ("ru", None), ("uk", None), ("el", None), ("tr", None), ("ar", None),
515 ("he", None), ("zh", None), ("zh", Some("TW")), ("ja", None), ("ko", None), ("hi", None), ("la", None),
516 ("ca", None), ("hu", None), ("ro", None), ("xx", None),
517];
518
519#[test]
520fn local_names_match_the_typst_oracle() -> Outcome<()> {
521 if skip() {
522 println!("EVAL_ORACLE_SKIP=1: oracle comparison skipped");
523 return Ok(());
524 }
525 // One document: per language a heading, a figure of each named kind and an outline, whose synthesised
526 // supplements and default title Typst reports.
527 let mut doc = String::new();
528 for (l, r) in LANGS {
529 let region = r.map(|r| fmt!(", region: \"{}\"", r)).unwrap_or_default();
530 doc.push_str(&fmt!("#[#set text(lang: \"{}\"{})\n= H\n#figure(rect())\n#figure(table[a])\n#figure(`x`)\n]\n",
531 l, region));
532 }
533 let work = std::env::temp_dir().join(fmt!("u5-local-{}", std::process::id()));
534 res!(std::fs::create_dir_all(&work).map_err(|e| err!("{}", e; IO)));
535 let path = work.join("local.typ");
536 res!(std::fs::write(&path, &doc).map_err(|e| err!("{}", e; IO)));
537 let p = path.to_string_lossy().to_string();
538 let (ok, out, err) = res!(typst(&["eval", "--in", &p,
539 "(query(heading).map(h => h.supplement.text), query(figure).map(f => f.supplement.text))"]));
540 let _ = std::fs::remove_dir_all(&work);
541 assert!(ok, "typst failed: {}", first_error(&err));
542 let got = res!(json::parse(out.trim()));
543 let (heads, figs) = match &got {
544 J::Arr(a) if a.len() == 2 => (a[0].clone(), a[1].clone()),
545 other => return Err(err!("unexpected oracle output {}", other.render(); Input, Invalid)),
546 };
547 let mut failures = Vec::new();
548 for (i, (l, r)) in LANGS.iter().enumerate() {
549 let want_h = match &heads { J::Arr(a) => a.get(i).and_then(|x| x.as_str()).unwrap_or("").to_string(), _ => String::new() };
550 let have_h = local_name("heading", l, *r);
551 if want_h != have_h {
552 failures.push(fmt!("{}-{:?} heading: typst {:?}, austenite {:?}", l, r, want_h, have_h));
553 }
554 for (j, key) in ["figure", "table", "raw"].iter().enumerate() {
555 let want = match &figs { J::Arr(a) => a.get(i * 3 + j).and_then(|x| x.as_str()).unwrap_or("").to_string(), _ => String::new() };
556 let have = local_name(key, l, *r);
557 if want != have {
558 failures.push(fmt!("{}-{:?} {}: typst {:?}, austenite {:?}", l, r, key, want, have));
559 }
560 }
561 }
562 println!("local names: {} languages, {} differences", LANGS.len(), failures.len());
563 assert!(failures.is_empty(), "{}", failures.join("\n"));
564 Ok(())
565}
566
567// Bibliographies
568
569/// The text of every `<li>` of the bibliography section and every citation link, entities decoded.
570fn html_texts(html: &str) -> (Vec<String>, Vec<String>) {
571 fn strip(s: &str) -> String {
572 let mut out = String::new();
573 let mut in_tag = false;
574 for c in s.chars() {
575 match c {
576 '<' => in_tag = true,
577 '>' => in_tag = false,
578 c if !in_tag => out.push(c),
579 _ => (),
580 }
581 }
582 out.replace("&#x20;", " ").replace("&amp;", "&").replace("&lt;", "<").replace("&gt;", ">")
583 .replace("&quot;", "\"").replace("&#39;", "'")
584 }
585 let mut entries = Vec::new();
586 let section = html.split("role=\"doc-bibliography\"").nth(1).unwrap_or("");
587 for li in section.split("<li").skip(1) {
588 let body = li.split("</li>").next().unwrap_or("");
589 let body = body.splitn(2, '>').nth(1).unwrap_or("");
590 entries.push(strip(body).trim().to_string());
591 }
592 let mut cites = Vec::new();
593 let before = html.split("role=\"doc-bibliography\"").next().unwrap_or("");
594 for a in before.split("role=\"doc-biblioref\">").skip(1) {
595 cites.push(strip(a.split("</a>").next().unwrap_or("")).to_string());
596 }
597 (entries, cites)
598}
599
600const BIB_KEYS: &[&str] = &["smith2020", "brown2018", "lee2019", "gray2015", "web2021", "tech2017", "phd2016",
601 "many2022"];
602
603#[test]
604fn bibliography_formatting_matches_the_typst_oracle() -> Outcome<()> {
605 if skip() {
606 println!("EVAL_ORACLE_SKIP=1: oracle comparison skipped");
607 return Ok(());
608 }
609 let bib_text = res!(std::fs::read_to_string(dir().join("refs.bib")).map_err(|e| err!("{}", e; IO)));
610 let lib = res!(Bibliography::parse(&bib_text));
611 let cited: Vec<String> = BIB_KEYS.iter().map(|k| k.to_string()).collect();
612 let mut failures = Vec::new();
613 let mut compared = 0;
614 for (style, name) in [(CiteStyle::Ieee, "ieee"), (CiteStyle::ChicagoAuthorDate, "chicago-author-date")] {
615 let src = dir().join(fmt!("bib_{}.typ", name));
616 let work = std::env::temp_dir().join(fmt!("u5-bib-{}-{}", name, std::process::id()));
617 res!(std::fs::create_dir_all(&work).map_err(|e| err!("{}", e; IO)));
618 let html = work.join("out.html");
619 let (s, h) = (src.to_string_lossy().to_string(), html.to_string_lossy().to_string());
620 let (ok, _, err) = res!(typst(&["compile", "--features", "html", "--format", "html", &s, &h]));
621 assert!(ok, "typst failed on {}: {}", s, first_error(&err));
622 let text = res!(std::fs::read_to_string(&html).map_err(|e| err!("{}", e; IO)));
623 let _ = std::fs::remove_dir_all(&work);
624 let (want_entries, want_cites) = html_texts(&text);
625 // The list.
626 let have: Vec<String> = lib.render_list(style, &cited, false).iter().map(|e| match &e.prefix {
627 Some(p) => fmt!("{} {}", p, e.body.plain()),
628 None => e.body.plain(),
629 }).collect();
630 if want_entries.len() != have.len() {
631 failures.push(fmt!("{}: typst lists {} entries, austenite {}", name, want_entries.len(), have.len()));
632 }
633 for (w, h) in want_entries.iter().zip(have.iter()) {
634 compared += 1;
635 if norm(w) != norm(h) {
636 failures.push(fmt!("{} entry:\n typst {}\n austenite {}", name, w, h));
637 }
638 }
639 // The citations, in the fixture's order: eight single ones (the second and third cited together),
640 // then a supplement, prose, author, year and full form.
641 let mut have_cites = Vec::new();
642 let one = |k: &str, f: CiteForm, sup: Option<&str>| -> Outcome<String> {
643 let runs = res!(lib.render_citation(style, k, f, sup, &cited));
644 Ok(runs.iter().map(|r| r.text.as_str()).collect())
645 };
646 have_cites.push(res!(one("smith2020", CiteForm::Normal, None)));
647 let group = res!(lib.render_group(style, &[("brown2018", None), ("lee2019", None)], &cited));
648 for g in group {
649 have_cites.push(g.iter().map(|r| r.text.as_str()).collect());
650 }
651 for k in &BIB_KEYS[3..] {
652 have_cites.push(res!(one(k, CiteForm::Normal, None)));
653 }
654 have_cites.push(res!(one("smith2020", CiteForm::Normal, Some("p. 5"))));
655 // Typst's HTML links an author-date citation inside its parentheses and group punctuation.
656 if style == CiteStyle::ChicagoAuthorDate {
657 for c in have_cites.iter_mut() {
658 *c = c.trim_start_matches('(').trim_end_matches(|x| x == ')' || x == ';').to_string();
659 }
660 }
661 have_cites.push(res!(one("smith2020", CiteForm::Prose, None)));
662 have_cites.push(res!(one("brown2018", CiteForm::Author, None)));
663 have_cites.push(res!(one("brown2018", CiteForm::Year, None)));
664 let mut full = res!(one("lee2019", CiteForm::Full, None));
665 // The full stop closing an author-date reference sits outside the link.
666 if style == CiteStyle::ChicagoAuthorDate {
667 full = full.trim_end_matches('.').to_string();
668 }
669 have_cites.push(full);
670 if want_cites.len() != have_cites.len() {
671 failures.push(fmt!("{}: typst has {} citations {:?}, austenite {} {:?}", name, want_cites.len(),
672 want_cites, have_cites.len(), have_cites));
673 }
674 for (w, h) in want_cites.iter().zip(have_cites.iter()) {
675 compared += 1;
676 if norm(w) != norm(h) {
677 failures.push(fmt!("{} citation:\n typst {}\n austenite {}", name, w, h));
678 }
679 }
680 }
681 println!("bibliography: {} strings compared, {} differ", compared, failures.len());
682 assert!(compared > 30, "only {} strings were compared", compared);
683 assert!(failures.is_empty(), "{}", failures.join("\n"));
684 Ok(())
685}
686
687fn norm(s: &str) -> String { s.split_whitespace().collect::<Vec<_>>().join(" ") }
688
689#[test]
690fn bibliography_lists_match_the_typst_oracle() -> Outcome<()> {
691 if skip() {
692 println!("EVAL_ORACLE_SKIP=1: oracle comparison skipped");
693 return Ok(());
694 }
695 let bib_text = res!(std::fs::read_to_string(dir().join("refs2.bib")).map_err(|e| err!("{}", e; IO)));
696 let lib = res!(Bibliography::parse(&bib_text));
697 let mut failures = Vec::new();
698 let mut compared = 0;
699 for (style, name) in [(CiteStyle::Ieee, "ieee"), (CiteStyle::ChicagoAuthorDate, "chicago-author-date")] {
700 let src = dir().join(fmt!("bib2_{}.typ", name));
701 let text = res!(std::fs::read_to_string(&src).map_err(|e| err!("{}", e; IO)));
702 let cited: Vec<String> = text.lines().next().and_then(|l| l.strip_prefix("// cited: "))
703 .map(|q| q.split_whitespace().map(|s| s.to_string()).collect()).unwrap_or_default();
704 let work = std::env::temp_dir().join(fmt!("u5-bib2-{}-{}", name, std::process::id()));
705 res!(std::fs::create_dir_all(&work).map_err(|e| err!("{}", e; IO)));
706 let html = work.join("out.html");
707 let (s, h) = (src.to_string_lossy().to_string(), html.to_string_lossy().to_string());
708 let (ok, _, err) = res!(typst(&["compile", "--features", "html", "--format", "html", &s, &h]));
709 assert!(ok, "typst failed on {}: {}", s, first_error(&err));
710 let out = res!(std::fs::read_to_string(&html).map_err(|e| err!("{}", e; IO)));
711 let _ = std::fs::remove_dir_all(&work);
712 let (want, _) = html_texts(&out);
713 let have: Vec<String> = lib.render_list(style, &cited, false).iter().map(|e| match &e.prefix {
714 Some(p) => fmt!("{} {}", p, e.body.plain()),
715 None => e.body.plain(),
716 }).collect();
717 if want.len() != have.len() {
718 failures.push(fmt!("{}: typst lists {} entries, austenite {}", name, want.len(), have.len()));
719 }
720 for (w, h) in want.iter().zip(have.iter()) {
721 compared += 1;
722 if norm(w) != norm(h) {
723 failures.push(fmt!("{} entry:\n typst {}\n austenite {}", name, w, h));
724 }
725 }
726 }
727 println!("bibliography lists: {} entries compared, {} differ", compared, failures.len());
728 assert!(failures.is_empty(), "{}", failures.join("\n"));
729 assert!(compared >= 20, "only {} entries were compared", compared);
730 Ok(())
731}