[fine] Tests for type errors
WHEEEEEEE!
This commit is contained in:
parent
618e0028d3
commit
a9c1b04920
8 changed files with 123 additions and 47 deletions
|
|
@ -85,6 +85,58 @@ fn assert_concrete(tree: &SyntaxTree, expected: &str, source_path: &str) {
|
|||
}
|
||||
}
|
||||
|
||||
fn report_semantic_error(semantics: &Semantics, message: &str) {
|
||||
let tree = semantics.tree();
|
||||
|
||||
println!("{message}! Parsed the tree as:");
|
||||
println!("\n{}", tree.dump(true));
|
||||
|
||||
let errors = semantics.snapshot_errors();
|
||||
if errors.len() == 0 {
|
||||
println!("There were no errors reported during checking.\n");
|
||||
} else {
|
||||
println!(
|
||||
"{} error{} reported during checking:",
|
||||
errors.len(),
|
||||
if errors.len() == 1 { "" } else { "s" }
|
||||
);
|
||||
for error in &errors {
|
||||
println!(" Error: {error}");
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! semantic_panic {
|
||||
($semantics:expr, $($t:tt)*) => {{
|
||||
let message = format!($($t)*);
|
||||
report_semantic_error($semantics, &message);
|
||||
panic!("{message}");
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! semantic_assert {
|
||||
($semantics:expr, $pred:expr, $($t:tt)*) => {{
|
||||
if !$pred {
|
||||
let message = format!($($t)*);
|
||||
report_semantic_error($semantics, &message);
|
||||
panic!("{message}");
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! semantic_assert_eq {
|
||||
($semantics:expr, $left:expr, $right:expr, $($t:tt)*) => {{
|
||||
let ll = $left;
|
||||
let rr = $right;
|
||||
if ll != rr {
|
||||
let message = format!($($t)*);
|
||||
report_semantic_error($semantics, &message);
|
||||
assert_eq!(ll, rr, "{}", message);
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
fn assert_type_at(
|
||||
tree: &SyntaxTree,
|
||||
lines: &Lines,
|
||||
|
|
@ -92,46 +144,49 @@ fn assert_type_at(
|
|||
expected: &str,
|
||||
_source_path: &str,
|
||||
) {
|
||||
let semantics = Semantics::new(tree, lines);
|
||||
let tree_ref = match tree.find_tree_at(pos) {
|
||||
Some(t) => t,
|
||||
None => {
|
||||
println!("Unable to find the subtee at position {pos}! Parsed the tree as:");
|
||||
println!("\n{}", tree.dump(true));
|
||||
panic!("Cannot find tree at position {pos}");
|
||||
}
|
||||
None => semantic_panic!(&semantics, "Unable to find the subtee at position {pos}"),
|
||||
};
|
||||
|
||||
let semantics = Semantics::new(tree, lines);
|
||||
let tree_type = semantics.type_of(tree_ref, true);
|
||||
|
||||
let actual = format!("{}", tree_type.unwrap_or(Type::Error));
|
||||
if actual != expected {
|
||||
println!(
|
||||
"The type of the {:?} tree at position {pos} had the wrong type! Parsed the tree as:",
|
||||
tree[tree_ref].kind
|
||||
);
|
||||
println!("\n{}", tree.dump(true));
|
||||
semantic_assert_eq!(
|
||||
&semantics,
|
||||
expected,
|
||||
actual,
|
||||
"The type of the tree at position {pos} was incorrect"
|
||||
);
|
||||
}
|
||||
|
||||
let errors = semantics.snapshot_errors();
|
||||
if errors.len() == 0 {
|
||||
println!("There were no errors reported during type checking.\n");
|
||||
} else {
|
||||
println!(
|
||||
"{} error{} reported during type checking:",
|
||||
errors.len(),
|
||||
if errors.len() == 1 { "" } else { "s" }
|
||||
);
|
||||
for error in &errors {
|
||||
println!(" Error: {error}");
|
||||
}
|
||||
println!();
|
||||
}
|
||||
fn assert_type_error_at(
|
||||
tree: &SyntaxTree,
|
||||
lines: &Lines,
|
||||
pos: usize,
|
||||
expected: &str,
|
||||
_source_path: &str,
|
||||
) {
|
||||
let semantics = Semantics::new(tree, lines);
|
||||
let tree_ref = match tree.find_tree_at(pos) {
|
||||
Some(t) => t,
|
||||
None => semantic_panic!(&semantics, "Unable to find the subtee at position {pos}"),
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
expected, actual,
|
||||
"The type of the tree at position {pos} was incorrect"
|
||||
);
|
||||
}
|
||||
let tree_type = semantics.type_of(tree_ref, true);
|
||||
semantic_assert!(
|
||||
&semantics,
|
||||
matches!(tree_type, Some(Type::Error)),
|
||||
"The type of the {:?} tree at position {pos} was '{tree_type:?}', not an error",
|
||||
tree[tree_ref].kind
|
||||
);
|
||||
|
||||
let errors = semantics.snapshot_errors();
|
||||
semantic_assert!(
|
||||
&semantics,
|
||||
errors.iter().any(|e| e.message == expected),
|
||||
"Unable to find the expected error message '{expected}'"
|
||||
);
|
||||
}
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/generated_tests.rs"));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue