diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e010d9..1d3a586 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -94,6 +94,11 @@ reads that marker. `.snap` file the macro wrote, and every failure summary, named a variable the test never wrote. Snapshots already committed still match; `cargo insta test --force-update-snapshots` rewrites their header. (#851) +- `Screen::parse`'s rustdoc and DESIGN §3 no longer say it accepts + "exactly" what `Display` and `with_styles` write. It also reads a few + shapes they never write, such as `size: +4x+1` or a row's spans split + over two lines, each into the screen its canonical form gives; the docs + now list them, and a test pins each one. (#849) - Both published crates, `termlens` and `termlens-cli`, ship `LICENSE-MIT` and `LICENSE-APACHE`. They are `MIT OR Apache-2.0`, and both licences ask for their text in every copy, but the texts sat only at the repository diff --git a/crates/termlens/src/screen/parse.rs b/crates/termlens/src/screen/parse.rs index d485d24..ffe085d 100644 --- a/crates/termlens/src/screen/parse.rs +++ b/crates/termlens/src/screen/parse.rs @@ -20,12 +20,19 @@ use crate::{Error, Result}; impl Screen { /// Parse the snapshot text format back into a `Screen`. /// - /// Accepts exactly what [`Display`](std::fmt::Display) and + /// Accepts what [`Display`](std::fmt::Display) and /// [`with_styles`](Self::with_styles) write: the `size:`/`cursor:` /// header, the grid one row per line with trailing blanks stripped /// (missing trailing rows and columns are blank), and optionally a blank - /// line, `styles:` and its span lines (or `(none)`). Everything else is - /// an [`Error::Parse`] naming the line. + /// line, `styles:` and its span lines (or `(none)`). It also reads a few + /// shapes they never write, each into the screen its canonical form + /// gives: a `+` before a number in the header, one space or none before + /// `cursor:`, a row's spans split over several lines, blank lines + /// between span lines, `(none)` after spans, a one-column range written + /// `0-0`, a token repeated or out of order, and a span with no token + /// (the default style). Any other line is an [`Error::Parse`] naming + /// it. A file that parses is therefore not proof that termlens wrote + /// it; rendering it back gives the canonical form. /// /// **The header's row count decides where the grid ends**, so a grid may /// contain the words `styles:` or `(none)` as ordinary content. The one @@ -502,6 +509,63 @@ mod tests { assert!(screen.diff(&none).is_empty()); } + /// Shapes `Display` and `with_styles` never write, which the reader + /// takes as the canonical form beside them (#849). Each reads to the + /// screen its canonical form gives, and writes back as that form. + #[test] + fn the_reader_accepts_these_shapes_beside_the_canonical_ones() { + for (written, canonical) in [ + // A leading `+` on the size and the cursor. + ("size: +4x+1 cursor: 0,0\nhi", "size: 4x1 cursor: 0,0\nhi"), + ("size: 4x1 cursor: +0,+0\nhi", "size: 4x1 cursor: 0,0\nhi"), + // One space before `cursor:`, or none. + ("size: 4x1 cursor: 0,0\nhi", "size: 4x1 cursor: 0,0\nhi"), + ("size: 4x1cursor: 0,0\nhi", "size: 4x1 cursor: 0,0\nhi"), + // A row listed twice, its spans merged. + ( + "size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold\n0: 1 italic", + "size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold; 1 italic", + ), + // `(none)` after spans, and blank lines between span lines. + ( + "size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold\n(none)", + "size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold", + ), + ( + "size: 4x2 cursor: 0,0\nhi\nho\n\nstyles:\n0: 0 bold\n\n1: 1 italic", + "size: 4x2 cursor: 0,0\nhi\nho\n\nstyles:\n0: 0 bold\n1: 1 italic", + ), + // A one-column range, a repeated token, and a run's tokens in + // another order. + ( + "size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0-0 bold", + "size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold", + ), + ( + "size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold bold", + "size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold", + ), + ( + "size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 bold fg=1", + "size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0 fg=1 bold", + ), + // A span with no token is the default style. + ( + "size: 4x1 cursor: 0,0\nhi\n\nstyles:\n0: 0", + "size: 4x1 cursor: 0,0\nhi", + ), + ] { + let screen = Screen::parse(written).unwrap_or_else(|e| panic!("{written:?}: {e}")); + let expected = Screen::parse(canonical).unwrap(); + assert!(screen.diff(&expected).is_empty(), "{written:?}"); + assert_eq!( + screen.with_styles().to_string(), + expected.with_styles().to_string(), + "{written:?}" + ); + } + } + /// The header's row count decides where the grid ends, so these words /// are content when they fall inside it (#296). #[test] diff --git a/docs/DESIGN.md b/docs/DESIGN.md index 945f5ef..af71dd5 100644 --- a/docs/DESIGN.md +++ b/docs/DESIGN.md @@ -974,9 +974,15 @@ from the one the macro targets. ### Reading it back `Screen::parse` (0.10.0) is the reader for everything above: it accepts -exactly what `Display` and `with_styles` write — the header, the grid, and -optionally the blank line, `styles:` and its spans — and every other line is -an `Error::Parse` naming it. The `termlens` CLI is built on it: `diff` and +what `Display` and `with_styles` write — the header, the grid, and +optionally the blank line, `styles:` and its spans — and a few shapes they +never write, each read as its canonical form: a `+` before a number in the +header, one space or none before `cursor:`, a row's spans split over +several lines or with blank lines between them, `(none)` after spans, a +range written `0-0`, a token repeated or out of order, and a span with no +token. Every other line is an `Error::Parse` naming it. The tolerances stay +because STABILITY promises that a file any release from 0.10.1 wrote parses +in every later one, and a hand-edited snapshot may hold any of them. The `termlens` CLI is built on it: `diff` and `render` take saved screens, so a snapshot outlives the test run that produced it. @@ -1025,8 +1031,9 @@ compares `==`. ### What a reader skips, and what it does not The format proper begins at the `size:` header and ends with the grid or -the `styles:` block. `Screen::parse` accepts exactly that — every other -line is an `Error::Parse` naming it. termlens itself writes two things +the `styles:` block. `Screen::parse` accepts that, with the tolerances +listed under "Reading it back" — every other line is an `Error::Parse` +naming it. termlens itself writes two things *around* a screen, and its own reader, the `termlens` CLI, skips both: - **Above:** the insta header, `---` / `source: …` / `---`, which a `.snap`