diff options
| author | Michael Williamson <mike@zwobble.org> | 2024-12-19 18:39:32 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2024-12-19 18:39:32 +0000 |
| commit | fd8ec8d57f27a669e657d80c4534dd09dc922b4f (patch) | |
| tree | 6c20fc7e01716b7caa28864cfa1404ce157a6d71 /src/test/java | |
| parent | 69d904e54d413e58ee854de474fbd82d3e94a81e (diff) | |
Type check method parameters
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java | 71 |
1 files changed, 70 insertions, 1 deletions
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() + )) + )) + ); + } } |
