aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2024-12-19 17:28:51 +0000
committerMichael Williamson <mike@zwobble.org>2024-12-19 17:28:51 +0000
commitb758aab829a5ddb20fd5284b1fad581db36742c6 (patch)
tree3fb84c99663ba3440b90b27f63de6fbd27a507df /src/test/java
parent89b5f09504528f6e7ee94c76df7cd0a91a9adfca (diff)
Create method information for methods in struct
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/TypedStructDefinitionNodeMatcher.java6
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerStructDefinitionTests.java55
2 files changed, 58 insertions, 3 deletions
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<Iterable<? extends TypedFunctionDefinitionNode>> 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()
+ ))
+ ))
+ );
+ }
}