From 9d66911592c3e3f34aaafdd09c7dd9d96e720e18 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 6 Jun 2023 19:42:44 +0100 Subject: Parse call with no effects and no args --- .../compiler/ast/UntypedArgsNodeMatcher.java | 35 ++++++++++++++++++ .../compiler/ast/UntypedCallNodeMatcher.java | 40 +++++++++++++++++++++ .../shedlang/compiler/ast/UntypedNodeMatchers.java | 8 +++++ .../shedlang/compiler/parser/ParserCallTests.java | 29 +++++++++++++++ .../shedlang/compiler/testing/MatcherBuilder.java | 42 ++++++++++++++++++++++ 5 files changed, 154 insertions(+) create mode 100644 src/test/java/org/shedlang/compiler/ast/UntypedArgsNodeMatcher.java create mode 100644 src/test/java/org/shedlang/compiler/ast/UntypedCallNodeMatcher.java create mode 100644 src/test/java/org/shedlang/compiler/parser/ParserCallTests.java create mode 100644 src/test/java/org/shedlang/compiler/testing/MatcherBuilder.java (limited to 'src/test/java') diff --git a/src/test/java/org/shedlang/compiler/ast/UntypedArgsNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/UntypedArgsNodeMatcher.java new file mode 100644 index 00000000..315f87d3 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/UntypedArgsNodeMatcher.java @@ -0,0 +1,35 @@ +package org.shedlang.compiler.ast; + +import org.pcollections.PVector; +import org.shedlang.compiler.testing.MatcherBuilder; +import org.zwobble.precisely.Matcher; + +import static org.zwobble.precisely.Matchers.has; + +public class UntypedArgsNodeMatcher extends MatcherBuilder { + public UntypedArgsNodeMatcher() { + super(); + } + + public UntypedArgsNodeMatcher(PVector> submatchers) { + super(submatchers); + } + + @Override + protected Class expectedType() { + return UntypedArgsNode.class; + } + + @Override + protected UntypedArgsNodeMatcher create(PVector> submatchers) { + return new UntypedArgsNodeMatcher(); + } + + public UntypedArgsNodeMatcher withPositional(Matcher> positional) { + return with(has("positional", x -> x.positional(), positional)); + } + + public UntypedArgsNodeMatcher withNamed(Matcher> named) { + return with(has("named", x -> x.named(), named)); + } +} diff --git a/src/test/java/org/shedlang/compiler/ast/UntypedCallNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/UntypedCallNodeMatcher.java new file mode 100644 index 00000000..ef4ad9da --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/UntypedCallNodeMatcher.java @@ -0,0 +1,40 @@ +package org.shedlang.compiler.ast; + +import org.pcollections.PVector; +import org.shedlang.compiler.testing.MatcherBuilder; +import org.zwobble.precisely.Matcher; + +import static org.zwobble.precisely.Matchers.equalTo; +import static org.zwobble.precisely.Matchers.has; + +public class UntypedCallNodeMatcher extends MatcherBuilder { + public UntypedCallNodeMatcher() { + super(); + } + + public UntypedCallNodeMatcher(PVector> submatchers) { + super(submatchers); + } + + @Override + protected Class expectedType() { + return UntypedCallNode.class; + } + + @Override + protected UntypedCallNodeMatcher create(PVector> submatchers) { + return new UntypedCallNodeMatcher(); + } + + public UntypedCallNodeMatcher withArgs(Matcher args) { + return with(has("args", x -> x.args(), args)); + } + + public UntypedCallNodeMatcher withHasEffects(boolean hasEffects) { + return with(has("hasEffects", x -> x.hasEffects(), equalTo(hasEffects))); + } + + public UntypedCallNodeMatcher withReceiver(Matcher receiver) { + return with(has("receiver", x -> x.receiver(), receiver)); + } +} diff --git a/src/test/java/org/shedlang/compiler/ast/UntypedNodeMatchers.java b/src/test/java/org/shedlang/compiler/ast/UntypedNodeMatchers.java index 59048782..21e8cf1b 100644 --- a/src/test/java/org/shedlang/compiler/ast/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/UntypedNodeMatchers.java @@ -8,6 +8,14 @@ public class UntypedNodeMatchers { private UntypedNodeMatchers() { } + public static UntypedArgsNodeMatcher isUntypedArgsNode() { + return new UntypedArgsNodeMatcher(); + } + + public static UntypedCallNodeMatcher isUntypedCallNode() { + return new UntypedCallNodeMatcher(); + } + public static Matcher isUntypedReferenceNode(String name) { return instanceOf( UntypedReferenceNode.class, diff --git a/src/test/java/org/shedlang/compiler/parser/ParserCallTests.java b/src/test/java/org/shedlang/compiler/parser/ParserCallTests.java new file mode 100644 index 00000000..a5c41cba --- /dev/null +++ b/src/test/java/org/shedlang/compiler/parser/ParserCallTests.java @@ -0,0 +1,29 @@ +package org.shedlang.compiler.parser; + +import org.junit.jupiter.api.Test; + +import static org.shedlang.compiler.ast.UntypedNodeMatchers.*; +import static org.shedlang.compiler.parser.ParserTesting.parseString; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class ParserCallTests { + @Test + public void canParseCallWithoutEffectsWithNoArguments() { + var source = "f()"; + + var node = parseString(source, Parser::parseTopLevelExpression); + + assertThat( + node, + isUntypedCallNode() + .withReceiver(isUntypedReferenceNode("f")) + .withHasEffects(false) + .withArgs( + isUntypedArgsNode() + .withPositional(isSequence()) + .withNamed(isSequence()) + ) + ); + } +} diff --git a/src/test/java/org/shedlang/compiler/testing/MatcherBuilder.java b/src/test/java/org/shedlang/compiler/testing/MatcherBuilder.java new file mode 100644 index 00000000..6736659f --- /dev/null +++ b/src/test/java/org/shedlang/compiler/testing/MatcherBuilder.java @@ -0,0 +1,42 @@ +package org.shedlang.compiler.testing; + +import org.pcollections.Empty; +import org.pcollections.PVector; +import org.zwobble.precisely.MatchResult; +import org.zwobble.precisely.Matcher; +import org.zwobble.precisely.TextTree; + +import static org.zwobble.precisely.Matchers.instanceOf; + +public abstract class MatcherBuilder implements Matcher { + private final PVector> submatchers; + + public MatcherBuilder() { + this(Empty.vector()); + } + + public MatcherBuilder(PVector> submatchers) { + this.submatchers = submatchers; + } + + @Override + public MatchResult match(T actual) { + return toMatcher().match(actual); + } + + @Override + public TextTree describe() { + return toMatcher().describe(); + } + + protected abstract Class expectedType(); + protected abstract This create(PVector> submatchers); + + protected This with(Matcher submatcher) { + return create(submatchers.plus(submatcher)); + } + + private Matcher toMatcher() { + return instanceOf(expectedType(), submatchers); + } +} -- cgit v1.2.3