From 1c60ecab9d6323b4d1fe6a2b2a510c6c5a5672e3 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 23 Dec 2023 11:44:45 +0000 Subject: Parse struct type level params --- .../parser/ParserStructDefinitionTests.java | 70 +++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) (limited to 'src/test/java/org/shedlang/compiler/parser') 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") + ))) + ); + } } -- cgit v1.2.3