diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-04-25 10:56:55 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-04-25 10:56:55 +0100 |
| commit | 72c84ea17a7e4b27cf9428ef23f713e32f5339d3 (patch) | |
| tree | 05f6d962ef88e92f292c834adb1225086824d749 /src/test/java/org/zwobble | |
| parent | 39ef07c887e33aff56c087388980a92e750b547c (diff) | |
Type check struct fields
Diffstat (limited to 'src/test/java/org/zwobble')
| -rw-r--r-- | src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java index b6fddba..f13731f 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/typechecker/TypeCheckerStructDefinitionTests.java @@ -2,10 +2,16 @@ package org.zwobble.hobgoblin.compiler.typechecker; import org.junit.jupiter.api.Test; import org.zwobble.hobgoblin.compiler.ast.typed.TypedStructDefinitionNode; +import org.zwobble.hobgoblin.compiler.ast.typed.TypedStructFieldDefinitionNode; +import org.zwobble.hobgoblin.compiler.ast.typed.TypedTypeLevelExpressionNode; +import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedArb; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNode; +import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructFieldDefinitionNode; import org.zwobble.hobgoblin.compiler.sources.NullSource; import org.zwobble.hobgoblin.compiler.types.NamespaceName; +import org.zwobble.hobgoblin.compiler.types.ScalarType; import org.zwobble.hobgoblin.compiler.types.StructType; +import org.zwobble.hobgoblin.compiler.types.TypeLevelValueType; import java.util.List; @@ -33,4 +39,51 @@ public class TypeCheckerStructDefinitionTests { ) )); } + + @Test + public void fieldsAreTypeChecked() { + var int32Type = new ScalarType("Int32"); + var int64Type = new ScalarType("Int64"); + var untyped = new UntypedStructDefinitionNode( + "X", + List.of( + new UntypedStructFieldDefinitionNode("a", UntypedArb.typeLevelReference("Int32"), NullSource.INSTANCE), + new UntypedStructFieldDefinitionNode("b", UntypedArb.typeLevelReference("Int64"), NullSource.INSTANCE) + ), + NullSource.INSTANCE + ); + var context = TypeCheckerContextArb.namespaceContext(NamespaceName.of("a", "b")); + context.declare(int32Type.name()); + context.define(int32Type.name(), new TypeLevelValueType(int32Type)); + context.declare(int64Type.name()); + context.define(int64Type.name(), new TypeLevelValueType(int64Type)); + + var typed = TypeChecker.typeCheckNamespaceStatement(untyped, context); + + assertThat(typed, instanceOf( + TypedStructDefinitionNode.class, + has( + "fields", + TypedStructDefinitionNode::fields, + isSequence( + allOf( + has("name", TypedStructFieldDefinitionNode::name, equalTo("a")), + has("type", TypedStructFieldDefinitionNode::type, has( + "value", + TypedTypeLevelExpressionNode::value, + equalTo(int32Type) + )) + ), + allOf( + has("name", TypedStructFieldDefinitionNode::name, equalTo("b")), + has("type", TypedStructFieldDefinitionNode::type, has( + "value", + TypedTypeLevelExpressionNode::value, + equalTo(int64Type) + )) + ) + ) + ) + )); + } } |
