oxedyne/fe2o3/fe2o3_austenite/tests/locate_edit.rs
12.8 KiB, 47 runs
created by r1870400018:68811, 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 | //! Locations under a keystroke (speed programme U6): a location is made from where the element stands, its |
| 2 | //! kind, what it is and its ordinal among equals there, never from where it was written. Where an element |
| 3 | //! stands is a place, and an element that lays a body out is given one made from its shell (its kind and the |
| 4 | //! fields that hold no content) and its ordinal among the same shell there, never from what it holds. Two |
| 5 | //! compiles one letter apart therefore differ in location only in the edited element, the elements that hold |
| 6 | //! it and the equal elements that follow an edited one, whose ordinal moves; no place moves, so what lies in |
| 7 | //! a body keeps its location and a warm introspector still answers for it. |
| 8 | //! |
| 9 | //! Each case compiles a document, types one letter into it, compiles again, and lines the two compiles' |
| 10 | //! located elements up in document order (the structure is the same, only a letter differs). An element is |
| 11 | //! edited when its source span holds the letter. Four things must hold of every pair of compiles: |
| 12 | //! |
| 13 | //! 1. an edited element whose content differs has a new location, so a location never stands for two |
| 14 | //! contents (an element that merely reads what the edit changed, as a reference reads its target's |
| 15 | //! text, is not edited and keeps its location); |
| 16 | //! 2. an element of the same content moves only when an earlier element equal to it changed, so that its |
| 17 | //! ordinal among the equals moved; |
| 18 | //! 3. every element that lays a body out keeps its place; and |
| 19 | //! 4. the elements outside the edit's section of the document do not move at all (the synthetic case). |
| 20 | //! |
| 21 | //! `AUST_LOC_DOCS=a.typ,b.typ cargo test --release --test locate_edit -- --ignored --nocapture` runs the same |
| 22 | //! comparison over whole documents, at `AUST_LOC_EDITS` (default 6) body paragraphs spread through each. |
| 23 | |
| 24 | use oxedyne_fe2o3_austenite::compile::Session; |
| 25 | use oxedyne_fe2o3_austenite::emit::sinks::CountSink; |
| 26 | use oxedyne_fe2o3_austenite::eval::content::ElemKind; |
| 27 | use oxedyne_fe2o3_austenite::flow::text::FontStore; |
| 28 | use oxedyne_fe2o3_austenite::syntax::Span; |
| 29 | |
| 30 | use oxedyne_fe2o3_core::prelude::*; |
| 31 | |
| 32 | use std::path::{ |
| 33 | Path, |
| 34 | PathBuf, |
| 35 | }; |
| 36 | |
| 37 | // The compiles |
| 38 | |
| 39 | // A located element as one compile placed it. |
| 40 | #[derive(Clone, Debug, PartialEq)] |
| 41 | struct Rec { |
| 42 | loc: u64, |
| 43 | kind: Option<ElemKind>, // none for a sequence |
| 44 | fp: u128, |
| 45 | place: Option<u64>, // lays a body out, so gives what it holds a place of its own |
| 46 | span: Span, |
| 47 | } |
| 48 | |
| 49 | fn dir(name: &str) -> Outcome<PathBuf> { |
| 50 | let d = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join("locate_edit").join(name); |
| 51 | let _ = std::fs::remove_dir_all(&d); |
| 52 | res!(std::fs::create_dir_all(&d)); |
| 53 | Ok(res!(std::fs::canonicalize(&d))) |
| 54 | } |
| 55 | |
| 56 | /// The located elements of `src`, compiled cold in a directory of its own, in document order. |
| 57 | fn compiled(name: &str, src: &str) -> Outcome<Vec<Rec>> { |
| 58 | let root: PathBuf = res!(dir(name)); |
| 59 | let main: PathBuf = root.join("main.typ"); |
| 60 | res!(std::fs::write(&main, src)); |
| 61 | let mut session = Session::new(FontStore::default()); |
| 62 | let done = res!(session.compile(&main, &root, &mut CountSink::default(), None, None, true)); |
| 63 | let laid = res!(done.laid.map_err(|e| err!("{}: the compile failed: {}", name, e.plain(); Test))); |
| 64 | Ok(laid.intro.iter().filter_map(|r| r.elem.location().map(|l| Rec { |
| 65 | loc: l.0, |
| 66 | kind: r.elem.kind(), |
| 67 | fp: r.elem.fingerprint().as_u128(), |
| 68 | place: r.elem.place().map(|p| p.0), |
| 69 | span: r.elem.span(), |
| 70 | })).collect()) |
| 71 | } |
| 72 | |
| 73 | // The edits |
| 74 | |
| 75 | /// Types `letter` into `text` at byte `at`. |
| 76 | fn typed(text: &str, at: usize, letter: &str) -> String { |
| 77 | let mut s = String::with_capacity(text.len() + letter.len()); |
| 78 | s.push_str(&text[..at]); |
| 79 | s.push_str(letter); |
| 80 | s.push_str(&text[at..]); |
| 81 | s |
| 82 | } |
| 83 | |
| 84 | /// Where a letter is typed to edit each body paragraph and each heading: just after the first word of a line |
| 85 | /// that follows a blank line, a heading or the top of the file, as the incremental harness does. |
| 86 | fn sites(text: &str) -> Vec<usize> { |
| 87 | let mut out = Vec::new(); |
| 88 | let mut off = 0; |
| 89 | let mut after = true; |
| 90 | for line in text.split('\n') { |
| 91 | let b = line.as_bytes(); |
| 92 | let first = b.first().copied().unwrap_or(b' '); |
| 93 | let heading = line.starts_with("= ") || line.starts_with("== "); |
| 94 | if after && (first.is_ascii_uppercase() || heading) { |
| 95 | let skip = if heading { line.find(' ').map(|i| i + 1).unwrap_or(0) } else { 0 }; |
| 96 | let w = b[skip..].iter().take_while(|c| c.is_ascii_alphanumeric()).count(); |
| 97 | if w > 0 && skip + w < b.len() && b[skip + w] == b' ' { |
| 98 | out.push(off + skip + w); |
| 99 | } |
| 100 | } |
| 101 | after = line.trim().is_empty() || line.starts_with('='); |
| 102 | off += line.len() + 1; |
| 103 | } |
| 104 | out |
| 105 | } |
| 106 | |
| 107 | // The comparison |
| 108 | |
| 109 | /// What differs between two compiles one letter apart, as indices into the located elements. |
| 110 | #[derive(Debug, Default)] |
| 111 | struct Diff { |
| 112 | n: usize, |
| 113 | edited: Vec<usize>, // the source span holds the letter |
| 114 | readers: Vec<usize>, // not edited, but the content differs: a reference or an outline entry carrying the text |
| 115 | moved: Vec<usize>, // not edited, the content the same, the location not |
| 116 | replaced: Vec<usize>, // the place differs |
| 117 | shifted: Vec<usize>, // every element whose location differs |
| 118 | } |
| 119 | |
| 120 | // Does the span of an element of the compile before the edit hold a letter typed at byte `at`? |
| 121 | fn holds(s: Span, at: usize) -> bool { !s.is_detached() && (s.start as usize) < at && at <= s.end as usize } |
| 122 | |
| 123 | fn diff(name: &str, a: &[Rec], b: &[Rec], at: usize) -> Outcome<Diff> { |
| 124 | if a.len() != b.len() { |
| 125 | return Err(err!("{}: {} located elements before the edit and {} after.", name, a.len(), b.len(); Test)); |
| 126 | } |
| 127 | let mut d = Diff { n: a.len(), ..Diff::default() }; |
| 128 | for (i, (x, y)) in a.iter().zip(b).enumerate() { |
| 129 | if x.kind != y.kind { |
| 130 | return Err(err!("{}: element {} is {:?} before the edit and {:?} after.", name, i, x.kind, y.kind; Test)); |
| 131 | } |
| 132 | let edited = holds(x.span, at); |
| 133 | if x.loc != y.loc { |
| 134 | d.shifted.push(i); |
| 135 | } |
| 136 | if edited { |
| 137 | d.edited.push(i); |
| 138 | if x.fp != y.fp && x.loc == y.loc { |
| 139 | return Err(err!("{}: element {} ({:?}) holds the edit, has different content and kept its location.", |
| 140 | name, i, y.kind; Test)); |
| 141 | } |
| 142 | } else if x.fp != y.fp { |
| 143 | d.readers.push(i); |
| 144 | } else if x.loc != y.loc { |
| 145 | d.moved.push(i); |
| 146 | } |
| 147 | if x.place != y.place { |
| 148 | d.replaced.push(i); |
| 149 | } |
| 150 | } |
| 151 | Ok(d) |
| 152 | } |
| 153 | |
| 154 | /// An element whose content is the same moves only when an earlier element equal to it changed, and no place |
| 155 | /// moves. |
| 156 | fn check(name: &str, a: &[Rec], b: &[Rec], d: &Diff) -> Outcome<()> { |
| 157 | for &i in &d.moved { |
| 158 | let renumbered = (0..i).any(|j| a[j].kind == a[i].kind && a[j].fp == a[i].fp && b[j].fp != a[j].fp); |
| 159 | if !renumbered { |
| 160 | return Err(err!("{}: element {} ({:?}) moved and no earlier element equal to it changed; \ |
| 161 | edited {:?}, reading {:?}, moved {:?}.", name, i, b[i].kind, d.edited, d.readers, d.moved; Test)); |
| 162 | } |
| 163 | } |
| 164 | if !d.replaced.is_empty() { |
| 165 | return Err(err!("{}: the place of element(s) {:?} moved; edited {:?}.", name, d.replaced, d.edited; Test)); |
| 166 | } |
| 167 | Ok(()) |
| 168 | } |
| 169 | |
| 170 | // The cases |
| 171 | |
| 172 | const FIXTURES: &[&str] = &[ |
| 173 | "counters_context", "floats", "footnote_spill", "headings_refs_outline", |
| 174 | "page_columns", "pagebreak_parity", "table_split", |
| 175 | ]; |
| 176 | |
| 177 | fn fixture(name: &str) -> Outcome<String> { |
| 178 | let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/incremental").join(fmt!("{}.typ", name)); |
| 179 | Ok(res!(std::fs::read_to_string(&path))) |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn a_letter_typed_into_a_fixture_moves_only_what_holds_it() -> Outcome<()> { |
| 184 | let mut cases = 0; |
| 185 | let mut edited = 0; |
| 186 | let mut total = Diff::default(); |
| 187 | for name in FIXTURES { |
| 188 | let base = res!(fixture(name)); |
| 189 | let before = res!(compiled(name, &base)); |
| 190 | assert!(!before.is_empty(), "{}: no located element at all", name); |
| 191 | for (k, at) in sites(&base).into_iter().enumerate() { |
| 192 | let label = fmt!("{} site {}", name, k); |
| 193 | let after = res!(compiled(&label.replace(' ', "_"), &typed(&base, at, "q"))); |
| 194 | let d = res!(diff(&label, &before, &after, at)); |
| 195 | res!(check(&label, &before, &after, &d)); |
| 196 | cases += 1; |
| 197 | edited += d.edited.len().min(1); |
| 198 | total.n += d.n; |
| 199 | total.readers.extend(d.readers.iter().map(|_| 0)); |
| 200 | total.moved.extend(d.moved.iter().map(|_| 0)); |
| 201 | total.shifted.extend(d.shifted.iter().map(|_| 0)); |
| 202 | } |
| 203 | } |
| 204 | println!("{} edits, {} located elements compared: {} shifted, of which {} read the edit and {} moved under it", |
| 205 | cases, total.n, total.shifted.len(), total.readers.len(), total.moved.len()); |
| 206 | assert!(cases >= 20, "only {} cases ran", cases); |
| 207 | assert!(edited >= 10, "only {} of the {} edits changed an element that is located", edited, cases); |
| 208 | Ok(()) |
| 209 | } |
| 210 | |
| 211 | /// A document of forty sections, each a heading, a paragraph with a footnote and a labelled table. A letter in |
| 212 | /// one section's paragraph moves that paragraph and nothing else. |
| 213 | fn sections(n: usize, tag: &str) -> String { |
| 214 | let mut s = String::from("#set page(width: 220pt, height: 130pt, margin: 16pt)\n#set text(size: 9pt)\n"); |
| 215 | for i in 0..n { |
| 216 | s.push_str(&fmt!("\n= Section {}\n\nParagraph {} has words of its own{} and a note#footnote[Note {}.] to close.\n\n\ |
| 217 | #table(columns: 2, [A{}], [B{}]) <t{}>\n", i, i, if i == 17 { tag } else { "" }, i, i, i, i)); |
| 218 | } |
| 219 | s |
| 220 | } |
| 221 | |
| 222 | #[test] |
| 223 | fn a_letter_typed_into_a_paragraph_moves_that_paragraph_and_nothing_else() -> Outcome<()> { |
| 224 | let (plain, typed) = (sections(40, ""), sections(40, "q")); |
| 225 | let at = plain.bytes().zip(typed.bytes()).take_while(|(x, y)| x == y).count(); |
| 226 | let before = res!(compiled("sections_a", &plain)); |
| 227 | let after = res!(compiled("sections_b", &typed)); |
| 228 | let d = res!(diff("sections", &before, &after, at)); |
| 229 | res!(check("sections", &before, &after, &d)); |
| 230 | assert!(!d.edited.is_empty(), "the letter changed no located element"); |
| 231 | // A section is a heading, a paragraph, its footnote and a table, six located elements in all. The paragraph |
| 232 | // is a placed element that holds the edit, and its footnote lies in its body: the paragraph alone moves. |
| 233 | let per = before.len() / 40; |
| 234 | assert!(per >= 3, "a section holds {} located elements", per); |
| 235 | assert_eq!(d.shifted, d.edited, "more than the edited paragraph moved, of {} elements", d.n); |
| 236 | assert_eq!(d.edited.len(), 1, "the letter edited {:?}", d.edited); |
| 237 | assert!(d.edited[0] / per == 17, "the edit lies in section {}", d.edited[0] / per); |
| 238 | Ok(()) |
| 239 | } |
| 240 | |
| 241 | /// Equal elements are told apart by their ordinal among the equals at one place, as Typst's locator does, so |
| 242 | /// an edit that makes one of them unequal renumbers the equal ones after it. This pins that reach: the later |
| 243 | /// equals move, the earlier ones and the unequal ones do not. |
| 244 | #[test] |
| 245 | fn an_edited_element_renumbers_only_the_equal_ones_after_it() -> Outcome<()> { |
| 246 | let doc = |q: &str| { |
| 247 | let mut s = String::from("#set page(width: 220pt, height: 400pt, margin: 16pt)\n= Title\n\n"); |
| 248 | for i in 0..5 { |
| 249 | s.push_str(&fmt!("{}\n\n", if i == 1 { fmt!("Same words{} here.", q) } else { fmt!("Same words here.") })); |
| 250 | } |
| 251 | s.push_str("= Another\n"); |
| 252 | s |
| 253 | }; |
| 254 | let (plain, typed) = (doc(""), doc("q")); |
| 255 | let at = plain.bytes().zip(typed.bytes()).take_while(|(x, y)| x == y).count(); |
| 256 | let before = res!(compiled("equals_a", &plain)); |
| 257 | let after = res!(compiled("equals_b", &typed)); |
| 258 | let d = res!(diff("equals", &before, &after, at)); |
| 259 | // The paragraphs are the located elements of kind Par; the second is edited. |
| 260 | let pars: Vec<usize> = before.iter().enumerate().filter(|(_, r)| r.kind == Some(ElemKind::Par)).map(|(i, _)| i).collect(); |
| 261 | assert_eq!(pars.len(), 5, "the document has {} paragraphs of the five typed", pars.len()); |
| 262 | assert!(d.shifted.contains(&pars[1]), "the edited paragraph kept its location"); |
| 263 | assert!(!d.shifted.contains(&pars[0]), "the equal paragraph before the edit moved"); |
| 264 | for &p in &pars[2..] { |
| 265 | assert!(d.shifted.contains(&p), "an equal paragraph after the edit kept its ordinal"); |
| 266 | } |
| 267 | // Their places are made from the shell and its ordinal, which an edit of their text leaves alone. |
| 268 | assert!(d.replaced.is_empty(), "the place of {:?} moved", d.replaced); |
| 269 | let heads: Vec<usize> = before.iter().enumerate().filter(|(_, r)| r.kind == Some(ElemKind::Heading)).map(|(i, _)| i).collect(); |
| 270 | assert!(heads.iter().all(|h| !d.shifted.contains(h)), "a heading moved"); |
| 271 | Ok(()) |
| 272 | } |
| 273 | |
| 274 | /// Whole documents, named in `AUST_LOC_DOCS`, edited at `AUST_LOC_EDITS` paragraphs spread through each. |
| 275 | #[test] |
| 276 | #[ignore] |
| 277 | fn a_letter_typed_into_a_document_moves_only_what_holds_it() -> Outcome<()> { |
| 278 | let docs = res!(std::env::var("AUST_LOC_DOCS").map_err(|_| err!("Set AUST_LOC_DOCS to a list of .typ files."; Test))); |
| 279 | let edits: usize = std::env::var("AUST_LOC_EDITS").ok().and_then(|s| s.parse().ok()).unwrap_or(6); |
| 280 | for doc in docs.split(',') { |
| 281 | let base = res!(std::fs::read_to_string(doc)); |
| 282 | let all = sites(&base); |
| 283 | assert!(!all.is_empty(), "{}: no site to edit", doc); |
| 284 | let before = res!(compiled("doc_a", &base)); |
| 285 | for k in 0..edits { |
| 286 | let at = all[k * all.len() / edits]; |
| 287 | let after = res!(compiled("doc_b", &typed(&base, at, "q"))); |
| 288 | let d = res!(diff(doc, &before, &after, at)); |
| 289 | res!(check(doc, &before, &after, &d)); |
| 290 | println!("{}: edit {} at byte {}: {} located, {} edited, {} reading, {} moved, {} in all", doc, k, at, d.n, |
| 291 | d.edited.len(), d.readers.len(), d.moved.len(), d.shifted.len()); |
| 292 | } |
| 293 | } |
| 294 | Ok(()) |
| 295 | } |