basic typing tests

This commit is contained in:
Romain Paquet 2024-06-25 11:02:26 +02:00
parent ba838292f6
commit e157bf036a

33
src/typing/tests.rs Normal file
View file

@ -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
}));
}