From 7a72d57dae13ab993baaa77f54023c062e56025d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 31 Oct 2023 19:09:16 +0000 Subject: Parse effect functions --- .../UntypedEffectDefinitionNodeMatcher.java | 6 +++ .../compiler/ast/untyped/UntypedNodeMatchers.java | 10 ++++ .../parser/ParserEffectDefinitionTests.java | 53 +++++++++++++++++++++- 3 files changed, 67 insertions(+), 2 deletions(-) (limited to 'src/test/java') 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> 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 isUntypedEffectDefinitionFunctionNode( + String name, + Matcher type + ) { + return allOf( + has("name", x -> x.name(), equalTo(name)), + has("type", x -> x.type(), type) + ); + } + public static Matcher isUntypedEqualsNode( Matcher left, Matcher 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")) + ) + )) + ); + } } -- cgit v1.2.3