aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-10-20 18:55:42 +0100
committerMichael Williamson <mike@zwobble.org>2023-10-20 18:55:55 +0100
commit8c9b4c92c1493d96d4bb2a5b88bf9e98e3a584e2 (patch)
tree876266a4f385d8c373538d74aa243949d7ae21fc /src/test/java
parentef4b9d4241777933cfba1a7140597a36c8d93638 (diff)
Support import of namespaces in type checker
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java2
-rw-r--r--src/test/java/org/shedlang/compiler/ast/untyped/Untyped.java4
-rw-r--r--src/test/java/org/shedlang/compiler/typechecker/TypeCheckerImportTests.java52
3 files changed, 57 insertions, 1 deletions
diff --git a/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java b/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java
index c5281090..1fe83958 100644
--- a/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java
+++ b/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java
@@ -194,7 +194,7 @@ public class TypedNodeMatchers {
public static Matcher<TypedNamespaceNode> isTypedNamespaceNode(
Matcher<Iterable<? extends TypedNamespaceStatementNode>> body,
- Matcher<Iterable<? extends String>> fieldNames
+ Matcher<? super Iterable<? extends String>> fieldNames
) {
return allOf(
has("body", x -> x.body(), body),
diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/Untyped.java b/src/test/java/org/shedlang/compiler/ast/untyped/Untyped.java
index 2c2aad8c..3e925601 100644
--- a/src/test/java/org/shedlang/compiler/ast/untyped/Untyped.java
+++ b/src/test/java/org/shedlang/compiler/ast/untyped/Untyped.java
@@ -102,6 +102,10 @@ public class Untyped {
return new UntypedIfNode(conditionalBranches, elseBody, Sources.NULL);
}
+ public static UntypedImportNode import_(NamespaceName namespaceName) {
+ return new UntypedImportNode(namespaceName, Sources.NULL);
+ }
+
public static UntypedIntLiteralNode intLiteral(int value) {
return new UntypedIntLiteralNode(BigInteger.valueOf(value), Sources.NULL);
}
diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerImportTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerImportTests.java
new file mode 100644
index 00000000..984bc84a
--- /dev/null
+++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerImportTests.java
@@ -0,0 +1,52 @@
+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.UntypedNamespaceNode;
+import org.shedlang.compiler.types.NamespaceName;
+import org.shedlang.compiler.types.Types;
+import org.shedlang.compiler.util.P;
+
+import java.util.Map;
+
+import static org.shedlang.compiler.ast.typed.TypedNodeMatchers.*;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.anything;
+import static org.zwobble.precisely.Matchers.isSequence;
+
+public class TypeCheckerImportTests {
+ @Test
+ public void canReferenceImport() {
+ var importedNamespaceName = NamespaceName.of("Imported");
+ var untyped = UntypedNamespaceNode.builder()
+ .addImport(Untyped.import_(importedNamespaceName))
+ .addStatement(
+ UntypedFunctionDefinitionNode.builder()
+ .withReturnType(Untyped.typeLevelReference("Unit"))
+ .addStatement(Untyped.expressionStatement(Untyped.reference("Imported")))
+ .build()
+ )
+ .build();
+ var context = TypeCheckerContext.EMPTY
+ .enterGlobal(P.map(Map.entry("Unit", Types.metaType(Types.UNIT))));
+
+ var result = TypeChecker.typeCheckNamespace(untyped, context);
+
+ assertThat(
+ result.value(),
+ // TODO: custom matcher
+ isTypedNamespaceNode(
+ isSequence(
+ isTypedFunctionDefinitionNode()
+ .withBody(isTypedBlockNode(isSequence(
+ isTypedExpressionStatementNode(
+ isTypedReferenceNode("Imported", Types.namespace(importedNamespaceName))
+ )
+ )))
+ ),
+ anything()
+ )
+ );
+ }
+}