diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-11-02 18:59:09 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-11-02 19:00:09 +0000 |
| commit | 71544066ce3b74c18973e00348ea44faf052d5e5 (patch) | |
| tree | 305635d30c993db52a75512c37704aa60424b1a3 /src/test/java | |
| parent | 7f6bc2dcceb75538993af15167ca291db63107a8 (diff) | |
Parse try expressions
Diffstat (limited to 'src/test/java')
3 files changed, 96 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 517ca77a..a9befa70 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java @@ -311,6 +311,10 @@ public class UntypedNodeMatchers { ); } + public static UntypedTryNodeMatcher isUntypedTryNode() { + return UntypedTryNodeMatcher.empty(); + } + public static Matcher<UntypedExpressionNode> isUntypedTupleNode( Matcher<Iterable<? extends UntypedExpressionNode>> elements ) { diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedTryNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedTryNodeMatcher.java new file mode 100644 index 00000000..e8f3cfb6 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedTryNodeMatcher.java @@ -0,0 +1,50 @@ +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.has; + +public class UntypedTryNodeMatcher extends MatcherBuilder< + UntypedExpressionNode, + UntypedTryNode, + UntypedTryNodeMatcher +> { + public static UntypedTryNodeMatcher empty() { + return new UntypedTryNodeMatcher(P.vector()); + } + + public UntypedTryNodeMatcher(PVector<Matcher<? super UntypedTryNode>> submatchers) { + super(submatchers); + } + + @Override + protected Class<UntypedTryNode> expectedType() { + return UntypedTryNode.class; + } + + @Override + protected UntypedTryNodeMatcher create( + PVector<Matcher<? super UntypedTryNode>> submatchers + ) { + return new UntypedTryNodeMatcher(submatchers); + } + + public UntypedTryNodeMatcher withBody(Matcher<UntypedBlockNode> body) { + return with(has("body", x -> x.body(), body)); + } + + public UntypedTryNodeMatcher withHandledEffect( + Matcher<UntypedTypeLevelExpressionNode> handledEffect + ) { + return with(has("handledEffect", x -> x.handledEffect(), handledEffect)); + } + + public UntypedTryNodeMatcher withHandlers( + Matcher<Iterable<? extends UntypedFunctionDefinitionNode>> handlers + ) { + return with(has("handlers", x -> x.handlers(), handlers)); + } +} diff --git a/src/test/java/org/shedlang/compiler/parser/ParserTryTests.java b/src/test/java/org/shedlang/compiler/parser/ParserTryTests.java new file mode 100644 index 00000000..951fd687 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/parser/ParserTryTests.java @@ -0,0 +1,42 @@ +package org.shedlang.compiler.parser; + +import org.junit.jupiter.api.Test; + +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; + +public class ParserTryTests { + @Test + public void canParseTry() { + var source = """ + try { + 1; + } handle State { + fun read() { + } + + fun write() { + } + } + """; + + var node = parseString(source, Parser::parseTopLevelExpression); + + assertThat( + node, + isUntypedTryNode() + .withBody(isUntypedBlockNode(isSequence( + isUntypedExpressionStatementNode(isUntypedIntLiteralNode(1)) + ))) + .withHandledEffect(isUntypedTypeLevelReferenceNode("State")) + .withHandlers(isSequence( + isUntypedFunctionDefinitionNode() + .withName("read"), + isUntypedFunctionDefinitionNode() + .withName("write") + )) + ); + } +} |
