refactor: another test helper macro

This commit is contained in:
Jeremy Wall 2023-08-25 20:41:27 -04:00 committed by Jeremy Wall
parent 3e8771476f
commit 46e55484dd
3 changed files with 51 additions and 24 deletions

View File

@ -776,14 +776,6 @@ impl ModuleDef {
}
}
fn normalize_path(p: PathBuf) -> PathBuf {
let mut normalized = PathBuf::new();
for segment in p.components() {
normalized.push(segment);
}
return normalized;
}
/// RangeDef defines a range with optional step.
#[derive(Debug, PartialEq, Clone)]
pub struct RangeDef {

View File

@ -42,6 +42,10 @@ impl Checker {
};
}
pub fn pop_shape(&mut self) -> Option<Shape> {
self.shape_stack.pop()
}
pub fn result(mut self) -> Result<BTreeMap<String, Shape>, BuildError> {
if let Some(err) = self.err_stack.pop() {
Err(err)

View File

@ -1,7 +1,7 @@
use std::convert::Into;
use crate::ast::walk::Walker;
use crate::ast::Position;
use crate::ast::{Position, PositionedItem};
use crate::parse;
use super::*;
@ -16,33 +16,64 @@ macro_rules! assert_type_fail {
let err = result.unwrap_err();
assert_eq!(err.msg, $msg);
assert_eq!(err.pos.unwrap(), $pos);
}}
}};
}
macro_rules! assert_type_success {
($e:expr, $shap:expr) => {{
let mut checker = Checker::new();
let mut expr = parse($e.into(), None).unwrap();
checker.walk_statement_list(expr.iter_mut().collect());
let maybe_shape = checker.pop_shape();
let result = checker.result();
assert!(result.is_ok(), "We expect this to typecheck successfully.");
assert!(
result.unwrap().is_empty(),
"We don't expect a symbol table entry."
);
assert!(maybe_shape.is_some(), "We got a shape out of it");
assert_eq!(maybe_shape.unwrap(), $shap);
}};
}
#[test]
fn simple_binary_typecheck() {
let mut checker = Checker::new();
let expr_str = "1 + 1;";
let mut expr = parse(expr_str.into(), None).unwrap();
checker.walk_statement_list(expr.iter_mut().collect());
let result = checker.result();
assert!(result.is_ok(), "We expect this to typecheck successfully.");
assert!(
result.unwrap().is_empty(),
"We don't expect a symbol table entry."
assert_type_success!(
"1 + 1;",
Shape::Int(PositionedItem::new(1, Position::new(1, 1, 0)))
);
}
// TODO Test that leverages symbol tables and let bindings.
#[test]
fn simple_binary_typefail() {
assert_type_fail!("1 + true;", "Expected int but got boolean", Position::new(1, 5, 4));
assert_type_fail!("1 + \"\";", "Expected int but got str", Position::new(1, 5, 4));
assert_type_fail!("1 + [];", "Expected int but got list", Position::new(1, 5, 4));
assert_type_fail!("1 + {};", "Expected int but got tuple", Position::new(1, 5, 4));
assert_type_fail!(
"1 + true;",
"Expected int but got boolean",
Position::new(1, 5, 4)
);
assert_type_fail!(
"1 + \"\";",
"Expected int but got str",
Position::new(1, 5, 4)
);
assert_type_fail!(
"1 + [];",
"Expected int but got list",
Position::new(1, 5, 4)
);
assert_type_fail!(
"1 + {};",
"Expected int but got tuple",
Position::new(1, 5, 4)
);
}
#[test]
fn multiple_binary_typefail() {
assert_type_fail!("1 + 1 + true;", "Expected int but got boolean", Position::new(1, 9, 8));
assert_type_fail!(
"1 + 1 + true;",
"Expected int but got boolean",
Position::new(1, 9, 8)
);
}