Add test for duplicate types in symbol table
This commit is contained in:
parent
b2d9622feb
commit
30fbc9a721
@ -211,7 +211,6 @@ impl SymbolTable {
|
||||
spec,
|
||||
def_id,
|
||||
});
|
||||
println!("In add_symbol(), adding: {:?}", symbol);
|
||||
self.symbol_trie.insert(&fqsn);
|
||||
self.fqsn_to_symbol.insert(fqsn, symbol.clone());
|
||||
self.id_to_symbol.insert(*id, symbol);
|
||||
|
@ -1,12 +1,13 @@
|
||||
#![cfg(test)]
|
||||
use super::*;
|
||||
use crate::util::quick_ast;
|
||||
use assert_matches::assert_matches;
|
||||
|
||||
use super::*;
|
||||
use crate::{tokenizing::Location, util::quick_ast};
|
||||
|
||||
fn add_symbols(src: &str) -> (SymbolTable, Result<(), Vec<SymbolError>>) {
|
||||
let ast = quick_ast(src);
|
||||
let mut symbol_table = SymbolTable::new();
|
||||
let mut type_context = crate::type_inference::TypeContext::new();
|
||||
let mut type_context = crate::type_inference::TypeContext::new();
|
||||
let result = symbol_table.process_ast(&ast, &mut type_context);
|
||||
(symbol_table, result)
|
||||
}
|
||||
@ -30,16 +31,8 @@ fn basic_symbol_table() {
|
||||
let (symbols, _) = add_symbols(src);
|
||||
|
||||
symbols.types.table.get(&make_fqsn(&["Option"])).unwrap();
|
||||
symbols
|
||||
.types
|
||||
.table
|
||||
.get(&make_fqsn(&["Option", "Some"]))
|
||||
.unwrap();
|
||||
symbols
|
||||
.types
|
||||
.table
|
||||
.get(&make_fqsn(&["Option", "None"]))
|
||||
.unwrap();
|
||||
symbols.types.table.get(&make_fqsn(&["Option", "Some"])).unwrap();
|
||||
symbols.types.table.get(&make_fqsn(&["Option", "None"])).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -76,6 +69,26 @@ fn no_variable_definition_duplicates() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_type_definition_duplicates() {
|
||||
let source = r#"
|
||||
let x = 9
|
||||
type Food = Japchae | Burrito | Other
|
||||
type Food = GoodJapchae | Breadfruit
|
||||
"#;
|
||||
let (_, output) = add_symbols(source);
|
||||
let errs = output.unwrap_err();
|
||||
let err = &errs[0];
|
||||
|
||||
match err {
|
||||
SymbolError::DuplicateName { location, prev_name } => {
|
||||
assert_eq!(prev_name, &Fqsn::from_strs(&["Food"]));
|
||||
assert_eq!(location, &Location { line_num: 2, char_num: 2 });
|
||||
}
|
||||
_ => panic!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_variable_definition_duplicates_in_function() {
|
||||
let source = r#"
|
||||
@ -112,11 +125,7 @@ fn dont_falsely_detect_duplicates() {
|
||||
let (symbols, _) = add_symbols(source);
|
||||
|
||||
assert!(symbols.fq_names.table.get(&make_fqsn(&["a"])).is_some());
|
||||
assert!(symbols
|
||||
.fq_names
|
||||
.table
|
||||
.get(&make_fqsn(&["some_func", "a"]))
|
||||
.is_some());
|
||||
assert!(symbols.fq_names.table.get(&make_fqsn(&["some_func", "a"])).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -129,16 +138,8 @@ fn inner_func(arg) {
|
||||
x + inner_func(x)
|
||||
}"#;
|
||||
let (symbols, _) = add_symbols(source);
|
||||
assert!(symbols
|
||||
.fq_names
|
||||
.table
|
||||
.get(&make_fqsn(&["outer_func"]))
|
||||
.is_some());
|
||||
assert!(symbols
|
||||
.fq_names
|
||||
.table
|
||||
.get(&make_fqsn(&["outer_func", "inner_func"]))
|
||||
.is_some());
|
||||
assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func"])).is_some());
|
||||
assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "inner_func"])).is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -157,29 +158,13 @@ fn second_inner_func() {
|
||||
inner_func(x)
|
||||
}"#;
|
||||
let (symbols, _) = add_symbols(source);
|
||||
assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func"])).is_some());
|
||||
assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "inner_func"])).is_some());
|
||||
assert!(symbols.fq_names.table.get(&make_fqsn(&["outer_func", "second_inner_func"])).is_some());
|
||||
assert!(symbols
|
||||
.fq_names
|
||||
.table
|
||||
.get(&make_fqsn(&["outer_func"]))
|
||||
.is_some());
|
||||
assert!(symbols
|
||||
.fq_names
|
||||
.table
|
||||
.get(&make_fqsn(&["outer_func", "inner_func"]))
|
||||
.is_some());
|
||||
assert!(symbols
|
||||
.fq_names
|
||||
.table
|
||||
.get(&make_fqsn(&["outer_func", "second_inner_func"]))
|
||||
.is_some());
|
||||
assert!(symbols
|
||||
.fq_names
|
||||
.table
|
||||
.get(&make_fqsn(&[
|
||||
"outer_func",
|
||||
"second_inner_func",
|
||||
"another_inner_func"
|
||||
]))
|
||||
.get(&make_fqsn(&["outer_func", "second_inner_func", "another_inner_func"]))
|
||||
.is_some());
|
||||
}
|
||||
|
||||
@ -218,11 +203,7 @@ fn item()
|
||||
let (symbols, _) = add_symbols(source);
|
||||
symbols.fq_names.table.get(&make_fqsn(&["stuff"])).unwrap();
|
||||
symbols.fq_names.table.get(&make_fqsn(&["item"])).unwrap();
|
||||
symbols
|
||||
.fq_names
|
||||
.table
|
||||
.get(&make_fqsn(&["stuff", "item"]))
|
||||
.unwrap();
|
||||
symbols.fq_names.table.get(&make_fqsn(&["stuff", "item"])).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user