diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-10-22 13:44:07 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-10-22 13:44:13 +0100 |
| commit | bc94a020433c8b7dc0adaa2a6adf235da9c9b4e9 (patch) | |
| tree | 0c5229feca5ef3a03d1c9321fad3a7531c555d17 /src/test/java | |
| parent | 21a0bdcedd03ac7664bbd3c3fdb5e3ac544a9813 (diff) | |
Parse visibility for sum type
Diffstat (limited to 'src/test/java')
3 files changed, 95 insertions, 18 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 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<UntypedNamespaceStatementNode> isUntypedSumTypeDefinitionNode( - String name, - Matcher<Iterable<? extends UntypedTypeLevelExpressionNode>> 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<UntypedTargetNode> 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<Matcher<? super UntypedSumTypeDefinitionNode>> submatchers + ) { + super(submatchers); + } + + @Override + protected Class<UntypedSumTypeDefinitionNode> expectedType() { + return UntypedSumTypeDefinitionNode.class; + } + + @Override + protected UntypedSumTypeDefinitionNodeMatcher create( + PVector<Matcher<? super UntypedSumTypeDefinitionNode>> submatchers + ) { + return new UntypedSumTypeDefinitionNodeMatcher(submatchers); + } + + public UntypedSumTypeDefinitionNodeMatcher withName(String name) { + return with(has("name", x -> x.name(), equalTo(name))); + } + + public UntypedSumTypeDefinitionNodeMatcher withVariants( + Matcher<Iterable<? extends UntypedTypeLevelExpressionNode>> 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) ); } } |
