Files
lucisv/crates/vlog_formatter/tests/fences.rs
T

112 lines
5.4 KiB
Rust

//! `// verilog_format: off` fences: the properties the `.cases` table cannot
//! state directly — byte-exactness of the spliced range, idempotency of a
//! second pass, and that the safety net still rejects a *corrupted* region.
//! Spec: `docs/features/format-fences/README.md`.
use vlog_formatter::fence::{fence_of, Fence};
use vlog_formatter::{format_source, verify_equivalence, Config};
fn fmt(src: &str) -> String {
format_source(src, &Config::default()).expect("format")
}
/// Format, then assert the three properties every fenced input must have:
/// the region survives byte-for-byte, the output re-lexes to the input, and
/// formatting again is a fixed point.
fn fenced(src: &str, region: &str) -> String {
let out = fmt(src);
assert!(src.contains(region), "the region is not in the input:\n{region}");
assert!(out.contains(region), "region not emitted byte-exact:\nwant\n{region}\ngot\n{out}");
verify_equivalence(src, &out).expect("output is token-equivalent to the input");
assert_eq!(fmt(&out), out, "second pass changed the output");
out
}
#[test]
fn a_region_is_emitted_byte_for_byte() {
// Interior spacing, alignment, trailing comments and a blank line inside
// the region all survive; the item after it is formatted normally.
let src = "module m;\n // lucisv: off\n wire [3:0] a = 4'h1; // lo\n\n wire [31:0] b= 32'h2;\n // lucisv: on\n wire c;\nendmodule\n";
let region =
" // lucisv: off\n wire [3:0] a = 4'h1; // lo\n\n wire [31:0] b= 32'h2;\n // lucisv: on";
let out = fenced(src, region);
assert!(out.ends_with(" wire c;\nendmodule\n"), "{out}");
}
#[test]
fn a_region_keeps_its_own_indentation_on_every_line() {
// The source is indented four spaces inside a module the formatter
// indents by two: the region's first line is not re-indented either.
let src =
"module m;\n // lucisv: off\n wire a;\n // lucisv: on\n wire b;\nendmodule\n";
let out = fenced(src, " // lucisv: off\n wire a;\n // lucisv: on");
assert!(
out.contains("\n wire b;\n"),
"formatting did not resume at the module indent:\n{out}"
);
}
#[test]
fn an_unmatched_off_runs_to_the_end_of_its_scope_only() {
// It must stop at the module's last item — the comment trailing that item
// included, since `Meta.trailing` has no span of its own to splice from —
// and the next module must still be formatted.
let src = "module m;\n // lucisv: off\n wire a; // note\nendmodule\n\nmodule n;\n wire b;\nendmodule\n";
let out = fenced(src, " // lucisv: off\n wire a; // note");
assert!(out.contains("module n;\n wire b;\n"), "the next module was not formatted:\n{out}");
}
#[test]
fn comments_before_the_off_fence_are_still_formatted_and_never_dropped() {
let src =
"module m;\n// doc for the table\n// lucisv: off\nwire a;\n// lucisv: on\nendmodule\n";
let out = fenced(src, "// lucisv: off\nwire a;\n// lucisv: on");
assert!(out.contains(" // doc for the table\n"), "leading comment lost or unindented:\n{out}");
}
#[test]
fn a_corrupted_fenced_region_is_still_rejected_by_the_safety_net() {
// The fence is not a licence to change bytes: a hand-built "output" that
// reformats, drops or adds anything inside the region must fail
// equivalence exactly as it would outside one.
let src = "module m;\n // lucisv: off\n wire [3:0] a;\n wire b; // note\n // lucisv: on\nendmodule\n";
let corrupted = [
// a token dropped from inside the region
"module m;\n // lucisv: off\n wire a;\n wire b; // note\n // lucisv: on\nendmodule\n",
// the region's trailing comment dropped
"module m;\n // lucisv: off\n wire [3:0] a;\n wire b;\n // lucisv: on\nendmodule\n",
// a fence comment itself dropped
"module m;\n wire [3:0] a;\n wire b; // note\n // lucisv: on\nendmodule\n",
// a token added inside the region
"module m;\n // lucisv: off\n wire [3:0] a, z;\n wire b; // note\n // lucisv: on\nendmodule\n",
// a fence turned into an ordinary comment
"module m;\n // lucisv: off\n wire [3:0] a;\n wire b; // note\n // lucisv: onward\nendmodule\n",
];
// The real formatter gets it right...
verify_equivalence(src, &fmt(src)).expect("the real output is equivalent");
// ...and every corruption of it is caught.
for bad in corrupted {
assert!(
verify_equivalence(src, bad).is_err(),
"a corrupted fenced region was accepted:\n{bad}"
);
}
}
#[test]
fn the_matcher_agrees_with_what_the_formatter_honours() {
// The unit tests in `fence.rs` pin the matcher; this pins that the
// formatter is driven by it, in both directions.
let body = "module m;\n {}\n wire a;\nendmodule\n";
for spelling in ["// verilog_format: off", "//lucisv: off", "//\tverilog_format: off"] {
assert_eq!(fence_of(spelling), Some(Fence::Off));
let out = fmt(&body.replace("{}", spelling));
assert!(out.contains("wire a;"), "{spelling} did not fence:\n{out}");
}
for spelling in ["// lucisv: off now", "// lucisv:off", "/* lucisv: off */", "// LUCISV: OFF"] {
assert_eq!(fence_of(spelling), None);
let out = fmt(&body.replace("{}", spelling));
assert!(out.contains("wire a;"), "{spelling} fenced but should not have:\n{out}");
}
}