From 27a2459d91c36ba24b78155826ee8d80afb918ce Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 9 Aug 2026 21:39:21 +0100 Subject: Parse type params for struct definition --- .../UntypedStructDefinitionNodeMatcher.java | 6 ++++ .../parser/ParserStructDefinitionTests.java | 39 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) (limited to 'src/test/java') diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java index a198802..d559c0d 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java @@ -36,6 +36,12 @@ public final class UntypedStructDefinitionNodeMatcher implements org.zwobble.pre return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNodeMatcher(submatchers); } + public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNodeMatcher withTypeParams(org.zwobble.precisely.Matcher>> typeParams) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("typeParams", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNode::typeParams, typeParams)); + return new org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNodeMatcher(submatchers); + } + public org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNodeMatcher withFields(org.zwobble.precisely.Matcher>> fields) { var submatchers = new java.util.ArrayList>(this.submatchers); submatchers.add(org.zwobble.precisely.Matchers.has("fields", org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNode::fields, fields)); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserStructDefinitionTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserStructDefinitionTests.java index 0b8ba5e..2b2fe33 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserStructDefinitionTests.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserStructDefinitionTests.java @@ -6,6 +6,8 @@ import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructFieldDefinitionNode; import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelReferenceNode; +import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedStructDefinitionNodeMatcher.isUntypedStructDefinitionNode; +import static org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeParamNodeMatcher.isUntypedTypeParamNode; import static org.zwobble.hobgoblin.compiler.parser.ParserTesting.parseString; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.*; @@ -29,6 +31,43 @@ public class ParserStructDefinitionTests { )); } + @Test + public void oneTypeParam() { + var source = """ + struct A[B] { + }"""; + + var node = parseString( + source, + Parser::parseNamespaceStatement + ); + + assertThat(node, isUntypedStructDefinitionNode() + .withTypeParams(isOptionalOf(isSequence( + isUntypedTypeParamNode().withName(equalTo("B")) + ))) + ); + } + + @Test + public void manyTypeParams() { + var source = """ + struct A[B, C] { + }"""; + + var node = parseString( + source, + Parser::parseNamespaceStatement + ); + + assertThat(node, isUntypedStructDefinitionNode() + .withTypeParams(isOptionalOf(isSequence( + isUntypedTypeParamNode().withName(equalTo("B")), + isUntypedTypeParamNode().withName(equalTo("C")) + ))) + ); + } + @Test public void structWithFields() { var source = """ -- cgit v1.2.3