summaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-11-29 11:21:01 +0000
committerMichael Williamson <mike@zwobble.org>2026-11-29 11:21:01 +0000
commitc93c58c1bf84fac0903dbc7788eed9634123a0e2 (patch)
treec26b94cc47cabb274b8eaa075b2b9d553f2222e4 /src/test/java
parentdacc15d3b7e0081cb45364b08689876d84c5933d (diff)
Type check function expression params
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerFunctionExpressionTests.java34
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)
+ )))
+ );
+ }
}