diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-10-21 13:23:09 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-10-21 13:23:09 +0100 |
| commit | f56afdfe4c659ac3ea3cd5f11b84455c5e424063 (patch) | |
| tree | 4ac6317a4ba9e06457a4705bc3d3bdc58ea5faab /src/test/java | |
| parent | b1313781607516060ce36fe676eaed521fbe75d2 (diff) | |
Add imports to typed namespace node
Diffstat (limited to 'src/test/java')
4 files changed, 43 insertions, 5 deletions
diff --git a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java index 7c5aa0fc..057e6062 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -163,7 +163,14 @@ public class Typed { List<String> boundVariableNames, List<String> fieldNames ) { - return new TypedNamespaceNode(namespaceName, body, boundVariableNames, fieldNames, Sources.NULL); + return new TypedNamespaceNode( + namespaceName, + List.of(), + body, + boundVariableNames, + fieldNames, + Sources.NULL + ); } public static TypedStringLiteralNode stringLiteral(String value) { diff --git a/src/test/java/org/shedlang/compiler/ast/typed/TypedNamespaceNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/typed/TypedNamespaceNodeMatcher.java index 0d7ecf83..f8ee5c8e 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/TypedNamespaceNodeMatcher.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/TypedNamespaceNodeMatcher.java @@ -34,6 +34,13 @@ public class TypedNamespaceNodeMatcher implements Matcher<TypedNamespaceNode> { return allOf(submatchers); } + public TypedNamespaceNodeMatcher withImports( + Matcher<Iterable<? extends TypedImportNode>> imports + ) { + return new TypedNamespaceNodeMatcher( + submatchers.plus(has("imports", x -> x.imports(), imports)) + ); + } public TypedNamespaceNodeMatcher withBody( Matcher<Iterable<? extends TypedNamespaceStatementNode>> body 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 1fe83958..dde00bca 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/TypedNodeMatchers.java @@ -1,9 +1,6 @@ package org.shedlang.compiler.ast.typed; -import org.shedlang.compiler.types.SumType; -import org.shedlang.compiler.types.TupleType; -import org.shedlang.compiler.types.Type; -import org.shedlang.compiler.types.TypeLevelValue; +import org.shedlang.compiler.types.*; import org.zwobble.precisely.Matcher; import java.math.BigInteger; @@ -139,6 +136,12 @@ public class TypedNodeMatchers { ); } + public static Matcher<TypedImportNode> isTypedImportNode( + NamespaceName namespaceName + ) { + return has("namespaceName", x -> x.namespaceName(), equalTo(namespaceName)); + } + public static Matcher<TypedExpressionNode> isTypedIntAddNode( Matcher<TypedExpressionNode> left, Matcher<TypedExpressionNode> right diff --git a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNamespaceTests.java b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNamespaceTests.java index 508bf6cd..a0fdd837 100644 --- a/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNamespaceTests.java +++ b/src/test/java/org/shedlang/compiler/typechecker/TypeCheckerNamespaceTests.java @@ -3,6 +3,7 @@ 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.ast.untyped.UntypedStructDefinitionNode; import org.shedlang.compiler.typechecker.errors.ReferenceBeforeDefinitionError; import org.shedlang.compiler.types.*; @@ -33,6 +34,26 @@ public class TypeCheckerNamespaceTests { } @Test + public void canTypeCheckImports() { + var untyped = UntypedNamespaceNode.builder() + .addImport(Untyped.import_(NamespaceName.of("A"))) + .addImport(Untyped.import_(NamespaceName.of("B"))) + .build(); + var context = TypeCheckerContext.EMPTY + .enterGlobal(P.map(Map.entry("Unit", Types.metaType(Types.UNIT)))); + + var result = TypeChecker.typeCheckNamespace(untyped, context); + + assertThat( + result.value(), + isTypedNamespaceNode().withImports(isSequence( + isTypedImportNode(NamespaceName.of("A")), + isTypedImportNode(NamespaceName.of("B")) + )) + ); + } + + @Test public void canTypeCheckMinimalFunctionDefinition() { var untyped = Untyped.namespace(List.of( UntypedFunctionDefinitionNode.builder() |
