# case-tables ## Goal Give lucisv the backbone Verible's formatter has: a large table of small text-in/text-out `{input, expected}` pairs, organized by construct, that runs on every `cargo test`. Verible's `formatter_test.cc` is thousands of such pairs and is where years of edge cases live; the existing snapshot suite in `tests/fixtures/` covers whole files well but is a poor place to pin down "what exactly happens to *this* three-line construct". Each case asserts both `format(input) == expected` and `format(expected) == expected`, so every pair buys a formatting check and an idempotency check. ## Assumptions - `format_source` takes a complete source file, so every case input is a full compilation unit (`module ... endmodule`, `package ... endpackage`, ...). There is no fragment/statement-level entry point to test against. - Cases only exercise syntax the top-level `README.md` lists as supported. Nothing here depends on interfaces, classes, assertions-as-properties, etc. - Default `Config` (2-space indent, 100 columns). No case varies the config; if configurable behavior needs coverage later, add a per-file header rather than a second directory. - A `#` at the start of a line can only be a case header, never Verilog: a delay like `#5` is legal at statement level but never at the top level of a file, which is where every case input begins. ## Handshakes - **Consumes** `vlog_formatter::{format_source, Config}` only. If the signature of `format_source` changes, `tests/cases.rs` needs the same edit as `tests/fixtures.rs`. - **Consumes** the runtime verification added by the correctness-safety-net work (`src/verify.rs`): `format_source` now returns `Err` when equivalence or convergence fails, and the harness reports that as `format failed: ...`. Cases that used to produce (wrong) output now produce an error instead — three of the bugs below are of that shape. - **Promises** nothing to other modules; nothing imports this test. - **Requests** to other agents: none. All findings below are reported, not fixed — formatter `src/` is out of this feature's scope. ## File format One `.cases` file per construct area, under `crates/vlog_formatter/tests/cases/`. Within a file: ```text # a short name for the case module m;wire w;endmodule ---- module m; wire w; endmodule ==== # the next case ... ``` - A line that is exactly `====` separates cases. The last case needs no trailing separator. - A line that is exactly `----` separates input from expected output. - Lines starting with `#` at the *top* of a case are its name (joined with spaces). Once a non-`#`, non-blank line is seen, the input has started and `#` is no longer special. - A case with a `#` header line that *starts with* `SKIP` (written `#SKIP ...`; `SKIP` elsewhere in a name has no effect) is the parking lot for inputs the formatter gets wrong: the expected half holds the output we *want*. The harness asserts a `#SKIP` case still *fails* — the moment the bug is fixed the suite demands the `#SKIP` line be deleted, turning the case into a live regression test. - Do not use bare `----` or `====` lines *inside* case content (e.g. as an ASCII banner in a block comment) — they are always parsed as separators and will silently split the case at the wrong place. - Both halves are compared with a trailing newline, matching what `format_source` produces. The harness (`crates/vlog_formatter/tests/cases.rs`, ~170 lines, std only) discovers every `*.cases` file, parses it, and for each case asserts `format(input) == expected` then `format(expected) == expected`. Failures are collected (not aborted on the first one) and reported as `file:line [case name]` plus a line-oriented diff (`-` expected, `+` got). ## Status / TODO - [x] Harness `crates/vlog_formatter/tests/cases.rs` with discovery, `====` / `----` parsing, `#SKIP` support, per-case diff reporting. - [x] `module_headers.cases` — 17 cases - [x] `declarations.cases` — 18 cases - [x] `assignments.cases` — 14 cases - [x] `statements.cases` — 26 cases - [x] `instantiation.cases` — 18 cases - [x] `expressions.cases` — 26 cases - [x] `comments.cases` — 22 cases - [x] `generate.cases` — 12 cases - [x] `functions_tasks.cases` — 15 cases - [x] `typedef.cases` — 15 cases - [x] TODO.md §5 "massive text-in/text-out table tests" checked off. - [ ] Port more cases directly from Verible's `formatter_test.cc` (only a thematic sample is covered so far; the count should grow by an order of magnitude). - [ ] Cases for `--indent` / `--line-length` other than the defaults, once there is a way to express config in a case file. - [ ] Re-enable the four `#SKIP` cases as the bugs below are fixed. - [ ] Close QA coverage gaps: `final` blocks, bare module-level `case` (without a `generate` wrapper), an `initial fork ... join` case backing the style observation, comma-separated sensitivity lists, multiple `import`s in one module. ### Suspected bugs (not fixed — out of scope for this feature) **1. Multi-line block comments are re-indented on every pass (idempotency violation, hard invariant #2).** Highest severity: it breaks a documented hard invariant and makes any file with a banner comment inside a module body unformattable. ```verilog module m; /* * doc block */ wire w; endmodule ``` Pass 1 produces ` * doc block` (2 spaces of indent prepended to the original 1 space), pass 2 produces ` * doc block`, pass 3 seven spaces — it grows without bound. With runtime convergence checking in place this now fails outright: ``` error: verification failed: formatting is not idempotent; line 3 changed from " * doc block" to " * doc block" ``` The continuation lines of a block comment should be re-indented relative to the comment's *original* opening column, not by prepending the current indent to whatever the previous pass left. Parked as `#SKIP` in `comments.cases`. **2. A comment inside an expression is lifted out of its group instead of forcing it to break.** `README.md` states "A comment inside a group forces it to break"; that holds for port lists, connection lists, case items and struct members, but not for expression groups. ```verilog module m; assign y = { a, // high b }; endmodule ``` Before runtime verification existed this emitted `assign y = {a, b}; // high` — the comment survived (invariant #3 held) but moved out of the concatenation and changed meaning. Now it is rejected: ``` error: verification failed: token stream diverges at index 9: input has comment `// high` at 3:2, output has identifier `b` at 2:18 ``` The same happens for a comment inside parentheses (`(a // hi\n + b)`), inside a call argument list (`f(a, // hi\n b)`), and inside an assignment pattern (`'{a: 1, // hi\n b: 2}`). Parked as `#SKIP` in `comments.cases`. **3. Closing block labels are dropped.** `endmodule : m` is preserved, but every other closing label is silently discarded: | input | output | |---|---| | `initial begin : main ... end : main` | `... end` | | `fork : f ... join : f` | `... join` | | `function int f(); ... endfunction : f` | `... endfunction` | | `task t; ... endtask : t` | `... endtask` | | `package p; ... endpackage : p` | `... endpackage` | This is token loss, so with runtime verification it is now a hard error (`token stream diverges ... input has symbol ':' ... output has keyword 'endmodule'`). Parked as `#SKIP` in `statements.cases`; the opening label (`begin : main`) is handled correctly and is covered by a passing case. **4. A declaration with several instances always breaks, and the continuation is not indented.** ```verilog module m; sub u0(.a(x)), u1(.a(y)); endmodule ``` gives ```verilog module m; sub u0 (.a(x)), u1 (.a(y)); endmodule ``` Two problems: the whole thing is 30 columns wide and should stay on one line, and the continuation sits at the same indent as the head, so `u1` reads as a new statement. Parked as `#SKIP` in `instantiation.cases` with the one-line form as the wanted output. ### Style observations (kept as expected output — defensible, but worth a look) - Unpacked dimensions and instance-array ranges get a space before the bracket: `logic [7:0] mem [0:255];`, `sub u0 [3:0] (...)`. Verible writes `mem[0:255]` and `u0[3:0]`; packed dimensions after a type keyword do take a space in both. Whichever way this lands, it should be stated in `README.md` next to the "tight dimension expressions" rule. - A long operator chain indents its first operand two columns less than the rest: ```verilog assign ready_signal = first_condition_name && second_condition_name && third_condition_name; ``` A flat chain reads better all at one indent. - `timeunit 1ns/1ps;` becomes `timeunit 1ns / 1ps;` — the `/` there is a separator, not a division operator. - `initial begin` cuddles, but `initial fork` does not: `initial` and `fork` end up on separate lines. - A blank line between a leading comment and the item it documents is removed (`// between\n\nmodule b;` → `// between\nmodule b;`). Reasonable, since the comment binds to the following item, but it is a silent change. - `typedef struct packed {...}` always breaks one member per line even when it would fit; enums break only when they must. ## Work log **2026-08-18 — dev agent** - Wrote `crates/vlog_formatter/tests/cases.rs`. Chose the `====` / `----` plain-text format over one-file-per-case (thousands of tiny files) and over a Rust `&[(&str, &str)]` table (unreadable escaping for multi-line Verilog). The whole harness is one integration test that collects all failures before asserting, so a behavior change shows every affected case at once. - Added `#SKIP`, which was not in the original plan, once the first genuinely-broken input turned up. Without it the choice would have been between committing a red test and losing the finding; with it, the wanted output is committed and the case flips green when the bug is fixed. - Derived every expected output by piping the input through `target/debug/lucisv -`, then reviewed each one by hand against the "Style" section of the top-level `README.md`. Four outputs failed that review and became the `#SKIP` cases; six more are recorded above as style observations where the output is defensible but debatable. - Mid-run the correctness-safety-net agent landed `src/verify.rs`, which made bugs 1-3 hard errors instead of quietly wrong output. Rebuilt and re-derived every expected output against the new binary; no passing case changed. Confirmed `verify.rs` deliberately whitelists the `@*` → `@(*)` spelling, so the `always @*` case keeps `@(*)` as expected output. - 183 cases total, 179 asserted, 4 `#SKIP`. `cargo test` green, `cargo clippy --all-targets` reports nothing for `tests/cases.rs`, `cargo fmt --check` reports nothing for `tests/cases.rs` (it does report pre-existing diffs across `src/` files owned by other agents). ## QA log **2026-08-18 — QA agent** Ran the full check suite, audited `tests/cases.rs` line by line with deliberate probe `.cases` files (added, exercised, then deleted — none left in the tree), spot-checked case output against the top-level `README.md` Style section (read essentially every case across all 10 files, not just a sample), and reproduced all four `#SKIP` bugs by temporarily stripping the `#SKIP` prefix in the actual `.cases` files and re-running `cargo test`, then restoring them byte-for-byte (`diff` confirmed clean). **Results** - `cargo test` (full workspace): green. `cases_table` reports 179 asserted (183 parsed − 4 skip), matching the claimed count exactly; independently recounted per-file with a script and got the same 17/18/14/26/18/26/22/12/ 15/15 = 183, 4 skip. - `cargo clippy --all-targets`: 4 pre-existing warnings, none in `tests/cases.rs` (`format.rs` dead_code, `printer.rs` manual_repeat_n, `parser.rs` if_same_then_else x2). Confirmed via `grep -i cases.rs` on the clippy output. - `cargo fmt --check`: 151 diff blocks across `src/`, none in `tests/cases.rs`. Did not run in write mode. - Un-skipping all four `#SKIP` cases at once and re-running `cargo test -p vlog_formatter --test cases` reproduces exactly the four documented failures, with error text matching the README's bug write-ups verbatim (idempotency divergence at line 3, token-stream divergence at index 9/12, and the un-indented multi-instance continuation). Also reproduced each bug independently via `target/debug/lucisv -` on a minimal repro; error messages match character-for-character. **Findings** *Should-fix* 1. **Stale `#SKIP` cases are not detected.** `cases.rs:138-140` `continue`s on `case.skip` before ever calling `format_source`, so a case marked `#SKIP` that quietly starts passing (bug fixed elsewhere, or a bad `#SKIP` added by mistake) gives zero signal — `cargo test` stays green forever. Verified by adding a `#SKIP`-tagged case whose expected output already matches actual output today; the suite reports `ok` with no indication anything is skipped. Since the whole point of `#SKIP` (per this README) is "delete the line the moment the bug is fixed," nothing currently prompts that deletion. A cheap fix would be to assert skipped cases *do* fail (i.e., invert the check) so a newly-passing case turns into a failure demanding the `#SKIP` be removed — that's a design change for the dev agent to consider, not something I should make. 2. **A bare `----` or `====` line inside case *content* (e.g. a block-comment ASCII banner) silently misparses instead of erroring clearly.** Probed with a case whose input contains a legitimate `/* ---- */` banner line; the harness split the case at the banner instead of the real `----` separator, and the failure surfaced as an opaque `lex error: unterminated block comment` rather than anything pointing at the ambiguity. No existing case triggers this (checked all 10 files for bare `----`/`====` lines outside separators — none found), so it's latent, not live. Worth a one-line callout in the "File format" section warning case authors off using bare `----`/`====` as content, since bug #1 (block comments) is exactly the kind of case most likely to grow a banner divider later. 3. **Coverage gaps against `README.md` "Supported syntax":** no case exercises a `final` block (`initial`/`final` are both listed as supported), and no case exercises a bare module-level `case` without a `generate` wrapper (module-level `if`/`for` both have non-generate cases in `generate.cases`, `case` doesn't). Confirmed both parse and format correctly via `target/debug/lucisv -` — the formatter isn't wrong, the table just doesn't pin either down. Minor others: `always` with a comma-separated (not `or`-separated) sensitivity list, multiple `import` statements in one module. None of these are blocking; they're the kind of gap the TODO already flags generally ("port more cases … the count should grow by an order of magnitude"). 4. **The "defensible style observations" list has no case backing the `initial fork` claim.** The README's style-observations section states "`initial begin` cuddles, but `initial fork` does not." I confirmed this is true (`target/debug/lucisv -` on `initial fork ... join` puts `initial` and `fork` on separate lines), but no `.cases` file actually contains an `initial fork` (only `fork` nested inside `initial begin`, in `statements.cases`). The other five observations are backed by real cases (unpacked-dim spacing: `declarations.cases`/`instantiation.cases`; operator-chain indent and the exactly-100-column boundary: `expressions.cases`; `timeunit` spacing and blank-line-after-comment collapse: `module_headers.cases`/`comments.cases`; struct-always-breaks: `typedef.cases`). None of the six observations actually contradict a written Style rule — all are about behavior the Style section is simply silent on, so "defensible" is the right call for all six. *Nit* 5. `README.md`'s bug #4 write-up says the one-line multi-instance form "is 30 columns wide"; the actual wanted output (` sub u0 (.a(x)), u1 (.a(y));`) is 29 columns. Off by one, cosmetic only, not fixing since it doesn't affect the finding. 6. The File-format section describes `#SKIP` as "a case whose name *contains* SKIP," but the parser (`split_case`, `cases.rs:57-60`) only sets the skip flag when a `#`-line *starts with* `SKIP` after trimming, not when `SKIP` appears anywhere in the assembled name. Harmless today — all four real `#SKIP` cases use the line-initial form the code expects — but a future case author relying on the literal wording could write `# note: might SKIP this` expecting it to be skipped and be wrong. **Harness audit (no bugs found beyond the above)** - Discovery (`cases_dir`/`read_dir`) panics loudly if the directory is missing (`.expect("cases dir")`); an empty-but-present directory is caught by `assert!(!files.is_empty(), ...)`. Neither path passes vacuously. (Verified by code inspection only — did not delete the real directory to test live, to avoid disturbing other in-flight work.) - Last case needing no trailing `====`: verified live (probe file, single case, no trailing separator, parsed and ran correctly). - Missing `----` separator: verified live, fails loudly with correct `file:line` (`zz_probe2.cases:1: case has no ` + "`----`" + ` separator`). - CRLF line endings: verified live with a CRLF-only probe file; `str::lines` strips the `\r`, no corruption, case parses and runs correctly. No CRLF files exist in the real `tests/cases/` tree today. - Blank lines directly after a `====` separator (before the next case's name/content) were checked for across all 10 files — none exist, so the theoretical off-by-one in `start` line tracking (a blank line right after `====` would itself become `start`, one line before the name) is latent, not live. - No case has an empty input or empty expected half (checked programmatically across all 10 files). - `file:line` accuracy for a failure in the *second* case of a file (not just the first) was verified live and points at the exact line of the case's `#` header, not the file start or the previous case. - Diff rendering (`-` expected / `+` got) matches its documented convention, confirmed on a live failing probe case. **Spot-check of case quality** Read essentially all 183 cases (not just ~20) against the Style section. Nothing found where an expected output was rubber-stamping formatter behavior that contradicts a *written* Style rule — the closest candidates (unpacked-dim spacing, operator-chain indent, struct-always-breaks) are all already called out as observations, and none of them contradict a stated rule since the Style section doesn't speak to any of them. The "exactly 100 columns stays on one line" case (`expressions.cases:231`) was checked byte-for-byte: the expected line is exactly 100 columns. All four `#SKIP` expected outputs match the "wanted" output described in the bug write-ups and are not what the formatter currently produces (confirmed above). **Claim verdicts** 1. Harness behavior (discovery, `====`/`----`/`#`/`#SKIP` parsing, dual assertion, collected failures with file:line + diff): **pass**, with should-fix findings #1 and #2 above (both about corner cases the current design doesn't defend against, not about anything mis-implemented relative to the documented spec). 2. 183 cases (179 asserted + 4 SKIP) across 10 files, hand-reviewed against Style: **pass**, counts verified independently; found no case that contradicts a written rule. 3. Four suspected bugs + six style observations: **pass**. All four bugs reproduce exactly as documented (byte-identical error messages) both via the harness and via the standalone binary. Five of six observations are directly backed by a case; the sixth (`initial fork`) is true but untested (finding #4). ## Review **2026-08-18 — lead. Verdict: accepted and committed, with three lead fixes.** Good work on both sides. The harness is exactly the right size (~180 lines, std only), the format is readable, and the dev agent's decision to report formatter bugs instead of rubber-stamping output is the most valuable thing this feature produced — all four bugs are real, and two of them break hard invariants. The QA pass was genuinely adversarial (probe files, un-skip + restore, byte-level recounts) and left the tree clean; that is the standard going forward. Changes I made while accepting (small and unambiguous, so lead-fixed rather than another dev round): 1. **Inverted `#SKIP` semantics** (QA finding 1): a `#SKIP` case must still *fail*; if it starts passing the suite now fails with "remove the #SKIP marker". `tests/cases.rs` gained a small `passes()` helper. Verified: all four parked bugs still fail, suite green. 2. **File-format doc corrections** (QA findings 2 and 6): documented the line-initial `#SKIP` rule as implemented, and warned against bare `----`/`====` banner lines inside case content. The parser was right; the prose was loose. 3. **Coverage gaps → TODO** (QA findings 3 and 4): `final`, bare module-level `case`, `initial fork`, comma sensitivity lists, multiple imports — recorded as an unchecked TODO item for the next dev round on this feature, not silently dropped. Direction for the next round: the four suspected bugs move to a dedicated fix feature (they are formatter work, not test work); the six style observations should each get a one-line ruling in the top-level `README.md` Style section once decided — the QA agent is right that "defensible" only holds while the Style section is silent.