oxedyne/fe2o3/fe2o3_austenite/tests/eval_survey.rs
25.3 KiB, 5 runs
created by r1870400018:66901, 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 | //! Survey fixes (DEV, D-20261004-08): constructs a survey of the document trees found the evaluator |
| 2 | //! refusing while `typst` 0.15.1 builds them. Typst is asked first and must compile each source; the door |
| 3 | //! must then compile what Typst compiled, to the same page count and the same text, or, where Typst |
| 4 | //! refuses, refuse at Typst's site with Typst's message. Every expectation comes from the oracle at run |
| 5 | //! time, never from Austenite's own output. |
| 6 | //! |
| 7 | //! The door's columns are 1-based UTF-16 code units, Typst's are 0-based characters. |
| 8 | |
| 9 | #[allow(dead_code)] |
| 10 | #[path = "eval_oracle/mod.rs"] |
| 11 | mod harness; |
| 12 | |
| 13 | use harness::pdf::{ |
| 14 | self, |
| 15 | tool, |
| 16 | TYPST, |
| 17 | }; |
| 18 | |
| 19 | use oxedyne_fe2o3_austenite::compile::{ |
| 20 | supply_typst_package_cache, |
| 21 | DiagnosticKind, |
| 22 | }; |
| 23 | use oxedyne_fe2o3_austenite::door::{ |
| 24 | Failure, |
| 25 | Instance, |
| 26 | Project, |
| 27 | LOOP_BUDGET, |
| 28 | }; |
| 29 | use oxedyne_fe2o3_austenite::eval::content::{ |
| 30 | ElemKind, |
| 31 | FieldType, |
| 32 | }; |
| 33 | |
| 34 | use oxedyne_fe2o3_core::prelude::*; |
| 35 | |
| 36 | use std::path::{ |
| 37 | Path, |
| 38 | PathBuf, |
| 39 | }; |
| 40 | use std::process::Command; |
| 41 | |
| 42 | fn work_dir(name: &str) -> Outcome<PathBuf> { pdf::work_dir(&fmt!("eval-survey-{}", name)) } |
| 43 | |
| 44 | /// Typst's run with the cap every oracle run is given: whether it compiled, and its standard error. |
| 45 | fn typst(main: &Path, pdf: &Path) -> Outcome<(bool, String)> { |
| 46 | let out = res!(Command::new("systemd-run") |
| 47 | .args(["--user", "--scope", "--quiet", "-p", "MemoryMax=3G", "--slice=claude-rc.slice", TYPST]) |
| 48 | .args(["compile", "--diagnostic-format", "short"]) |
| 49 | .arg(main) |
| 50 | .arg(pdf) |
| 51 | .output()); |
| 52 | Ok((out.status.success(), String::from_utf8_lossy(&out.stderr).to_string())) |
| 53 | } |
| 54 | |
| 55 | fn pages_of(pdf: &Path) -> Outcome<usize> { |
| 56 | let info = res!(tool("pdfinfo", &[], pdf, &[])); |
| 57 | let line = res!(info.lines().find(|l| l.starts_with("Pages:")).ok_or_else(|| err!("pdfinfo gave no page count"; Test))); |
| 58 | Ok(res!(line.trim_start_matches("Pages:").trim().parse::<usize>().map_err(|e| err!("{}", e; Test)))) |
| 59 | } |
| 60 | |
| 61 | /// The PDF's text with every space gone, so a thin space between two glyphs is not a difference. |
| 62 | fn glyphs_of(pdf: &Path) -> Outcome<String> { |
| 63 | Ok(res!(pdf::text_of(pdf)).chars().filter(|c| !c.is_whitespace()).collect()) |
| 64 | } |
| 65 | |
| 66 | /// Writes `src` as `main.typ` under a case directory and returns its path. |
| 67 | fn write_main(dir: &Path, name: &str, src: &str) -> Outcome<PathBuf> { |
| 68 | let case = dir.join(name); |
| 69 | res!(std::fs::create_dir_all(&case)); |
| 70 | let main = case.join("main.typ"); |
| 71 | res!(std::fs::write(&main, src)); |
| 72 | Ok(main) |
| 73 | } |
| 74 | |
| 75 | /// Compiles `src` under Typst and through the door, not strict, and asserts they agree on the pages and the |
| 76 | /// text. Returns the text. |
| 77 | fn builds_alike(dir: &Path, name: &str, src: &str) -> Outcome<String> { |
| 78 | builds_alike_with(dir, name, src, Instance::new()) |
| 79 | } |
| 80 | |
| 81 | /// As [`builds_alike`], through the door instance given, whose host options the caller has set. |
| 82 | fn builds_alike_with(dir: &Path, name: &str, src: &str, mut inst: Instance) -> Outcome<String> { |
| 83 | let main = res!(write_main(dir, name, src)); |
| 84 | let want = main.with_extension("pdf"); |
| 85 | let (ok, stderr) = res!(typst(&main, &want)); |
| 86 | assert!(ok, "typst refuses {}: {}", name, stderr); |
| 87 | let made = match inst.compile_pdf(&Project::single(src)) { |
| 88 | Ok(m) => m, |
| 89 | Err(f) => return Err(err!("The door refused {}, which typst builds: {} {:?}", name, f.head, f.head.kind; Test)), |
| 90 | }; |
| 91 | let ours = main.with_file_name("ours.pdf"); |
| 92 | res!(std::fs::write(&ours, made.product.to_vec())); |
| 93 | assert_eq!(res!(pages_of(&ours)), res!(pages_of(&want)), "{}: pages", name); |
| 94 | let text = res!(glyphs_of(&want)); |
| 95 | assert_eq!(res!(glyphs_of(&ours)), text, "{}: text", name); |
| 96 | Ok(text) |
| 97 | } |
| 98 | |
| 99 | /// Typst's first error: its message, and its line and 0-based column. |
| 100 | fn typst_error(dir: &Path, name: &str, src: &str) -> Outcome<(String, usize, usize)> { |
| 101 | let main = res!(write_main(dir, name, src)); |
| 102 | let (ok, stderr) = res!(typst(&main, &main.with_extension("pdf"))); |
| 103 | assert!(!ok, "typst builds {}, which should be refused", name); |
| 104 | for l in stderr.lines() { |
| 105 | if let Some(at) = l.find(": error: ") { |
| 106 | let message = l[at + ": error: ".len()..].to_string(); |
| 107 | let mut parts = l[..at].rsplitn(3, ':'); |
| 108 | let col = parts.next().and_then(|c| c.parse::<usize>().ok()); |
| 109 | let line = parts.next().and_then(|c| c.parse::<usize>().ok()); |
| 110 | if let (Some(line), Some(col)) = (line, col) { |
| 111 | return Ok((message, line, col)); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | Err(err!("Typst gave no error site for {}: {}", name, stderr; Test)) |
| 116 | } |
| 117 | |
| 118 | /// The column the door gives for Typst's 0-based character column `col` on `line` of `text`. |
| 119 | fn door_col(text: &str, line: usize, col: usize) -> usize { |
| 120 | let row = text.split('\n').nth(line - 1).unwrap_or(""); |
| 121 | let before: usize = row.chars().take(col).map(|c| c.len_utf16()).sum(); |
| 122 | before + 1 |
| 123 | } |
| 124 | |
| 125 | /// Asserts the door refuses `src` at Typst's site with Typst's message, and returns the failure. |
| 126 | fn refuses_alike(dir: &Path, name: &str, src: &str) -> Outcome<Failure> { |
| 127 | let (message, line, col) = res!(typst_error(dir, name, src)); |
| 128 | let mut inst = Instance::new(); |
| 129 | let f = match inst.compile_pdf(&Project::single(src)) { |
| 130 | Ok(_) => return Err(err!("The door builds {}, which typst refuses with: {}", name, message; Test)), |
| 131 | Err(f) => f, |
| 132 | }; |
| 133 | assert_eq!(f.head.message, message, "{}: the message", name); |
| 134 | assert_eq!((f.head.line, f.head.col), (line, door_col(src, line, col)), "{}: the site", name); |
| 135 | Ok(f) |
| 136 | } |
| 137 | |
| 138 | /// As [`refuses_alike`] for the message alone: Typst reports a method's wrong argument at the argument, which |
| 139 | /// the door has no span for, so it names the call. |
| 140 | fn refuses_message_alike(dir: &Path, name: &str, src: &str) -> Outcome<Failure> { |
| 141 | let (message, _, _) = res!(typst_error(dir, name, src)); |
| 142 | let mut inst = Instance::new(); |
| 143 | let f = match inst.compile_pdf(&Project::single(src)) { |
| 144 | Ok(_) => return Err(err!("The door builds {}, which typst refuses with: {}", name, message; Test)), |
| 145 | Err(f) => f, |
| 146 | }; |
| 147 | assert_eq!(f.head.message, message, "{}: the message", name); |
| 148 | Ok(f) |
| 149 | } |
| 150 | |
| 151 | // Maths: `floor` and `ceil` are symbols |
| 152 | |
| 153 | // The sources are the shapes a document writes: the delimiter as a variant of the symbol, called, set |
| 154 | // beside its partner, and the symbol alone. |
| 155 | const FLOORS: [(&str, &str); 6] = [ |
| 156 | ("variant-called", "$ T = floor.l(2^256 dot n slash N) $\n"), |
| 157 | ("both-sides", "$ floor.l a floor.r + ceil.l b ceil.r $\n"), |
| 158 | ("right-called", "$ floor.r(x) + ceil.r(y) $\n"), |
| 159 | ("alone", "$floor$ and $ceil$ and $floor.l$ and $ceil.r$\n"), |
| 160 | ("mixed-with-calls", "$ floor(x) + floor.l(y) + ceil(z) + ceil.l(w) + round(v) $\n"), |
| 161 | ("in-code", "#math.floor.l($x$) #math.ceil.l($y$) #sym.floor.r #sym.ceil.l\n"), |
| 162 | ]; |
| 163 | |
| 164 | #[test] |
| 165 | fn floor_and_ceil_are_symbols_with_left_and_right_variants_in_maths_as_typst_has_them() -> Outcome<()> { |
| 166 | let dir = res!(work_dir("floor")); |
| 167 | for (name, src) in FLOORS { |
| 168 | res!(builds_alike(&dir, name, src)); |
| 169 | } |
| 170 | // A symbol, not a function: its type and its variants are what `typst` reports. |
| 171 | let ty = res!(builds_alike(&dir, "type-of", "#type(math.floor) #type(math.ceil) #repr(math.floor) #repr(math.ceil.r)\n")); |
| 172 | assert!(ty.starts_with("symbolsymbol"), "typst's own answer: {}", ty); |
| 173 | Ok(()) |
| 174 | } |
| 175 | |
| 176 | #[test] |
| 177 | fn a_function_of_maths_that_has_no_variants_still_refuses_one() -> Outcome<()> { |
| 178 | let dir = res!(work_dir("variants")); |
| 179 | // `round` and `abs` are functions in Typst, so the field is not found there. |
| 180 | for (name, src) in [ |
| 181 | ("round-l", "$round.l$\n"), |
| 182 | ("abs-l", "$abs.l$\n"), |
| 183 | ("modifier", "$floor.l.double$\n"), |
| 184 | ] { |
| 185 | let f = res!(refuses_alike(&dir, name, src)); |
| 186 | assert!(!f.head.message.is_empty(), "{}", name); |
| 187 | } |
| 188 | Ok(()) |
| 189 | } |
| 190 | |
| 191 | // Code: a symbol is callable where it is an accent or an opening delimiter |
| 192 | |
| 193 | #[test] |
| 194 | fn a_symbol_is_called_in_code_where_it_is_an_accent_or_an_opening_delimiter() -> Outcome<()> { |
| 195 | let dir = res!(work_dir("call")); |
| 196 | for (name, src) in [ |
| 197 | ("floor", "#sym.floor($x$) #sym.floor.l($y$) #math.ceil($z$)\n"), |
| 198 | ("paren", "#sym.paren.l($x$) #sym.bracket.l($y$)\n"), |
| 199 | ("accent", "#sym.acute($x$) #sym.arrow.r($y$)\n"), |
| 200 | ("bound", "#let f = sym.floor\n#f($x$)\n"), |
| 201 | ] { |
| 202 | res!(builds_alike(&dir, name, src)); |
| 203 | } |
| 204 | for (name, src) in [ |
| 205 | ("letter", "#sym.alpha($x$)\n"), |
| 206 | ("closing", "#sym.floor.r($x$)\n"), |
| 207 | ("plain", "#sym.plus($x$)\n"), |
| 208 | ] { |
| 209 | let f = res!(refuses_alike(&dir, name, src)); |
| 210 | assert!(f.head.message.ends_with("is not callable"), "{}: {}", name, f.head.message); |
| 211 | } |
| 212 | Ok(()) |
| 213 | } |
| 214 | |
| 215 | // Functions: a callable symbol is a function wherever a function is expected |
| 216 | |
| 217 | // A symbol that is an accent or an opening delimiter casts to the function it calls as, so a document hands |
| 218 | // `math.floor` or `sym.paren.l` to `map` as it hands any function. |
| 219 | const SYMBOL_FUNCS: [(&str, &str); 8] = [ |
| 220 | ("floor-mapped", "#([a], [b]).map(math.floor).join()\n"), |
| 221 | ("ceil-mapped", "#([a], [b]).map(math.ceil).join()\n"), |
| 222 | ("variant-mapped", "#([a], [b]).map(math.floor.l).join() #([a], [b]).map(sym.ceil.l).join()\n"), |
| 223 | ("delimiter-mapped", "#([a], [b]).map(sym.paren.l).join() #([a], [b]).map(sym.bracket.l).join()\n"), |
| 224 | ("accent-mapped", "#([a], [b]).map(sym.hat).join()\n"), |
| 225 | ("key-of-dedup", "#([a], [b], [a]).dedup(key: sym.paren.l).len()\n"), |
| 226 | ("in-maths", "$#([a], [b]).map(math.ceil).join()$ $#([a], [b]).map(sym.paren.l).join()$\n"), |
| 227 | ("bound-first", "#let g = math.floor\n#([a], [b]).map(g).join()\n"), |
| 228 | ]; |
| 229 | |
| 230 | #[test] |
| 231 | fn a_callable_symbol_is_a_function_where_a_function_is_expected_as_typst_casts_it() -> Outcome<()> { |
| 232 | let dir = res!(work_dir("symbol-funcs")); |
| 233 | for (name, src) in SYMBOL_FUNCS { |
| 234 | res!(builds_alike(&dir, name, src)); |
| 235 | } |
| 236 | Ok(()) |
| 237 | } |
| 238 | |
| 239 | #[test] |
| 240 | fn a_symbol_that_is_not_callable_is_refused_as_a_function_with_typsts_message() -> Outcome<()> { |
| 241 | let dir = res!(work_dir("symbol-not-callable")); |
| 242 | // Typst reports each at the argument, which the door has no span for, so it names the call. |
| 243 | for (name, src) in [ |
| 244 | ("map", "#(1, 2).map(sym.alpha)\n"), |
| 245 | ("filter", "#(1, 2).filter(sym.alpha)\n"), |
| 246 | ("any", "#(1, 2).any(sym.alpha)\n"), |
| 247 | ("fold", "#(1, 2).fold(0, sym.alpha)\n"), |
| 248 | ("reduce", "#(1, 2).reduce(sym.alpha)\n"), |
| 249 | ("sorted-key", "#(1, 2).sorted(key: sym.alpha)\n"), |
| 250 | ("dedup-key", "#(1, 2).dedup(key: sym.alpha)\n"), |
| 251 | ("closing", "#(1, 2).map(sym.floor.r)\n"), |
| 252 | ("plain", "#(1, 2).map(sym.plus)\n"), |
| 253 | ] { |
| 254 | let f = res!(refuses_message_alike(&dir, name, src)); |
| 255 | assert!(f.head.message.starts_with("symbol "), "{}: {}", name, f.head.message); |
| 256 | assert!(f.head.message.ends_with(" is not callable"), "{}: {}", name, f.head.message); |
| 257 | } |
| 258 | Ok(()) |
| 259 | } |
| 260 | |
| 261 | // Keywords: a parameter that takes a closed set of strings takes a string alone |
| 262 | |
| 263 | // Each is a parameter Typst types as a keyword: `@` is the value given. A symbol is never cast to the |
| 264 | // string it stands for here, as it is where a parameter is a `str`, so Typst refuses it by naming what it |
| 265 | // found, as it names any other type; a string outside the set is named without what was found. |
| 266 | const KEYWORDS: [(&str, &str); 28] = [ |
| 267 | ("text-style", "#text(style: @)[x]\n"), |
| 268 | ("text-weight", "#text(weight: @)[x]\n"), |
| 269 | ("text-top-edge", "#text(top-edge: @)[x]\n"), |
| 270 | ("text-bottom-edge", "#text(bottom-edge: @)[x]\n"), |
| 271 | ("text-number-type", "#text(number-type: @)[x]\n"), |
| 272 | ("text-number-width", "#text(number-width: @)[x]\n"), |
| 273 | ("set-text-weight", "#set text(weight: @)\nx\n"), |
| 274 | ("set-text-style", "#set text(style: @)\nx\n"), |
| 275 | ("highlight-top-edge", "#highlight(top-edge: @)[x]\n"), |
| 276 | ("highlight-bottom-edge", "#highlight(bottom-edge: @)[x]\n"), |
| 277 | ("frac-style", "#math.frac([a], [b], style: @)\n"), |
| 278 | ("math-class", "#math.class(@, [x])\n"), |
| 279 | ("place-scope", "#place(scope: @)[x]\n"), |
| 280 | ("figure-scope", "#figure([a], scope: @)\n"), |
| 281 | ("page-paper", "#set page(paper: @)\n"), |
| 282 | ("pagebreak-to", "#pagebreak(to: @)\n"), |
| 283 | ("curve-close-mode", "#curve(curve.move((0pt, 0pt)), curve.close(mode: @))\n"), |
| 284 | ("curve-fill-rule", "#curve(fill-rule: @, curve.line((1pt, 1pt)))\n"), |
| 285 | ("polygon-fill-rule", "#polygon(fill-rule: @, (0pt, 0pt), (1pt, 1pt), (0pt, 2pt))\n"), |
| 286 | ("stroke-cap", "#line(stroke: (cap: @))\n"), |
| 287 | ("stroke-join", "#line(stroke: (join: @))\n"), |
| 288 | ("gradient-relative", "#gradient.linear(red, blue, relative: @)\n"), |
| 289 | ("par-linebreaks", "#set par(linebreaks: @)\n"), |
| 290 | ("par-line-scope", "#set par.line(numbering-scope: @)\n"), |
| 291 | ("ref-form", "#ref(<a>, form: @)\n"), |
| 292 | ("image-format", "#image(\"x.svg\", format: @)\n"), |
| 293 | ("image-fit", "#image(\"x.svg\", fit: @)\n"), |
| 294 | ("image-scaling", "#image(\"x.svg\", scaling: @)\n"), |
| 295 | ]; |
| 296 | |
| 297 | #[test] |
| 298 | fn a_keyword_parameter_refuses_a_symbol_and_any_other_type_with_typsts_message() -> Outcome<()> { |
| 299 | // One SVG, `x.svg`, for the `image` cases: written beside Typst's copy and supplied as the project's own. |
| 300 | const SVG: &str = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"4\" height=\"4\"><rect width=\"4\" height=\"4\"/></svg>"; |
| 301 | let dir = res!(work_dir("keywords")); |
| 302 | let mut bad: Vec<String> = Vec::new(); |
| 303 | let mut cases = 0; |
| 304 | for (name, template) in KEYWORDS { |
| 305 | for (given, found) in [("sym.alpha", "symbol"), ("true", "boolean")] { |
| 306 | cases += 1; |
| 307 | let case = fmt!("{}-{}", name, found); |
| 308 | let src = template.replace('@', given); |
| 309 | let main = res!(write_main(&dir, &case, &src)); |
| 310 | res!(std::fs::write(main.with_file_name("x.svg"), SVG)); |
| 311 | let (want, _, _) = res!(typst_error(&dir, &case, &src)); |
| 312 | let mut project = Project::single(&src); |
| 313 | project.assets.push(("/x.svg".to_string(), SVG.as_bytes().to_vec())); |
| 314 | let mut inst = Instance::new(); |
| 315 | let got = match inst.compile_pdf(&project) { |
| 316 | Ok(_) => "it builds".to_string(), |
| 317 | Err(f) => f.head.message, |
| 318 | }; |
| 319 | if got != want || !want.ends_with(&fmt!(", found {}", found)) { |
| 320 | bad.push(fmt!("{}: typst says `{}`, the door `{}`", case, want, got)); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | assert!(bad.is_empty(), "{} of {} keyword cases differ:\n{}", bad.len(), cases, bad.join("\n")); |
| 325 | Ok(()) |
| 326 | } |
| 327 | |
| 328 | // The fields the schema types as keywords, each one in `KEYWORDS` above: a field typed so and left out |
| 329 | // is not held to Typst, and one listed here that is no longer a keyword is a stale line. |
| 330 | const KEYWORD_FIELDS: [&str; 11] = [ |
| 331 | "text.style", "text.weight", "text.top-edge", "text.bottom-edge", "text.number-type", "text.number-width", |
| 332 | "highlight.top-edge", "highlight.bottom-edge", "math.frac.style", "math.class.class", "place.scope", |
| 333 | ]; |
| 334 | |
| 335 | #[test] |
| 336 | fn every_field_the_schema_types_as_a_keyword_is_held_to_typst_above() -> Outcome<()> { |
| 337 | let mut have: Vec<String> = Vec::new(); |
| 338 | for kind in ElemKind::ALL { |
| 339 | for spec in kind.fields() { |
| 340 | if matches!(spec.ty, FieldType::Keyword(..)) { |
| 341 | have.push(fmt!("{}.{}", kind.path(), spec.name)); |
| 342 | } |
| 343 | } |
| 344 | } |
| 345 | have.sort(); |
| 346 | have.dedup(); |
| 347 | let mut want: Vec<String> = KEYWORD_FIELDS.iter().map(|s| s.to_string()).collect(); |
| 348 | want.sort(); |
| 349 | want.dedup(); |
| 350 | assert_eq!(have, want, "the schema's keyword fields and the test's list differ"); |
| 351 | Ok(()) |
| 352 | } |
| 353 | |
| 354 | // Loops: Typst bounds one `while`, not the document |
| 355 | |
| 356 | // Eleven million empty iterations and a body that ends at once: more than the ten million the evaluator |
| 357 | // once allowed across a whole compile, as the plot of a technical document spends them. |
| 358 | const MANY: &str = "#{ for i in range(3200) { for j in range(3400) { } }; [done] }\n"; |
| 359 | |
| 360 | // A host that can stop a compile itself declares so by setting no budget. |
| 361 | #[test] |
| 362 | fn a_document_may_run_more_loop_iterations_than_ten_million_as_typst_lets_it() -> Outcome<()> { |
| 363 | let dir = res!(work_dir("fuel")); |
| 364 | let mut inst = Instance::new(); |
| 365 | inst.set_loop_budget(None); |
| 366 | let text = res!(builds_alike_with(&dir, "many", MANY, inst)); |
| 367 | assert_eq!(text, "done"); |
| 368 | Ok(()) |
| 369 | } |
| 370 | |
| 371 | // A host cannot stop a compile that has begun (Daimond's runs on the page's own thread), so the door bounds |
| 372 | // the loops of one unless the host says it can stop it. The command line has no bound, as Typst has none. |
| 373 | #[test] |
| 374 | fn the_door_refuses_a_document_that_spends_more_loop_iterations_than_its_default_budget() -> Outcome<()> { |
| 375 | let mut inst = Instance::new(); |
| 376 | let f = match inst.compile_pdf(&Project::single(MANY)) { |
| 377 | Ok(_) => return Err(err!("The door builds eleven million loop iterations, past its default budget"; Test)), |
| 378 | Err(f) => f, |
| 379 | }; |
| 380 | assert_eq!(f.head.kind, DiagnosticKind::Limit); |
| 381 | assert_eq!(f.head.message, "loop seems to be infinite"); |
| 382 | Ok(()) |
| 383 | } |
| 384 | |
| 385 | #[test] |
| 386 | fn a_host_sets_the_loop_budget_to_a_number_or_to_none_and_a_small_one_refuses_earlier() -> Outcome<()> { |
| 387 | assert_eq!(LOOP_BUDGET, 10_000_000); |
| 388 | assert_eq!(Instance::new().loop_budget(), Some(LOOP_BUDGET)); |
| 389 | let dir = res!(work_dir("budget-host")); |
| 390 | // Five thousand iterations of an empty loop, and the range it walks, spend ten thousand of the budget. |
| 391 | let few = "#{ for i in range(5000) { }; [done] }\n"; |
| 392 | let mut small = Instance::new(); |
| 393 | small.set_loop_budget(Some(1_000)); |
| 394 | assert_eq!(small.loop_budget(), Some(1_000)); |
| 395 | let f = match small.compile_pdf(&Project::single(few)) { |
| 396 | Ok(_) => return Err(err!("A budget of a thousand builds a document of five thousand iterations"; Test)), |
| 397 | Err(f) => f, |
| 398 | }; |
| 399 | assert_eq!(f.head.kind, DiagnosticKind::Limit); |
| 400 | assert_eq!(f.head.message, "loop seems to be infinite"); |
| 401 | // The default allows it, as does a budget the host raises or removes. |
| 402 | for (name, budget) in [("default", Some(LOOP_BUDGET)), ("raised", Some(100_000_000)), ("none", None)] { |
| 403 | let mut inst = Instance::new(); |
| 404 | inst.set_loop_budget(budget); |
| 405 | assert_eq!(inst.loop_budget(), budget); |
| 406 | assert_eq!(res!(builds_alike_with(&dir, name, few, inst)), "done"); |
| 407 | } |
| 408 | // The host's budget is read again by the next compile of an instance that has refused one. |
| 409 | small.set_loop_budget(None); |
| 410 | assert!(small.compile_pdf(&Project::single(few)).is_ok()); |
| 411 | Ok(()) |
| 412 | } |
| 413 | |
| 414 | #[test] |
| 415 | fn a_project_is_document_data_and_carries_no_loop_budget() -> Outcome<()> { |
| 416 | // Every field of a project, named: one added for a budget stops this compiling, so the choice to let a |
| 417 | // project that arrives by sync set its own bound is made here, deliberately, or not at all. |
| 418 | let Project { main, sources, assets, fonts, strict, known, want } = Project::single("#[a]\n"); |
| 419 | assert!(main.is_empty() && assets.is_empty() && fonts.is_empty() && known.is_empty() && want.is_none() && !strict); |
| 420 | assert_eq!(sources.len(), 1); |
| 421 | // A source that spells a budget is only source: the door's bound is the same with it. |
| 422 | let mut inst = Instance::new(); |
| 423 | let src = "// loopBudget: null\n#let loop-budget = none\n#{ for i in range(3200) { for j in range(3400) { } }; [done] }\n"; |
| 424 | let f = match inst.compile_pdf(&Project::single(src)) { |
| 425 | Ok(_) => return Err(err!("A project that spells a budget lifted the door's bound"; Test)), |
| 426 | Err(f) => f, |
| 427 | }; |
| 428 | assert_eq!(f.head.kind, DiagnosticKind::Limit); |
| 429 | assert_eq!(inst.loop_budget(), Some(LOOP_BUDGET)); |
| 430 | Ok(()) |
| 431 | } |
| 432 | |
| 433 | #[test] |
| 434 | fn the_command_line_has_no_loop_budget_and_builds_the_document_the_door_refuses() -> Outcome<()> { |
| 435 | let dir = res!(work_dir("budget-cli")); |
| 436 | res!(write_main(&dir, "many", MANY)); |
| 437 | let out_dir = dir.join("many").join("out"); |
| 438 | let out = res!(Command::new(env!("CARGO_BIN_EXE_austenite")) |
| 439 | .current_dir(dir.join("many")) |
| 440 | .args(["--eval", "--root", ".", "main.typ"]) |
| 441 | .arg(&out_dir) |
| 442 | .output()); |
| 443 | assert!(out.status.success(), "the command line builds it: {}", String::from_utf8_lossy(&out.stderr)); |
| 444 | let pdf = out_dir.join("document.pdf"); |
| 445 | assert!(pdf.is_file(), "{} is written", pdf.display()); |
| 446 | assert_eq!(res!(glyphs_of(&pdf)), "done"); |
| 447 | Ok(()) |
| 448 | } |
| 449 | |
| 450 | #[test] |
| 451 | fn a_while_loop_still_ends_at_ten_thousand_iterations_where_typst_ends_it() -> Outcome<()> { |
| 452 | let dir = res!(work_dir("while")); |
| 453 | let text = res!(builds_alike(&dir, "edge", "#{ let n = 0; while n < 10000 { n += 1 }; [#n] }\n")); |
| 454 | assert_eq!(text, "10000"); |
| 455 | let f = res!(refuses_alike(&dir, "over", "#{ let n = 0; while n < 10001 { n += 1 }; [#n] }\n")); |
| 456 | assert_eq!(f.head.message, "loop seems to be infinite"); |
| 457 | Ok(()) |
| 458 | } |
| 459 | |
| 460 | // Strings: a symbol is cast to the string it stands for wherever Typst expects a string |
| 461 | |
| 462 | // A computed key of a dictionary is cast to a string, so a symbol is the key its text spells, the |
| 463 | // variant its modifiers pick: the shape of a table of arrows that maps each to a mark's name. |
| 464 | const KEYS: [(&str, &str); 4] = [ |
| 465 | ("arrows", "#let d = ((sym.arrow.r): \"x\", (sym.arrow.l): \"y\", (sym.arrow.r.l): \"z\", (sym.arrow.double.long.r): \"w\")\n#repr(d) #d.keys()\n"), |
| 466 | ("later-wins", "#let d = ((sym.alpha): 1, (sym.zws): 2, (math.floor): 3, (\"a\".first()): 4, (\"a\" + \"b\"): 5, (sym.arrow): 6, (sym.arrow.r): 7)\n#repr(d)\n"), |
| 467 | ("looked-up", "#let d = ((sym.arrow.r): 1)\n#d.at(\"→\") #d.at(sym.arrow.r) #d.at(sym.arrow.l, default: 5)\n"), |
| 468 | ("a-string", "#let d = ((\"a\" + \"b\"): 1, (str(1)): 2)\n#repr(d)\n"), |
| 469 | ]; |
| 470 | |
| 471 | #[test] |
| 472 | fn a_symbol_is_a_dictionary_key_as_the_text_it_stands_for_and_any_other_non_string_is_refused() -> Outcome<()> { |
| 473 | let dir = res!(work_dir("keys")); |
| 474 | for (name, src) in KEYS { |
| 475 | res!(builds_alike(&dir, name, src)); |
| 476 | } |
| 477 | // The key's own expression is where Typst reports a value that is not a string. |
| 478 | for (name, src, found) in [ |
| 479 | ("integer", "#let d = ((1): 1)\n", "integer"), |
| 480 | ("none", "#let d = ((none): 1)\n", "none"), |
| 481 | ("float", "#let d = ((1.5): 1)\n", "float"), |
| 482 | ("content", "#let d = (([a]): 1)\n", "content"), |
| 483 | ("label", "#let d = ((<lbl>): 1)\n", "label"), |
| 484 | ] { |
| 485 | let f = res!(refuses_alike(&dir, name, src)); |
| 486 | assert_eq!(f.head.message, fmt!("expected string, found {}", found), "{}", name); |
| 487 | } |
| 488 | Ok(()) |
| 489 | } |
| 490 | |
| 491 | // Each is a place Typst's parameter is a string, so a symbol is accepted and read as its text. |
| 492 | const CASTS: [(&str, &str); 14] = [ |
| 493 | ("dict-at", "#let d = (\"α\": 1, \"→\": 2)\n#d.at(sym.alpha) #d.at(sym.arrow.r) #d.at(sym.beta, default: 7)\n"), |
| 494 | ("dict-insert", "#let d = (a: 1)\n#{ d.insert(sym.alpha, 2) } #d.remove(sym.alpha) #d.remove(sym.beta, default: 3)\n#repr(d)\n"), |
| 495 | ("dict-assign", "#let d = (\"α\": 1)\n#{ d.at(sym.alpha) = 9 }\n#{ d.at(sym.alpha) += 1 }\n#repr(d)\n"), |
| 496 | ("dict-nested", "#let d = (a: (:))\n#{ d.a.insert(sym.alpha, 1) }\n#repr(d)\n"), |
| 497 | ("str-patterns", "#\"xαy\".contains(sym.alpha) #\"xαy\".starts-with(sym.alpha) #\"xα\".ends-with(sym.alpha) #\"xαy\".find(sym.alpha) #\"xαy\".position(sym.alpha) #\"xαy\".match(sym.alpha).text #\"xαyα\".matches(sym.alpha).len() #\"xαy\".split(sym.alpha) #\"αxα\".trim(sym.alpha)\n"), |
| 498 | ("str-replace", "#\"xαy\".replace(sym.alpha, sym.beta) #\"xαy\".replace(\"x\", sym.beta) #\"a→b\".replace(regex(\"→\"), m => sym.beta)\n"), |
| 499 | ("constructors", "#regex(sym.alpha) #bytes(sym.alpha).len() #str.to-unicode(sym.alpha) #repr(label(sym.alpha))\n"), |
| 500 | ("keys", "#let c = counter(sym.alpha)\n#context c.get()\n#let s = state(sym.alpha, 5)\n#context s.get()\n#numbering(sym.alpha, 1)\n"), |
| 501 | ("case", "#upper(sym.alpha) #lower(sym.Alpha)\n"), |
| 502 | ("eval", "#eval(sym.alpha, mode: \"markup\")\n"), |
| 503 | ("fields", "#raw(sym.alpha) #raw(sym.alpha, lang: sym.beta)\n"), |
| 504 | ("font", "#text(font: sym.alpha)[x]\n"), |
| 505 | ("args-at", "#let f(..a) = a\n#f(α: 1).at(sym.alpha) #f(α: 1).at(sym.beta, default: 0)\n"), |
| 506 | ("content-at", "#[a].has(sym.alpha) #[a].at(sym.alpha, default: 1)\n"), |
| 507 | ]; |
| 508 | |
| 509 | #[test] |
| 510 | fn a_symbol_is_read_as_its_text_wherever_typst_expects_a_string() -> Outcome<()> { |
| 511 | let dir = res!(work_dir("casts")); |
| 512 | for (name, src) in CASTS { |
| 513 | res!(builds_alike(&dir, name, src)); |
| 514 | } |
| 515 | Ok(()) |
| 516 | } |
| 517 | |
| 518 | #[test] |
| 519 | fn a_value_that_is_neither_a_string_nor_a_symbol_is_refused_where_a_string_is_expected_as_typst_does() -> Outcome<()> { |
| 520 | let dir = res!(work_dir("string-refusals")); |
| 521 | for (name, src) in [ |
| 522 | ("at-label", "#let d = (a: 1)\n#d.at(<lbl>)\n"), |
| 523 | ("at-none", "#let d = (a: 1)\n#d.at(none)\n"), |
| 524 | ("at-float", "#let d = (a: 1)\n#d.at(1.5)\n"), |
| 525 | ("remove-integer", "#let d = (a: 1)\n#{ d.remove(1) }\n"), |
| 526 | ("insert-integer", "#let d = (a: 1)\n#{ d.insert(1, 2) }\n"), |
| 527 | ] { |
| 528 | let f = res!(refuses_message_alike(&dir, name, src)); |
| 529 | assert!(f.head.message.starts_with("expected string, found "), "{}: {}", name, f.head.message); |
| 530 | } |
| 531 | for (name, src) in [ |
| 532 | ("assign-integer", "#let d = (a: 1)\n#{ d.at(2) = 3 }\n"), |
| 533 | // A symbol is a key; an absent one is refused at the assignment as any absent key is. |
| 534 | ("assign-absent", "#let d = (a: 1)\n#{ d.at(sym.alpha) = 3 }\n"), |
| 535 | ("assert-message", "#assert(false, message: sym.alpha)\n"), |
| 536 | ] { |
| 537 | let f = res!(refuses_alike(&dir, name, src)); |
| 538 | assert!(!f.head.message.is_empty(), "{}", name); |
| 539 | } |
| 540 | Ok(()) |
| 541 | } |
| 542 | |
| 543 | // Packages: fletcher draws its edges' marks from a table keyed by symbols |
| 544 | |
| 545 | // Synthetic documents that use two packages from the cache Typst keeps its own in: a commutative square |
| 546 | // and a second diagram whose edges are marked by strings and by symbols, a page of marks, shapes and |
| 547 | // loops, a page of nodes named and joined by paths and a figure, and a canvas of cetz with a plot. |
| 548 | const FLETCHER: [(&str, &str); 3] = [ |
| 549 | ("square", include_str!("fixtures/survey/fletcher_square.typ")), |
| 550 | ("marks", include_str!("fixtures/survey/fletcher_marks.typ")), |
| 551 | ("shapes", include_str!("fixtures/survey/fletcher_shapes.typ")), |
| 552 | ]; |
| 553 | |
| 554 | /// Is the package in Typst's cache? Without it the test is skipped only on the owner's word. |
| 555 | fn package_cached(spec: &str) -> bool { |
| 556 | let home = std::env::var("HOME").unwrap_or_default(); |
| 557 | if Path::new(&home).join(".cache/typst/packages/preview").join(spec).is_dir() { |
| 558 | return true; |
| 559 | } |
| 560 | let skip = std::env::var("EVAL_ORACLE_SKIP").map(|v| v == "1").unwrap_or(false); |
| 561 | assert!(skip, "no {} in Typst's package cache (set EVAL_ORACLE_SKIP=1 to skip explicitly)", spec); |
| 562 | false |
| 563 | } |
| 564 | |
| 565 | #[test] |
| 566 | fn a_fletcher_diagram_marked_by_symbols_and_strings_builds_to_typsts_pages_and_text() -> Outcome<()> { |
| 567 | if !package_cached("fletcher/0.5.7") { |
| 568 | return Ok(()); |
| 569 | } |
| 570 | supply_typst_package_cache(); |
| 571 | let dir = res!(work_dir("fletcher")); |
| 572 | for (name, src) in FLETCHER { |
| 573 | res!(builds_alike(&dir, name, src)); |
| 574 | } |
| 575 | Ok(()) |
| 576 | } |
| 577 | |
| 578 | // Not a fault this unit found: the next construct a package of that kind is known for, held to the same |
| 579 | // account. |
| 580 | #[test] |
| 581 | fn a_cetz_canvas_with_shapes_arcs_and_a_plot_builds_to_typsts_pages_and_text() -> Outcome<()> { |
| 582 | if !package_cached("cetz-plot/0.1.1") { |
| 583 | return Ok(()); |
| 584 | } |
| 585 | supply_typst_package_cache(); |
| 586 | let dir = res!(work_dir("cetz")); |
| 587 | res!(builds_alike(&dir, "canvas", include_str!("fixtures/survey/cetz_canvas.typ"))); |
| 588 | Ok(()) |
| 589 | } |