diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-06 19:42:44 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-06 19:42:44 +0100 |
| commit | 9d66911592c3e3f34aaafdd09c7dd9d96e720e18 (patch) | |
| tree | 0e033546c49d221163f85d60f52f608a8b8f137e /src/test/java | |
| parent | 7c358460db1c42a5b7e79b7c6706ebf2adcff39f (diff) | |
Parse call with no effects and no args
Diffstat (limited to 'src/test/java')
5 files changed, 154 insertions, 0 deletions
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<UntypedArgsNode, UntypedArgsNode, UntypedArgsNodeMatcher> { + public UntypedArgsNodeMatcher() { + super(); + } + + public UntypedArgsNodeMatcher(PVector<Matcher<? super UntypedArgsNode>> submatchers) { + super(submatchers); + } + + @Override + protected Class<UntypedArgsNode> expectedType() { + return UntypedArgsNode.class; + } + + @Override + protected UntypedArgsNodeMatcher create(PVector<Matcher<? super UntypedArgsNode>> submatchers) { + return new UntypedArgsNodeMatcher(); + } + + public UntypedArgsNodeMatcher withPositional(Matcher<? super Iterable<UntypedExpressionNode>> positional) { + return with(has("positional", x -> x.positional(), positional)); + } + + public UntypedArgsNodeMatcher withNamed(Matcher<? super Iterable<UntypedNamedArgNode>> 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<UntypedExpressionNode, UntypedCallNode, UntypedCallNodeMatcher> { + public UntypedCallNodeMatcher() { + super(); + } + + public UntypedCallNodeMatcher(PVector<Matcher<? super UntypedCallNode>> submatchers) { + super(submatchers); + } + + @Override + protected Class<UntypedCallNode> expectedType() { + return UntypedCallNode.class; + } + + @Override + protected UntypedCallNodeMatcher create(PVector<Matcher<? super UntypedCallNode>> submatchers) { + return new UntypedCallNodeMatcher(); + } + + public UntypedCallNodeMatcher withArgs(Matcher<UntypedArgsNode> 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<UntypedExpressionNode> 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<UntypedExpressionNode> 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<T, U, This> implements Matcher<T> { + private final PVector<Matcher<? super U>> submatchers; + + public MatcherBuilder() { + this(Empty.vector()); + } + + public MatcherBuilder(PVector<Matcher<? super U>> submatchers) { + this.submatchers = submatchers; + } + + @Override + public MatchResult match(T actual) { + return toMatcher().match(actual); + } + + @Override + public TextTree describe() { + return toMatcher().describe(); + } + + protected abstract Class<U> expectedType(); + protected abstract This create(PVector<Matcher<? super U>> submatchers); + + protected This with(Matcher<? super U> submatcher) { + return create(submatchers.plus(submatcher)); + } + + private Matcher<T> toMatcher() { + return instanceOf(expectedType(), submatchers); + } +} |
