From 39928746d5d65f74061aae83e0d1ace7b17a3bb3 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 13 Jun 2026 20:59:52 +0100 Subject: Parse fields in sum definition --- .../compiler/parser/ParserSumDefinitionTests.java | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'src/test/java/org/zwobble') diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserSumDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserSumDefinitionTests.java index 34b94fc..20de7ba 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserSumDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserSumDefinitionTests.java @@ -6,6 +6,9 @@ import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumVariantDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelReferenceNode; +import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructFieldDefinitionNodeMatcher.isUntypedStructFieldDefinitionNode; +import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedSumDefinitionNodeMatcher.isUntypedSumDefinitionNode; +import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelReferenceNodeMatcher.isUntypedTypeLevelReferenceNode; import static org.zwobble.hobgoblin.compiler.parser.ParserTesting.parseString; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.*; @@ -81,6 +84,48 @@ public class ParserSumDefinitionTests { )); } + @Test + public void emptySumHasNoFields() { + var source = """ + sum Shape { + }"""; + + var node = parseString( + source, + Parser::parseNamespaceStatement + ); + + assertThat(node, instanceOf( + UntypedSumDefinitionNode.class, + has("fields", UntypedSumDefinitionNode::fields, isSequence()) + )); + } + + @Test + public void sumWithFields() { + var source = """ + sum Shape { + field height: Int32; + field width: Int64; + }"""; + + var node = parseString( + source, + Parser::parseNamespaceStatement + ); + + assertThat(node, isUntypedSumDefinitionNode() + .withFields(isSequence( + isUntypedStructFieldDefinitionNode() + .withName(equalTo("height")) + .withType(isUntypedTypeLevelReferenceNode().withName(equalTo("Int32"))), + isUntypedStructFieldDefinitionNode() + .withName(equalTo("width")) + .withType(isUntypedTypeLevelReferenceNode().withName(equalTo("Int64"))) + )) + ); + } + @Test public void sumWithDocComment() { var source = """ -- cgit v1.2.3