diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-09-30 11:01:22 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-09-30 11:01:22 +0100 |
| commit | 107ce1646e24b0d30f7fc42e831a7e5f03daecc3 (patch) | |
| tree | bdcf0c83b78e5ef4272964b9fdfe3735f301eb29 /src/test/java | |
| parent | fa6b2a24aad096824790501550623a7c3c2418c1 (diff) | |
Parse when expressions
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java | 21 | ||||
| -rw-r--r-- | src/test/java/org/shedlang/compiler/parser/ParserWhenTests.java | 41 |
2 files changed, 62 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 ddb7db96..fdbb8f53 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java @@ -279,6 +279,27 @@ public class UntypedNodeMatchers { ); } + public static Matcher<UntypedExpressionNode> isUntypedWhenNode( + Matcher<UntypedExpressionNode> expression, + Matcher<Iterable<? extends UntypedWhenCaseNode>> cases + ) { + return instanceOf( + UntypedWhenNode.class, + has("expression", x -> x.expression(), expression), + has ("cases", x -> x.cases(), cases) + ); + } + + public static Matcher<UntypedWhenCaseNode> isUntypedWhenCaseNode( + Matcher<UntypedTypeLevelExpressionNode> caseType, + Matcher<UntypedBlockNode> body + ) { + return allOf( + has("caseType", x -> x.caseType(), caseType), + has("body", x -> x.body(), body) + ); + } + public static Matcher<UntypedFunctionStatementNode> isUntypedWhileNode( Matcher<UntypedExpressionNode> condition, Matcher<UntypedBlockNode> body diff --git a/src/test/java/org/shedlang/compiler/parser/ParserWhenTests.java b/src/test/java/org/shedlang/compiler/parser/ParserWhenTests.java new file mode 100644 index 00000000..709d7cce --- /dev/null +++ b/src/test/java/org/shedlang/compiler/parser/ParserWhenTests.java @@ -0,0 +1,41 @@ +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 ParserWhenTests { + @Test + public void canParseWhen() { + var source = """ + when (x) { + is A { + return 4; + } + is B { + } + } + """; + + var node = parseString(source, Parser::parseTopLevelExpression); + + assertThat(node, isUntypedWhenNode( + isUntypedReferenceNode("x"), + isSequence( + isUntypedWhenCaseNode( + isUntypedTypeLevelReferenceNode("A"), + isUntypedBlockNode(isSequence( + isUntypedFunctionReturnNode(isUntypedIntLiteralNode(4)) + )) + ), + isUntypedWhenCaseNode( + isUntypedTypeLevelReferenceNode("B"), + isUntypedBlockNode(isSequence()) + ) + ) + )); + } +} |
