From 46e55484dd1c802e6c1df0843fcdfefa7715c228 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Fri, 25 Aug 2023 20:41:27 -0400 Subject: [PATCH] refactor: another test helper macro --- src/ast/mod.rs | 8 ----- src/ast/typecheck/mod.rs | 4 +++ src/ast/typecheck/test.rs | 63 +++++++++++++++++++++++++++++---------- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 7ce0f26..d993dc4 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -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 { diff --git a/src/ast/typecheck/mod.rs b/src/ast/typecheck/mod.rs index be23993..4f14801 100644 --- a/src/ast/typecheck/mod.rs +++ b/src/ast/typecheck/mod.rs @@ -42,6 +42,10 @@ impl Checker { }; } + pub fn pop_shape(&mut self) -> Option { + self.shape_stack.pop() + } + pub fn result(mut self) -> Result, BuildError> { if let Some(err) = self.err_stack.pop() { Err(err) diff --git a/src/ast/typecheck/test.rs b/src/ast/typecheck/test.rs index be1f55b..f0defa4 100644 --- a/src/ast/typecheck/test.rs +++ b/src/ast/typecheck/test.rs @@ -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) + ); }