oxedyne/fe2o3/fe2o3_austenite/tests/incremental.rs
34.0 KiB, 3 runs
created by r1870400018:67963, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The warm-against-cold harness. A long-lived [`Instance`] is driven through a script of edits and, after |
| 2 | //! every state, a fresh one compiles the same project; the two must agree on everything a consumer reads: |
| 3 | //! every page's SVG, the PDF with its `/ID` masked, the delta's page ids, and the report. The delta call is |
| 4 | //! played as the live view plays it, holding the pages it was sent and rebuilding the document from `order`. |
| 5 | //! |
| 6 | //! The fixtures under `tests/fixtures/incremental/` are small and feature-dense. `corpus` is the same |
| 7 | //! comparison over whole documents; it is `#[ignore]`d and run alone in release: |
| 8 | //! |
| 9 | //! `AUST_DIFF_DOCS=a.typ,b.typ=/root AUST_DIFF_FONTS=<dir> cargo test --release --test incremental -- --ignored --test-threads=1` |
| 10 | //! |
| 11 | //! with `AUST_DIFF_EDITS` (default 10; edit j types a letter at position j mod 5 of the body paragraphs), |
| 12 | //! `AUST_DIFF_KINDS` (default `pdf,delta`), `AUST_DIFF_LOG` (a line per state with warm and cold ms) and |
| 13 | //! `AUST_DIFF_DUMP` (a directory for the first divergent pair). The corpus prints numbers and kinds only. |
| 14 | |
| 15 | use oxedyne_fe2o3_austenite::compile::{ |
| 16 | self, |
| 17 | Report, |
| 18 | }; |
| 19 | use oxedyne_fe2o3_austenite::door::{ |
| 20 | Instance, |
| 21 | Project, |
| 22 | }; |
| 23 | use oxedyne_fe2o3_austenite::emit::sinks::VectorSink; |
| 24 | use oxedyne_fe2o3_austenite::flow::text::FontStore; |
| 25 | |
| 26 | use oxedyne_fe2o3_core::prelude::*; |
| 27 | |
| 28 | use std::collections::{ |
| 29 | BTreeMap, |
| 30 | HashMap, |
| 31 | HashSet, |
| 32 | }; |
| 33 | use std::io::Write; |
| 34 | use std::path::{ |
| 35 | Path, |
| 36 | PathBuf, |
| 37 | }; |
| 38 | use std::time::Instant; |
| 39 | |
| 40 | const LETTERS: [&str; 5] = ["q", "x", "z", "j", "k"]; |
| 41 | const KINDS: [&str; 3] = ["delta", "svg", "pdf"]; |
| 42 | const EXTRA: &str = "An added paragraph, set between two others, with enough ordinary words in it to run on to a second line of the page."; |
| 43 | |
| 44 | // The document |
| 45 | |
| 46 | #[derive(Clone, Debug, Default)] |
| 47 | struct Doc { |
| 48 | name: String, |
| 49 | main: String, // empty for `/main.typ` |
| 50 | sources: Vec<(String, String)>, // project path, text |
| 51 | assets: Vec<(String, Vec<u8>)>, |
| 52 | fonts: Vec<(String, Vec<u8>)>, |
| 53 | } |
| 54 | |
| 55 | impl Doc { |
| 56 | fn main_path(&self) -> String { |
| 57 | if self.main.is_empty() { "/main.typ".to_string() } else { self.main.clone() } |
| 58 | } |
| 59 | |
| 60 | fn index(&self, path: &str) -> Option<usize> { |
| 61 | self.sources.iter().position(|(p, _)| p == path) |
| 62 | } |
| 63 | |
| 64 | /// The project of this document with the sources in `over` replaced. |
| 65 | fn project(&self, over: &[(usize, String)], known: &[u64]) -> Project { |
| 66 | let mut sources = self.sources.clone(); |
| 67 | for (i, text) in over { |
| 68 | sources[*i].1 = text.clone(); |
| 69 | } |
| 70 | Project { |
| 71 | main: self.main.clone(), |
| 72 | sources, |
| 73 | assets: self.assets.clone(), |
| 74 | fonts: self.fonts.clone(), |
| 75 | strict: false, |
| 76 | known: known.to_vec(), |
| 77 | want: None, |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | fn fixture(name: &str) -> Outcome<Self> { |
| 82 | let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/incremental").join(fmt!("{}.typ", name)); |
| 83 | let text = res!(std::fs::read_to_string(&path)); |
| 84 | Ok(Self { name: name.to_string(), sources: vec![("/main.typ".to_string(), text)], ..Self::default() }) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // The paragraphs a script edits |
| 89 | |
| 90 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 91 | struct Para { |
| 92 | src: usize, |
| 93 | start: usize, // the first byte of the paragraph's first line |
| 94 | at: usize, // just after its first word, where a letter is typed |
| 95 | end: usize, // the end of its last line, before the newline |
| 96 | } |
| 97 | |
| 98 | /// The body paragraphs of `text`: a line that begins with an uppercase ASCII letter (any letter when `any`), |
| 99 | /// after a blank line, a heading line or the top of the file, whose first word is followed by a space. |
| 100 | fn paragraphs(src: usize, text: &str, any: bool) -> Vec<Para> { |
| 101 | let lines: Vec<&str> = text.split('\n').collect(); |
| 102 | let mut starts = Vec::with_capacity(lines.len()); |
| 103 | let mut off = 0; |
| 104 | for l in &lines { |
| 105 | starts.push(off); |
| 106 | off += l.len() + 1; |
| 107 | } |
| 108 | let mut out = Vec::new(); |
| 109 | let mut after = true; |
| 110 | for (i, line) in lines.iter().enumerate() { |
| 111 | let b = line.as_bytes(); |
| 112 | let first = b.first().copied().unwrap_or(b' '); |
| 113 | let letter = if any { first.is_ascii_alphabetic() } else { first.is_ascii_uppercase() }; |
| 114 | if after && letter { |
| 115 | let w = b.iter().take_while(|c| c.is_ascii_alphanumeric()).count(); |
| 116 | if w < b.len() && b[w] == b' ' { |
| 117 | let mut j = i; |
| 118 | while j + 1 < lines.len() && !lines[j + 1].trim().is_empty() { |
| 119 | j += 1; |
| 120 | } |
| 121 | out.push(Para { src, start: starts[i], at: starts[i] + w, end: starts[j] + lines[j].len() }); |
| 122 | } |
| 123 | } |
| 124 | after = line.trim().is_empty() || line.starts_with('='); |
| 125 | } |
| 126 | out |
| 127 | } |
| 128 | |
| 129 | /// The paths `#include "…"` names in `text`. |
| 130 | fn includes(text: &str) -> Vec<String> { |
| 131 | let mut out = Vec::new(); |
| 132 | let mut rest = text; |
| 133 | while let Some(i) = rest.find("#include \"") { |
| 134 | rest = &rest[i + 10..]; |
| 135 | if let Some(j) = rest.find('"') { |
| 136 | out.push(rest[..j].to_string()); |
| 137 | rest = &rest[j..]; |
| 138 | } |
| 139 | } |
| 140 | out |
| 141 | } |
| 142 | |
| 143 | /// A path resolved against the including file's directory, `/x` against the root, `.` and `..` folded. |
| 144 | fn resolve(dir: &str, rel: &str) -> String { |
| 145 | let joined = if rel.starts_with('/') { rel.to_string() } else { fmt!("{}/{}", dir, rel) }; |
| 146 | let mut parts: Vec<&str> = Vec::new(); |
| 147 | for seg in joined.split('/') { |
| 148 | match seg { |
| 149 | "" | "." => {}, |
| 150 | ".." => { parts.pop(); }, |
| 151 | s => parts.push(s), |
| 152 | } |
| 153 | } |
| 154 | fmt!("/{}", parts.join("/")) |
| 155 | } |
| 156 | |
| 157 | /// The main source and the sources it includes, depth first. |
| 158 | fn pool(doc: &Doc) -> Vec<usize> { |
| 159 | fn walk(doc: &Doc, i: usize, seen: &mut HashSet<usize>, out: &mut Vec<usize>) { |
| 160 | if !seen.insert(i) { |
| 161 | return; |
| 162 | } |
| 163 | out.push(i); |
| 164 | let path = &doc.sources[i].0; |
| 165 | let dir = match path.rfind('/') { Some(k) => path[..k].to_string(), None => String::new() }; |
| 166 | for target in includes(&doc.sources[i].1) { |
| 167 | if let Some(j) = doc.index(&resolve(&dir, &target)) { |
| 168 | walk(doc, j, seen, out); |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | let mut out = Vec::new(); |
| 173 | if let Some(i) = doc.index(&doc.main_path()) { |
| 174 | walk(doc, i, &mut HashSet::new(), &mut out); |
| 175 | } |
| 176 | out |
| 177 | } |
| 178 | |
| 179 | /// The body paragraphs of the pool in order; any letter starts one when the pool has fewer than five. |
| 180 | fn pool_paras(doc: &Doc) -> Vec<Para> { |
| 181 | let sources = pool(doc); |
| 182 | for any in [false, true] { |
| 183 | let mut all = Vec::new(); |
| 184 | for &s in &sources { |
| 185 | all.extend(paragraphs(s, &doc.sources[s].1, any)); |
| 186 | } |
| 187 | if all.len() >= 5 || any { |
| 188 | return all; |
| 189 | } |
| 190 | } |
| 191 | Vec::new() |
| 192 | } |
| 193 | |
| 194 | /// The letters typed so far, keyed by source and offset in the base text, rendered into the sources. |
| 195 | type Typed = BTreeMap<(usize, usize), String>; |
| 196 | |
| 197 | fn render(doc: &Doc, typed: &Typed) -> Vec<(usize, String)> { |
| 198 | let mut srcs: Vec<usize> = typed.keys().map(|k| k.0).collect(); |
| 199 | srcs.dedup(); |
| 200 | let mut out = Vec::new(); |
| 201 | for s in srcs { |
| 202 | let base = &doc.sources[s].1; |
| 203 | let mut text = String::with_capacity(base.len() + 8); |
| 204 | let mut last = 0; |
| 205 | for ((_, at), t) in typed.range((s, 0)..(s + 1, 0)) { |
| 206 | text.push_str(&base[last..*at]); |
| 207 | text.push_str(t); |
| 208 | last = *at; |
| 209 | } |
| 210 | text.push_str(&base[last..]); |
| 211 | out.push((s, text)); |
| 212 | } |
| 213 | out |
| 214 | } |
| 215 | |
| 216 | // The script |
| 217 | |
| 218 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 219 | enum Bite { |
| 220 | Free, // no claim |
| 221 | Base, // the cold PDF is the base's |
| 222 | Moves, // the cold PDF is not the base's |
| 223 | } |
| 224 | |
| 225 | #[derive(Clone, Debug)] |
| 226 | struct State { |
| 227 | label: String, |
| 228 | over: Vec<(usize, String)>, |
| 229 | reload: bool, // the consumer drops what it holds before the call |
| 230 | bite: Bite, |
| 231 | } |
| 232 | |
| 233 | fn state(label: &str, over: Vec<(usize, String)>, reload: bool, bite: Bite) -> State { |
| 234 | State { label: label.to_string(), over, reload, bite } |
| 235 | } |
| 236 | |
| 237 | fn edit_once(text: &str, find: &str, rep: &str) -> Outcome<String> { |
| 238 | let n = text.matches(find).count(); |
| 239 | if n != 1 { |
| 240 | return Err(err!("The anchor {:?} occurs {} times, not once.", find, n; Test)); |
| 241 | } |
| 242 | Ok(text.replacen(find, rep, 1)) |
| 243 | } |
| 244 | |
| 245 | /// The fixture script: open, a no-op save, five letters one after another, the revert, each table edit and |
| 246 | /// its undo, a paragraph added and one removed, the consumer's reload, a reload after an edit, the base. |
| 247 | fn fixture_script(doc: &Doc, tables: &[(&str, &str, &str)]) -> Outcome<Vec<State>> { |
| 248 | let paras = pool_paras(doc); |
| 249 | if paras.len() < 5 { |
| 250 | return Err(err!("{} has {} body paragraphs, under five.", doc.name, paras.len(); Test)); |
| 251 | } |
| 252 | let mut out = vec![ |
| 253 | state("open", Vec::new(), false, Bite::Free), |
| 254 | state("save", Vec::new(), false, Bite::Base), |
| 255 | ]; |
| 256 | let mut typed = Typed::new(); |
| 257 | for j in 0..5 { |
| 258 | let p = paras[j * paras.len() / 5]; |
| 259 | typed.entry((p.src, p.at)).or_default().push_str(LETTERS[j]); |
| 260 | out.push(state(&fmt!("edit {}", j + 1), render(doc, &typed), false, Bite::Moves)); |
| 261 | } |
| 262 | out.push(state("revert", Vec::new(), false, Bite::Base)); |
| 263 | for (label, find, rep) in tables { |
| 264 | let text = res!(edit_once(&doc.sources[0].1, find, rep)); |
| 265 | out.push(state(label, vec![(0, text)], false, Bite::Moves)); |
| 266 | out.push(state(&fmt!("{} undone", label), Vec::new(), false, Bite::Base)); |
| 267 | } |
| 268 | let add = paras[1]; |
| 269 | let mut text = doc.sources[add.src].1.clone(); |
| 270 | text.insert_str(add.end, &fmt!("\n\n{}", EXTRA)); |
| 271 | out.push(state("paragraph added", vec![(add.src, text)], false, Bite::Moves)); |
| 272 | let cut = paras[paras.len() / 2]; |
| 273 | let mut text = doc.sources[cut.src].1.clone(); |
| 274 | text.replace_range(cut.start..cut.end, ""); |
| 275 | out.push(state("paragraph removed", vec![(cut.src, text)], false, Bite::Moves)); |
| 276 | out.push(state("reload", Vec::new(), true, Bite::Base)); |
| 277 | let mut one = Typed::new(); |
| 278 | one.entry((paras[0].src, paras[0].at)).or_default().push_str(LETTERS[0]); |
| 279 | out.push(state("reload after edit", render(doc, &one), true, Bite::Moves)); |
| 280 | out.push(state("base again", Vec::new(), false, Bite::Base)); |
| 281 | Ok(out) |
| 282 | } |
| 283 | |
| 284 | /// The corpus script: open, a no-op save, then `edits` letters, edit j at position j mod 5, kept. |
| 285 | fn corpus_script(doc: &Doc, edits: usize) -> Outcome<Vec<State>> { |
| 286 | let paras = pool_paras(doc); |
| 287 | if paras.is_empty() { |
| 288 | return Err(err!("{} has no body paragraph to edit.", doc.name; Test)); |
| 289 | } |
| 290 | let mut out = vec![ |
| 291 | state("open", Vec::new(), false, Bite::Free), |
| 292 | state("save", Vec::new(), false, Bite::Base), |
| 293 | ]; |
| 294 | let mut typed = Typed::new(); |
| 295 | for j in 0..edits { |
| 296 | let p = paras[(j % 5) * paras.len() / 5]; |
| 297 | typed.entry((p.src, p.at)).or_default().push_str(LETTERS[j % 5]); |
| 298 | out.push(state(&fmt!("edit {}", j + 1), render(doc, &typed), false, Bite::Moves)); |
| 299 | } |
| 300 | Ok(out) |
| 301 | } |
| 302 | |
| 303 | // What one call showed |
| 304 | |
| 305 | #[derive(Clone, Debug, Default)] |
| 306 | struct Seen { |
| 307 | n: usize, |
| 308 | order: Vec<u64>, |
| 309 | pages: Vec<String>, |
| 310 | pdf: Vec<u8>, |
| 311 | diags: Vec<String>, // kind first, then the whole site; the summary lines follow |
| 312 | } |
| 313 | |
| 314 | fn diags_of(report: &Report, needs: &[String]) -> Vec<String> { |
| 315 | let mut v: Vec<String> = report.diagnostics.iter().map(|d| fmt!("{} {:?}", d.kind.as_str(), d)).collect(); |
| 316 | v.push(fmt!("skipped {:?}", report.skipped)); |
| 317 | v.push(fmt!("summary {:?}", report.summary)); |
| 318 | v.push(fmt!("needs {:?}", needs)); |
| 319 | v.push(fmt!("empty {}", report.empty)); |
| 320 | v |
| 321 | } |
| 322 | |
| 323 | /// Blanks both halves of the trailer's `/ID`, which is a hash of the body and so moves with it. |
| 324 | fn mask_id(pdf: &mut [u8]) { |
| 325 | let key = b"/ID ["; |
| 326 | if let Some(i) = pdf.windows(key.len()).position(|w| w == key) { |
| 327 | let mut k = i + key.len(); |
| 328 | while k < pdf.len() && pdf[k] != b']' { |
| 329 | if pdf[k].is_ascii_hexdigit() { |
| 330 | pdf[k] = b'0'; |
| 331 | } |
| 332 | k += 1; |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /// One side of the comparison: an instance and the pages a live-view consumer holds from its deltas. |
| 338 | struct Side { |
| 339 | inst: Instance, |
| 340 | known: Vec<u64>, |
| 341 | held: HashMap<u64, String>, |
| 342 | version: u32, |
| 343 | } |
| 344 | |
| 345 | impl Side { |
| 346 | fn new() -> Self { |
| 347 | Self { inst: Instance::new(), known: Vec::new(), held: HashMap::new(), version: 0 } |
| 348 | } |
| 349 | |
| 350 | fn call(&mut self, kind: &str, doc: &Doc, st: &State) -> Result<Seen, String> { |
| 351 | match kind { |
| 352 | "delta" => self.delta(doc, st), |
| 353 | "svg" => self.svg(doc, st), |
| 354 | _ => self.pdf(doc, st), |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | fn delta(&mut self, doc: &Doc, st: &State) -> Result<Seen, String> { |
| 359 | if st.reload { |
| 360 | self.known.clear(); |
| 361 | self.held.clear(); |
| 362 | } |
| 363 | let was_empty = self.known.is_empty(); |
| 364 | let p = doc.project(&st.over, &self.known); |
| 365 | let made = match self.inst.compile_delta(&p) { |
| 366 | Ok(m) => m, |
| 367 | Err(f) => return Err(fmt!("{:?}", f)), |
| 368 | }; |
| 369 | let d = made.product; |
| 370 | if d.reset != was_empty { |
| 371 | return Err(fmt!("protocol: reset is {} though the consumer held {} pages", d.reset, self.known.len())); |
| 372 | } |
| 373 | if d.rendered as usize != d.changed.len() { |
| 374 | return Err(fmt!("protocol: rendered {} but changed holds {}", d.rendered, d.changed.len())); |
| 375 | } |
| 376 | if d.version != self.version + 1 { |
| 377 | return Err(fmt!("protocol: version {} after {}", d.version, self.version)); |
| 378 | } |
| 379 | self.version = d.version; |
| 380 | for (id, svg) in d.changed { |
| 381 | self.held.insert(id, svg); |
| 382 | } |
| 383 | let mut pages = Vec::with_capacity(d.order.len()); |
| 384 | for id in &d.order { |
| 385 | match self.held.get(id) { |
| 386 | Some(s) => pages.push(s.clone()), |
| 387 | None => return Err(fmt!("protocol: the delta lacks page {:016x}", id)), |
| 388 | } |
| 389 | } |
| 390 | let keep: HashSet<u64> = d.order.iter().copied().collect(); |
| 391 | self.held.retain(|id, _| keep.contains(id)); |
| 392 | self.known = d.order.clone(); |
| 393 | Ok(Seen { n: made.report.pages, order: d.order, pages, pdf: Vec::new(), diags: diags_of(&made.report, &made.needs) }) |
| 394 | } |
| 395 | |
| 396 | fn svg(&mut self, doc: &Doc, st: &State) -> Result<Seen, String> { |
| 397 | let p = doc.project(&st.over, &[]); |
| 398 | match self.inst.compile_svg(&p) { |
| 399 | Ok(m) => Ok(Seen { n: m.report.pages, order: Vec::new(), pages: m.product, pdf: Vec::new(), diags: diags_of(&m.report, &m.needs) }), |
| 400 | Err(f) => Err(fmt!("{:?}", f)), |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | fn pdf(&mut self, doc: &Doc, st: &State) -> Result<Seen, String> { |
| 405 | let p = doc.project(&st.over, &[]); |
| 406 | match self.inst.compile_pdf(&p) { |
| 407 | Ok(m) => { |
| 408 | let mut bytes = m.product.to_vec(); |
| 409 | mask_id(&mut bytes); |
| 410 | Ok(Seen { n: m.report.pages, order: Vec::new(), pages: Vec::new(), pdf: bytes, diags: diags_of(&m.report, &m.needs) }) |
| 411 | }, |
| 412 | Err(f) => Err(fmt!("{:?}", f)), |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // The comparison |
| 418 | |
| 419 | fn first_diff(a: &[u8], b: &[u8]) -> Option<usize> { |
| 420 | let n = a.len().min(b.len()); |
| 421 | for i in 0..n { |
| 422 | if a[i] != b[i] { |
| 423 | return Some(i); |
| 424 | } |
| 425 | } |
| 426 | if a.len() != b.len() { Some(n) } else { None } |
| 427 | } |
| 428 | |
| 429 | fn excerpt(b: &[u8], at: usize) -> String { |
| 430 | let lo = at.saturating_sub(40); |
| 431 | let hi = (at + 40).min(b.len()); |
| 432 | String::from_utf8_lossy(&b[lo..hi]).into_owned() |
| 433 | } |
| 434 | |
| 435 | /// Why two results differ, naming the field and where, or `None` when they agree. Quiet gives numbers and |
| 436 | /// kinds only, never a word of the document. |
| 437 | fn diff_seen(warm: &Result<Seen, String>, cold: &Result<Seen, String>, quiet: bool) -> Option<String> { |
| 438 | let (w, c) = match (warm, cold) { |
| 439 | (Ok(w), Ok(c)) => (w, c), |
| 440 | (Ok(_), Err(e)) => return Some(if quiet { "warm compiled, cold refused".to_string() } else { fmt!("warm compiled, cold refused: {}", e) }), |
| 441 | (Err(e), Ok(_)) => return Some(if quiet { "warm refused, cold compiled".to_string() } else { fmt!("warm refused, cold compiled: {}", e) }), |
| 442 | (Err(a), Err(b)) => { |
| 443 | return match first_diff(a.as_bytes(), b.as_bytes()) { |
| 444 | None => None, |
| 445 | Some(at) => Some(if quiet { |
| 446 | fmt!("both refused, differently, at byte {} (lengths {} and {})", at, a.len(), b.len()) |
| 447 | } else { |
| 448 | fmt!("both refused, differently, at byte {}: warm {:?}, cold {:?}", at, excerpt(a.as_bytes(), at), excerpt(b.as_bytes(), at)) |
| 449 | }), |
| 450 | }; |
| 451 | }, |
| 452 | }; |
| 453 | if w.n != c.n { |
| 454 | return Some(fmt!("page count: warm {}, cold {}", w.n, c.n)); |
| 455 | } |
| 456 | if w.order != c.order { |
| 457 | let slot = (0..w.order.len().max(c.order.len())).find(|&i| w.order.get(i) != c.order.get(i)).unwrap_or(0); |
| 458 | return Some(fmt!("order differs at slot {} (lengths {} and {})", slot, w.order.len(), c.order.len())); |
| 459 | } |
| 460 | if w.pages.len() != c.pages.len() { |
| 461 | return Some(fmt!("pages: warm {}, cold {}", w.pages.len(), c.pages.len())); |
| 462 | } |
| 463 | for (i, (a, b)) in w.pages.iter().zip(c.pages.iter()).enumerate() { |
| 464 | if let Some(at) = first_diff(a.as_bytes(), b.as_bytes()) { |
| 465 | return Some(if quiet { |
| 466 | fmt!("page {} differs at byte {} (lengths {} and {})", i + 1, at, a.len(), b.len()) |
| 467 | } else { |
| 468 | fmt!("page {} differs at byte {}: warm {:?}, cold {:?}", i + 1, at, excerpt(a.as_bytes(), at), excerpt(b.as_bytes(), at)) |
| 469 | }); |
| 470 | } |
| 471 | } |
| 472 | if let Some(at) = first_diff(&w.pdf, &c.pdf) { |
| 473 | return Some(fmt!("pdf differs at byte {} (lengths {} and {})", at, w.pdf.len(), c.pdf.len())); |
| 474 | } |
| 475 | if w.diags.len() != c.diags.len() { |
| 476 | return Some(fmt!("diagnostics: warm {}, cold {}", w.diags.len(), c.diags.len())); |
| 477 | } |
| 478 | for (i, (a, b)) in w.diags.iter().zip(c.diags.iter()).enumerate() { |
| 479 | if a != b { |
| 480 | let kind = |s: &str| s.split(' ').next().unwrap_or("").to_string(); |
| 481 | return Some(if quiet { |
| 482 | fmt!("diagnostic {} differs (kinds {} and {})", i, kind(a), kind(b)) |
| 483 | } else { |
| 484 | fmt!("diagnostic {} differs: warm {}, cold {}", i, a, b) |
| 485 | }); |
| 486 | } |
| 487 | } |
| 488 | None |
| 489 | } |
| 490 | |
| 491 | // The run |
| 492 | |
| 493 | #[derive(Default)] |
| 494 | struct Opts { |
| 495 | log: Option<PathBuf>, |
| 496 | dump: Option<PathBuf>, |
| 497 | } |
| 498 | |
| 499 | #[derive(Clone, Copy, Debug)] |
| 500 | struct Kinds { |
| 501 | delta: bool, |
| 502 | svg: bool, |
| 503 | pdf: bool, |
| 504 | } |
| 505 | |
| 506 | impl Kinds { |
| 507 | const ALL: Self = Self { delta: true, svg: true, pdf: true }; |
| 508 | |
| 509 | fn parse(s: &str) -> Outcome<Self> { |
| 510 | let mut k = Self { delta: false, svg: false, pdf: false }; |
| 511 | for w in s.split(',') { |
| 512 | match w.trim() { |
| 513 | "delta" => k.delta = true, |
| 514 | "svg" => k.svg = true, |
| 515 | "pdf" => k.pdf = true, |
| 516 | other => return Err(err!("AUST_DIFF_KINDS names {:?}, not delta, svg or pdf.", other; Test)), |
| 517 | } |
| 518 | } |
| 519 | Ok(k) |
| 520 | } |
| 521 | |
| 522 | fn has(&self, kind: &str) -> bool { |
| 523 | match kind { "delta" => self.delta, "svg" => self.svg, _ => self.pdf } |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | /// What a state's cold compile gave, for the claims a script makes about its edits. |
| 528 | struct Trace { |
| 529 | label: String, |
| 530 | n: Option<usize>, |
| 531 | ok: bool, |
| 532 | pdf: Vec<u8>, |
| 533 | diags: Vec<String>, |
| 534 | } |
| 535 | |
| 536 | struct Verdict { |
| 537 | diverged: Option<String>, |
| 538 | traces: Vec<Trace>, |
| 539 | ran: usize, |
| 540 | } |
| 541 | |
| 542 | fn run_script(doc: &Doc, states: &[State], kinds: Kinds, quiet: bool, opts: &Opts) -> Verdict { |
| 543 | let mut v = Verdict { diverged: None, traces: Vec::new(), ran: 0 }; |
| 544 | let mut warm = Side::new(); |
| 545 | for (idx, st) in states.iter().enumerate() { |
| 546 | let mut trace = Trace { label: st.label.clone(), n: None, ok: false, pdf: Vec::new(), diags: Vec::new() }; |
| 547 | let mut first = true; |
| 548 | for kind in KINDS { |
| 549 | if !kinds.has(kind) { |
| 550 | continue; |
| 551 | } |
| 552 | let t0 = Instant::now(); |
| 553 | let w = warm.call(kind, doc, st); |
| 554 | let warm_ms = t0.elapsed().as_secs_f64() * 1000.0; |
| 555 | let t1 = Instant::now(); |
| 556 | let c = Side::new().call(kind, doc, st); |
| 557 | let cold_ms = t1.elapsed().as_secs_f64() * 1000.0; |
| 558 | if let Some(log) = &opts.log { |
| 559 | if let Ok(mut f) = std::fs::OpenOptions::new().create(true).append(true).open(log) { |
| 560 | let _ = writeln!(f, "{}\t{}\t{}\t{:.1}\t{:.1}", doc.name, st.label, kind, warm_ms, cold_ms); |
| 561 | } |
| 562 | } |
| 563 | if first { |
| 564 | trace.ok = c.is_ok(); |
| 565 | match &c { |
| 566 | Ok(s) => { trace.n = Some(s.n); trace.diags = s.diags.clone(); }, |
| 567 | Err(e) => trace.diags = vec![e.clone()], |
| 568 | } |
| 569 | first = false; |
| 570 | } |
| 571 | if let Ok(s) = &c { |
| 572 | if kind == "pdf" { |
| 573 | trace.pdf = s.pdf.clone(); |
| 574 | } |
| 575 | } |
| 576 | if let Some(why) = diff_seen(&w, &c, quiet) { |
| 577 | if let Some(dir) = &opts.dump { |
| 578 | let _ = std::fs::create_dir_all(dir); |
| 579 | for (side, r) in [("warm", &w), ("cold", &c)] { |
| 580 | if let Ok(s) = r { |
| 581 | let bytes = if kind == "pdf" { s.pdf.clone() } else { s.pages.join("\n<!-- page -->\n").into_bytes() }; |
| 582 | let _ = std::fs::write(dir.join(fmt!("{}_{}_{}_{}.bin", doc.name, idx, kind, side)), bytes); |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | v.diverged = Some(fmt!("{} / {} / {}: {}", doc.name, st.label, kind, why)); |
| 587 | v.traces.push(trace); |
| 588 | v.ran = idx + 1; |
| 589 | return v; |
| 590 | } |
| 591 | } |
| 592 | v.traces.push(trace); |
| 593 | v.ran = idx + 1; |
| 594 | } |
| 595 | v |
| 596 | } |
| 597 | |
| 598 | // The fixtures |
| 599 | |
| 600 | struct Fixture { |
| 601 | name: &'static str, |
| 602 | edits: &'static [(&'static str, &'static str, &'static str)], |
| 603 | warns: bool, // the document never settles, and both sides say so |
| 604 | pages: &'static [(&'static str, usize)], // the cold page count at named states |
| 605 | } |
| 606 | |
| 607 | fn fixture(f: &Fixture) -> Outcome<()> { |
| 608 | let doc = res!(Doc::fixture(f.name)); |
| 609 | let states = res!(fixture_script(&doc, f.edits)); |
| 610 | let v = run_script(&doc, &states, Kinds::ALL, false, &Opts::default()); |
| 611 | if let Some(d) = &v.diverged { |
| 612 | return Err(err!("{}", d; Test)); |
| 613 | } |
| 614 | let base = &v.traces[0]; |
| 615 | let mut counts = String::new(); |
| 616 | for (st, tr) in states.iter().zip(v.traces.iter()) { |
| 617 | assert!(tr.ok, "{} / {}: the cold compile refused: {:?}", f.name, st.label, tr.diags); |
| 618 | counts.push_str(&fmt!(" {}={}", st.label, tr.n.unwrap_or(0))); |
| 619 | match st.bite { |
| 620 | Bite::Base => assert!(tr.pdf == base.pdf, "{} / {}: the state should give the base's PDF and does not", f.name, st.label), |
| 621 | Bite::Moves => assert!(tr.pdf != base.pdf, "{} / {}: the edit changes nothing in the cold PDF, so it does not bite", f.name, st.label), |
| 622 | Bite::Free => {}, |
| 623 | } |
| 624 | let said = tr.diags.iter().any(|d| d.contains("did not converge")); |
| 625 | assert_eq!(said, f.warns, "{} / {}: the non-convergence warning should be {}: {:?}", f.name, st.label, f.warns, tr.diags); |
| 626 | } |
| 627 | println!("{} pages:{}", f.name, counts); |
| 628 | for (label, n) in f.pages { |
| 629 | let tr = res!(v.traces.iter().find(|t| t.label == *label).ok_or_else(|| err!("{} has no state {:?}.", f.name, label; Test))); |
| 630 | assert_eq!(tr.n, Some(*n), "{} / {}: the cold page count", f.name, label); |
| 631 | } |
| 632 | Ok(()) |
| 633 | } |
| 634 | |
| 635 | const FOOTNOTE_SPILL: Fixture = Fixture { |
| 636 | name: "footnote_spill", |
| 637 | edits: &[ |
| 638 | ("third note grown", "hour by hour through the evening", "hour by hour through the evening and then through the small hours of the night, and again at each turn of the tide"), |
| 639 | ("second note shortened", "so that it competes with the text above for the room that is left, and a good deal of it carries over onto the following page when the paragraph that cites it falls near the bottom", "so that it is brief"), |
| 640 | ("paragraph lengthened", "both were still warm when the first of the party arrived at the door.", "both were still warm when the first of the party arrived at the door. It had been a long walk from the harbour, and the party was glad of the rest, and of the fire."), |
| 641 | ("footnote dropped", "#footnote[Short.] ", ""), |
| 642 | ("footnote added", "moved on without comment.", "moved on without comment.#footnote[A late note, added after the fact.]"), |
| 643 | ], |
| 644 | warns: false, |
| 645 | pages: &[("open", 7), ("third note grown", 8), ("second note shortened", 6), ("footnote added", 8), ("paragraph removed", 6)], |
| 646 | }; |
| 647 | |
| 648 | const FLOATS: Fixture = Fixture { |
| 649 | name: "floats", |
| 650 | edits: &[ |
| 651 | ("float one taller", "height: 44pt", "height: 90pt"), |
| 652 | ("float two to the bottom", "#place(top + center, float: true, clearance: 6pt)", "#place(bottom + center, float: true, clearance: 6pt)"), |
| 653 | ("clause added", "Another paragraph arrives here,", "Another paragraph arrives here, and it goes on for rather longer than it did before, with a clause that takes a line or two more,"), |
| 654 | ("tall float shortened", "height: 70pt", "height: 20pt"), |
| 655 | ], |
| 656 | warns: false, |
| 657 | pages: &[("open", 5), ("float one taller", 6), ("paragraph added", 6)], |
| 658 | }; |
| 659 | |
| 660 | const COUNTERS_CONTEXT: Fixture = Fixture { |
| 661 | name: "counters_context", |
| 662 | edits: &[ |
| 663 | ("second step", "Third paragraph. #note.step()", "Third paragraph. #note.step() #note.step()"), |
| 664 | ("long clause", "It steps both again and carries on", "It steps both again, and then runs on for a good many words more than it did before, so that the page fills sooner and a new page is added at the end of the text, and carries on"), |
| 665 | ("state update dropped", "#seen.update(n => n + 1) A fourth", "A fourth"), |
| 666 | ], |
| 667 | warns: false, |
| 668 | pages: &[("open", 3), ("long clause", 4), ("paragraph added", 4)], |
| 669 | }; |
| 670 | |
| 671 | const HEADINGS_REFS_OUTLINE: Fixture = Fixture { |
| 672 | name: "headings_refs_outline", |
| 673 | edits: &[ |
| 674 | ("long clause in method", "It is set out here at some length, since", "It is set out here at some length, and then at greater length still, with a good many more words than before to push the later headings onto a later page, since the later parts depend on it and on what is said here about the method, the data, the checks that were made on the data, the steps taken when a check failed, the notes kept for each step, and the reasons for each choice. Further"), |
| 675 | ("results renamed", "= Results <results>", "= Findings and results <results>"), |
| 676 | ("more details demoted", "== More details <more>", "=== More details <more>"), |
| 677 | ("references swapped", "@details and in @more", "@more and in @details"), |
| 678 | ], |
| 679 | warns: false, |
| 680 | pages: &[("open", 5), ("long clause in method", 6), ("paragraph added", 6)], |
| 681 | }; |
| 682 | |
| 683 | const PAGEBREAK_PARITY: Fixture = Fixture { |
| 684 | name: "pagebreak_parity", |
| 685 | edits: &[ |
| 686 | ("first section grows", "It has a second short paragraph,", "It has a second short paragraph, followed by a good deal more text than before, enough to run the first section over onto a second page and so flip the parity of the break that follows it,"), |
| 687 | ("first section shrinks", "The first section sits on the first page and runs on for a few lines, long enough to matter but short enough to leave room.", "The first section."), |
| 688 | ("second section grows", "A second paragraph of the second section,", "A second paragraph of the second section, which now runs a good deal longer than it did, to carry the section onto a further page and move the break after it,"), |
| 689 | ("odd break made even", "#pagebreak(to: \"odd\")", "#pagebreak(to: \"even\")"), |
| 690 | ("even break made weak", "#pagebreak(to: \"even\")", "#pagebreak(weak: true)"), |
| 691 | ], |
| 692 | warns: false, |
| 693 | pages: &[("open", 6), ("odd break made even", 4), ("even break made weak", 5), ("paragraph removed", 4)], |
| 694 | }; |
| 695 | |
| 696 | const PAGE_COLUMNS: Fixture = Fixture { |
| 697 | name: "page_columns", |
| 698 | edits: &[ |
| 699 | ("longer note", "#footnote[A note in the columns.]", "#footnote[A note in the columns, now a good deal longer, with several lines of words that take room from the column it stands in.]"), |
| 700 | ("column break removed", "#colbreak()", ""), |
| 701 | ("one-column paragraph long", "Another paragraph in the one column follows it,", "Another paragraph in the one column follows it, and this time it is a long one, with enough text in it to fill the page from the top to the foot and a good way onto the page after it, which is the whole point of putting it here, in the middle of the one-column part of the document, where the flow is simplest and the effect of an edit is the easiest to see on the page that follows it,"), |
| 702 | ("three columns to two", "#set page(columns: 3)", "#set page(columns: 2)"), |
| 703 | ], |
| 704 | warns: false, |
| 705 | pages: &[("open", 4), ("one-column paragraph long", 5)], |
| 706 | }; |
| 707 | |
| 708 | const TABLE_SPLIT: Fixture = Fixture { |
| 709 | name: "table_split", |
| 710 | edits: &[ |
| 711 | ("cell wraps", "[Blankets]", "[Blankets, woollen, grey, in bales of ten, stored in the loft above the stores room]"), |
| 712 | ("row added", "[Buckets, galvanised, some with holes], [11],", "[Buckets, galvanised, some with holes], [11],\n\t[13], [Nails, box, assorted], [40],"), |
| 713 | ("long cell shortened", "[Charts of the coast and the approaches, folded, with the harbour plans and the tide tables bound in]", "[Charts]"), |
| 714 | ("header renamed", "table.header([No.], [Item], [Qty])", "table.header([Number], [Item and description], [Quantity])"), |
| 715 | ], |
| 716 | warns: false, |
| 717 | pages: &[("open", 7), ("header renamed", 8), ("paragraph added", 8)], |
| 718 | }; |
| 719 | |
| 720 | const NONCONVERGE: Fixture = Fixture { |
| 721 | name: "nonconverge", |
| 722 | edits: &[ |
| 723 | ("a line longer", "set before the state is read and long enough", "set before the state is read and now a good many words longer than it was, enough to take another line or more, and long enough"), |
| 724 | ("a paragraph removed", "The fifth paragraph is a short one, and the sixth follows it.\n\n", ""), |
| 725 | ], |
| 726 | warns: true, |
| 727 | pages: &[("open", 3), ("paragraph added", 4)], |
| 728 | }; |
| 729 | |
| 730 | #[test] |
| 731 | fn footnote_spill_warm_equals_cold() -> Outcome<()> { fixture(&FOOTNOTE_SPILL) } |
| 732 | |
| 733 | #[test] |
| 734 | fn floats_warm_equals_cold() -> Outcome<()> { fixture(&FLOATS) } |
| 735 | |
| 736 | #[test] |
| 737 | fn counters_context_warm_equals_cold() -> Outcome<()> { fixture(&COUNTERS_CONTEXT) } |
| 738 | |
| 739 | #[test] |
| 740 | fn headings_refs_outline_warm_equals_cold() -> Outcome<()> { fixture(&HEADINGS_REFS_OUTLINE) } |
| 741 | |
| 742 | #[test] |
| 743 | fn pagebreak_parity_warm_equals_cold() -> Outcome<()> { fixture(&PAGEBREAK_PARITY) } |
| 744 | |
| 745 | #[test] |
| 746 | fn page_columns_warm_equals_cold() -> Outcome<()> { fixture(&PAGE_COLUMNS) } |
| 747 | |
| 748 | #[test] |
| 749 | fn table_split_warm_equals_cold() -> Outcome<()> { fixture(&TABLE_SPLIT) } |
| 750 | |
| 751 | #[test] |
| 752 | fn nonconverge_warm_equals_cold_and_both_warn() -> Outcome<()> { fixture(&NONCONVERGE) } |
| 753 | |
| 754 | // The harness's own claims |
| 755 | |
| 756 | #[test] |
| 757 | fn the_comparator_names_the_first_differing_page_and_field() { |
| 758 | let a = Seen { n: 3, order: vec![1, 2, 3], pages: vec!["<a/>".into(), "<b/>".into(), "<c/>".into()], pdf: b"%PDF".to_vec(), diags: vec!["limit x".into()] }; |
| 759 | let ok = |s: &Seen| -> Result<Seen, String> { Ok(s.clone()) }; |
| 760 | assert_eq!(diff_seen(&ok(&a), &ok(&a), false), None); |
| 761 | let mut b = a.clone(); |
| 762 | b.pages[1] = "<b>!</b>".to_string(); |
| 763 | let why = diff_seen(&ok(&a), &ok(&b), false).expect("a differing page"); |
| 764 | assert!(why.contains("page 2 differs at byte 2"), "{}", why); |
| 765 | let quiet = diff_seen(&ok(&a), &ok(&b), true).expect("a differing page"); |
| 766 | assert!(quiet.contains("page 2 differs at byte 2") && !quiet.contains("<b"), "{}", quiet); |
| 767 | let mut b = a.clone(); |
| 768 | b.n = 4; |
| 769 | assert!(diff_seen(&ok(&a), &ok(&b), false).expect("a count").starts_with("page count")); |
| 770 | let mut b = a.clone(); |
| 771 | b.order[2] = 9; |
| 772 | assert!(diff_seen(&ok(&a), &ok(&b), false).expect("an order").contains("order differs at slot 2")); |
| 773 | let mut b = a.clone(); |
| 774 | b.pages.pop(); |
| 775 | assert!(diff_seen(&ok(&a), &ok(&b), false).expect("a length").starts_with("pages: warm 3, cold 2")); |
| 776 | let mut b = a.clone(); |
| 777 | b.pdf = b"%PDX".to_vec(); |
| 778 | assert!(diff_seen(&ok(&a), &ok(&b), false).expect("a pdf").contains("pdf differs at byte 3")); |
| 779 | let mut b = a.clone(); |
| 780 | b.diags = vec!["lint x".into()]; |
| 781 | assert!(diff_seen(&ok(&a), &ok(&b), true).expect("a diagnostic").contains("kinds limit and lint")); |
| 782 | let e: Result<Seen, String> = Err("no".to_string()); |
| 783 | assert!(diff_seen(&ok(&a), &e, false).expect("a refusal").starts_with("warm compiled, cold refused")); |
| 784 | assert!(diff_seen(&e, &e, false).is_none()); |
| 785 | } |
| 786 | |
| 787 | #[test] |
| 788 | fn the_paragraph_finder_returns_the_expected_offsets() { |
| 789 | let text = "// Head.\n#set text(size: 9pt)\n\nFirst para line one\nline two\n\n= Title\nSecond here\n\n Indented one\n\nlower case start\n\nOneWord\n"; |
| 790 | let at = |s: &str| text.find(s).expect("an anchor"); |
| 791 | let p = paragraphs(0, text, false); |
| 792 | assert_eq!(p.len(), 2, "{:?}", p); |
| 793 | assert_eq!(p[0], Para { src: 0, start: at("First"), at: at("First") + 5, end: at("\n\n= Title") }); |
| 794 | assert_eq!(p[1], Para { src: 0, start: at("Second"), at: at("Second") + 6, end: at("\n\n Indented") }); |
| 795 | let any = paragraphs(0, text, true); |
| 796 | assert_eq!(any.len(), 3, "a lower-case start counts when any letter will do: {:?}", any); |
| 797 | } |
| 798 | |
| 799 | #[test] |
| 800 | fn the_include_pool_runs_depth_first_and_resolves_paths() { |
| 801 | let src = |p: &str, t: &str| (p.to_string(), t.to_string()); |
| 802 | let doc = Doc { |
| 803 | name: "pool".to_string(), |
| 804 | main: "/book/main.typ".to_string(), |
| 805 | sources: vec![ |
| 806 | src("/book/main.typ", "Intro.\n#include \"ch/one.typ\"\n#include \"/shared/two.typ\"\n"), |
| 807 | src("/book/ch/one.typ", "One.\n#include \"../three.typ\"\n"), |
| 808 | src("/shared/two.typ", "Two.\n#include \"/book/main.typ\"\n"), |
| 809 | src("/book/three.typ", "Three.\n"), |
| 810 | src("/book/unused.typ", "Unused.\n"), |
| 811 | ], |
| 812 | ..Doc::default() |
| 813 | }; |
| 814 | assert_eq!(pool(&doc), vec![0, 1, 3, 2]); |
| 815 | assert_eq!(resolve("/a/b", "../c/./d.typ"), "/a/c/d.typ"); |
| 816 | assert_eq!(resolve("/a/b", "/x.typ"), "/x.typ"); |
| 817 | } |
| 818 | |
| 819 | #[test] |
| 820 | fn the_id_mask_blanks_both_halves_and_nothing_else() { |
| 821 | let mut pdf = b"<< /Size 19 /ID [<3869b46160a40fed> <3869b46160a40fed>] >>\n/Other <abcdef>".to_vec(); |
| 822 | mask_id(&mut pdf); |
| 823 | assert_eq!(String::from_utf8_lossy(&pdf), "<< /Size 19 /ID [<0000000000000000> <0000000000000000>] >>\n/Other <abcdef>"); |
| 824 | } |
| 825 | |
| 826 | // The corpus |
| 827 | |
| 828 | fn env_opt(name: &str) -> Option<String> { |
| 829 | std::env::var(name).ok().filter(|s| !s.is_empty()) |
| 830 | } |
| 831 | |
| 832 | /// A document read through one real compile: the files the compile read are the project, so that a long |
| 833 | /// book is not loaded whole. |
| 834 | fn prime(main: &Path, root: &Path, fonts_dir: Option<&Path>) -> Outcome<Doc> { |
| 835 | compile::supply_typst_package_cache(); |
| 836 | let mut fonts = FontStore::default(); |
| 837 | if let Some(d) = fonts_dir { |
| 838 | fonts.add_dir(d.to_path_buf()); |
| 839 | } |
| 840 | let done = res!(compile::assemble_eval(main, root, fonts, &mut VectorSink::default())); |
| 841 | let root = res!(std::fs::canonicalize(root)); |
| 842 | let main_c = res!(std::fs::canonicalize(main)); |
| 843 | let mut doc = Doc::default(); |
| 844 | for p in done.files_read() { |
| 845 | if !p.is_file() { |
| 846 | continue; |
| 847 | } |
| 848 | let bytes = res!(std::fs::read(&p)); |
| 849 | let ext = p.extension().and_then(|e| e.to_str()).map(|e| e.to_ascii_lowercase()).unwrap_or_default(); |
| 850 | if matches!(ext.as_str(), "ttf" | "otf" | "ttc" | "otc") { |
| 851 | doc.fonts.push((p.display().to_string(), bytes)); |
| 852 | continue; |
| 853 | } |
| 854 | let rel = res!(p.strip_prefix(&root).map_err(|_| err!("A file was read from outside the root: {}", p.display(); Test))); |
| 855 | let at = fmt!("/{}", rel.display()); |
| 856 | if ext == "typ" { |
| 857 | doc.sources.push((at, res!(String::from_utf8(bytes).map_err(|_| err!("{} is not UTF-8.", p.display(); Test))))); |
| 858 | } else { |
| 859 | doc.assets.push((at, bytes)); |
| 860 | } |
| 861 | } |
| 862 | doc.sources.sort(); |
| 863 | doc.assets.sort_by(|a, b| a.0.cmp(&b.0)); |
| 864 | doc.main = fmt!("/{}", res!(main_c.strip_prefix(&root).map_err(|_| err!("The main file is outside the root."; Test))).display()); |
| 865 | doc.name = main.file_stem().and_then(|s| s.to_str()).unwrap_or("doc").to_string(); |
| 866 | Ok(doc) |
| 867 | } |
| 868 | |
| 869 | #[test] |
| 870 | #[ignore = "the corpus: reads AUST_DIFF_DOCS, run alone in release (see the header)"] |
| 871 | fn corpus() -> Outcome<()> { |
| 872 | let docs = res!(std::env::var("AUST_DIFF_DOCS").map_err(|_| err!("Set AUST_DIFF_DOCS to a comma-separated list of doc[=root] paths."; Test))); |
| 873 | let fonts = env_opt("AUST_DIFF_FONTS").map(PathBuf::from); |
| 874 | let edits = match env_opt("AUST_DIFF_EDITS") { |
| 875 | Some(s) => res!(s.parse::<usize>().map_err(|e| err!("AUST_DIFF_EDITS: {}", e; Test))), |
| 876 | None => 10, |
| 877 | }; |
| 878 | let kinds = res!(Kinds::parse(&env_opt("AUST_DIFF_KINDS").unwrap_or_else(|| "pdf,delta".to_string()))); |
| 879 | let opts = Opts { log: env_opt("AUST_DIFF_LOG").map(PathBuf::from), dump: env_opt("AUST_DIFF_DUMP").map(PathBuf::from) }; |
| 880 | let mut bad = Vec::new(); |
| 881 | for spec in docs.split(',').filter(|s| !s.trim().is_empty()) { |
| 882 | let (main, root) = match spec.split_once('=') { |
| 883 | Some((m, r)) => (PathBuf::from(m), PathBuf::from(r)), |
| 884 | None => { |
| 885 | let m = PathBuf::from(spec); |
| 886 | let r = m.parent().map(|p| p.to_path_buf()).unwrap_or_default(); |
| 887 | (m, r) |
| 888 | }, |
| 889 | }; |
| 890 | let doc = res!(prime(&main, &root, fonts.as_deref())); |
| 891 | let states = res!(corpus_script(&doc, edits)); |
| 892 | let v = run_script(&doc, &states, kinds, true, &opts); |
| 893 | let pages = v.traces.first().and_then(|t| t.n).unwrap_or(0); |
| 894 | let files = doc.sources.len() + doc.assets.len(); |
| 895 | match &v.diverged { |
| 896 | None => println!("corpus {}: {} files, {} paragraphs in the pool, {} pages, {} of {} states equal", doc.name, files, pool_paras(&doc).len(), pages, v.ran, states.len()), |
| 897 | Some(d) => { |
| 898 | println!("corpus {}: DIVERGED after {} of {} states: {}", doc.name, v.ran, states.len(), d); |
| 899 | bad.push(d.clone()); |
| 900 | }, |
| 901 | } |
| 902 | } |
| 903 | if !bad.is_empty() { |
| 904 | return Err(err!("{} document(s) diverged: {}", bad.len(), bad.join("; "); Test)); |
| 905 | } |
| 906 | Ok(()) |
| 907 | } |