diff options
Diffstat (limited to 'src/test/java/org/zwobble')
| -rw-r--r-- | src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java index 9a261c6..0dde897 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java @@ -2,16 +2,21 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.junit.jupiter.api.Test; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedFunctionExpressionNode; +import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedNumberLiteralNode; +import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedParamNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedReferenceNode; import org.zwobble.hobgoblin.compiler.builtins.NativeTypes; import org.zwobble.hobgoblin.compiler.sources.NullSource; import org.zwobble.hobgoblin.compiler.types.FunctionType; import org.zwobble.hobgoblin.compiler.types.NamespaceName; +import org.zwobble.hobgoblin.compiler.types.Types; +import java.math.BigDecimal; import java.util.List; import java.util.Optional; import static org.zwobble.hobgoblin.compiler.ast.typed.TypedFunctionExpressionNodeMatcher.isTypedFunctionExpressionNode; +import static org.zwobble.hobgoblin.compiler.ast.typed.TypedParamNodeMatcher.isTypedParamNode; import static org.zwobble.hobgoblin.compiler.ast.typed.TypedReferenceNodeMatcher.isTypedReferenceNode; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.equalTo; @@ -40,4 +45,33 @@ public class TypeCheckerFunctionExpressionTests { ))) ); } + + @Test + public void paramTypesAreInferredFromHint() { + var untyped = UntypedFunctionExpressionNode.arbitrary() + .addParam(UntypedParamNode.arbitrary().withName("a")) + .addParam(UntypedParamNode.arbitrary().withName("b")) + .withBody(UntypedNumberLiteralNode.arbitrary().withValue(BigDecimal.ZERO)) + .build(); + var globalContext = TypeCheckerContextArb.globalContext(); + var namespaceName = NamespaceName.of("App"); + var namespaceContext = globalContext.enterNamespace(namespaceName); + + var typeHint = new FunctionType( + List.of(NativeTypes.INT_64, NativeTypes.STRING), + NativeTypes.INT_32 + ); + var typed = TypeChecker.typeCheckExpression(untyped, Optional.of(typeHint), namespaceContext); + + assertThat(typed, isTypedFunctionExpressionNode() + .withParams(isSequence( + isTypedParamNode().withName(equalTo("a")).withType(equalTo(NativeTypes.INT_64)), + isTypedParamNode().withName(equalTo("b")).withType(equalTo(NativeTypes.STRING)) + )) + .withType(equalTo(new FunctionType( + List.of(NativeTypes.INT_64, NativeTypes.STRING), + Types.constantNumber(BigDecimal.ZERO) + ))) + ); + } } |
