//! Behavior tests for the port refactorings: what the engine writes, and what //! it refuses to write. //! //! 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::{Path, PathBuf}; use vlog_refactor::{ add_port, port_actions, propagate_ports, remove_port, ModuleRef, 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 } /// Every warning joined, for a `contains` assertion. fn warnings(edit: &RefactorEdit) -> String { edit.warnings.join("\n") } // ---------------------------------------------------------------------- // add // ---------------------------------------------------------------------- const SUB: &str = "module sub (\n input logic clk,\n input logic rst\n);\n wire x;\nendmodule\n"; #[test] fn a_port_added_to_an_ansi_header_reaches_every_named_connection() { let workspace = files(&[ ("/rtl/sub.sv", SUB), ("/rtl/top.sv", "module top;\n logic cfg;\n sub u1 (.clk(c), .rst(r));\nendmodule\n"), ("/rtl/other.sv", "module other;\n logic cfg;\n sub u2 (.clk(a), .rst(b));\nendmodule\n"), ]); let edit = add_port(&workspace, ModuleRef::Name("sub"), "input logic [7:0] cfg").expect("adds"); assert_eq!(edit.file_count(), 3, "the declaration and both instantiating files"); assert_eq!( applied(&workspace, &edit, "/rtl/sub.sv"), "module sub (\n input logic clk,\n input logic rst,\n input logic [7:0] cfg\n);\n wire x;\nendmodule\n" ); assert_eq!( applied(&workspace, &edit, "/rtl/top.sv"), "module top;\n logic cfg;\n sub u1 (.clk(c), .rst(r), .cfg(cfg));\nendmodule\n" ); assert_eq!( applied(&workspace, &edit, "/rtl/other.sv"), "module other;\n logic cfg;\n sub u2 (.clk(a), .rst(b), .cfg(cfg));\nendmodule\n" ); assert!(edit.warnings.is_empty(), "{:?}", edit.warnings); } #[test] fn a_connection_with_no_signal_of_that_name_is_left_as_a_todo() { let workspace = files(&[ ("/rtl/sub.sv", SUB), ("/rtl/top.sv", "module top;\n sub u1 (.clk(c), .rst(r));\nendmodule\n"), ]); let edit = add_port(&workspace, ModuleRef::Name("sub"), "input logic cfg").expect("adds"); assert_eq!( applied(&workspace, &edit, "/rtl/top.sv"), "module top;\n sub u1 (.clk(c), .rst(r), .cfg(/* TODO: connect */));\nendmodule\n" ); assert!(warnings(&edit).contains("TODO"), "{:?}", edit.warnings); assert!(warnings(&edit).contains("`u1` (top.sv:2)"), "{:?}", edit.warnings); } #[test] fn a_one_per_line_connection_list_stays_one_per_line() { let workspace = files(&[ ("/rtl/sub.sv", SUB), ( "/rtl/top.sv", "module top;\n logic cfg;\n sub u1 (\n .clk(c),\n .rst(r)\n );\nendmodule\n", ), ]); let edit = add_port(&workspace, ModuleRef::Name("sub"), "input logic cfg").expect("adds"); assert_eq!( applied(&workspace, &edit, "/rtl/top.sv"), "module top;\n logic cfg;\n sub u1 (\n .clk(c),\n .rst(r),\n .cfg(cfg)\n );\nendmodule\n" ); } #[test] fn a_comment_on_the_last_port_keeps_its_port() { let workspace = files(&[( "/rtl/sub.sv", "module sub (\n input clk, // the clock\n input rst // the reset\n);\nendmodule\n", )]); let edit = add_port(&workspace, ModuleRef::Name("sub"), "input cfg").expect("adds"); assert_eq!( applied(&workspace, &edit, "/rtl/sub.sv"), "module sub (\n input clk, // the clock\n input rst, // the reset\n input cfg\n);\nendmodule\n", "the comma goes before the comment, the port after it" ); } #[test] fn a_port_added_to_a_non_ansi_header_is_declared_in_the_body_too() { let workspace = files(&[ ("/rtl/sub.sv", "module sub (a, b);\n input a;\n output b;\n wire c;\nendmodule\n"), ("/rtl/top.sv", "module top;\n logic cfg;\n sub u1 (.a(x), .b(y));\nendmodule\n"), ]); let edit = add_port(&workspace, ModuleRef::Name("sub"), "input logic cfg").expect("adds"); assert_eq!( applied(&workspace, &edit, "/rtl/sub.sv"), "module sub (a, b, cfg);\n input a;\n output b;\n input logic cfg;\n wire c;\nendmodule\n", "the header takes the name, the body takes the declaration" ); assert_eq!( applied(&workspace, &edit, "/rtl/top.sv"), "module top;\n logic cfg;\n sub u1 (.a(x), .b(y), .cfg(cfg));\nendmodule\n" ); } #[test] fn an_ordered_instantiation_gains_a_positional_connection() { let workspace = files(&[ ("/rtl/sub.sv", "module sub (input a, input b);\nendmodule\n"), ("/rtl/top.sv", "module top;\n logic cfg;\n sub u1 (x, y);\nendmodule\n"), ]); let edit = add_port(&workspace, ModuleRef::Name("sub"), "input cfg").expect("adds"); assert_eq!( applied(&workspace, &edit, "/rtl/top.sv"), "module top;\n logic cfg;\n sub u1 (x, y, cfg);\nendmodule\n" ); } #[test] fn an_ordered_instantiation_that_connects_a_different_number_of_ports_is_refused() { let workspace = files(&[ ("/rtl/sub.sv", "module sub (input a, input b);\nendmodule\n"), ("/rtl/top.sv", "module top;\n sub u1 (x);\nendmodule\n"), ]); let message = add_port(&workspace, ModuleRef::Name("sub"), "input cfg").expect_err("cannot be sure"); assert!(message.contains("by position"), "{message}"); assert!(message.contains("named connections first"), "{message}"); } #[test] fn an_empty_connection_list_is_left_alone_and_reported() { let workspace = files(&[ ("/rtl/sub.sv", "module sub (input a);\nendmodule\n"), ("/rtl/top.sv", "module top;\n sub u1 ();\nendmodule\n"), ]); let edit = add_port(&workspace, ModuleRef::Name("sub"), "input cfg").expect("adds"); assert_eq!( applied(&workspace, &edit, "/rtl/top.sv"), "module top;\n sub u1 ();\nendmodule\n", "an empty list connects nothing, before and after" ); assert!(warnings(&edit).contains("empty connection list"), "{:?}", edit.warnings); } #[test] fn a_wildcard_connection_is_left_alone_and_warned_about_only_when_it_would_not_bind() { let bound = files(&[ ("/rtl/sub.sv", "module sub (input a);\nendmodule\n"), ("/rtl/top.sv", "module top;\n logic cfg;\n sub u1 (.*);\nendmodule\n"), ]); let edit = add_port(&bound, ModuleRef::Name("sub"), "input cfg").expect("adds"); assert_eq!(applied(&bound, &edit, "/rtl/top.sv"), bound[1].1, "`.*` is never rewritten"); assert!(edit.warnings.is_empty(), "a signal named `cfg` is right there: {:?}", edit.warnings); let unbound = files(&[ ("/rtl/sub.sv", "module sub (input a);\nendmodule\n"), ("/rtl/top.sv", "module top;\n sub u1 (.*);\nendmodule\n"), ]); let edit = add_port(&unbound, ModuleRef::Name("sub"), "input cfg").expect("adds"); assert!(warnings(&edit).contains("will not bind"), "{:?}", edit.warnings); } #[test] fn an_empty_port_list_is_filled_in_the_ansi_way() { let workspace = files(&[("/rtl/sub.sv", "module sub ();\nendmodule\n")]); let edit = add_port(&workspace, ModuleRef::Name("sub"), "input clk").expect("adds"); assert_eq!(applied(&workspace, &edit, "/rtl/sub.sv"), "module sub (input clk);\nendmodule\n"); } #[test] fn a_header_with_no_port_list_at_all_gains_one() { let workspace = files(&[("/rtl/sub.sv", "module sub;\nendmodule\n")]); let edit = add_port(&workspace, ModuleRef::Name("sub"), "input clk").expect("adds"); assert_eq!(applied(&workspace, &edit, "/rtl/sub.sv"), "module sub (input clk);\nendmodule\n"); } #[test] fn the_module_can_be_named_by_a_position_instead_of_by_name() { let workspace = files(&[("/rtl/sub.sv", SUB)]); let offset = at(SUB, "wire x"); let edit = add_port(&workspace, ModuleRef::At { file: Path::new("/rtl/sub.sv"), offset }, "input cfg") .expect("the module the cursor is in"); assert!(applied(&workspace, &edit, "/rtl/sub.sv").contains("input cfg")); } #[test] fn a_module_declared_twice_is_refused() { let workspace = files(&[ ("/rtl/a.sv", "module sub (input a);\nendmodule\n"), ("/rtl/b.sv", "module sub (input a);\nendmodule\n"), ]); let message = add_port(&workspace, ModuleRef::Name("sub"), "input cfg").expect_err("ambiguous"); assert!(message.contains("more than one indexed file"), "{message}"); } #[test] fn a_port_that_already_exists_is_refused() { let workspace = files(&[("/rtl/sub.sv", SUB)]); let message = add_port(&workspace, ModuleRef::Name("sub"), "input logic clk").expect_err("a duplicate"); assert!(message.contains("already has a port named `clk`"), "{message}"); } #[test] fn a_port_that_would_capture_a_body_name_is_refused() { // `cfg` already means the net; a port called `cfg` would take the name // over, and every use of it in the body would quietly change meaning. let workspace = files(&[( "/rtl/sub.sv", "module sub (\n input clk\n);\n wire cfg;\n assign cfg = clk;\nendmodule\n", )]); let message = add_port(&workspace, ModuleRef::Name("sub"), "input cfg") .expect_err("a captured body reference"); assert!(message.contains("would change what"), "{message}"); } #[test] fn an_unknown_module_is_refused() { let workspace = files(&[("/rtl/sub.sv", SUB)]); let message = add_port(&workspace, ModuleRef::Name("nope"), "input cfg").expect_err("unknown"); assert!(message.contains("no module named `nope`"), "{message}"); } #[test] fn an_instantiation_inside_a_generate_block_is_updated_too() { let workspace = files(&[ ("/rtl/sub.sv", "module sub (input a);\nendmodule\n"), ( "/rtl/top.sv", "module top;\n logic cfg;\n if (1) begin : g\n sub u1 (.a(x));\n end\nendmodule\n", ), ]); let edit = add_port(&workspace, ModuleRef::Name("sub"), "input cfg").expect("adds"); assert!( applied(&workspace, &edit, "/rtl/top.sv").contains(".a(x), .cfg(cfg)"), "{}", applied(&workspace, &edit, "/rtl/top.sv") ); } #[test] fn adding_a_port_twice_is_refused_the_second_time() { let workspace = files(&[("/rtl/sub.sv", SUB)]); let edit = add_port(&workspace, ModuleRef::Name("sub"), "input cfg").expect("adds"); let once = files(&[("/rtl/sub.sv", &applied(&workspace, &edit, "/rtl/sub.sv"))]); let message = add_port(&once, ModuleRef::Name("sub"), "input cfg").expect_err("already there"); assert!(message.contains("already has a port"), "{message}"); } // ---------------------------------------------------------------------- // remove // ---------------------------------------------------------------------- #[test] fn a_removed_port_leaves_the_header_and_every_connection() { let sub = "module sub (\n input a,\n input b\n);\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", sub), ( "/rtl/top.sv", "module top;\n sub u1 (.a(x), .b(y));\n sub u2 (.b(z), .a(w));\nendmodule\n", ), ]); let edit = remove_port(&workspace, Path::new("/rtl/sub.sv"), at(sub, "input b") + 6).expect("removes"); assert_eq!( applied(&workspace, &edit, "/rtl/sub.sv"), "module sub (\n input a\n);\nendmodule\n" ); assert_eq!( applied(&workspace, &edit, "/rtl/top.sv"), "module top;\n sub u1 (.a(x));\n sub u2 (.a(w));\nendmodule\n" ); } #[test] fn removing_a_non_ansi_port_removes_its_body_declaration() { let sub = "module sub (a, b);\n input a;\n output b;\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", sub), ("/rtl/top.sv", "module top;\n sub u1 (.a(x), .b(y));\nendmodule\n"), ]); // From the header... let from_header = remove_port(&workspace, Path::new("/rtl/sub.sv"), at(sub, "b);")).expect("removes"); assert_eq!( applied(&workspace, &from_header, "/rtl/sub.sv"), "module sub (a);\n input a;\nendmodule\n" ); // ...and from the body declaration, which is the same removal. let from_body = remove_port(&workspace, Path::new("/rtl/sub.sv"), at(sub, "output b") + 7) .expect("removes"); assert_eq!( applied(&workspace, &from_body, "/rtl/sub.sv"), "module sub (a);\n input a;\nendmodule\n" ); } #[test] fn a_non_ansi_declaration_naming_several_ports_loses_only_one_of_them() { let sub = "module sub (a, b);\n input a, b;\nendmodule\n"; let workspace = files(&[("/rtl/sub.sv", sub)]); let edit = remove_port(&workspace, Path::new("/rtl/sub.sv"), at(sub, "b);")).expect("removes"); assert_eq!( applied(&workspace, &edit, "/rtl/sub.sv"), "module sub (a);\n input a;\nendmodule\n" ); } #[test] fn a_port_still_used_in_the_body_is_refused() { let sub = "module sub (\n input a,\n input b\n);\n wire c;\n assign c = b;\nendmodule\n"; let workspace = files(&[("/rtl/sub.sv", sub)]); let message = remove_port(&workspace, Path::new("/rtl/sub.sv"), at(sub, "input b") + 6) .expect_err("still used"); assert!(message.contains("still used inside `sub`"), "{message}"); assert!(message.contains("sub.sv:6"), "the line it is used on: {message}"); } #[test] fn a_positional_connection_anywhere_refuses_the_whole_removal() { let sub = "module sub (input a, input b);\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", sub), ("/rtl/top.sv", "module top;\n sub u1 (.a(x), .b(y));\nendmodule\n"), ("/rtl/legacy.sv", "module legacy;\n sub u2 (p, q);\nendmodule\n"), ]); let message = remove_port(&workspace, Path::new("/rtl/sub.sv"), at(sub, "input b") + 6) .expect_err("a positional connection"); assert!(message.contains("by position"), "{message}"); assert!(message.contains("named connections first"), "{message}"); } #[test] fn a_dropped_expression_that_is_more_than_the_ports_own_name_is_reported() { let sub = "module sub (input a, input b);\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", sub), ("/rtl/top.sv", "module top;\n sub u1 (.a(a), .b(count + 1));\nendmodule\n"), ]); let edit = remove_port(&workspace, Path::new("/rtl/sub.sv"), at(sub, "input b") + 6).expect("removes"); assert!(warnings(&edit).contains("`.b(count + 1)`"), "{:?}", edit.warnings); assert!(warnings(&edit).contains("deleted with the port"), "{:?}", edit.warnings); } #[test] fn a_plain_connection_of_the_ports_own_name_is_dropped_silently() { let sub = "module sub (input a, input b);\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", sub), ("/rtl/top.sv", "module top;\n logic a, b;\n sub u1 (.a(a), .b(b));\nendmodule\n"), ]); let edit = remove_port(&workspace, Path::new("/rtl/sub.sv"), at(sub, "input b") + 6).expect("removes"); assert!(edit.warnings.is_empty(), "nothing was lost: {:?}", edit.warnings); } #[test] fn a_wildcard_site_is_warned_about_rather_than_edited() { let sub = "module sub (input a, input b);\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", sub), ("/rtl/top.sv", "module top;\n logic a, b;\n sub u1 (.*);\nendmodule\n"), ]); let edit = remove_port(&workspace, Path::new("/rtl/sub.sv"), at(sub, "input b") + 6).expect("removes"); assert_eq!(applied(&workspace, &edit, "/rtl/top.sv"), workspace[1].1, "`.*` is untouched"); assert!(warnings(&edit).contains("may now be unused"), "{:?}", edit.warnings); } #[test] fn a_commented_connection_is_refused_rather_than_silently_uncommented() { let sub = "module sub (input a, input b);\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", sub), ( "/rtl/top.sv", "module top;\n sub u1 (\n .a(x),\n .b(y /* keep me */)\n );\nendmodule\n", ), ]); let message = remove_port(&workspace, Path::new("/rtl/sub.sv"), at(sub, "input b") + 6) .expect_err("a comment inside the connection"); assert!(message.contains("comments are never dropped"), "{message}"); } #[test] fn removing_the_only_port_leaves_an_empty_list() { let sub = "module sub (input a);\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", sub), ("/rtl/top.sv", "module top;\n sub u1 (.a(x));\nendmodule\n"), ]); let edit = remove_port(&workspace, Path::new("/rtl/sub.sv"), at(sub, "input a") + 6).expect("removes"); assert_eq!(applied(&workspace, &edit, "/rtl/sub.sv"), "module sub ();\nendmodule\n"); assert_eq!(applied(&workspace, &edit, "/rtl/top.sv"), "module top;\n sub u1 ();\nendmodule\n"); } #[test] fn a_position_that_is_not_a_port_is_refused() { let workspace = files(&[("/rtl/sub.sv", SUB)]); let message = remove_port(&workspace, Path::new("/rtl/sub.sv"), at(SUB, "wire x") + 5) .expect_err("a net, not a port"); assert!(message.contains("no module port at this position"), "{message}"); } #[test] fn a_file_outside_the_scope_is_refused() { let workspace = files(&[("/rtl/sub.sv", SUB)]); let message = remove_port(&workspace, Path::new("/elsewhere/x.sv"), 0).expect_err("not indexed"); assert!(message.contains("not one of the files vfmt indexes"), "{message}"); } // ---------------------------------------------------------------------- // propagate // ---------------------------------------------------------------------- #[test] fn propagating_adds_only_the_connections_that_are_missing() { let top = "module top;\n logic a, c;\n sub u1 (.a(a));\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", "module sub (input a, input b, input c);\nendmodule\n"), ("/rtl/top.sv", top), ]); let edit = propagate_ports(&workspace, Path::new("/rtl/top.sv"), at(top, "u1")).expect("adds"); assert_eq!( applied(&workspace, &edit, "/rtl/top.sv"), "module top;\n logic a, c;\n sub u1 (.a(a), .b(/* TODO: connect */), .c(c));\nendmodule\n" ); assert!(warnings(&edit).contains("`b`"), "{:?}", edit.warnings); assert_eq!(edit.file_count(), 1, "propagation is one instantiation's business"); } #[test] fn propagating_a_one_per_line_list_stays_one_per_line() { let top = "module top;\n logic a, b;\n sub u1 (\n .a(a)\n );\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", "module sub (input a, input b);\nendmodule\n"), ("/rtl/top.sv", top), ]); let edit = propagate_ports(&workspace, Path::new("/rtl/top.sv"), at(top, "u1")).expect("adds"); assert_eq!( applied(&workspace, &edit, "/rtl/top.sv"), "module top;\n logic a, b;\n sub u1 (\n .a(a),\n .b(b)\n );\nendmodule\n" ); } #[test] fn propagating_a_fully_connected_instantiation_is_refused() { let top = "module top;\n logic a;\n sub u1 (.a(a));\nendmodule\n"; let workspace = files(&[("/rtl/sub.sv", "module sub (input a);\nendmodule\n"), ("/rtl/top.sv", top)]); let message = propagate_ports(&workspace, Path::new("/rtl/top.sv"), at(top, "u1")) .expect_err("nothing to do"); assert!(message.contains("already connects every port"), "{message}"); } #[test] fn propagating_an_ordered_instantiation_is_refused() { let top = "module top;\n sub u1 (x);\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", "module sub (input a, input b);\nendmodule\n"), ("/rtl/top.sv", top), ]); let message = propagate_ports(&workspace, Path::new("/rtl/top.sv"), at(top, "u1")) .expect_err("positional"); assert!(message.contains("named connections only"), "{message}"); } #[test] fn propagating_a_wildcard_instantiation_is_refused() { let top = "module top;\n logic a;\n sub u1 (.*);\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", "module sub (input a, input b);\nendmodule\n"), ("/rtl/top.sv", top), ]); let message = propagate_ports(&workspace, Path::new("/rtl/top.sv"), at(top, "u1")).expect_err("`.*`"); assert!(message.contains("`.*`"), "{message}"); } #[test] fn propagating_needs_to_know_which_instance_when_a_statement_declares_several() { let top = "module top;\n sub u1 (.a(x)), u2 (.a(y));\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", "module sub (input a, input b);\nendmodule\n"), ("/rtl/top.sv", top), ]); let message = propagate_ports(&workspace, Path::new("/rtl/top.sv"), at(top, "sub u1")) .expect_err("which one?"); assert!(message.contains("put the cursor on the one"), "{message}"); // On an instance name it is unambiguous. let edit = propagate_ports(&workspace, Path::new("/rtl/top.sv"), at(top, "u2")).expect("that one"); assert_eq!( applied(&workspace, &edit, "/rtl/top.sv"), "module top;\n sub u1 (.a(x)), u2 (.a(y), .b(/* TODO: connect */));\nendmodule\n" ); } #[test] fn a_position_that_is_not_an_instantiation_is_refused() { let top = "module top;\n logic a;\nendmodule\n"; let workspace = files(&[("/rtl/sub.sv", "module sub (input a);\nendmodule\n"), ("/rtl/top.sv", top)]); let message = propagate_ports(&workspace, Path::new("/rtl/top.sv"), at(top, "logic")) .expect_err("nothing here"); assert!(message.contains("no module instantiation at this position"), "{message}"); } // ---------------------------------------------------------------------- // the code-action surface // ---------------------------------------------------------------------- #[test] fn a_port_declaration_offers_its_removal_with_the_count_in_the_title() { let sub = "module sub (input a, input b);\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", sub), ( "/rtl/top.sv", "module top;\n sub u1 (.a(x), .b(y));\n sub u2 (.a(p), .b(q));\nendmodule\n", ), ]); let actions = port_actions(&workspace, Path::new("/rtl/sub.sv"), at(sub, "input b") + 6); assert_eq!(actions.len(), 1, "{actions:#?}"); assert_eq!(actions[0].title, "vfmt: remove port `b` (updates 2 instantiations)"); assert_eq!(actions[0].edit.file_count(), 2); } #[test] fn an_instantiation_missing_connections_offers_to_fill_them_in() { let top = "module top;\n logic a, b;\n sub u1 (.a(a));\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", "module sub (input a, input b);\nendmodule\n"), ("/rtl/top.sv", top), ]); let actions = port_actions(&workspace, Path::new("/rtl/top.sv"), at(top, "u1")); assert_eq!(actions.len(), 1, "{actions:#?}"); assert_eq!(actions[0].title, "vfmt: connect 1 missing port"); } #[test] fn nothing_is_offered_where_nothing_applies() { let workspace = files(&[("/rtl/sub.sv", SUB)]); assert!(port_actions(&workspace, Path::new("/rtl/sub.sv"), at(SUB, "wire x")).is_empty()); } #[test] fn a_named_list_that_also_writes_a_wildcard_is_left_alone() { // `.a(x), .*` is a wildcard site: the wildcard already binds whatever is // not named, so writing `.cfg(cfg)` beside it would be redundant at best. let workspace = files(&[ ("/rtl/sub.sv", "module sub (input a);\nendmodule\n"), ("/rtl/top.sv", "module top;\n logic cfg;\n sub u1 (.a(x), .*);\nendmodule\n"), ]); let edit = add_port(&workspace, ModuleRef::Name("sub"), "input cfg").expect("adds"); assert_eq!(applied(&workspace, &edit, "/rtl/top.sv"), workspace[1].1); } #[test] fn a_comment_beside_an_untouched_connection_survives_the_removal() { let sub = "module sub (input a, input b);\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", sub), ( "/rtl/top.sv", "module top;\n sub u1 (\n .a(x), // about a\n .b(y)\n );\nendmodule\n", ), ]); let edit = remove_port(&workspace, Path::new("/rtl/sub.sv"), at(sub, "input b") + 6).expect("removes"); assert_eq!( applied(&workspace, &edit, "/rtl/top.sv"), "module top;\n sub u1 (\n .a(x) // about a\n );\nendmodule\n", "the comment stays with `.a`, and the emptied line goes" ); } #[test] fn a_module_instantiated_in_its_own_file_is_edited_once() { let text = "module sub (input a, input b);\nendmodule\n\nmodule top;\n sub u1 (.a(x), .b(y));\nendmodule\n"; let workspace = files(&[("/rtl/both.sv", text)]); let edit = remove_port(&workspace, Path::new("/rtl/both.sv"), at(text, "input b") + 6) .expect("removes"); assert_eq!(edit.file_count(), 1); assert_eq!( applied(&workspace, &edit, "/rtl/both.sv"), "module sub (input a);\nendmodule\n\nmodule top;\n sub u1 (.a(x));\nendmodule\n" ); } #[test] fn the_cheap_pre_check_says_yes_exactly_where_an_action_could_be_offered() { use vlog_refactor::may_have_port_action; let text = "module sub (\n input a,\n input b\n);\n wire c;\n assign c = a;\n other u1 (.x(c));\nendmodule\n"; assert!(may_have_port_action(text, at(text, "input b") + 6), "a header port"); assert!(may_have_port_action(text, at(text, "u1")), "an instance"); assert!(may_have_port_action(text, at(text, "other")), "an instantiation's module name"); assert!(!may_have_port_action(text, at(text, "wire c") + 5), "a net"); assert!(!may_have_port_action(text, at(text, "assign")), "a statement"); assert!(!may_have_port_action(text, 0), "the `module` keyword"); let non_ansi = "module sub (a);\n input a;\nendmodule\n"; assert!(may_have_port_action(non_ansi, at(non_ansi, "input a") + 6), "a non-ANSI declaration"); } // ---------------------------------------------------------------------- // what the preprocessor owns // ---------------------------------------------------------------------- /// QA's reproduction: the last declared port sits inside an `` `ifdef `` arm, /// so "after the last port" is a position inside that arm. const CONDITIONAL: &str = "module m (\n input clk\n`ifdef HAS_RST\n , input rst\n`endif\n);\nendmodule\n"; #[test] fn a_port_that_would_land_inside_an_ifdef_arm_is_refused() { let workspace = files(&[("/rtl/m.sv", CONDITIONAL)]); let message = add_port(&workspace, ModuleRef::Name("m"), "input logic cfg") .expect_err("the append point is inside the conditional arm"); assert!(message.contains("conditionally compiled"), "{message}"); assert!(message.contains("`ifdef HAS_RST"), "the arm is named: {message}"); assert!(message.contains("m.sv:3"), "and located: {message}"); assert!(message.contains("place it by hand"), "{message}"); } #[test] fn a_port_list_with_a_closed_conditional_is_edited_and_reported() { // The arm closes *before* the last port, so the new port lands outside it — // but the module's port set is still configuration-dependent, and that is // worth a sentence. let text = "module m (\n`ifdef HAS_RST\n input rst,\n`endif\n input clk\n);\nendmodule\n"; let workspace = files(&[("/rtl/m.sv", text)]); let edit = add_port(&workspace, ModuleRef::Name("m"), "input logic cfg").expect("adds"); assert_eq!( applied(&workspace, &edit, "/rtl/m.sv"), "module m (\n`ifdef HAS_RST\n input rst,\n`endif\n input clk,\n input logic cfg\n);\nendmodule\n" ); assert!(warnings(&edit).contains("carries compiler directives"), "{:?}", edit.warnings); assert!(warnings(&edit).contains("outside every conditional arm"), "{:?}", edit.warnings); } #[test] fn a_non_ansi_body_declaration_that_would_land_inside_an_ifdef_arm_is_refused() { // The header list is unconditional; the *body*'s last port declaration is // not, and that is where the declaration would be written. let text = "module m (a, b);\n input a;\n`ifdef WIDE\n input [7:0] b;\n`endif\nendmodule\n"; let workspace = files(&[("/rtl/m.sv", text)]); let message = add_port(&workspace, ModuleRef::Name("m"), "input logic cfg") .expect_err("the body declaration would be conditional"); assert!(message.contains("the body of `m`"), "{message}"); assert!(message.contains("`ifdef WIDE"), "{message}"); } #[test] fn a_port_only_used_inside_a_macro_invocation_is_reported_when_it_is_removed() { // QA's reproduction: `BUMP(rst)` is one opaque token, so no reference // exists for the body-use refusal to find — but the expansion still names // the port. let text = "`define BUMP(x) x = x + 1\nmodule m (\n input clk,\n input rst\n);\n initial `BUMP(rst);\nendmodule\n"; let workspace = files(&[("/rtl/m.sv", text)]); let edit = remove_port(&workspace, Path::new("/rtl/m.sv"), at(text, "input rst") + 6) .expect("removes"); assert!( applied(&workspace, &edit, "/rtl/m.sv").contains("`BUMP(rst)"), "the call is untouched" ); assert!(warnings(&edit).contains("macro invocation `BUMP`"), "{:?}", edit.warnings); assert!(warnings(&edit).contains("m.sv:6"), "{:?}", edit.warnings); assert!(warnings(&edit).contains("no longer has"), "{:?}", edit.warnings); } #[test] fn a_removed_port_named_in_a_macro_body_is_reported_too() { let text = "`define TIE rst = 1'b0\nmodule m (\n input clk,\n input rst\n);\nendmodule\n"; let workspace = files(&[("/rtl/m.sv", text)]); let edit = remove_port(&workspace, Path::new("/rtl/m.sv"), at(text, "input rst") + 6) .expect("removes"); assert!(warnings(&edit).contains("body of macro `TIE`"), "{:?}", edit.warnings); } #[test] fn a_macro_invocation_in_an_instantiating_file_is_reported_when_the_port_goes() { let sub = "module sub (input a, input b);\nendmodule\n"; let workspace = files(&[ ("/rtl/sub.sv", sub), ( "/rtl/top.sv", "`define WIRE_UP(x) .b(x)\nmodule top;\n sub u1 (.a(p), .b(q));\n initial `WIRE_UP(b);\nendmodule\n", ), ]); let edit = remove_port(&workspace, Path::new("/rtl/sub.sv"), at(sub, "input b") + 6).expect("removes"); assert!( warnings(&edit).contains("`WIRE_UP`"), "an edited file is scanned too: {:?}", edit.warnings ); }