diff options
Diffstat (limited to 'src/test')
3 files changed, 75 insertions, 0 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNodeMatchers.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNodeMatchers.java new file mode 100644 index 0000000..bbf6a84 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/typed/TypedNodeMatchers.java @@ -0,0 +1,27 @@ +package org.zwobble.hobgoblin.compiler.ast.typed; + +import org.zwobble.hobgoblin.compiler.types.ConstructedNativeType; +import org.zwobble.precisely.Matcher; + +import static org.zwobble.precisely.Matchers.*; + +public class TypedNodeMatchers { + private TypedNodeMatchers() { + } + + public static Matcher<TypedTypeLevelExpressionNode<?>> isTypedConstructedTypeNode( + Matcher<ConstructedNativeType> value + ) { + return instanceOf( + TypedConstructedTypeNode.class, + has("value", TypedConstructedTypeNode::value, value) + ); + } + + public static Matcher<TypedTypeLevelExpressionNode<?>> isTypedTypeLevelReferenceNode(String name) { + return instanceOf( + TypedTypeLevelReferenceNode.class, + has("name", TypedTypeLevelReferenceNode::name, equalTo(name)) + ); + } +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedArb.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedArb.java index 5a011d7..3943bc7 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedArb.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedArb.java @@ -2,10 +2,23 @@ package org.zwobble.hobgoblin.compiler.ast.untyped; import org.zwobble.hobgoblin.compiler.sources.NullSource; +import java.util.List; + public class UntypedArb { private UntypedArb() { } + public static UntypedConstructedTypeNode constructedType( + UntypedTypeLevelExpressionNode receiver, + List<UntypedTypeLevelExpressionNode> args + ) { + return new UntypedConstructedTypeNode( + receiver, + args, + NullSource.INSTANCE + ); + } + public static UntypedTypeLevelReferenceNode typeLevelReference(String name) { return new UntypedTypeLevelReferenceNode(name, NullSource.INSTANCE); } diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerConstructedTypeTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerConstructedTypeTests.java new file mode 100644 index 0000000..2fd1e91 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerConstructedTypeTests.java @@ -0,0 +1,35 @@ +package org.zwobble.hobgoblin.compiler.typechecker; + +import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedArb; +import org.zwobble.hobgoblin.compiler.types.*; + +import java.util.List; + +import static org.zwobble.hobgoblin.compiler.ast.typed.TypedNodeMatchers.isTypedConstructedTypeNode; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; + +public class TypeCheckerConstructedTypeTests { + @Test + public void whenArgsMatchParamsThenCanConstructTypeFromTypeConstructor() { + var untyped = UntypedArb.constructedType( + UntypedArb.typeLevelReference("List"), + List.of(UntypedArb.typeLevelReference("Int32")) + ); + var context = TypeCheckerContextArb.namespaceContext(); + context.declare("List"); + var listType = new SimpleNativeType("List"); + var listTypeConstructor = new TypeConstructor<>(List.of(new TypeParam("T")), listType); + context.define("List", new TypeLevelValueType(listTypeConstructor)); + context.declare("Int32"); + var int32Type = new SimpleNativeType("Int32"); + context.define("Int32", new TypeLevelValueType(int32Type)); + + var typed = TypeChecker.typeCheckTypeLevelExpression(untyped, context); + + assertThat(typed, isTypedConstructedTypeNode( + equalTo(new ConstructedNativeType(listTypeConstructor, List.of(int32Type))) + )); + } +} |
