Refactored symbol_table test

This commit is contained in:
greg 2019-10-23 14:02:27 -07:00
parent 11a9a60a34
commit a2f30b6136

View File

@ -6,16 +6,21 @@ use super::*;
use crate::util::quick_ast; use crate::util::quick_ast;
use crate::source_map; use crate::source_map;
fn add_symbols_from_source(src: &str) -> (SymbolTable, Result<(), String>) {
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new()));
let mut symbol_table = SymbolTable::new(source_map);
let ast = quick_ast(src);
let result = symbol_table.add_top_level_symbols(&ast);
(symbol_table, result)
}
macro_rules! values_in_table { macro_rules! values_in_table {
($source:literal, $single_value:expr) => { ($source:literal, $single_value:expr) => {
values_in_table!($source | $single_value); values_in_table!($source | $single_value);
}; };
($source:literal | $( $value:expr ),* ) => { ($source:literal | $( $value:expr ),* ) => {
{ {
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new())); let (symbol_table, _) = add_symbols_from_source($source);
let mut symbol_table = SymbolTable::new(source_map);
let ast = quick_ast($source);
symbol_table.add_top_level_symbols(&ast).unwrap();
$( $(
match symbol_table.lookup_by_fqsn($value) { match symbol_table.lookup_by_fqsn($value) {
Some(_spec) => (), Some(_spec) => (),
@ -42,11 +47,8 @@ fn no_duplicates() {
fn b() { 2 } fn b() { 2 }
fn a() { 3 } fn a() { 3 }
"#; "#;
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new())); let (_, output) = add_symbols_from_source(source);
let mut symbol_table = SymbolTable::new(source_map); assert!(output.unwrap_err().contains("Duplicate"))
let ast = quick_ast(source);
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
assert!(output.contains("Duplicate"))
} }
#[test] #[test]
@ -56,11 +58,9 @@ fn no_duplicates_2() {
let q = 39; let q = 39;
let a = 30; let a = 30;
"#; "#;
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new())); let (_, output) = add_symbols_from_source(source);
let mut symbol_table = SymbolTable::new(source_map); let output = output.unwrap_err();
let ast = quick_ast(source); println!("OUTPUT: {:?}", output);
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
println!("OUTPUT: {}", output);
assert!(output.contains("Duplicate")); assert!(output.contains("Duplicate"));
assert!(output.contains("Line 3")); assert!(output.contains("Line 3"));
} }
@ -79,11 +79,8 @@ fn no_duplicates_3() {
let x = 33 let x = 33
} }
"#; "#;
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new())); let (_, output) = add_symbols_from_source(source);
let mut symbol_table = SymbolTable::new(source_map); assert!(output.unwrap_err().contains("Duplicate"))
let ast = quick_ast(source);
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
assert!(output.contains("Duplicate"))
} }
#[test] #[test]
@ -96,10 +93,7 @@ fn dont_falsely_detect_duplicates() {
} }
let q = 39; let q = 39;
"#; "#;
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new())); let (symbol_table, _) = add_symbols_from_source(source);
let mut symbol_table = SymbolTable::new(source_map);
let ast = quick_ast(source);
symbol_table.add_top_level_symbols(&ast).unwrap();
assert!(symbol_table.lookup_by_fqsn(&fqsn!["a"; tr]).is_some()); assert!(symbol_table.lookup_by_fqsn(&fqsn!["a"; tr]).is_some());
assert!(symbol_table.lookup_by_fqsn(&fqsn!["some_func"; fn, "a";tr]).is_some()); assert!(symbol_table.lookup_by_fqsn(&fqsn!["some_func"; fn, "a";tr]).is_some());
} }
@ -113,10 +107,7 @@ fn inner_func(arg) {
} }
x + inner_func(x) x + inner_func(x)
}"#; }"#;
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new())); let (symbol_table, _) = add_symbols_from_source(source);
let mut symbol_table = SymbolTable::new(source_map);
let ast = quick_ast(source);
symbol_table.add_top_level_symbols(&ast).unwrap();
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; tr)).is_some()); assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; tr)).is_some());
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "inner_func"; tr)).is_some()); assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "inner_func"; tr)).is_some());
} }
@ -136,10 +127,7 @@ fn second_inner_func() {
inner_func(x) inner_func(x)
}"#; }"#;
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new())); let (symbol_table, _) = add_symbols_from_source(source);
let mut symbol_table = SymbolTable::new(source_map);
let ast = quick_ast(source);
symbol_table.add_top_level_symbols(&ast).unwrap();
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; tr)).is_some()); assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; tr)).is_some());
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "inner_func"; tr)).is_some()); assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "inner_func"; tr)).is_some());
assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "second_inner_func"; tr)).is_some()); assert!(symbol_table.lookup_by_fqsn(&fqsn!("outer_func"; fn, "second_inner_func"; tr)).is_some());
@ -163,9 +151,6 @@ fn second_inner_func() {
inner_func(x) inner_func(x)
}"#; }"#;
let source_map = Rc::new(RefCell::new(source_map::SourceMap::new())); let (_, output) = add_symbols_from_source(source);
let mut symbol_table = SymbolTable::new(source_map); assert!(output.unwrap_err().contains("Duplicate"))
let ast = quick_ast(source);
let output = symbol_table.add_top_level_symbols(&ast).unwrap_err();
assert!(output.contains("Duplicate"))
} }