mirror of
https://github.com/zaphar/ucg.git
synced 2025-07-25 18:49:50 -04:00
refactor: another test helper macro
This commit is contained in:
parent
3e8771476f
commit
46e55484dd
@ -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.
|
/// RangeDef defines a range with optional step.
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub struct RangeDef {
|
pub struct RangeDef {
|
||||||
|
@ -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> {
|
pub fn result(mut self) -> Result<BTreeMap<String, Shape>, BuildError> {
|
||||||
if let Some(err) = self.err_stack.pop() {
|
if let Some(err) = self.err_stack.pop() {
|
||||||
Err(err)
|
Err(err)
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::convert::Into;
|
use std::convert::Into;
|
||||||
|
|
||||||
use crate::ast::walk::Walker;
|
use crate::ast::walk::Walker;
|
||||||
use crate::ast::Position;
|
use crate::ast::{Position, PositionedItem};
|
||||||
use crate::parse;
|
use crate::parse;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
@ -16,33 +16,64 @@ macro_rules! assert_type_fail {
|
|||||||
let err = result.unwrap_err();
|
let err = result.unwrap_err();
|
||||||
assert_eq!(err.msg, $msg);
|
assert_eq!(err.msg, $msg);
|
||||||
assert_eq!(err.pos.unwrap(), $pos);
|
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]
|
#[test]
|
||||||
fn simple_binary_typecheck() {
|
fn simple_binary_typecheck() {
|
||||||
let mut checker = Checker::new();
|
assert_type_success!(
|
||||||
let expr_str = "1 + 1;";
|
"1 + 1;",
|
||||||
let mut expr = parse(expr_str.into(), None).unwrap();
|
Shape::Int(PositionedItem::new(1, Position::new(1, 1, 0)))
|
||||||
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."
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO Test that leverages symbol tables and let bindings.
|
// TODO Test that leverages symbol tables and let bindings.
|
||||||
#[test]
|
#[test]
|
||||||
fn simple_binary_typefail() {
|
fn simple_binary_typefail() {
|
||||||
assert_type_fail!("1 + true;", "Expected int but got boolean", Position::new(1, 5, 4));
|
assert_type_fail!(
|
||||||
assert_type_fail!("1 + \"\";", "Expected int but got str", Position::new(1, 5, 4));
|
"1 + true;",
|
||||||
assert_type_fail!("1 + [];", "Expected int but got list", Position::new(1, 5, 4));
|
"Expected int but got boolean",
|
||||||
assert_type_fail!("1 + {};", "Expected int but got tuple", Position::new(1, 5, 4));
|
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]
|
#[test]
|
||||||
fn multiple_binary_typefail() {
|
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)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user