diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-09-27 20:22:34 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-09-27 20:22:34 +0100 |
| commit | a0074f328bfabc0c28d7e5b1202761daaa265fc8 (patch) | |
| tree | 28336e56dbf2341e6630d5ea27f7a9348b46ccde /src/test/java | |
| parent | 71e8f7645a022dbe913cb0e5ea3a1ab5e9b33a0f (diff) | |
Parse struct without fields
Diffstat (limited to 'src/test/java')
3 files changed, 67 insertions, 0 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 0dac3b15..95d666cd 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java @@ -194,6 +194,10 @@ public class UntypedNodeMatchers { ); } + public static UntypedStructDefinitionNodeMatcher isUntypedStructDefinitionNode() { + return UntypedStructDefinitionNodeMatcher.empty(); + } + public static Matcher<UntypedExpressionNode> isUntypedSubNode( Matcher<UntypedExpressionNode> left, Matcher<UntypedExpressionNode> right diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java new file mode 100644 index 00000000..5a73f49b --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.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 UntypedStructDefinitionNodeMatcher extends MatcherBuilder<UntypedNamespaceStatementNode, UntypedStructDefinitionNode, UntypedStructDefinitionNodeMatcher> { + public static UntypedStructDefinitionNodeMatcher empty() { + return new UntypedStructDefinitionNodeMatcher(P.vector()); + } + + public UntypedStructDefinitionNodeMatcher(PVector<Matcher<? super UntypedStructDefinitionNode>> submatchers) { + super(submatchers); + } + + @Override + protected Class<UntypedStructDefinitionNode> expectedType() { + return UntypedStructDefinitionNode.class; + } + + @Override + protected UntypedStructDefinitionNodeMatcher create(PVector<Matcher<? super UntypedStructDefinitionNode>> submatchers) { + return new UntypedStructDefinitionNodeMatcher(submatchers); + } + + public UntypedStructDefinitionNodeMatcher withName(String name) { + return with(has("name", x -> x.name(), equalTo(name))); + } + + 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 new file mode 100644 index 00000000..8a0fe6b6 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java @@ -0,0 +1,26 @@ +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.parser.ParserTesting.parseString; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class ParserStructDefinitionTests { + @Test + public void canParseStructWithNoFields() { + var source = """ + struct X() {} + """; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedStructDefinitionNode() + .withName("X") + .withFields(isSequence()) + ); + } +} |
