From fd8ec8d57f27a669e657d80c4534dd09dc922b4f Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 19 Dec 2024 18:39:32 +0000 Subject: Type check method parameters --- .../TypeCheckerStructDefinitionTests.java | 71 +++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java index eba1b735..09df039c 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java @@ -282,7 +282,7 @@ public class TypeCheckerStructDefinitionTests { } @Test - public void canTypeCheckMethodWithNoParams() { + public void canTypeCheckMethodWithOnlyThisParam() { var untyped = UntypedStructDefinitionNode.builder() .withName("ExampleStruct") .addMethod( @@ -328,4 +328,73 @@ public class TypeCheckerStructDefinitionTests { )) ); } + + @Test + public void canTypeCheckMethodWithMultipleParams() { + var untyped = UntypedStructDefinitionNode.builder() + .withName("ExampleStruct") + .addMethod( + UntypedFunctionDefinitionNode.builder() + .addPositionalParam("this", Optional.empty()) + .addPositionalParam("a", Untyped.typeLevelReference("Bool")) + .addNamedParam("b", Untyped.typeLevelReference("Int")) + .withName("a") + .withReturnType(Untyped.typeLevelReference("Unit")) + .build() + ) + .build(); + var context = TypeCheckerContext.EMPTY + .enterGlobal(P.map( + Map.entry("Unit", Types.metaType(Types.UNIT)), + Map.entry("Bool", Types.metaType(Types.BOOL)), + Map.entry("Int", Types.metaType(Types.INT)) + )); + + var typed = typeCheckNamespaceStatement(untyped, context); + + assertThat( + typed.value(), + isTypedStructDefinitionNode() + .withMethods(isSequence( + isTypedFunctionDefinitionNode() + .withParams(isTypedParamsNode( + isSequence( + isTypedParamNode("this", isOptionalEmpty()), + isTypedParamNode( + "a", + isOptionalOf(isTypedTypeLevelReferenceNode( + "Bool", + Types.BOOL + )) + ) + ), + isSequence( + isTypedParamNode( + "b", + isOptionalOf(isTypedTypeLevelReferenceNode( + "Int", + Types.INT + )) + ) + ) + )) + )) + ); + var structTypeInfo = typed.context().toTypeInfos() + .structTypeInfo(Types.struct(NAMESPACE_NAME, "ExampleStruct")) + .orElseThrow(); + assertThat( + structTypeInfo, + has("methods", x -> x.methods(), isSequence( + equalTo(new MethodInfo( + "a", + FunctionType.builder() + .addPositionalParam(Types.BOOL) + .addNamedParam("b", Types.INT) + .withReturnType(Types.UNIT) + .build() + )) + )) + ); + } } -- cgit v1.2.3