Files
lucisv/crates/vlog_refactor/tests/expand.rs
T
lucioandClaude Opus 5.5 ca83f6f99f Review lows L-1–L-4, L-12, L-13: end labels, duplicate generate labels, dotted comments, lex-failure fallbacks, file naming
- L-1: an unowned block's closing label is a use of a same-named block only
  for block constructs and only when it spells the construct's own opening
  label (use_block_end_label); a mismatched `end : a` or an `endfunction : g`
  beside block `g` records nothing.
- L-2: duplicate-declaration stays silent for two block labels only when
  both may be arms of one generate if/case; sibling `for` blocks and a
  procedural block beside a generate one are reported in either order.
  OpenTitan check: 1 158 findings, identical to 0.60.0.
- L-3: a block comment between `.` and the name is still dotted.
- L-4: a bind file that does not lex counts as binding (K2); a pasted
  fragment that does not lex is said, not skipped.
- L-12: no unchecked slice in rename/arms.rs.
- L-13: expand/ansi/inline/extract messages name files by Scope::name_of.

7 tests (L-3/L-4's fail with the fixes disabled). Per-crate suites under the
guard: 80 suites, 5 085 passed, 0 failed, 51 ignored (one combined run was
cut at 1 800 s in vlog_refactor/tests/corpus.rs with syspolicyd at 95 % —
the §65.3 stall).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-14 13:33:27 -07:00

430 lines
19 KiB
Rust

//! Behavior tests for `expand_wildcard`: what the `.*` expansion writes, what
//! it refuses, and what the formatter says about the result.
//!
//! Every test states the whole workspace and asserts on the *applied* text,
//! because that is what the user ends up with. A refusal is asserted on its
//! sentence, since that sentence is the entire answer the editor shows.
use std::path::PathBuf;
use vlog_refactor::{expand_wildcard, port_actions, RefactorEdit};
/// A workspace: `(path, text)` pairs, the shape the engine takes.
fn files(pairs: &[(&str, &str)]) -> Vec<(PathBuf, String)> {
pairs.iter().map(|(path, text)| (PathBuf::from(*path), text.to_string())).collect()
}
fn at(text: &str, needle: &str) -> u32 {
text.find(needle).expect("needle present") as u32
}
/// One file's text with the refactoring applied, the way a client applies a
/// `WorkspaceEdit`: back to front, so every range still means what it said.
fn applied(files: &[(PathBuf, String)], edit: &RefactorEdit, path: &str) -> String {
let path = PathBuf::from(path);
let mut text = files
.iter()
.find(|(p, _)| *p == path)
.map(|(_, text)| text.clone())
.expect("a file in the workspace");
for change in edit.edits_for(&path).iter().rev() {
text.replace_range(
change.range.start as usize..change.range.end as usize,
&change.new_text,
);
}
text
}
/// The three-port module every test instantiates.
const SUB: &str =
"module sub (\n input logic clk,\n input logic rst,\n output logic done\n);\nendmodule\n";
/// The workspace, the expansion, and the cursor's file afterwards.
fn expand(top: &str, needle: &str) -> Result<(RefactorEdit, String), String> {
let workspace = files(&[("/rtl/sub.sv", SUB), ("/rtl/top.sv", top)]);
let edit = expand_wildcard(&workspace, &PathBuf::from("/rtl/top.sv"), at(top, needle))?;
let text = applied(&workspace, &edit, "/rtl/top.sv");
Ok((edit, text))
}
/// What the refusal at this cursor says.
fn refusal(top: &str, needle: &str) -> String {
expand(top, needle).expect_err("a refusal").to_string()
}
// ----------------------------------------------------------------------
// what it writes
// ----------------------------------------------------------------------
#[test]
fn the_wildcard_becomes_every_port_the_instance_does_not_name() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (\n .clk(clk),\n .*\n );\nendmodule\n";
let (edit, text) = expand(top, ".*").expect("expands");
assert_eq!(
text,
"module top;\n logic clk, rst, done;\n sub u_sub (\n .clk(clk),\n .rst(rst),\n .done(done)\n );\nendmodule\n"
);
assert_eq!(edit.file_count(), 1, "nothing outside the cursor's file is touched");
assert!(edit.warnings.is_empty(), "{:?}", edit.warnings);
}
#[test]
fn an_inline_list_stays_inline() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (.clk(clk), .*);\nendmodule\n";
let (_, text) = expand(top, ".*").expect("expands");
assert_eq!(
text,
"module top;\n logic clk, rst, done;\n sub u_sub (.clk(clk), .rst(rst), .done(done));\nendmodule\n"
);
}
#[test]
fn a_wildcard_alone_on_its_line_writes_one_connection_per_line() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (\n .*\n );\nendmodule\n";
let (_, text) = expand(top, ".*").expect("expands");
assert_eq!(
text,
"module top;\n logic clk, rst, done;\n sub u_sub (\n .clk(clk),\n .rst(rst),\n .done(done)\n );\nendmodule\n"
);
}
#[test]
fn a_wildcard_alone_inline_writes_one_line() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (.*);\nendmodule\n";
let (_, text) = expand(top, ".*").expect("expands");
assert_eq!(
text,
"module top;\n logic clk, rst, done;\n sub u_sub (.clk(clk), .rst(rst), .done(done));\nendmodule\n"
);
}
#[test]
fn a_port_with_no_signal_in_scope_is_written_unconnected_and_said_out_loud() {
let top = "module top;\n logic clk, rst;\n sub u_sub (.*);\nendmodule\n";
let (edit, text) = expand(top, ".*").expect("expands");
assert_eq!(
text,
"module top;\n logic clk, rst;\n sub u_sub (.clk(clk), .rst(rst), .done());\nendmodule\n"
);
let warning = edit.warnings.join("\n");
assert!(warning.contains("`done`"), "{warning}");
assert!(warning.contains("no net, variable or port named"), "{warning}");
assert!(warning.contains("top.sv:3"), "the site is named: {warning}");
}
#[test]
fn a_same_named_parameter_is_not_a_signal_the_wildcard_binds() {
// IEEE 1800 §23.3.2.4: `.*` binds a port to a net or variable of the same
// name. `done` here is a constant, and `.done(done)` would connect an
// output port to it — legal-looking, silent, and wrong.
let top = "module top;\n logic clk, rst;\n localparam int done = 1;\n sub u_sub (.*);\nendmodule\n";
let (edit, text) = expand(top, ".*").expect("expands");
assert!(text.contains(".done()"), "{text}");
assert!(!text.contains(".done(done)"), "the parameter is not bound: {text}");
assert!(edit.warnings.join("\n").contains("`done`"), "{:?}", edit.warnings);
// ...and the same for a `parameter` in the header.
let top = "module top #(parameter int done = 1) ();\n logic clk, rst;\n sub u_sub (.*);\nendmodule\n";
let (_, text) = expand(top, ".*").expect("expands");
assert!(text.contains(".done()"), "{text}");
}
#[test]
fn a_genvar_of_the_same_name_is_not_bound_either() {
let sub = "module sub (\n input logic clk,\n input logic i\n);\nendmodule\n";
let top = "module top;\n logic clk;\n for (genvar i = 0; i < 2; i++) begin : g\n sub u_sub (.*);\n end\nendmodule\n";
let workspace = files(&[("/rtl/sub.sv", sub), ("/rtl/top.sv", top)]);
let edit =
expand_wildcard(&workspace, &PathBuf::from("/rtl/top.sv"), at(top, ".*")).expect("expands");
let text = applied(&workspace, &edit, "/rtl/top.sv");
assert!(text.contains(".clk(clk), .i()"), "a genvar is not a net or a variable: {text}");
}
#[test]
fn a_generate_blocks_own_declaration_is_in_scope_and_so_is_the_modules() {
let top = "module top;\n logic clk, rst;\n if (1) begin : g\n logic done;\n sub u_sub (.*);\n end\nendmodule\n";
let (edit, text) = expand(top, ".*").expect("expands");
assert!(
text.contains("sub u_sub (.clk(clk), .rst(rst), .done(done));"),
"the block's `done` and the module's `clk`/`rst`: {text}"
);
assert!(edit.warnings.is_empty(), "{:?}", edit.warnings);
}
#[test]
fn ports_are_written_in_the_modules_declaration_order() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (.done(done), .*);\nendmodule\n";
let (_, text) = expand(top, ".*").expect("expands");
assert!(text.contains(".done(done), .clk(clk), .rst(rst)"), "{text}");
}
#[test]
fn a_wildcard_written_before_an_explicit_connection_expands_in_place() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (.*, .done(done));\nendmodule\n";
let (_, text) = expand(top, ".*").expect("expands");
assert!(text.contains("(.clk(clk), .rst(rst), .done(done))"), "{text}");
}
#[test]
fn an_implicit_or_empty_connection_counts_as_explicit() {
// `.clk` and `.rst()` are connections the source wrote, so `.*` never
// bound those ports and the expansion must not write them a second time.
let top = "module top;\n logic clk, rst, done;\n sub u_sub (.clk, .rst(), .*);\nendmodule\n";
let (_, text) = expand(top, ".*").expect("expands");
assert_eq!(
text,
"module top;\n logic clk, rst, done;\n sub u_sub (.clk, .rst(), .done(done));\nendmodule\n"
);
}
// ----------------------------------------------------------------------
// comments
// ----------------------------------------------------------------------
#[test]
fn the_comment_after_the_wildcard_stays_with_the_first_connection() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (\n .clk(clk),\n .* // and the rest\n );\nendmodule\n";
let (_, text) = expand(top, ".*").expect("expands");
assert_eq!(
text,
"module top;\n logic clk, rst, done;\n sub u_sub (\n .clk(clk),\n .rst(rst), // and the rest\n .done(done)\n );\nendmodule\n"
);
}
#[test]
fn the_comment_before_the_wildcard_stays_before_the_first_connection() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (\n .clk(clk),\n // bind the rest\n .*\n );\nendmodule\n";
let (_, text) = expand(top, ".*").expect("expands");
assert_eq!(
text,
"module top;\n logic clk, rst, done;\n sub u_sub (\n .clk(clk),\n // bind the rest\n .rst(rst),\n .done(done)\n );\nendmodule\n"
);
}
#[test]
fn a_block_comment_written_inside_the_wildcard_refuses_rather_than_dropping_it() {
// The comment sits between the `.` and the `*`, inside the element the
// expansion replaces. Comments are never dropped (invariant 3), and lucisv
// will not decide where this one should move.
let top = "module top;\n logic clk, rst, done;\n sub u_sub (. /* here */ *);\nendmodule\n";
let said = refusal(top, ". /* here */ *");
assert!(said.contains("comments are never dropped"), "{said}");
}
// ----------------------------------------------------------------------
// refusals
// ----------------------------------------------------------------------
#[test]
fn an_instantiation_without_a_wildcard_is_refused() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (.clk(clk));\nendmodule\n";
assert!(refusal(top, "u_sub").contains("writes no `.*` to expand"));
}
#[test]
fn a_position_that_is_not_an_instantiation_is_refused() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (.*);\nendmodule\n";
assert!(refusal(top, "logic clk").contains("no module instantiation at this position"));
}
#[test]
fn a_positional_connection_list_is_refused() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (clk, rst, done);\nendmodule\n";
let said = refusal(top, "u_sub");
assert!(said.contains("by position"), "{said}");
assert!(said.contains("no `.*` to expand"), "{said}");
}
#[test]
fn two_wildcards_in_one_instance_are_refused() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (.*, .*);\nendmodule\n";
let said = refusal(top, ".*");
assert!(said.contains("writes `.*` 2 times"), "{said}");
assert!(said.contains("illegal"), "{said}");
}
#[test]
fn a_wildcard_inside_a_bind_directive_is_refused() {
let top = "module top;\n logic clk, rst, done;\nendmodule\nbind top sub u_sub (.*);\n";
let said = refusal(top, ".*");
assert!(said.contains("`bind` directive"), "{said}");
assert!(said.contains("target's scope"), "{said}");
}
#[test]
fn a_module_the_workspace_does_not_declare_is_refused() {
let top = "module top;\n logic clk;\n nowhere u_x (.*);\nendmodule\n";
assert!(refusal(top, ".*").contains("indexes no module named `nowhere`"));
}
#[test]
fn a_module_declared_twice_is_refused() {
let workspace = files(&[
("/rtl/sub.sv", SUB),
("/rtl/other.sv", SUB),
("/rtl/top.sv", "module top;\n logic clk, rst, done;\n sub u_sub (.*);\nendmodule\n"),
]);
let top = &workspace[2].1.clone();
let said = expand_wildcard(&workspace, &PathBuf::from("/rtl/top.sv"), at(top, ".*"))
.expect_err("a refusal");
assert!(said.contains("declared in more than one indexed file"), "{said}");
}
#[test]
fn a_file_that_does_not_parse_is_refused_on_either_side() {
// The cursor's own file.
let top = "module top;\n logic clk;\n sub u_sub (.*);\n always @(begin\nendmodule\n";
let said = refusal(top, ".*");
assert!(said.contains("does not parse"), "{said}");
assert!(said.contains("top.sv"), "{said}");
// ...and the module's.
let workspace = files(&[
("/rtl/sub.sv", "module sub (input logic clk);\n always @(begin\nendmodule\n"),
("/rtl/top.sv", "module top;\n logic clk;\n sub u_sub (.*);\nendmodule\n"),
]);
let top = workspace[1].1.clone();
let said = expand_wildcard(&workspace, &PathBuf::from("/rtl/top.sv"), at(&top, ".*"))
.expect_err("a refusal");
assert!(said.contains("sub.sv"), "{said}");
assert!(said.contains("does not parse"), "{said}");
}
#[test]
fn a_port_list_carrying_compiler_directives_is_refused() {
let workspace = files(&[
(
"/rtl/sub.sv",
"module sub (\n input logic clk\n`ifdef HAS_RST\n , input logic rst\n`endif\n);\nendmodule\n",
),
("/rtl/top.sv", "module top;\n logic clk, rst;\n sub u_sub (.*);\nendmodule\n"),
]);
let top = workspace[1].1.clone();
let said = expand_wildcard(&workspace, &PathBuf::from("/rtl/top.sv"), at(&top, ".*"))
.expect_err("a refusal");
assert!(said.contains("carries compiler directives"), "{said}");
assert!(said.contains("depends on the configuration"), "{said}");
}
#[test]
fn a_connection_list_carrying_compiler_directives_is_refused() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (\n .clk(clk),\n`ifdef X\n .rst(rst),\n`endif\n .*\n );\nendmodule\n";
let said = refusal(top, ".*\n )");
assert!(said.contains("connection list carries compiler directives"), "{said}");
}
#[test]
fn a_wildcard_that_binds_nothing_is_refused_rather_than_deleted() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (.clk(clk), .rst(rst), .done(done), .*);\nendmodule\n";
let said = refusal(top, ".*");
assert!(said.contains("already connects every port"), "{said}");
assert!(said.contains("binds nothing"), "{said}");
}
#[test]
fn a_statement_declaring_several_instances_asks_for_a_cursor_on_one() {
let top = "module top;\n logic clk, rst, done;\n sub u_a (.*), u_b (.*);\nendmodule\n";
let said = refusal(top, "sub u_a");
assert!(said.contains("declares 2 instances"), "{said}");
assert!(said.contains("put the cursor on the one to expand"), "{said}");
}
#[test]
fn a_file_outside_the_workspace_is_refused() {
let workspace = files(&[("/rtl/sub.sv", SUB)]);
let said =
expand_wildcard(&workspace, &PathBuf::from("/rtl/elsewhere.sv"), 0).expect_err("a refusal");
assert!(said.contains("is not one of the files lucisv indexes"), "{said}");
}
// ----------------------------------------------------------------------
// the code action, and what the formatter says
// ----------------------------------------------------------------------
#[test]
fn the_action_is_offered_at_the_wildcard_with_its_count_in_the_title() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (.clk(clk), .*);\nendmodule\n";
let workspace = files(&[("/rtl/sub.sv", SUB), ("/rtl/top.sv", top)]);
let actions = port_actions(&workspace, &PathBuf::from("/rtl/top.sv"), at(top, ".*"));
let titles: Vec<&str> = actions.iter().map(|a| a.title.as_str()).collect();
assert!(titles.contains(&"lucisv: expand .* (2 connections)"), "{titles:?}");
// The wildcard is why `propagate` declines here, so it is the only offer.
assert_eq!(titles.len(), 1, "{titles:?}");
// One connection is one connection, not "1 connections".
let top = "module top;\n logic clk, rst, done;\n sub u_sub (.clk(clk), .rst(rst), .*);\nendmodule\n";
let workspace = files(&[("/rtl/sub.sv", SUB), ("/rtl/top.sv", top)]);
let actions = port_actions(&workspace, &PathBuf::from("/rtl/top.sv"), at(top, ".*"));
assert_eq!(actions[0].title, "lucisv: expand .* (1 connection)");
}
#[test]
fn nothing_is_offered_where_the_expansion_would_be_refused() {
let top = "module top;\n logic clk, rst, done;\n sub u_sub (.*, .*);\nendmodule\n";
let workspace = files(&[("/rtl/sub.sv", SUB), ("/rtl/top.sv", top)]);
let actions = port_actions(&workspace, &PathBuf::from("/rtl/top.sv"), at(top, ".*"));
assert!(actions.is_empty(), "{:?}", actions.iter().map(|a| &a.title).collect::<Vec<_>>());
}
#[test]
fn the_expanded_file_formats_and_formatting_it_again_changes_nothing() {
let config = vlog_formatter::Config::default();
for top in [
"module top;\n logic clk, rst, done;\n sub u_sub (.clk(clk), .*);\nendmodule\n",
"module top;\n logic clk, rst;\n sub u_sub (\n .clk(clk),\n .* // the rest\n );\nendmodule\n",
"module top;\n logic clk, rst, done;\n sub u_sub (\n .*\n );\nendmodule\n",
] {
let (_, text) = expand(top, ".*").expect("expands");
let once = vlog_formatter::format_source(&text, &config)
.unwrap_or_else(|e| panic!("the formatter refuses the expansion: {e}\n{text}"));
let twice = vlog_formatter::format_source(&once, &config).expect("formats again");
assert_eq!(once, twice, "formatting the expansion does not converge");
}
}
#[test]
fn an_escaped_port_name_is_terminated_on_both_sides_of_the_parens() {
// An escaped identifier runs to the next whitespace and to nothing else
// (IEEE 1800 §5.6.1), so `.\my-port(\my-port)` is one token that swallows
// the parens and the rest of the line. The instantiation skeleton learned
// this from QA; the expansion writes the same shape.
let sub = "module sub (\n input logic \\my-port ,\n input logic clk\n);\nendmodule\n";
let top = "module top;\n logic clk;\n logic \\my-port ;\n sub u_sub (.*);\nendmodule\n";
let workspace = files(&[("/rtl/sub.sv", sub), ("/rtl/top.sv", top)]);
let edit =
expand_wildcard(&workspace, &PathBuf::from("/rtl/top.sv"), at(top, ".*")).expect("expands");
let text = applied(&workspace, &edit, "/rtl/top.sv");
assert!(text.contains("(.\\my-port (\\my-port ), .clk(clk))"), "{text}");
let (_, diagnostics) = vlog_parser::parse_source_tolerant(&text);
assert!(diagnostics.is_empty(), "{diagnostics:?}\n{text}");
}
#[test]
fn an_unbound_escaped_port_is_terminated_too() {
let sub = "module sub (\n input logic \\my-port ,\n input logic clk\n);\nendmodule\n";
let top = "module top;\n logic clk;\n sub u_sub (.*);\nendmodule\n";
let workspace = files(&[("/rtl/sub.sv", sub), ("/rtl/top.sv", top)]);
let edit =
expand_wildcard(&workspace, &PathBuf::from("/rtl/top.sv"), at(top, ".*")).expect("expands");
let text = applied(&workspace, &edit, "/rtl/top.sv");
assert!(text.contains("(.\\my-port (), .clk(clk))"), "{text}");
let (_, diagnostics) = vlog_parser::parse_source_tolerant(&text);
assert!(diagnostics.is_empty(), "{diagnostics:?}\n{text}");
}
/// Review L-13: a refusal names a file by its path when another file of the
/// set shares its base name, as every other refactoring's message does.
#[test]
fn a_file_that_does_not_parse_is_named_by_its_path_when_its_name_is_shared() {
let broken = "module top;\n sub u (.*);\n assign = ;\nendmodule\n";
let set = files(&[
("/w/a/top.sv", broken),
("/w/b/top.sv", "module other;\nendmodule\n"),
("/w/sub.sv", "module sub (input logic clk);\nendmodule\n"),
]);
let refusal = expand_wildcard(&set, &PathBuf::from("/w/a/top.sv"), at(broken, ".*"))
.expect_err("refused");
assert!(refusal.starts_with("/w/a/top.sv does not parse"), "{refusal}");
}