From e157bf036a97190a7b1ba8ab678c609cbee2fe30 Mon Sep 17 00:00:00 2001 From: Romain Paquet Date: Tue, 25 Jun 2024 11:02:26 +0200 Subject: [PATCH] basic typing tests --- src/typing/tests.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/typing/tests.rs 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 + })); +}