From b758aab829a5ddb20fd5284b1fad581db36742c6 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 19 Dec 2024 17:28:51 +0000 Subject: Create method information for methods in struct --- .../typed/TypedStructDefinitionNodeMatcher.java | 6 +++ .../TypeCheckerStructDefinitionTests.java | 55 ++++++++++++++++++++-- 2 files changed, 58 insertions(+), 3 deletions(-) (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/ast/typed/TypedStructDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/typed/TypedStructDefinitionNodeMatcher.java index c9a10623..d14aeeb6 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/TypedStructDefinitionNodeMatcher.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/TypedStructDefinitionNodeMatcher.java @@ -66,4 +66,10 @@ public class TypedStructDefinitionNodeMatcher extends MatcherBuilder< equalTo(fieldsArePositional) )); } + + public TypedStructDefinitionNodeMatcher withMethods( + Matcher> methods + ) { + return with(has("methods", x -> x.methods(), methods)); + } } diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java index 49833148..dee72117 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java @@ -2,15 +2,15 @@ package org.shedlang.compiler.typechecker; import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.untyped.Untyped; +import org.shedlang.compiler.ast.untyped.UntypedFunctionDefinitionNode; import org.shedlang.compiler.ast.untyped.UntypedStructDefinitionNode; import org.shedlang.compiler.typechecker.errors.CannotDefineStructOutsideOfNamespaceError; -import org.shedlang.compiler.types.FieldInfo; -import org.shedlang.compiler.types.NamespaceName; -import org.shedlang.compiler.types.Types; +import org.shedlang.compiler.types.*; import org.shedlang.compiler.util.P; import java.util.List; import java.util.Map; +import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.shedlang.compiler.ast.typed.TypedNodeMatchers.*; @@ -280,4 +280,53 @@ public class TypeCheckerStructDefinitionTests { assertThat(typed.context().compileTimeLookup("T1"), isOptionalEmpty()); } + + @Test + public void canTypeCheckMethodWithNoParams() { + var untyped = UntypedStructDefinitionNode.builder() + .withName("ExampleStruct") + .addMethod( + UntypedFunctionDefinitionNode.builder() + .addPositionalParam("this", Optional.empty()) + .withName("a") + .withReturnType(Untyped.typeLevelReference("Bool")) + .build() + ) + .build(); + var context = TypeCheckerContext.EMPTY + .enterGlobal(P.map( + Map.entry("Bool", Types.metaType(Types.BOOL)) + )); + + var typed = typeCheckNamespaceStatement(untyped, context); + +// assertThat( +// typed.value(), +// isTypedStructDefinitionNode() +// .withMethods(isSequence( +// isTypedFunctionDefinitionNode() +// .withParams(isTypedParamsNode( +// isSequence( +// isTypedParamNode("this", isOptionalEmpty()) +// ), +// isSequence() +// )) +// )) +// ); + 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.struct(NAMESPACE_NAME, "ExampleStruct")) + .withReturnType(Types.BOOL) + .build() + )) + )) + ); + } } -- cgit v1.2.3