diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-06-07 20:03:39 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-06-07 20:03:39 +0100 |
| commit | 244cc7ad6855696891cd8c72bfedb68c91fd1aaa (patch) | |
| tree | be447af55270b4a24bff3d685a1694dbeba0d9c6 /src/test/java/org | |
| parent | 8258a15b00687f6d619722ba04843e5f0aaebe73 (diff) | |
Add parser for minimal functions
Diffstat (limited to 'src/test/java/org')
3 files changed, 93 insertions, 0 deletions
diff --git a/src/test/java/org/shedlang/compiler/ast/UntypedFunctionDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/UntypedFunctionDefinitionNodeMatcher.java new file mode 100644 index 00000000..775cf5f5 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/UntypedFunctionDefinitionNodeMatcher.java @@ -0,0 +1,50 @@ +package org.shedlang.compiler.ast; + +import org.pcollections.PVector; +import org.shedlang.compiler.testing.MatcherBuilder; +import org.zwobble.precisely.Matcher; + +import java.util.Optional; + +import static org.zwobble.precisely.Matchers.equalTo; +import static org.zwobble.precisely.Matchers.has; + +public class UntypedFunctionDefinitionNodeMatcher extends MatcherBuilder<UntypedNamespaceStatementNode, UntypedFunctionDefinitionNode, UntypedFunctionDefinitionNodeMatcher> { + public UntypedFunctionDefinitionNodeMatcher() { + super(); + } + + public UntypedFunctionDefinitionNodeMatcher(PVector<Matcher<? super UntypedFunctionDefinitionNode>> submatchers) { + super(submatchers); + } + + @Override + protected Class<UntypedFunctionDefinitionNode> expectedType() { + return UntypedFunctionDefinitionNode.class; + } + + @Override + protected UntypedFunctionDefinitionNodeMatcher create(PVector<Matcher<? super UntypedFunctionDefinitionNode>> submatchers) { + return new UntypedFunctionDefinitionNodeMatcher(); + } + + public UntypedFunctionDefinitionNodeMatcher withBody(Matcher<Iterable<UntypedFunctionStatementNode>> body) { + return with(has("body", x -> x.body(), body)); + } + + public UntypedFunctionDefinitionNodeMatcher withEffect(Matcher<Optional<UntypedTypeLevelExpressionNode>> effect) { + return with(has("effect", x -> x.effect(), effect)); + } + + public UntypedFunctionDefinitionNodeMatcher withName(String name) { + return with(has("name", x -> x.name(), equalTo(name))); + } + + public UntypedFunctionDefinitionNodeMatcher withParams(Matcher<UntypedParamsNode> params) { + return with(has("params", x -> x.params(), params)); + } + + public UntypedFunctionDefinitionNodeMatcher withReturnType(Matcher<UntypedTypeLevelExpressionNode> returnType) { + return with(has("returnType", x -> x.returnType(), returnType)); + } +} diff --git a/src/test/java/org/shedlang/compiler/ast/UntypedNodeMatchers.java b/src/test/java/org/shedlang/compiler/ast/UntypedNodeMatchers.java index a6f0bf82..ca538a97 100644 --- a/src/test/java/org/shedlang/compiler/ast/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/UntypedNodeMatchers.java @@ -25,6 +25,10 @@ public class UntypedNodeMatchers { ); } + public static UntypedFunctionDefinitionNodeMatcher isUntypedFunctionDefinitionNode() { + return new UntypedFunctionDefinitionNodeMatcher(); + } + public static Matcher<UntypedNamedArgNode> isUntypedNamedArgNode( String name, Matcher<UntypedExpressionNode> expression @@ -35,6 +39,16 @@ public class UntypedNodeMatchers { ); } + public static Matcher<UntypedParamsNode> isUntypedParamsNode( + Matcher<Iterable<UntypedParamNode>> positional, + Matcher<Iterable<UntypedParamNode>> named + ) { + return allOf( + has("positional", x -> x.positional(), positional), + has("named", x -> x.named(), named) + ); + } + public static Matcher<UntypedExpressionNode> isUntypedReferenceNode(String name) { return instanceOf( UntypedReferenceNode.class, diff --git a/src/test/java/org/shedlang/compiler/parser/ParserFunctionDefinitionTests.java b/src/test/java/org/shedlang/compiler/parser/ParserFunctionDefinitionTests.java new file mode 100644 index 00000000..34c50065 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/parser/ParserFunctionDefinitionTests.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.*; + +public class ParserFunctionDefinitionTests { + @Test + public void canParseMinimalFunction() { + var source = """ + fun f() -> Unit { + }"""; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedFunctionDefinitionNode() + .withName("f") + .withParams(isUntypedParamsNode(isSequence(), isSequence())) + .withEffect(isOptionalEmpty()) + .withReturnType(isUntypedTypeLevelReferenceNode("Unit")) + .withBody(isSequence()) + ); + } +} |
