From 02d6a16cf83d38d606f9a7c88bbbc06685a11e36 Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Mon, 19 May 2025 20:22:13 -0500 Subject: [PATCH] Refactor name-analysis tests. --- src/name_analysis/mod.rs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/src/name_analysis/mod.rs b/src/name_analysis/mod.rs index ddfe3aa..9c81d99 100644 --- a/src/name_analysis/mod.rs +++ b/src/name_analysis/mod.rs @@ -43,7 +43,11 @@ mod tests { use pest::Parser; use std::collections::HashMap; - fn assert_no_diagnostics(sources: HashMap<&str, &str>, symbol_table: &mut SymbolTable) { + fn assert_number_of_diagnostics( + sources: HashMap<&str, &str>, + symbol_table: &mut SymbolTable, + n_diagnostics: usize, + ) { let mut files = SimpleFiles::new(); let mut compilation_units = vec![]; @@ -63,6 +67,8 @@ mod tests { let diagnostics = analyze_names(&mut compilation_units, symbol_table); + assert_eq!(n_diagnostics, diagnostics.len()); + if !diagnostics.is_empty() { let writer = StandardStream::stderr(ColorChoice::Always); let config = term::Config::default(); @@ -72,7 +78,6 @@ mod tests { } eprintln!("{}", symbol_table); - panic!("Diagnostics was not empty!"); } for compilation_unit in &compilation_units { @@ -80,6 +85,10 @@ mod tests { } } + fn assert_no_diagnostics(sources: HashMap<&str, &str>, symbol_table: &mut SymbolTable) { + assert_number_of_diagnostics(sources, symbol_table, 0); + } + #[test] fn params_seen() { let sources: HashMap<&str, &str> = HashMap::from([( @@ -130,4 +139,29 @@ mod tests { add_std_core_symbols(&mut symbol_table).expect("Failed to add std::core symbols."); assert_no_diagnostics(sources, &mut symbol_table); } + + #[test] + fn sees_duplicate_fn() { + let sources: HashMap<&str, &str> = HashMap::from([( + "main.dm", + indoc! {" + fn main(args: Array) {} + fn main(args: Array) {} + "}, + )]); + assert_number_of_diagnostics(sources, &mut SymbolTable::new(), 1); + } + + #[test] + fn symbol_shadows_import() { + let sources: HashMap<&str, &str> = HashMap::from([( + "main.dm", + indoc! {" + platform fn println(msg: Any) -> Void; + "} + )]); + let mut symbol_table = SymbolTable::new(); + add_std_core_symbols(&mut symbol_table).expect("Failed to add std::core symbols."); + assert_no_diagnostics(sources, &mut symbol_table); + } }