2019-10-16 19:51:43 -07:00
|
|
|
#![cfg(test)]
|
|
|
|
use super::*;
|
|
|
|
use crate::util::quick_ast;
|
|
|
|
|
2021-10-18 22:51:36 -07:00
|
|
|
fn add_symbols(src: &str) -> (SymbolTable, Result<(), String>) {
|
2021-10-14 06:18:17 -07:00
|
|
|
let ast = quick_ast(src);
|
|
|
|
let mut symbol_table = SymbolTable::new();
|
2021-10-18 22:51:36 -07:00
|
|
|
let result = symbol_table.process_ast(&ast);
|
2019-10-23 14:02:27 -07:00
|
|
|
(symbol_table, result)
|
|
|
|
}
|
|
|
|
|
2021-10-18 22:51:36 -07:00
|
|
|
fn make_fqsn(strs: &[&str]) -> FQSN {
|
2021-10-19 17:22:35 -07:00
|
|
|
FQSN::from_strs(strs)
|
2019-10-16 19:51:43 -07:00
|
|
|
}
|
|
|
|
|
2021-10-18 22:51:36 -07:00
|
|
|
|
2019-10-16 19:51:43 -07:00
|
|
|
#[test]
|
|
|
|
fn basic_symbol_table() {
|
2021-10-18 22:51:36 -07:00
|
|
|
let src = "let a = 10; fn b() { 20 }";
|
|
|
|
let (symbols, _) = add_symbols(src);
|
|
|
|
|
2021-10-19 17:22:35 -07:00
|
|
|
fn make_fqsn(strs: &[&str]) -> FQSN {
|
|
|
|
FQSN::from_strs(strs)
|
|
|
|
}
|
|
|
|
|
2021-10-18 22:51:36 -07:00
|
|
|
symbols.fq_names.table.get(&make_fqsn(&["b"])).unwrap();
|
|
|
|
|
|
|
|
let src = "type Option<T> = Some(T) | None";
|
|
|
|
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();
|
2019-10-16 19:51:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-10-24 01:34:13 -07:00
|
|
|
fn no_function_definition_duplicates() {
|
2019-10-16 19:51:43 -07:00
|
|
|
let source = r#"
|
|
|
|
fn a() { 1 }
|
|
|
|
fn b() { 2 }
|
|
|
|
fn a() { 3 }
|
|
|
|
"#;
|
2021-10-18 22:51:36 -07:00
|
|
|
let (_, output) = add_symbols(source);
|
|
|
|
//TODO test for right type of error
|
|
|
|
output.unwrap_err();
|
2019-10-16 19:51:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-10-24 01:34:13 -07:00
|
|
|
fn no_variable_definition_duplicates() {
|
2019-10-16 19:51:43 -07:00
|
|
|
let source = r#"
|
2019-10-23 14:45:12 -07:00
|
|
|
let x = 9
|
|
|
|
let a = 20
|
|
|
|
let q = 39
|
|
|
|
let a = 30
|
2019-10-16 19:51:43 -07:00
|
|
|
"#;
|
2021-10-18 22:51:36 -07:00
|
|
|
let (_, output) = add_symbols(source);
|
|
|
|
let _output = output.unwrap_err();
|
|
|
|
/*
|
2019-10-24 01:34:13 -07:00
|
|
|
assert!(output.contains("Duplicate variable definition: a"));
|
2019-10-23 14:45:12 -07:00
|
|
|
assert!(output.contains("already defined at 2"));
|
2021-10-18 22:51:36 -07:00
|
|
|
*/
|
2019-10-16 19:51:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-10-24 01:34:13 -07:00
|
|
|
fn no_variable_definition_duplicates_in_function() {
|
2019-10-16 19:51:43 -07:00
|
|
|
let source = r#"
|
|
|
|
fn a() {
|
|
|
|
let a = 20
|
|
|
|
let b = 40
|
|
|
|
a + b
|
|
|
|
}
|
|
|
|
|
|
|
|
fn q() {
|
2019-10-24 01:34:13 -07:00
|
|
|
let a = 29
|
2019-10-16 19:51:43 -07:00
|
|
|
let x = 30
|
|
|
|
let x = 33
|
|
|
|
}
|
|
|
|
"#;
|
2021-10-18 22:51:36 -07:00
|
|
|
let (_, output) = add_symbols(source);
|
|
|
|
let _err = output.unwrap_err();
|
|
|
|
//assert!(output.unwrap_err().contains("Duplicate variable definition: x"))
|
2019-10-16 19:51:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dont_falsely_detect_duplicates() {
|
|
|
|
let source = r#"
|
|
|
|
let a = 20;
|
|
|
|
fn some_func() {
|
|
|
|
let a = 40;
|
|
|
|
77
|
|
|
|
}
|
|
|
|
let q = 39;
|
|
|
|
"#;
|
2021-10-18 22:51:36 -07:00
|
|
|
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());
|
2019-10-16 19:51:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enclosing_scopes() {
|
|
|
|
let source = r#"
|
|
|
|
fn outer_func(x) {
|
|
|
|
fn inner_func(arg) {
|
|
|
|
arg
|
|
|
|
}
|
|
|
|
x + inner_func(x)
|
|
|
|
}"#;
|
2021-10-18 22:51:36 -07:00
|
|
|
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());
|
2019-10-16 19:51:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enclosing_scopes_2() {
|
|
|
|
let source = r#"
|
|
|
|
fn outer_func(x) {
|
|
|
|
fn inner_func(arg) {
|
|
|
|
arg
|
|
|
|
}
|
|
|
|
|
|
|
|
fn second_inner_func() {
|
|
|
|
fn another_inner_func() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inner_func(x)
|
|
|
|
}"#;
|
2021-10-18 22:51:36 -07:00
|
|
|
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", "second_inner_func", "another_inner_func"])).is_some());
|
2019-10-16 19:51:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn enclosing_scopes_3() {
|
|
|
|
let source = r#"
|
|
|
|
fn outer_func(x) {
|
|
|
|
fn inner_func(arg) {
|
|
|
|
arg
|
|
|
|
}
|
|
|
|
|
|
|
|
fn second_inner_func() {
|
|
|
|
fn another_inner_func() {
|
|
|
|
}
|
|
|
|
fn another_inner_func() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inner_func(x)
|
|
|
|
}"#;
|
2021-10-18 22:51:36 -07:00
|
|
|
let (_, output) = add_symbols(source);
|
|
|
|
let _err = output.unwrap_err();
|
2019-10-16 19:51:43 -07:00
|
|
|
}
|
2019-10-24 01:34:13 -07:00
|
|
|
|
2019-10-24 03:02:52 -07:00
|
|
|
#[test]
|
|
|
|
fn modules() {
|
|
|
|
let source = r#"
|
|
|
|
module stuff {
|
|
|
|
fn item() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn item()
|
|
|
|
"#;
|
2021-10-18 22:51:36 -07:00
|
|
|
|
|
|
|
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();
|
2019-10-24 03:02:52 -07:00
|
|
|
}
|
|
|
|
|
2019-10-24 01:34:13 -07:00
|
|
|
#[test]
|
2019-10-24 02:13:07 -07:00
|
|
|
fn duplicate_modules() {
|
2019-10-24 01:34:13 -07:00
|
|
|
let source = r#"
|
2019-10-24 02:13:07 -07:00
|
|
|
module q {
|
|
|
|
fn foo() { 4 }
|
|
|
|
}
|
2019-10-24 01:34:13 -07:00
|
|
|
|
|
|
|
module a {
|
2019-10-24 02:13:07 -07:00
|
|
|
fn foo() { 334 }
|
2019-10-24 01:34:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
module a {
|
2019-10-24 02:13:07 -07:00
|
|
|
fn foo() { 256.1 }
|
2019-10-24 01:34:13 -07:00
|
|
|
}
|
|
|
|
"#;
|
2021-10-18 22:51:36 -07:00
|
|
|
let (_, output) = add_symbols(source);
|
|
|
|
let _output = output.unwrap_err();
|
2019-10-24 01:34:13 -07:00
|
|
|
}
|