diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-09-27 20:28:09 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-09-27 20:28:09 +0100 |
| commit | 059d6276a4d2ef23672adb009e633a06f3603a8c (patch) | |
| tree | 162ba95c15b2ed66c323be4c2225c4b096313dfc /src/test/java | |
| parent | a0074f328bfabc0c28d7e5b1202761daaa265fc8 (diff) | |
Parse struct definition fields
Diffstat (limited to 'src/test/java')
3 files changed, 65 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 95d666cd..d08e784d 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java @@ -198,6 +198,10 @@ public class UntypedNodeMatchers { return UntypedStructDefinitionNodeMatcher.empty(); } + public static UntypedStructFieldDefinitionNodeMatcher isUntypedStructFieldDefinitionNode() { + return UntypedStructFieldDefinitionNodeMatcher.empty(); + } + public static Matcher<UntypedExpressionNode> isUntypedSubNode( Matcher<UntypedExpressionNode> left, Matcher<UntypedExpressionNode> right diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructFieldDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructFieldDefinitionNodeMatcher.java new file mode 100644 index 00000000..2e0f1d63 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructFieldDefinitionNodeMatcher.java @@ -0,0 +1,37 @@ +package org.shedlang.compiler.ast.untyped; + +import org.pcollections.PVector; +import org.shedlang.compiler.testing.MatcherBuilder; +import org.shedlang.compiler.util.P; +import org.zwobble.precisely.Matcher; + +import static org.zwobble.precisely.Matchers.equalTo; +import static org.zwobble.precisely.Matchers.has; + +public class UntypedStructFieldDefinitionNodeMatcher extends MatcherBuilder<UntypedStructFieldDefinitionNode, UntypedStructFieldDefinitionNode, UntypedStructFieldDefinitionNodeMatcher> { + public static UntypedStructFieldDefinitionNodeMatcher empty() { + return new UntypedStructFieldDefinitionNodeMatcher(P.vector()); + } + + public UntypedStructFieldDefinitionNodeMatcher(PVector<Matcher<? super UntypedStructFieldDefinitionNode>> submatchers) { + super(submatchers); + } + + @Override + protected Class<UntypedStructFieldDefinitionNode> expectedType() { + return UntypedStructFieldDefinitionNode.class; + } + + @Override + protected UntypedStructFieldDefinitionNodeMatcher create(PVector<Matcher<? super UntypedStructFieldDefinitionNode>> submatchers) { + return new UntypedStructFieldDefinitionNodeMatcher(submatchers); + } + + public UntypedStructFieldDefinitionNodeMatcher withName(String name) { + return with(has("name", x -> x.name(), equalTo(name))); + } + + public UntypedStructFieldDefinitionNodeMatcher withType(Matcher<UntypedTypeLevelExpressionNode> type) { + return with(has("type", x -> x.type(), type)); + } +} diff --git a/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java b/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java index 8a0fe6b6..3fd640b1 100644 --- a/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java @@ -2,7 +2,7 @@ package org.shedlang.compiler.parser; import org.junit.jupiter.api.Test; -import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.isUntypedStructDefinitionNode; +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; @@ -23,4 +23,27 @@ public class ParserStructDefinitionTests { .withFields(isSequence()) ); } + + @Test + public void canParseStructWithFields() { + var source = """ + struct X(.name: String, .accessLevel: AccessLevel) {} + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedStructDefinitionNode() + .withName("X") + .withFields(isSequence( + isUntypedStructFieldDefinitionNode() + .withName("name") + .withType(isUntypedTypeLevelReferenceNode("String")), + isUntypedStructFieldDefinitionNode() + .withName("accessLevel") + .withType(isUntypedTypeLevelReferenceNode("AccessLevel")) + )) + ); + } } |
