From a0074f328bfabc0c28d7e5b1202761daaa265fc8 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 27 Sep 2023 20:22:34 +0100 Subject: Parse struct without fields --- .../compiler/ast/untyped/UntypedNodeMatchers.java | 4 +++ .../UntypedStructDefinitionNodeMatcher.java | 37 ++++++++++++++++++++++ .../parser/ParserStructDefinitionTests.java | 26 +++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/ast/untyped/UntypedStructDefinitionNodeMatcher.java create mode 100644 src/test/java/org/shedlang/compiler/parser/ParserStructDefinitionTests.java (limited to 'src/test/java') 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 isUntypedSubNode( Matcher left, Matcher 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 { + public static UntypedStructDefinitionNodeMatcher empty() { + return new UntypedStructDefinitionNodeMatcher(P.vector()); + } + + public UntypedStructDefinitionNodeMatcher(PVector> submatchers) { + super(submatchers); + } + + @Override + protected Class expectedType() { + return UntypedStructDefinitionNode.class; + } + + @Override + protected UntypedStructDefinitionNodeMatcher create(PVector> submatchers) { + return new UntypedStructDefinitionNodeMatcher(submatchers); + } + + public UntypedStructDefinitionNodeMatcher withName(String name) { + return with(has("name", x -> x.name(), equalTo(name))); + } + + public UntypedStructDefinitionNodeMatcher withFields(Matcher> 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()) + ); + } +} -- cgit v1.2.3