diff --git a/src/typing/tests.rs b/src/typing/tests.rs new file mode 100644 index 0000000..e579554 --- /dev/null +++ b/src/typing/tests.rs @@ -0,0 +1,33 @@ +use crate::{ + ast::ModulePath, + parsing::parse_as_module, + typing::{ + error::{TypeError, TypeErrorKind}, + BinaryOperator, Type, + }, +}; + +#[test] +fn addition_int_and_float() { + let source = "fn add(a: int, b: float) int { a + b }"; + let mut ast = parse_as_module(source, ModulePath::default()).unwrap(); + let res = ast.type_check(); + assert!(res.is_err_and(|e| e.kind + == TypeErrorKind::InvalidBinaryOperator { + operator: BinaryOperator::Add, + lht: Type::Int, + rht: Type::Float + })); +} + +#[test] +fn return_int_instead_of_float() { + let source = "fn add(a: int, b: int) float { a + b }"; + let mut ast = parse_as_module(source, ModulePath::default()).unwrap(); + let res = ast.type_check(); + assert!(res.is_err_and(|e| e.kind + == TypeErrorKind::BlockTypeDoesNotMatchFunctionType { + block_type: Type::Int, + function_type: Type::Float + })); +}