diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-12-23 11:44:45 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-12-23 11:44:45 +0000 |
| commit | 1c60ecab9d6323b4d1fe6a2b2a510c6c5a5672e3 (patch) | |
| tree | 82d810b3fb2e6c6877133fb9c9eace8f02aa34e6 /src/test/java | |
| parent | ea47513fb5c1aef630be914295af7e8676d9fea9 (diff) | |
Parse struct type level params
Diffstat (limited to 'src/test/java')
3 files changed, 81 insertions, 1 deletions
diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java index 32f1d085..5b290949 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java @@ -355,6 +355,10 @@ public class UntypedNodeMatchers { ); } + public static Matcher<UntypedTypeLevelParamNode> isUntypedTypeLevelParamNode(String name) { + return has("name", x -> x.name(), equalTo(name)); + } + public static Matcher<UntypedTypeLevelExpressionNode> isUntypedTypeLevelReferenceNode(String name) { return instanceOf( UntypedTypeLevelReferenceNode.class, diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java index 98bb535f..ba87b50f 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java @@ -6,6 +6,8 @@ import org.shedlang.compiler.types.Visibility; import org.shedlang.compiler.util.P; import org.zwobble.precisely.Matcher; +import java.util.Optional; + import static org.zwobble.precisely.Matchers.equalTo; import static org.zwobble.precisely.Matchers.has; @@ -32,6 +34,12 @@ public class UntypedStructDefinitionNodeMatcher extends MatcherBuilder<UntypedNa return with(has("name", x -> x.name(), equalTo(name))); } + public UntypedStructDefinitionNodeMatcher withTypeLevelParams( + Matcher<Optional<? extends Iterable<? extends UntypedTypeLevelParamNode>>> typeLevelParams + ) { + return with(has("typeLevelParams", x -> x.typeLevelParams(), typeLevelParams)); + } + public UntypedStructDefinitionNodeMatcher withFields(Matcher<Iterable<? extends UntypedStructFieldDefinitionNode>> fields) { return with(has("fields", x -> x.fields(), fields)); } diff --git a/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java index cfe3e58e..ff8f3559 100644 --- a/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java @@ -6,7 +6,7 @@ import org.shedlang.compiler.types.Visibility; import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.*; import static org.shedlang.compiler.parser.ParserTesting.parseString; import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; +import static org.zwobble.precisely.Matchers.*; public class ParserStructDefinitionTests { @Test @@ -71,4 +71,72 @@ public class ParserStructDefinitionTests { isUntypedStructDefinitionNode().withVisibility(Visibility.PUBLIC) ); } + + @Test + public void canParseStructWithNoTypeLevelParams() { + var source = """ + struct X() {} + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedStructDefinitionNode() + .withTypeLevelParams(isOptionalEmpty()) + ); + } + + @Test + public void canParseStructWithSingleTypeLevelParam() { + var source = """ + struct X[T1]() {} + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedStructDefinitionNode() + .withTypeLevelParams(isOptionalOf(isSequence( + isUntypedTypeLevelParamNode("T1") + ))) + ); + } + + @Test + public void canParseStructWithMultipleTypeLevelParams() { + var source = """ + struct X[T1, T2, T3]() {} + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedStructDefinitionNode() + .withTypeLevelParams(isOptionalOf(isSequence( + isUntypedTypeLevelParamNode("T1"), + isUntypedTypeLevelParamNode("T2"), + isUntypedTypeLevelParamNode("T3") + ))) + ); + } + + @Test + public void typeLevelParamsCanHaveTrailingComma() { + var source = """ + struct X[T1,]() {} + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedStructDefinitionNode() + .withTypeLevelParams(isOptionalOf(isSequence( + isUntypedTypeLevelParamNode("T1") + ))) + ); + } } |
