From bc94a020433c8b7dc0adaa2a6adf235da9c9b4e9 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 22 Oct 2023 13:44:07 +0100 Subject: Parse visibility for sum type --- .../compiler/ast/untyped/UntypedNodeMatchers.java | 11 +---- .../UntypedSumTypeDefinitionNodeMatcher.java | 52 ++++++++++++++++++++++ .../parser/ParserSumTypeDefinitionTests.java | 50 +++++++++++++++++---- 3 files changed, 95 insertions(+), 18 deletions(-) create mode 100644 src/test/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNodeMatcher.java (limited to 'src/test/java') 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 26716e3d..35e8247a 100644 --- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedNodeMatchers.java @@ -217,15 +217,8 @@ public class UntypedNodeMatchers { return isUntypedBinaryOperationNode(UntypedBinaryOperator.SUB, left, right); } - public static Matcher isUntypedSumTypeDefinitionNode( - String name, - Matcher> variants - ) { - return instanceOf( - UntypedSumTypeDefinitionNode.class, - has("name", x -> x.name(), equalTo(name)), - has("variants", x -> x.variants(), variants) - ); + public static UntypedSumTypeDefinitionNodeMatcher isUntypedSumTypeDefinitionNode() { + return UntypedSumTypeDefinitionNodeMatcher.empty(); } public static Matcher isUntypedTargetIgnoreNode() { diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNodeMatcher.java new file mode 100644 index 00000000..aa1ba8d0 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNodeMatcher.java @@ -0,0 +1,52 @@ +package org.shedlang.compiler.ast.untyped; + +import org.pcollections.PVector; +import org.shedlang.compiler.testing.MatcherBuilder; +import org.shedlang.compiler.types.Visibility; +import org.shedlang.compiler.util.P; +import org.zwobble.precisely.Matcher; + +import static org.zwobble.precisely.Matchers.equalTo; +import static org.zwobble.precisely.Matchers.has; + +public class UntypedSumTypeDefinitionNodeMatcher extends MatcherBuilder< + UntypedNamespaceStatementNode, + UntypedSumTypeDefinitionNode, + UntypedSumTypeDefinitionNodeMatcher +> { + public static UntypedSumTypeDefinitionNodeMatcher empty() { + return new UntypedSumTypeDefinitionNodeMatcher(P.vector()); + } + + public UntypedSumTypeDefinitionNodeMatcher( + PVector> submatchers + ) { + super(submatchers); + } + + @Override + protected Class expectedType() { + return UntypedSumTypeDefinitionNode.class; + } + + @Override + protected UntypedSumTypeDefinitionNodeMatcher create( + PVector> submatchers + ) { + return new UntypedSumTypeDefinitionNodeMatcher(submatchers); + } + + public UntypedSumTypeDefinitionNodeMatcher withName(String name) { + return with(has("name", x -> x.name(), equalTo(name))); + } + + public UntypedSumTypeDefinitionNodeMatcher withVariants( + Matcher> variants + ) { + return with(has("variants", x -> x.variants(), variants)); + } + + public UntypedSumTypeDefinitionNodeMatcher withVisibility(Visibility visibility) { + return with(has("visibility", x -> x.visibility(), equalTo(visibility))); + } +} diff --git a/src/test/java/org/shedlang/compiler/parser/ParserSumTypeDefinitionTests.java b/src/test/java/org/shedlang/compiler/parser/ParserSumTypeDefinitionTests.java index 65a8ef6f..32392389 100644 --- a/src/test/java/org/shedlang/compiler/parser/ParserSumTypeDefinitionTests.java +++ b/src/test/java/org/shedlang/compiler/parser/ParserSumTypeDefinitionTests.java @@ -1,11 +1,13 @@ 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.*; +import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.isUntypedSumTypeDefinitionNode; +import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.isUntypedTypeLevelReferenceNode; import static org.shedlang.compiler.parser.ParserTesting.parseString; import static org.zwobble.precisely.AssertThat.assertThat; -import static org.zwobble.precisely.Matchers.isSequence; +import static org.zwobble.precisely.Matchers.*; public class ParserSumTypeDefinitionTests { @Test @@ -16,9 +18,11 @@ public class ParserSumTypeDefinitionTests { assertThat( node, - isUntypedSumTypeDefinitionNode("Option", isSequence( - isUntypedTypeLevelReferenceNode("Some") - )) + isUntypedSumTypeDefinitionNode() + .withName("Option") + .withVariants(isSequence( + isUntypedTypeLevelReferenceNode("Some") + )) ); } @@ -30,10 +34,38 @@ public class ParserSumTypeDefinitionTests { assertThat( node, - isUntypedSumTypeDefinitionNode("Option", isSequence( - isUntypedTypeLevelReferenceNode("Some"), - isUntypedTypeLevelReferenceNode("None") - )) + isUntypedSumTypeDefinitionNode() + .withName("Option") + .withVariants(isSequence( + isUntypedTypeLevelReferenceNode("Some"), + isUntypedTypeLevelReferenceNode("None") + )) + ); + } + + @Test + public void defaultVisibilityIsPrivate() { + var source = "sumtype Option = Some;"; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedSumTypeDefinitionNode() + .withVisibility(Visibility.PRIVATE) + ); + } + + @Test + public void whenHasPubPrefixThenVisibilityIsPublic() { + var source = "pub sumtype Option = Some;"; + + var node = parseString(source, Parser::parseNamespaceStatement); + + assertThat( + node, + isUntypedSumTypeDefinitionNode() + .withVisibility(Visibility.PUBLIC) ); } } -- cgit v1.2.3