Files
lucisv/crates/vlog_refactor/tests/references_and_scope.rs
T

90 lines
3.5 KiB
Rust

// Author: Guoliang Chen, a Senior Digital Design Engineer
//! Public integration tests for `find_references_with`, batch reference lookup,
//! `after_dot`, and bind-aware `home_set_for` / `home_sets_for`.
use std::cell::Cell;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use vlog_refactor::{
after_dot, find_references, find_references_batch, find_references_batch_with,
find_references_with, home_set, home_set_for, home_sets_for, KnownFacts,
};
use vlog_semantic::FileFacts;
struct MapFacts<'a>(HashMap<&'a Path, (&'a str, FileFacts)>, Cell<usize>);
impl KnownFacts for MapFacts<'_> {
fn facts_for(&self, path: &Path, text: &str) -> Option<FileFacts> {
self.1.set(self.1.get() + 1);
self.0.get(path).filter(|(held, _)| *held == text).map(|(_, f)| f.clone())
}
}
#[test]
fn public_batch_and_known_facts_match_plain_find_references() {
let sub = "module sub (input logic clk, output logic q);\n wire sig;\n assign q = sig;\nendmodule\n";
let top = "module top;\n sub u_a (.clk(1'b0), .q());\nendmodule\n";
let extra = "module extra;\n wire other;\n assign other = 1'b0;\nendmodule\n";
let files = vec![
(PathBuf::from("/p/sub.sv"), sub.to_string()),
(PathBuf::from("/p/top.sv"), top.to_string()),
(PathBuf::from("/p/extra.sv"), extra.to_string()),
];
let map = files
.iter()
.map(|(p, t)| (p.as_path(), (t.as_str(), FileFacts::of(p.clone(), t))))
.collect();
let known = MapFacts(map, Cell::new(0));
let sub_p = Path::new("/p/sub.sv");
let clk_off = sub.find("clk").unwrap_or(0) as u32;
let sig_off = (sub.find("wire sig").unwrap_or(0) + 5) as u32;
let queries = [(sub_p, clk_off), (sub_p, sig_off), (sub_p, 0)];
let plain_batch = find_references_batch(&files, &queries, false);
let known_batch = find_references_batch_with(&files, &queries, false, &known);
assert_eq!(plain_batch, known_batch);
assert_eq!(known_batch[0], find_references_with(&files, sub_p, clk_off, false, &known));
assert_eq!(known_batch[1], find_references(&files, sub_p, sig_off, false));
assert!(known_batch[2].is_err());
assert!(known.1.get() >= 3);
}
#[test]
fn public_after_dot_and_bind_aware_home_set_match_engine_rules() {
assert!(after_dot("a."));
assert!(after_dot("a . "));
assert!(after_dot("a./* c */"));
assert!(after_dot("a.``"));
assert!(after_dot("a. /* c */ `` "));
assert!(!after_dot("/* a. */ "));
assert!(!after_dot("a``"));
let files = vec![
(PathBuf::from("/p/a.sv"), "module a; logic s; endmodule\n".to_string()),
(PathBuf::from("/p/b.sv"), "module b; logic s; endmodule\n".to_string()),
(PathBuf::from("/p/bind_a.sv"), "bind a chk u();\n".to_string()),
(PathBuf::from("/p/defs.svh"), "`define B_B bind b chk u_c();\n".to_string()),
(PathBuf::from("/p/bind_b.sv"), "`include \"defs.svh\"\n`B_B\n".to_string()),
];
let a = Path::new("/p/a.sv");
let b = Path::new("/p/b.sv");
assert_eq!(home_set(&files, a, &[]), Some(vec![a.to_path_buf()]));
assert_eq!(
home_set_for(&files, a, Some("a"), &[]),
Some(vec![a.to_path_buf(), PathBuf::from("/p/bind_a.sv")])
);
assert_eq!(
home_sets_for(&files, &[(a, Some("a")), (b, Some("b"))], &[]),
Some(vec![
a.to_path_buf(),
b.to_path_buf(),
PathBuf::from("/p/bind_a.sv"),
PathBuf::from("/p/bind_b.sv"),
])
);
}
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com