Files
Guoliang ChenandClaude Mythos 5.2 71d13b5fbe
ci / checks (push) Successful in 1m0s
ci / corpus (push) Successful in 18s
ci / linux (push) Successful in 6m4s
release / build-and-publish (push) Successful in 5m31s
Release 0.66.2: every source file names its author
Every tracked file that can carry a comment (880 of them) now opens with
"Author: Guoliang Chen, a Senior Digital Design Engineer" and closes with
"Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love,
luciochen.com", in the file's own comment syntax. No behaviour change: the
diff outside the version bump and changelogs is comment lines only.

- tools/stamp_author.py: writes the lines; idempotent, `--check` lists
  what is missing. Headers go under a shebang and under Markdown front
  matter.
- Left alone: test fixtures, cases and snapshots (byte-exact inputs), JSON
  and JSONL, logs, images, lockfiles, docs/site/dist (regenerated) and the
  LICENSE files (already carry the copyright line).
- tools/linux_check.sh: --help reads its header from line 3, now that the
  author line sits under the shebang.
- tools/gates.sh green on this tree: 5 545 tests, corpus 814/6 as before.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-24 12:11:09 -08:00

450 lines
18 KiB
Rust

// Author: Guoliang Chen, a Senior Digital Design Engineer
//! Behavior tests for `organize_imports` and `unused_imports`: what the
//! organize writes, what it keeps, what it refuses, and the attack pair that
//! proves the verifier is the second net.
//!
//! 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::{organize_counts, organize_imports, unused_imports, 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
}
/// Two packages every test can import from.
const A_PKG: &str = "package a_pkg;\n typedef int a_t;\n parameter int A_W = 4;\nendpackage\n";
const B_PKG: &str = "package b_pkg;\n typedef int b_t;\n parameter int B_W = 8;\nendpackage\n";
/// The workspace, the organize at `needle`, and the cursor's file afterwards.
fn organize(top: &str, needle: &str) -> Result<(RefactorEdit, String), String> {
organize_in(
&[("/rtl/a_pkg.sv", A_PKG), ("/rtl/b_pkg.sv", B_PKG), ("/rtl/top.sv", top)],
top,
needle,
)
}
/// The same, over a workspace the test states in full.
fn organize_in(
pairs: &[(&str, &str)],
top: &str,
needle: &str,
) -> Result<(RefactorEdit, String), String> {
let workspace = files(pairs);
let edit = organize_imports(&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 {
organize(top, needle).expect_err("a refusal")
}
/// Every unused import of one file, as `(text, sentence)`.
fn unused(top: &str) -> Vec<(String, String)> {
let workspace =
files(&[("/rtl/a_pkg.sv", A_PKG), ("/rtl/b_pkg.sv", B_PKG), ("/rtl/top.sv", top)]);
unused_imports(&workspace, &PathBuf::from("/rtl/top.sv"))
.into_iter()
.map(|(span, message)| (top[span.start as usize..span.end as usize].to_string(), message))
.collect()
}
// ----------------------------------------------------------------------
// what it removes
// ----------------------------------------------------------------------
#[test]
fn an_import_nothing_uses_is_deleted_with_its_line() {
let top = "module m;\n import a_pkg::*;\n import b_pkg::*;\n b_t x;\nendmodule\n";
let (_, text) = organize(top, "module m").expect("organized");
assert_eq!(text, "module m;\n import b_pkg::*;\n b_t x;\nendmodule\n", "{text}");
}
#[test]
fn a_used_wildcard_and_a_used_item_both_stay() {
let top =
"module m;\n import b_pkg::*;\n import a_pkg::a_t;\n a_t p;\n b_t q;\nendmodule\n";
let (_, text) = organize(top, "module m").expect("organized");
assert!(text.contains("import a_pkg::a_t;") && text.contains("import b_pkg::*;"), "{text}");
assert_eq!(text.lines().nth(1), Some(" import a_pkg::a_t;"), "sorted by package: {text}");
}
#[test]
fn an_item_import_whose_name_is_never_written_goes() {
let top = "module m;\n import a_pkg::a_t;\n import a_pkg::A_W;\n a_t x;\nendmodule\n";
let (_, text) = organize(top, "module m").expect("organized");
assert_eq!(text, "module m;\n import a_pkg::a_t;\n a_t x;\nendmodule\n", "{text}");
}
#[test]
fn a_name_declared_locally_does_not_use_the_import() {
// `a_t` is a local typedef here, so the import supplies nothing.
let top = "module m;\n import a_pkg::*;\n typedef bit a_t;\n a_t x;\nendmodule\n";
let (_, text) = organize(top, "module m").expect("organized");
assert_eq!(text, "module m;\n typedef bit a_t;\n a_t x;\nendmodule\n", "{text}");
}
#[test]
fn a_qualified_use_does_not_keep_the_import() {
let top = "module m;\n import a_pkg::*;\n a_pkg::a_t x;\nendmodule\n";
let (_, text) = organize(top, "module m").expect("organized");
// The whole run goes, and so does its newline: no blank line is left
// where the imports were.
assert_eq!(text, "module m;\n a_pkg::a_t x;\nendmodule\n", "{text}");
}
#[test]
fn a_nested_block_keeps_the_outer_import() {
// Only the `initial` block names `A_W`, and only it names `b_t`.
let top = "module m;\n import b_pkg::*;\n import a_pkg::*;\n initial begin\n b_t v;\n int w = A_W;\n end\nendmodule\n";
let (_, text) = organize(top, "module m").expect("organized");
assert!(text.contains("import a_pkg::*;") && text.contains("import b_pkg::*;"), "{text}");
assert_eq!(text.lines().nth(1), Some(" import a_pkg::*;"), "sorted only: {text}");
}
#[test]
fn a_nested_class_keeps_the_package_s_import() {
// The class's own scope writes no import; the name arrives through the
// package's, so organizing the package must keep it.
let top = "package w_pkg;\n import b_pkg::*;\n import a_pkg::*;\n class c;\n a_t handle;\n b_t other;\n endclass\nendpackage\n";
let (_, text) = organize(top, "package w_pkg").expect("organized");
assert_eq!(text.matches("import").count(), 2, "both imports stay: {text}");
}
#[test]
fn a_name_shadowed_inside_a_nested_scope_still_uses_the_outer_import() {
// `a_t` is declared in the block, so *that* use is not the import's — but
// the module body's own `a_t` is.
let top = "module m;\n import a_pkg::*;\n import b_pkg::*;\n a_t outer;\n initial begin\n int a_t;\n a_t = 1;\n end\nendmodule\n";
let (_, text) = organize(top, "module m").expect("organized");
assert!(text.contains("import a_pkg::*;"), "{text}");
assert!(!text.contains("b_pkg"), "nothing names b_pkg: {text}");
}
#[test]
fn an_exact_duplicate_keeps_the_first() {
let top = "module m;\n import a_pkg::*;\n import a_pkg::*;\n a_t x;\nendmodule\n";
let (_, text) = organize(top, "module m").expect("organized");
assert_eq!(text.matches("import a_pkg::*;").count(), 1, "{text}");
}
// ----------------------------------------------------------------------
// order
// ----------------------------------------------------------------------
#[test]
fn the_run_is_sorted_by_package_then_item_with_the_wildcard_first() {
let top = "module m;\n import b_pkg::b_t;\n import a_pkg::A_W;\n import b_pkg::*;\n import a_pkg::a_t;\n a_t p;\n b_t q;\n int w = A_W;\n int v = B_W;\nendmodule\n";
let (_, text) = organize(top, "module m").expect("organized");
let run: Vec<&str> = text.lines().skip(1).take(4).collect();
assert_eq!(
run,
vec![
" import a_pkg::A_W;",
" import a_pkg::a_t;",
" import b_pkg::*;",
" import b_pkg::b_t;",
],
"{text}"
);
}
#[test]
fn organizing_twice_changes_nothing_the_second_time() {
let top = "module m;\n import b_pkg::*;\n import a_pkg::*;\n a_t p;\n b_t q;\nendmodule\n";
let (_, once) = organize(top, "module m").expect("organized");
let message = organize(&once, "module m").expect_err("a second pass has nothing to do");
assert!(message.contains("already sorted"), "{message}");
}
// ----------------------------------------------------------------------
// comments
// ----------------------------------------------------------------------
#[test]
fn a_comment_travels_with_the_import_it_sits_on() {
let top = "module m;\n // the b one\n import b_pkg::*;\n import a_pkg::*; // the a one\n a_t p;\n b_t q;\nendmodule\n";
let (_, text) = organize(top, "module m").expect("organized");
assert_eq!(
text,
"module m;\n import a_pkg::*; // the a one\n // the b one\n import b_pkg::*;\n a_t p;\n b_t q;\nendmodule\n",
"{text}"
);
}
#[test]
fn an_unused_import_with_a_comment_is_kept_and_reported() {
let top = "module m;\n import a_pkg::*; // why?\n import b_pkg::*;\n b_t q;\nendmodule\n";
let message = refusal(top, "module m");
assert!(message.contains("delete the comment first"), "{message}");
}
#[test]
fn a_commented_unused_import_beside_a_real_change_is_left_with_a_warning() {
let top = "module m;\n import b_pkg::*;\n import a_pkg::*; // why?\n b_t q;\nendmodule\n";
let (edit, text) = organize(top, "module m").expect("organized");
assert!(text.contains("import a_pkg::*; // why?"), "the comment survives: {text}");
assert!(
edit.warnings.iter().any(|w| w.contains("delete the comment first")),
"{:?}",
edit.warnings
);
}
// ----------------------------------------------------------------------
// refusals
// ----------------------------------------------------------------------
#[test]
fn an_unknown_package_refuses() {
let top = "module m;\n import nowhere_pkg::*;\n import a_pkg::*;\n a_t x;\nendmodule\n";
let message = refusal(top, "module m");
assert!(message.contains("nothing lucisv can see declares"), "{message}");
}
#[test]
fn a_package_declared_twice_refuses() {
let top = "module m;\n import a_pkg::*;\n import b_pkg::*;\n a_t x;\nendmodule\n";
let workspace = files(&[
("/rtl/a_pkg.sv", A_PKG),
("/rtl/a_pkg_copy.sv", A_PKG),
("/rtl/b_pkg.sv", B_PKG),
("/rtl/top.sv", top),
]);
let message = organize_imports(&workspace, &PathBuf::from("/rtl/top.sv"), at(top, "module m"))
.expect_err("two declarations are a refusal");
assert!(message.contains("more than once"), "{message}");
}
#[test]
fn a_macro_call_in_the_scope_refuses() {
let top = "module m;\n import a_pkg::*;\n import b_pkg::*;\n `MY_CHECK(a_t)\nendmodule\n";
let message = refusal(top, "module m");
assert!(message.contains("does not preprocess"), "{message}");
}
#[test]
fn a_conditional_directive_in_the_scope_refuses() {
let top = "module m;\n import a_pkg::*;\n import b_pkg::*;\n`ifdef FOO\n b_t q;\n`endif\nendmodule\n";
let message = refusal(top, "module m");
assert!(message.contains("does not preprocess"), "{message}");
}
#[test]
fn a_class_with_a_base_in_another_file_refuses() {
let top = "package w_pkg;\n import a_pkg::*;\n import b_pkg::*;\n class c extends uvm_object;\n endclass\nendpackage\n";
let message = refusal(top, "package w_pkg");
assert!(message.contains("declared in another file"), "{message}");
}
#[test]
fn a_file_that_does_not_parse_refuses() {
let top = "module m;\n import a_pkg::*;\n import b_pkg::*;\n always_comb begin\nendmodule\n";
let message = refusal(top, "module m");
assert!(message.contains("does not parse"), "{message}");
}
#[test]
fn two_imports_on_one_line_refuse() {
let top = "module m;\n import a_pkg::*; import b_pkg::*;\n b_t q;\nendmodule\n";
let message = refusal(top, "module m");
assert!(message.contains("one per line"), "{message}");
}
#[test]
fn a_cursor_where_there_is_nothing_to_organize_refuses() {
let top = "module m;\n wire a;\nendmodule\n";
let message = refusal(top, "wire a");
assert!(message.contains("already sorted"), "{message}");
}
// ----------------------------------------------------------------------
// the run's edges
// ----------------------------------------------------------------------
#[test]
fn a_blank_line_ends_the_run_and_is_not_swallowed() {
let top =
"module m;\n import b_pkg::*;\n\n import a_pkg::*;\n a_t p;\n b_t q;\nendmodule\n";
let message = refusal(top, "module m");
assert!(message.contains("already sorted"), "the run is one import: {message}");
}
#[test]
fn an_import_below_the_run_is_still_deleted_when_nothing_uses_it() {
let top = "module m;\n import b_pkg::*;\n wire w;\n import a_pkg::*;\n b_t q;\nendmodule\n";
let (_, text) = organize(top, "module m").expect("organized");
assert_eq!(text, "module m;\n import b_pkg::*;\n wire w;\n b_t q;\nendmodule\n", "{text}");
}
// ----------------------------------------------------------------------
// scopes
// ----------------------------------------------------------------------
#[test]
fn the_cursor_picks_the_innermost_scope() {
let top = "import a_pkg::*;\nmodule m;\n import b_pkg::*;\n import a_pkg::*;\n b_t q;\nendmodule\n";
let (_, text) = organize(top, "b_t q").expect("organized");
assert!(text.starts_with("import a_pkg::*;\n"), "the unit import is untouched: {text}");
assert_eq!(text.matches("import a_pkg::*;").count(), 1, "the module's copy went: {text}");
}
#[test]
fn a_package_declared_in_the_same_file_is_answered_by_the_binder() {
// `local_pkg` is not in the index's units under any other file, and the
// judgement never asks the project about it: the binder's own tables say
// whether it exports `l_t`.
let top = "package local_pkg;\n typedef int l_t;\nendpackage\nmodule m;\n import local_pkg::*;\n import b_pkg::*;\n l_t x;\nendmodule\n";
let (_, text) = organize(top, "module m").expect("organized");
assert!(text.contains("import local_pkg::*;"), "{text}");
assert!(!text.contains("b_pkg"), "nothing names b_pkg: {text}");
}
#[test]
fn a_package_scope_organizes_like_a_module() {
let top =
"package w_pkg;\n import b_pkg::*;\n import a_pkg::*;\n a_t p;\n b_t q;\nendpackage\n";
let (_, text) = organize(top, "package w_pkg").expect("organized");
assert_eq!(text.lines().nth(1), Some(" import a_pkg::*;"), "{text}");
}
#[test]
fn a_re_exported_name_keeps_the_import_that_reaches_it() {
// `c_pkg` declares nothing of its own and re-exports `a_pkg`'s names; the
// import of `c_pkg` is what `a_t` arrives through, so it stays.
let c_pkg = "package c_pkg;\n import a_pkg::*;\n export a_pkg::*;\nendpackage\n";
let top = "module m;\n import b_pkg::*;\n import c_pkg::*;\n a_t p;\nendmodule\n";
let (_, text) = organize_in(
&[
("/rtl/a_pkg.sv", A_PKG),
("/rtl/b_pkg.sv", B_PKG),
("/rtl/c_pkg.sv", c_pkg),
("/rtl/top.sv", top),
],
top,
"module m",
)
.expect("organized");
assert_eq!(text, "module m;\n import c_pkg::*;\n a_t p;\nendmodule\n", "{text}");
}
// ----------------------------------------------------------------------
// the census
// ----------------------------------------------------------------------
#[test]
fn the_census_names_every_unused_import_in_the_file() {
let top = "module m;\n import a_pkg::*;\n import b_pkg::*;\n b_t q;\nendmodule\n";
let found = unused(top);
assert_eq!(found.len(), 1, "{found:?}");
assert_eq!(found[0].0, "import a_pkg::*;");
assert!(found[0].1.contains("nothing in this scope uses it"), "{}", found[0].1);
}
#[test]
fn the_census_says_nothing_about_a_scope_it_refuses_to_judge() {
let top = "module m;\n import a_pkg::*;\n `MY_CHECK(x)\nendmodule\n";
assert!(unused(top).is_empty(), "a refusal is not an accusation");
}
// ----------------------------------------------------------------------
// counts
// ----------------------------------------------------------------------
#[test]
fn the_counts_are_what_a_title_promises() {
let top = "module m;\n import b_pkg::*;\n import a_pkg::*;\n import a_pkg::*;\n a_t p;\n b_t q;\nendmodule\n";
let workspace =
files(&[("/rtl/a_pkg.sv", A_PKG), ("/rtl/b_pkg.sv", B_PKG), ("/rtl/top.sv", top)]);
let (_, removed, sorted) =
organize_counts(&workspace, &PathBuf::from("/rtl/top.sv"), at(top, "module m"))
.expect("organized");
assert_eq!((removed, sorted), (1, 2));
}
// ----------------------------------------------------------------------
// an import a same-file export re-exports through (§52's QA, HIGH)
// ----------------------------------------------------------------------
/// IEEE 1800 §26.6: a package may only export what it imported or declared,
/// so `export a_pkg::*;` is backed by the `import a_pkg::*;` above it and the
/// import is *used* — deleting it would leave an export nothing backs.
#[test]
fn an_import_a_same_file_export_re_exports_through_is_kept() {
let top = "package c_pkg;\n import a_pkg::*;\n export a_pkg::*;\nendpackage\n";
let result = organize(top, "package c_pkg");
assert!(
result.is_err(),
"nothing to organize: the one import is used by the export, got {result:?}"
);
let workspace = files(&[("/rtl/a_pkg.sv", A_PKG), ("/rtl/top.sv", top)]);
assert!(
unused_imports(&workspace, &PathBuf::from("/rtl/top.sv")).is_empty(),
"the rule agrees"
);
}
#[test]
fn an_item_export_backs_only_the_import_of_that_item() {
// `export a_pkg::a_t;` is backed by `import a_pkg::a_t;` and by
// `import a_pkg::*;` alike; an import of a different package is not.
let top = "package c_pkg;\n import b_pkg::*;\n import a_pkg::a_t;\n export a_pkg::a_t;\nendpackage\n";
let (edit, text) = organize(top, "package c_pkg").expect("organized");
assert_eq!(edit.warnings.len(), 0, "{:?}", edit.warnings);
assert_eq!(
text, "package c_pkg;\n import a_pkg::a_t;\n export a_pkg::a_t;\nendpackage\n",
"{text}"
);
}
#[test]
fn a_star_star_export_backs_every_import() {
let top =
"package c_pkg;\n import b_pkg::*;\n import a_pkg::*;\n export *::*;\nendpackage\n";
let (_, text) = organize(top, "package c_pkg").expect("sorted, nothing removed");
assert_eq!(
text,
"package c_pkg;\n import a_pkg::*;\n import b_pkg::*;\n export *::*;\nendpackage\n",
"{text}"
);
}
#[test]
fn a_dpi_export_backs_no_import() {
let top = "package c_pkg;\n import a_pkg::*;\n function void f(); endfunction\n export \"DPI-C\" function f;\nendpackage\n";
let (_, text) = organize(top, "package c_pkg").expect("the unused import goes");
assert!(!text.contains("import a_pkg"), "{text}");
}
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com