thing2(text): verify per-cluster CellText idempotence in fuzz test

This commit is contained in:
Meta Vens 2026-12-06 05:23:08 +01:00
commit de80ee2414
2 changed files with 17 additions and 7 deletions

View file

@ -2,9 +2,10 @@
//! one `Measure` for Cells. Every width and every break a cell backend advances by is decided
//! here, per extended grapheme cluster, and no backend computes either.
//!
//! Order is sanitize, then segment and measure: the text is cleaned (controls become U+FFFD,
//! clusters without width are dropped) until segmenting it again changes nothing, so every width
//! recorded is the width of exactly the text a terminal receives.
//! Order is sanitize, then segment and measure: the text is cleaned once (controls become U+FFFD,
//! zero-width clusters are dropped while coalescing non-RI extensions across dropped boundaries)
//! and `emit` builds `CellText` directly from those kept clusters, so every width and break is
//! owned once and every emitted cluster re-segments to itself.
//!
//! Breaking is CSS `white-space: pre-wrap` with `overflow-wrap: break-word`: mandatory breaks
//! always break, soft breaks are UAX #14 Allowed positions on a cluster boundary, trailing spaces

View file

@ -249,26 +249,35 @@ fn fuzz_never_emits_a_control() {
for line in layout_lines(&text, wrap, None, width) {
assert!(!line.chars().any(|c| c.is_control() || matches!(c, '\u{2028}' | '\u{2029}')), "{text:?} -> {line:?}");
}
// What is emitted measures to what was recorded: measuring it again changes nothing.
// What is emitted is clean and each emitted cluster re-segments to itself with the same width.
let node = NodeId::new("n");
let request = TextRequest { node: &node, text: &text, style_token: "body", wrap, max_lines: None, geometry: false };
for line in CellMeasure.text(&request, width).lines {
assert!(line.stops.is_empty());
let Run::Cells { text: emitted, .. } = line.run else { panic!() };
assert_eq!(CellText::new(emitted.as_str()), emitted, "{text:?} at {width}");
assert!(emitted.clusters().all(|(_, w)| w >= 1));
assert_clean_cell_text(&emitted, &text, width);
}
if round % 20 == 0 {
check_geometry(&text, wrap, None, width);
}
let lone = CellText::new(&text);
assert_eq!(CellText::new(lone.as_str()), lone, "{text:?}");
assert_clean_cell_text(&lone, &text, width);
let _ = leaf_measure(&text, wrap, Some(3), Avail::Definite(width));
let _ = leaf_measure(&text, wrap, None, Avail::MinContent);
assert!(!CellText::new(&text).as_str().chars().any(|c| c.is_control()));
}
}
fn assert_clean_cell_text(ct: &CellText, raw: &str, width: f32) {
let re = CellText::new(ct.as_str());
assert_eq!(re.as_str(), ct.as_str(), "{raw:?} at {width}");
assert_eq!(re.width(), ct.width(), "{raw:?} at {width}");
for (cluster, w) in ct.clusters() {
assert!(w >= 1, "{raw:?} at {width}");
assert_eq!(CellText::new(cluster).clusters().collect::<Vec<_>>(), [(cluster, w)], "{raw:?} at {width}");
}
}
fn check_geometry(text: &str, wrap: bool, max_lines: Option<u32>, width: f32) {
let node = NodeId::new("n");
let off = CellMeasure.text(&TextRequest { node: &node, text, style_token: "body", wrap, max_lines, geometry: false }, width);