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