diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-10-31 19:09:16 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-10-31 19:09:16 +0000 |
| commit | 7a72d57dae13ab993baaa77f54023c062e56025d (patch) | |
| tree | 66399c2e1e96c9d1fd3797d26d7efd08d532511d /src/test/java | |
| parent | 01193de5040c7200de359ed0775b9ec5c7537ca2 (diff) | |
Parse effect functions
Diffstat (limited to 'src/test/java')
3 files changed, 67 insertions, 2 deletions
diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedEffectDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedEffectDefinitionNodeMatcher.java index a0730475..3f32cb2f 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedEffectDefinitionNodeMatcher.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedEffectDefinitionNodeMatcher.java @@ -43,4 +43,10 @@ public class UntypedEffectDefinitionNodeMatcher extends MatcherBuilder< public UntypedEffectDefinitionNodeMatcher withVisibility(Visibility visibility) { return with(has("visibility", x -> x.visibility(), equalTo(visibility))); } + + public UntypedEffectDefinitionNodeMatcher withFunctions( + Matcher<Iterable<? extends UntypedEffectDefinitionFunctionNode>> functions + ) { + return with(has("functions", x -> x.functions(), functions)); + } } 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 cadf11f8..517ca77a 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java @@ -88,6 +88,16 @@ public class UntypedNodeMatchers { return UntypedEffectDefinitionNodeMatcher.empty(); } + public static Matcher<UntypedEffectDefinitionFunctionNode> isUntypedEffectDefinitionFunctionNode( + String name, + Matcher<? super UntypedFunctionTypeNode> type + ) { + return allOf( + has("name", x -> x.name(), equalTo(name)), + has("type", x -> x.type(), type) + ); + } + public static Matcher<UntypedExpressionNode> isUntypedEqualsNode( Matcher<UntypedExpressionNode> left, Matcher<UntypedExpressionNode> right diff --git a/src/test/java/org/shedlang/compiler/parser/ParserEffectDefinitionTests.java b/src/test/java/org/shedlang/compiler/parser/ParserEffectDefinitionTests.java index 6fc623ca..03204719 100644 --- a/src/test/java/org/shedlang/compiler/parser/ParserEffectDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/parser/ParserEffectDefinitionTests.java @@ -3,13 +3,14 @@ package org.shedlang.compiler.parser; import org.junit.jupiter.api.Test; import org.shedlang.compiler.types.Visibility; -import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.isUntypedEffectDefinitionNode; +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 ParserEffectDefinitionTests { @Test - public void canParseEffectWithNoFunctions() { + public void canParseEffectName() { var source = """ effect State { }"""; @@ -50,4 +51,52 @@ public class ParserEffectDefinitionTests { isUntypedEffectDefinitionNode().withVisibility(Visibility.PUBLIC) ); } + + @Test + public void canParseEffectWithNoFunctions() { + var source = """ + effect State { + }"""; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedEffectDefinitionNode() + .withFunctions(isSequence()) + ); + } + + @Test + public void canParseEffectFunctions() { + var source = """ + effect State { + fun get() -> Int; + fun set(Int) -> Unit; + }"""; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedEffectDefinitionNode() + .withFunctions(isSequence( + isUntypedEffectDefinitionFunctionNode( + "get", + isUntypedFunctionTypeNode() + .withParams(isUntypedFunctionTypeParamsNode(isSequence(), isSequence())) + .withReturnType(isUntypedTypeLevelReferenceNode("Int")) + ), + isUntypedEffectDefinitionFunctionNode( + "set", + isUntypedFunctionTypeNode() + .withParams(isUntypedFunctionTypeParamsNode( + isSequence(isUntypedTypeLevelReferenceNode("Int")), + isSequence() + )) + .withReturnType(isUntypedTypeLevelReferenceNode("Unit")) + ) + )) + ); + } } |
