From 646c5b9673ffbbc691db246ca8a253cb3e6ea8a1 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 10 Jun 2023 11:17:03 +0100 Subject: Check positional arg type matches param type --- .../compiler/typechecker/TypeCheckerCallTests.java | 26 ++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/test/java/org') diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java index aa7e2bce..fb645d67 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerCallTests.java @@ -2,6 +2,7 @@ package org.shedlang.compiler.typechecker; import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.untyped.Untyped; +import org.shedlang.compiler.typechecker.typesets.TypeSets; import org.shedlang.compiler.types.FunctionType; import org.shedlang.compiler.types.Types; import org.shedlang.compiler.util.P; @@ -9,8 +10,10 @@ import org.shedlang.compiler.util.P; import java.util.List; import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.shedlang.compiler.ast.typed.TypedNodeMatchers.*; import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; import static org.zwobble.precisely.Matchers.isSequence; public class TypeCheckerCallTests { @@ -94,4 +97,27 @@ public class TypeCheckerCallTests { ))) ); } + + @Test + public void whenPositionalArgHasWrongTypeThenErrorIsThrown() { + var untyped = Untyped.call( + Untyped.reference("f"), + List.of( + Untyped.stringLiteral("hello") + ) + ); + var functionType = FunctionType.builder() + .addPositionalParam(Types.INT) + .build(); + var context = TypeCheckerContext.EMPTY + .enter(P.map(Map.entry("f", functionType))); + + var error = assertThrows( + UnexpectedTypeError.class, + () -> TypeChecker.typeCheckExpression(untyped, context) + ); + + assertThat(error.expected(), equalTo(TypeSets.type(Types.INT))); + assertThat(error.actual(), equalTo(Types.STRING)); + } } -- cgit v1.2.3