Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_austenite/tests/eval_foundation.rs

23.5 KiB, 5 runs

created by r1870400018:63582, 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//! U3's oracle tests: the foundation library (calc, str, array, dict, numbering, colour, sym, datetime,
2//! data, foundations, geometry) against `typst` 0.15.1.
3//!
4//! Each fixture under `tests/fixtures/eval/foundation/` is a list of Typst code expressions, one per line.
5//! A plain line must evaluate in both engines to the same `repr`; a line starting `!` must fail in both
6//! with the same first error message, and `!~` must fail in both whatever the message. Austenite
7//! evaluates each line through the evaluator proper (`eval_string` in code mode); the expected values
8//! come from `typst eval`, never from Austenite. A missing `typst` fails the suite unless
9//! `EVAL_ORACLE_SKIP=1` is set. Oracle answers are cached under the target directory, keyed on the
10//! exact text sent, so a changed fixture line is always asked afresh.
11
12use oxedyne_fe2o3_austenite::eval::eval::{
13 eval_string,
14 EvalMode,
15};
16use oxedyne_fe2o3_austenite::eval::lib::foundations::repr;
17use oxedyne_fe2o3_austenite::eval::lib::library;
18use oxedyne_fe2o3_austenite::eval::scope::Scope;
19use oxedyne_fe2o3_austenite::eval::value::Value;
20use oxedyne_fe2o3_austenite::eval::{
21 Engine,
22 World,
23};
24use oxedyne_fe2o3_austenite::syntax::Span;
25
26use oxedyne_fe2o3_core::prelude::*;
27use oxedyne_fe2o3_graphics::colour::{
28 Ink,
29 Rgba,
30};
31
32use std::collections::hash_map::DefaultHasher;
33use std::hash::{
34 Hash,
35 Hasher,
36};
37use std::path::PathBuf;
38use std::process::Command;
39
40fn fixtures_dir() -> PathBuf {
41 PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/eval/foundation")
42}
43
44/// Evaluates one expression; the error is the first error diagnostic's message, as Typst reports the
45/// first error first.
46fn eval_line(line: &str) -> std::result::Result<Value, String> {
47 let mut e = Engine::new(World::new(fixtures_dir()));
48 let out = eval_string(&mut e, line, EvalMode::Code, Scope::new(), Span::detached());
49 match out {
50 Ok(v) => Ok(v),
51 Err(err) => Err(match e.diags.iter().find(|d| d.is_error()) {
52 Some(d) => d.message.clone(),
53 None => err.msgs().last().cloned().unwrap_or_default(),
54 }),
55 }
56}
57
58// The oracle.
59
60fn typst() -> Option<String> {
61 let bin = std::env::var("TYPST").unwrap_or_else(|_| "typst".to_string());
62 match Command::new(&bin).arg("--version").output() {
63 Ok(o) if o.status.success() => Some(bin),
64 _ => None,
65 }
66}
67
68// Whether a missing oracle may pass silently: only when the skip is asked for by name.
69fn skip_allowed() -> bool { std::env::var("EVAL_ORACLE_SKIP").as_deref() == Ok("1") }
70
71fn scratch() -> Outcome<PathBuf> {
72 let d = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("eval_foundation");
73 res!(std::fs::create_dir_all(&d));
74 Ok(d)
75}
76
77// The oracle runs capped, so a runaway document cannot take the build slot's memory.
78fn capped(bin: &str) -> Command {
79 let mut c = Command::new("systemd-run");
80 c.args(["--user", "--scope", "--quiet", "-p", "MemoryMax=3G", "--slice=claude-rc.slice", bin]);
81 c
82}
83
84fn key(parts: &[&str]) -> String {
85 let mut h = DefaultHasher::new();
86 for p in parts {
87 p.hash(&mut h);
88 }
89 format!("{:016x}", h.finish())
90}
91
92// Runs the oracle once per distinct input: the answer is kept beside the input it answers.
93fn cached(tag: &str, input: &str, run: impl Fn() -> Outcome<String>) -> Outcome<String> {
94 let dir = res!(scratch()).join("cache");
95 res!(std::fs::create_dir_all(&dir));
96 let file = dir.join(format!("{}-{}", tag, key(&[tag, input])));
97 if let Ok(s) = std::fs::read_to_string(&file) {
98 if let Some(rest) = s.strip_prefix(input) {
99 if let Some(ans) = rest.strip_prefix("\n\u{0}\n") {
100 return Ok(ans.to_string());
101 }
102 }
103 }
104 let ans = res!(run());
105 res!(std::fs::write(&file, format!("{}\n\u{0}\n{}", input, ans)));
106 Ok(ans)
107}
108
109// Typst's reprs for many expressions at once, through a document that exposes them as metadata.
110fn oracle_reprs(bin: &str, name: &str, exprs: &[&str]) -> Outcome<Vec<String>> {
111 let mut src = String::from("#metadata((\n");
112 for e in exprs {
113 src.push_str(&format!(" repr({}),\n", e));
114 }
115 src.push_str(")) <reprs>\n");
116 let file = fixtures_dir().join(format!("_oracle_{}.typ", name));
117 let json = res!(cached("reprs", &src, || {
118 res!(std::fs::write(&file, &src));
119 let out = res!(capped(bin)
120 .args(["eval", "query(<reprs>).first().value", "--in"])
121 .arg(&file)
122 .args(["--format", "json", "--root"])
123 .arg(fixtures_dir())
124 .output());
125 let _ = std::fs::remove_file(&file);
126 if !out.status.success() {
127 return Err(err!("typst failed on {}: {}", name, String::from_utf8_lossy(&out.stderr); Test));
128 }
129 Ok(String::from_utf8_lossy(&out.stdout).to_string())
130 }));
131 parse_json_strings(&json)
132}
133
134// One expression's repr, or the oracle's first error line, from `typst eval` alone.
135fn oracle_one(bin: &str, expr: &str) -> Outcome<std::result::Result<String, String>> {
136 let ans = res!(cached("one", expr, || {
137 let file = fixtures_dir().join(format!("_oracle_one_{}.typ", key(&[expr])));
138 res!(std::fs::write(&file, ""));
139 let out = res!(capped(bin)
140 .args(["eval", &format!("repr({})", expr), "--in"])
141 .arg(&file)
142 .args(["--format", "json", "--root"])
143 .arg(fixtures_dir())
144 .output());
145 let _ = std::fs::remove_file(&file);
146 if out.status.success() {
147 return Ok(format!("V[{}]", String::from_utf8_lossy(&out.stdout).trim()));
148 }
149 let err = String::from_utf8_lossy(&out.stderr);
150 Ok(err.lines().find_map(|l| l.strip_prefix("error: ").map(|s| format!("E{}", s)))
151 .unwrap_or_else(|| "E".to_string()))
152 }));
153 match ans.strip_prefix('V') {
154 Some(json) => match res!(parse_json_strings(json)).into_iter().next() {
155 Some(r) => Ok(Ok(r)),
156 None => Err(err!("no oracle value for {}", expr; Test)),
157 },
158 None => Ok(Err(ans.strip_prefix('E').unwrap_or("").to_string())),
159 }
160}
161
162// The oracle's first error line, or `None` when the expression evaluates.
163fn oracle_error(bin: &str, expr: &str) -> Outcome<Option<String>> {
164 let ans = res!(cached("error", expr, || {
165 // One file per expression: the tests run in parallel.
166 let file = fixtures_dir().join(format!("_oracle_err_{}.typ", key(&[expr])));
167 res!(std::fs::write(&file, ""));
168 let out = res!(capped(bin)
169 .args(["eval", &format!("repr({})", expr), "--in"])
170 .arg(&file)
171 .arg("--root")
172 .arg(fixtures_dir())
173 .output());
174 let _ = std::fs::remove_file(&file);
175 if out.status.success() {
176 return Ok(String::new());
177 }
178 let err = String::from_utf8_lossy(&out.stderr);
179 Ok(err.lines().find_map(|l| l.strip_prefix("error: ").map(|s| format!("E{}", s)))
180 .unwrap_or_else(|| "E".to_string()))
181 }));
182 Ok(ans.strip_prefix('E').map(|s| s.to_string()))
183}
184
185// A JSON array of strings, as `typst eval --format json` prints one.
186fn parse_json_strings(s: &str) -> Outcome<Vec<String>> {
187 let b: Vec<char> = s.trim().chars().collect();
188 let hex = |from: usize| -> Outcome<u32> {
189 let h: String = b.get(from..from + 4).map(|x| x.iter().collect()).unwrap_or_default();
190 u32::from_str_radix(&h, 16).map_err(|e| err!("bad escape {}: {}", h, e; Decode))
191 };
192 let mut out = Vec::new();
193 let mut i = 0;
194 while i < b.len() {
195 if b[i] == '"' {
196 i += 1;
197 let mut cur = String::new();
198 while i < b.len() && b[i] != '"' {
199 if b[i] == '\\' {
200 i += 1;
201 match b.get(i).copied().unwrap_or(' ') {
202 'n' => cur.push('\n'),
203 't' => cur.push('\t'),
204 'r' => cur.push('\r'),
205 'u' => {
206 let mut cp = res!(hex(i + 1));
207 i += 4;
208 if (0xD800..0xDC00).contains(&cp) {
209 let lo = res!(hex(i + 3));
210 cp = 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00);
211 i += 6;
212 }
213 cur.push(res!(char::from_u32(cp).ok_or_else(|| err!("bad codepoint {}", cp; Decode))));
214 }
215 c => cur.push(c),
216 }
217 } else {
218 cur.push(b[i]);
219 }
220 i += 1;
221 }
222 out.push(cur);
223 }
224 i += 1;
225 }
226 Ok(out)
227}
228
229/// Equal reprs, allowing floats to differ by 1e-12 relative: the platform `libm` may round an `exp` or
230/// `ln` one unit differently from the one the oracle binary links (musl), which is not a library fault.
231/// Everything that is not a decimal float must match exactly.
232fn same_repr(got: &str, want: &str) -> bool {
233 if got == want {
234 return true;
235 }
236 let (g, w) = (tokens(got), tokens(want));
237 g.len() == w.len() && g.iter().zip(w.iter()).all(|(a, b)| match (a, b) {
238 (Tok::Float(x), Tok::Float(y)) => x == y || ((x - y).abs() <= 1e-12 * x.abs().max(y.abs())),
239 (Tok::Text(x), Tok::Text(y)) => x == y,
240 _ => false,
241 })
242}
243
244enum Tok {
245 Float(f64),
246 Text(String),
247}
248
249// Splits a repr into floats (a digit run with a `.` or an exponent) and everything else.
250fn tokens(s: &str) -> Vec<Tok> {
251 let b: Vec<char> = s.chars().collect();
252 let mut out = Vec::new();
253 let mut text = String::new();
254 let mut i = 0;
255 while i < b.len() {
256 let starts = b[i].is_ascii_digit() && (i == 0 || !(b[i - 1].is_ascii_alphanumeric() || b[i - 1] == '#'));
257 if starts {
258 let mut j = i;
259 while j < b.len() && (b[j].is_ascii_digit() || b[j] == '.' || b[j] == 'e'
260 || (b[j] == '-' && j > 0 && b[j - 1] == 'e')) {
261 j += 1;
262 }
263 let num: String = b[i..j].iter().collect();
264 let is_float = num.contains('.') || num.contains('e');
265 let alpha_after = j < b.len() && b[j].is_ascii_alphabetic() && !num.contains('.');
266 if is_float && !alpha_after {
267 if let Ok(x) = num.parse::<f64>() {
268 if !text.is_empty() {
269 out.push(Tok::Text(std::mem::take(&mut text)));
270 }
271 out.push(Tok::Float(x));
272 i = j;
273 continue;
274 }
275 }
276 text.push_str(&num);
277 i = j;
278 continue;
279 }
280 text.push(b[i]);
281 i += 1;
282 }
283 if !text.is_empty() {
284 out.push(Tok::Text(text));
285 }
286 out
287}
288
289enum Line {
290 Value(String),
291 Error(String, bool), // the expression, and whether the message must match
292}
293
294fn fixture(name: &str) -> Outcome<Vec<Line>> {
295 let path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/eval/foundation").join(name);
296 let text = res!(std::fs::read_to_string(&path));
297 Ok(text.lines()
298 .map(|l| l.trim())
299 .filter(|l| !l.is_empty() && !l.starts_with("//"))
300 .map(|l| {
301 if let Some(r) = l.strip_prefix("!~") {
302 Line::Error(r.trim().to_string(), false)
303 } else if let Some(r) = l.strip_prefix('!') {
304 Line::Error(r.trim().to_string(), true)
305 } else {
306 Line::Value(l.to_string())
307 }
308 })
309 .collect())
310}
311
312/// Runs a fixture against the oracle and returns every disagreement.
313fn check(name: &str) -> Outcome<Vec<String>> {
314 let bin = match typst() {
315 Some(b) => b,
316 None => {
317 if skip_allowed() {
318 eprintln!("typst missing: skipping {} because EVAL_ORACLE_SKIP=1", name);
319 return Ok(Vec::new());
320 }
321 return Err(err!("typst is not on PATH (set TYPST, or EVAL_ORACLE_SKIP=1 to skip deliberately)"; Test));
322 }
323 };
324 let lines = res!(fixture(name));
325 let exprs: Vec<&str> = lines.iter().filter_map(|l| match l {
326 Line::Value(e) => Some(e.as_str()),
327 _ => None,
328 }).collect();
329 let mut bad = Vec::new();
330 // One document for the whole fixture; when Typst rejects it, each line is asked alone, and a line
331 // Typst rejects without a `!` mark is itself a fixture fault.
332 let expected: Vec<String> = match oracle_reprs(&bin, name.trim_end_matches(".txt"), &exprs) {
333 Ok(v) if v.len() == exprs.len() => v,
334 _ => {
335 let mut v = Vec::with_capacity(exprs.len());
336 for e in &exprs {
337 match res!(oracle_one(&bin, e)) {
338 Ok(r) => v.push(r),
339 Err(m) => {
340 bad.push(format!("{}\n typst rejects an unmarked line: {}", e, m));
341 v.push(String::from("\u{0}"));
342 }
343 }
344 }
345 v
346 }
347 };
348 for (e, want) in exprs.iter().zip(expected.iter()) {
349 if want == "\u{0}" {
350 continue;
351 }
352 match eval_line(e) {
353 Ok(v) => {
354 let got = repr(&v);
355 if !same_repr(&got, want) {
356 bad.push(format!("{}\n typst: {}\n austenite: {}", e, want, got));
357 }
358 }
359 Err(m) => bad.push(format!("{}\n typst: {}\n austenite: error: {}", e, want, m)),
360 }
361 }
362 for l in &lines {
363 if let Line::Error(e, strict) = l {
364 let want = res!(oracle_error(&bin, e));
365 let got = eval_line(e);
366 match (want, got) {
367 (None, _) => bad.push(format!("{}\n typst accepted an expression marked as an error", e)),
368 (Some(w), Ok(v)) => bad.push(format!("{}\n typst: error: {}\n austenite: {}", e, w, repr(&v))),
369 (Some(w), Err(m)) => if *strict && w.lines().next() != m.lines().next() {
370 bad.push(format!("{}\n typst: error: {}\n austenite: error: {}", e, w, m));
371 },
372 }
373 }
374 }
375 eprintln!("{}: {} expressions, {} disagreements", name, lines.len(), bad.len());
376 Ok(bad)
377}
378
379fn run(name: &str) -> Outcome<()> {
380 let bad = res!(check(name));
381 if bad.is_empty() {
382 Ok(())
383 } else {
384 Err(err!("{} disagreement(s) with typst:\n{}", bad.len(), bad.join("\n"); Test))
385 }
386}
387
388#[test] fn foundations_agree_with_typst() -> Outcome<()> { run("foundations.txt") }
389#[test] fn repr_agrees_with_typst() -> Outcome<()> { run("repr.txt") }
390#[test] fn calc_agrees_with_typst() -> Outcome<()> { run("calc.txt") }
391#[test] fn strings_agree_with_typst() -> Outcome<()> { run("string.txt") }
392#[test] fn arrays_agree_with_typst() -> Outcome<()> { run("array.txt") }
393#[test] fn dicts_agree_with_typst() -> Outcome<()> { run("dict.txt") }
394#[test] fn numbering_agrees_with_typst() -> Outcome<()> { run("numbering.txt") }
395#[test] fn colours_agree_with_typst() -> Outcome<()> { run("color.txt") }
396#[test] fn datetimes_agree_with_typst() -> Outcome<()> { run("datetime.txt") }
397#[test] fn data_agrees_with_typst() -> Outcome<()> { run("data.txt") }
398#[test] fn geometry_agrees_with_typst() -> Outcome<()> { run("geom.txt") }
399#[test] fn symbols_agree_with_typst() -> Outcome<()> { run("sym.txt") }
400#[test] fn the_math_module_agrees_with_typst() -> Outcome<()> { run("math.txt") }
401
402#[test] fn coverage_agrees_with_typst() -> Outcome<()> { run("coverage.txt") }
403#[test] fn decimals_agree_with_typst() -> Outcome<()> { run("decimal.txt") }
404#[test] fn higher_order_agrees_with_typst() -> Outcome<()> { run("higher_order.txt") }
405
406/// Every symbol and emoji Typst has, with every variant: the generated tables against the oracle.
407#[test]
408fn every_symbol_agrees_with_typst() -> Outcome<()> {
409 let bin = match typst() {
410 Some(b) => b,
411 None if skip_allowed() => return Ok(()),
412 None => return Err(err!("typst is not on PATH"; Test)),
413 };
414 let lib = library();
415 let mut exprs = Vec::new();
416 let mut got = Vec::new();
417 for m in ["sym", "emoji"] {
418 collect_symbols(&lib, m, m, &mut exprs, &mut got);
419 }
420 if exprs.len() < 1000 {
421 return Err(err!("only {} symbols collected", exprs.len(); Test));
422 }
423 let refs: Vec<&str> = exprs.iter().map(|s| s.as_str()).collect();
424 let want = res!(oracle_reprs(&bin, "symbols", &refs));
425 let bad: Vec<String> = refs.iter().zip(want.iter().zip(got.iter()))
426 .filter(|(_, (w, g))| w != g)
427 .map(|(e, (w, g))| format!("{}: typst {} austenite {}", e, w, g))
428 .collect();
429 eprintln!("symbols: {} checked, {} differ", refs.len(), bad.len());
430 if bad.is_empty() {
431 Ok(())
432 } else {
433 Err(err!("{} symbol(s) differ:\n{}", bad.len(), bad.join("\n"); Test))
434 }
435}
436
437fn collect_symbols(lib: &Scope, root: &str, path: &str, exprs: &mut Vec<String>, got: &mut Vec<String>) {
438 let mut v = lib.get(root).cloned();
439 for p in path.split('.').skip(1) {
440 v = v.and_then(|m| match m {
441 Value::Module(m) => m.scope.get(p).cloned(),
442 _ => None,
443 });
444 }
445 if let Some(Value::Module(m)) = v {
446 let mut names: Vec<&String> = m.scope.map.keys().collect();
447 names.sort();
448 for n in names {
449 let p = format!("{}.{}", path, n);
450 match m.scope.get(n) {
451 Some(Value::Module(_)) => collect_symbols(lib, root, &p, exprs, got),
452 Some(s) => {
453 exprs.push(p);
454 got.push(repr(s));
455 }
456 None => (),
457 }
458 }
459 }
460}
461
462/// The harness can fail: the comparison sees a value and an error message, not just "something ran".
463#[test]
464fn harness_reads_values_and_errors() -> Outcome<()> {
465 let v = match eval_line("calc.pow(2, 10)") {
466 Ok(v) => v,
467 Err(m) => return Err(err!("calc.pow failed: {}", m; Test)),
468 };
469 if repr(&v) != "1024" {
470 return Err(err!("calc.pow(2, 10) gave {}", repr(&v); Test));
471 }
472 match eval_line("calc.pow(0, 0)") {
473 Err(m) if m == "zero to the power of zero is undefined" => Ok(()),
474 other => Err(err!("calc.pow(0, 0) gave {:?}", other.map(|v| repr(&v)); Test)),
475 }
476}
477
478/// CMYK to RGB is an ICC transform in Typst, reproduced here by interpolating a grid sampled from the
479/// oracle: exact on the grid, and within two eight-bit steps per channel between its points. The sample is
480/// fixed pseudo-random ink values, so the bound is tested off the grid, where it can fail.
481#[test]
482fn cmyk_conversion_stays_within_two_steps() -> Outcome<()> {
483 let bin = match typst() {
484 Some(b) => b,
485 None if skip_allowed() => return Ok(()),
486 None => return Err(err!("typst is not on PATH"; Test)),
487 };
488 let mut seed: u32 = 0x2545_f491;
489 let mut exprs = Vec::new();
490 for _ in 0..60 {
491 let mut ink = [0u32; 4];
492 for x in ink.iter_mut() {
493 seed ^= seed << 13;
494 seed ^= seed >> 17;
495 seed ^= seed << 5;
496 *x = seed % 101;
497 }
498 exprs.push(format!("rgb(cmyk({}%, {}%, {}%, {}%)).to-hex()", ink[0], ink[1], ink[2], ink[3]));
499 }
500 let refs: Vec<&str> = exprs.iter().map(|s| s.as_str()).collect();
501 let want = res!(oracle_reprs(&bin, "cmyk", &refs));
502 let (mut exact, mut bad) = (0, Vec::new());
503 for (e, w) in refs.iter().zip(want.iter()) {
504 let got = match eval_line(e) {
505 Ok(v) => repr(&v),
506 Err(m) => return Err(err!("{}: {}", e, m; Test)),
507 };
508 let chan = |s: &str, i: usize| i64::from_str_radix(s.get(2 + 2 * i..4 + 2 * i).unwrap_or("0"), 16).unwrap_or(-99);
509 let worst = (0..3).map(|i| (chan(&got, i) - chan(w, i)).abs()).max().unwrap_or(0);
510 if worst == 0 {
511 exact += 1;
512 }
513 if worst > 2 {
514 bad.push(format!("{}: typst {} austenite {}", e, w, got));
515 }
516 }
517 eprintln!("cmyk: {} of {} exact", exact, refs.len());
518 if bad.is_empty() {
519 Ok(())
520 } else {
521 Err(err!("{} conversion(s) off by more than two steps:\n{}", bad.len(), bad.join("\n"); Test))
522 }
523}
524
525/// `Color::to_rgba`, the conversion every fill and stroke takes to the back end, gives the eight-bit
526/// sRGB Typst's `to-hex` gives, in every colour space and with alpha.
527#[test]
528fn to_rgba_agrees_with_typst_hex() -> Outcome<()> {
529 let bin = match typst() {
530 Some(b) => b,
531 None if skip_allowed() => return Ok(()),
532 None => return Err(err!("typst is not on PATH"; Test)),
533 };
534 let colours = [
535 "red", "blue", "eastern", "gray", "silver", "black", "white", "lime", "rgb(10%, 20%, 30%)",
536 "rgb(\"#12345678\")", "rgb(1, 2, 3, 40%)", "luma(40%)", "luma(40%, 50%)", "oklab(60%, 0.1, -0.1)",
537 "oklch(70%, 0.1, 200deg)", "color.linear-rgb(10%, 50%, 90%)", "color.hsl(200deg, 40%, 60%)",
538 "color.hsv(20deg, 60%, 70%, 30%)", "cmyk(0%, 0%, 0%, 0%)", "cmyk(100%, 0%, 0%, 0%)",
539 "red.lighten(20%)", "blue.transparentize(50%)", "color.mix(red, blue)",
540 ];
541 let exprs: Vec<String> = colours.iter().map(|c| format!("({}).to-hex()", c)).collect();
542 let refs: Vec<&str> = exprs.iter().map(|s| s.as_str()).collect();
543 let want = res!(oracle_reprs(&bin, "to_rgba", &refs));
544 let mut bad = Vec::new();
545 for (c, w) in colours.iter().zip(want.iter()) {
546 let v = match eval_line(c) {
547 Ok(Value::Color(v)) => v,
548 Ok(other) => return Err(err!("{} is not a colour: {}", c, repr(&other); Test)),
549 Err(m) => return Err(err!("{}: {}", c, m; Test)),
550 };
551 let p = res!(v.to_rgba());
552 let hex = if p.a == 255 {
553 format!("\"#{:02x}{:02x}{:02x}\"", p.r, p.g, p.b)
554 } else {
555 format!("\"#{:02x}{:02x}{:02x}{:02x}\"", p.r, p.g, p.b, p.a)
556 };
557 if &hex != w {
558 bad.push(format!("{}: typst {} to_rgba {}", c, w, hex));
559 }
560 }
561 if bad.is_empty() {
562 Ok(())
563 } else {
564 Err(err!("{} colour(s) differ:\n{}", bad.len(), bad.join("\n"); Test))
565 }
566}
567
568/// `numbering::apply_trimmed` is what a reference shows of a heading's number, and `apply_kth` what an
569/// enumeration item shows at its depth. Typst's rendered text, through `pdftotext`, is the oracle.
570#[test]
571fn numbering_trimmed_and_kth_agree_with_typst() -> Outcome<()> {
572 use oxedyne_fe2o3_austenite::eval::lib::numbering;
573 let bin = match typst() {
574 Some(b) => b,
575 None if skip_allowed() => return Ok(()),
576 None => return Err(err!("typst is not on PATH"; Test)),
577 };
578 let dir = res!(scratch());
579 let (src, pdf) = (dir.join("numbering.typ"), dir.join("numbering.pdf"));
580 res!(std::fs::write(&src, concat!(
581 "#set page(width: 12cm, height: auto)\n#set heading(numbering: \"(I.a)\")\n= Alpha\n",
582 "== Beta <b>\nRef: @b\n#set enum(numbering: \"1.a.i)\")\n+ one\n + two\n + three\n + four\n")));
583 let out = res!(capped(&bin).arg("compile").arg(&src).arg(&pdf).output());
584 if !out.status.success() {
585 return Err(err!("typst compile failed: {}", String::from_utf8_lossy(&out.stderr); Test));
586 }
587 let text = res!(Command::new("pdftotext").args(["-layout"]).arg(&pdf).arg("-").output());
588 let lines: Vec<String> = String::from_utf8_lossy(&text.stdout).lines()
589 .map(|l| l.split_whitespace().collect::<Vec<_>>().join(" ")).filter(|l| !l.is_empty()).collect();
590 let mut e = Engine::new(World::new(fixtures_dir()));
591 let pat = Value::str("(I.a)");
592 let mut want = vec![
593 format!("({}) Alpha", "I"),
594 format!("{} Beta", repr(&res!(numbering::apply(&mut e, &pat, &[1, 1])))),
595 format!("Ref: Section {}", repr(&res!(numbering::apply_trimmed(&mut e, &pat, &[1, 1])))),
596 ];
597 let items = Value::str("1.a.i)");
598 for (k, word) in ["one", "two", "three", "four"].iter().enumerate() {
599 want.push(format!("{} {}", repr(&res!(numbering::apply_kth(&mut e, &items, k, 1))), word));
600 }
601 // The reprs carry quotes; the rendered text does not.
602 let want: Vec<String> = want.iter().map(|w| w.replace('"', "")).collect();
603 if lines != want {
604 return Err(err!("typst rendered {:?}, austenite gives {:?}", lines, want; Test));
605 }
606 Ok(())
607}
608
609/// The ink a colour value reaches the drawing layer as, and the colour's own sRGB lowering.
610fn ink_of(src: &str) -> Outcome<(Ink, Rgba)> {
611 match eval_line(src) {
612 Ok(Value::Color(c)) => Ok((c.to_ink(), res!(c.to_rgba()))),
613 Ok(other) => Err(err!("{} gave {}, not a colour", src, repr(&other); Test)),
614 Err(m) => Err(err!("{}: {}", src, m; Test)),
615 }
616}
617
618/// A colour keeps the space it was written in when it reaches the drawing layer: `luma` is a grey, `cmyk` a
619/// CMYK, and every other space eight-bit sRGB; whichever it is, the ink lowers to the colour's own RGBA.
620#[test]
621fn a_colour_lowers_to_the_ink_of_the_space_it_was_written_in() -> Outcome<()> {
622 let exact: [(&str, Ink); 8] = [
623 ("luma(50%)", Ink::Grey { v: 0.5, a: 255 }),
624 ("luma(50%, 25%)", Ink::Grey { v: 0.5, a: 64 }),
625 ("black", Ink::Grey { v: 0.0, a: 255 }),
626 ("white", Ink::Grey { v: 1.0, a: 255 }),
627 ("cmyk(0%, 50%, 100%, 0%)", Ink::Cmyk { c: 0.0, m: 0.5, y: 1.0, k: 0.0, a: 255 }),
628 ("rgb(\"#ff0000\")", Ink::Rgb(Rgba::new(255, 0, 0, 255))),
629 ("rgb(10, 20, 30, 40)", Ink::Rgb(Rgba::new(10, 20, 30, 40))),
630 ("rgb(0, 0, 0)", Ink::Rgb(Rgba::new(0, 0, 0, 255))),
631 ];
632 for (src, want) in exact {
633 let (ink, rgba) = res!(ink_of(src));
634 if ink != want {
635 return Err(err!("{} lowered to {:?}, expected {:?}", src, ink, want; Test));
636 }
637 if ink.to_rgba() != rgba {
638 return Err(err!("{}: the ink gives {:?}, the colour {:?}", src, ink.to_rgba(), rgba; Test));
639 }
640 }
641 // The rest are written in a space the page does not keep, so each is an RGB ink of the colour's own RGBA.
642 for src in [
643 "oklch(70%, 0.1, 40deg)",
644 "color.hsl(120deg, 50%, 50%)",
645 "color.hsv(200deg, 40%, 80%, 50%)",
646 "color.linear-rgb(20%, 40%, 60%)",
647 "color.oklab(60%, 0.1, -0.05)",
648 "oklch(70%, 0.1, 40deg).transparentize(40%)",
649 ] {
650 let (ink, rgba) = res!(ink_of(src));
651 match ink {
652 Ink::Rgb(c) if c == rgba => (),
653 other => return Err(err!("{} lowered to {:?}, expected an RGB ink of {:?}", src, other, rgba; Test)),
654 }
655 }
656 // Grey 0 and RGB 0 lower to one colour yet are two inks, which a fingerprint must tell apart.
657 let (grey, g) = res!(ink_of("luma(0%)"));
658 let (rgb, r) = res!(ink_of("rgb(0, 0, 0)"));
659 if g != r || grey == rgb {
660 return Err(err!("luma(0%) and rgb(0, 0, 0): colours {:?} {:?}, inks {:?} {:?}", g, r, grey, rgb; Test));
661 }
662 Ok(())
663}