[fine] Type testing with probes and reporting

I'm proud of the test harness here actually. Also fix a bug in
checking!
This commit is contained in:
John Doty 2024-01-05 17:10:15 -08:00
parent c0f40aa512
commit 618e0028d3
10 changed files with 192 additions and 78 deletions

View file

@ -1,4 +1,5 @@
use fine::parser::SyntaxTree;
use fine::semantics::{Semantics, Type};
use fine::tokens::Lines;
use pretty_assertions::assert_eq;
@ -70,7 +71,7 @@ fn rebase_concrete(source_path: &str, dump: &str) {
}
fn assert_concrete(tree: &SyntaxTree, expected: &str, source_path: &str) {
let dump = tree.dump();
let dump = tree.dump(false);
let rebase = std::env::var("FINE_TEST_REBASE")
.unwrap_or(String::new())
.to_lowercase();
@ -85,12 +86,52 @@ fn assert_concrete(tree: &SyntaxTree, expected: &str, source_path: &str) {
}
fn assert_type_at(
_tree: &SyntaxTree,
_lines: &Lines,
_pos: usize,
_expected: &str,
tree: &SyntaxTree,
lines: &Lines,
pos: usize,
expected: &str,
_source_path: &str,
) {
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}");
}
};
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));
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!();
}
assert_eq!(
expected, actual,
"The type of the tree at position {pos} was incorrect"
);
}
}
include!(concat!(env!("OUT_DIR"), "/generated_tests.rs"));

View file

@ -20,3 +20,5 @@
// | Semicolon:'";"'
//
1 * 2 + -3 * 4;
// type: 532 f64

View file

@ -20,3 +20,5 @@
// | Semicolon:'";"'
//
true and false or false and !true;
// type: 549 bool

View file

@ -24,3 +24,21 @@
// | RightBrace:'"}"'
//
if true { "discarded"; 23 } else { 45 }
// Here come some type probes!
// (type of the condition)
// type: 667 bool
//
// (the discarded expression)
// type: 674 string
//
// (the "then" clause)
// type: 686 f64
// type: 689 f64
//
// (the "else" clause)
// type: 696 f64
// type: 699 f64
//
// (the overall expression)
// type: 664 f64

View file

@ -4,6 +4,7 @@
// | LiteralExpression
// | Number:'"42"'
// | Semicolon:'";"'
//
// type: 138 Number
42;
// type: 129 f64

View file

@ -10,3 +10,5 @@
// | Semicolon:'";"'
//
"Hello " + 'world!';
// type: 261 string