aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-12-23 11:50:33 +0000
committerMichael Williamson <mike@zwobble.org>2023-12-23 11:50:33 +0000
commita7c33eef7d2598d5636ff5bcde07da4f9c057184 (patch)
tree43fd9d2e37c9a4b2e8ebc45ab0ba998aca2b62b1 /src/test/java
parent924ae7e0ebfd2abaf320a2ad4f3441e5034ffb91 (diff)
Parse type level params for sum types
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNodeMatcher.java8
-rw-r--r--src/test/java/org/shedlang/compiler/parser/ParserSumTypeDefinitionTests.java64
2 files changed, 70 insertions, 2 deletions
diff --git a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNodeMatcher.java b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNodeMatcher.java
index aa1ba8d0..42d30cff 100644
--- a/src/test/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNodeMatcher.java
+++ b/src/test/java/org/shedlang/compiler/ast/untyped/UntypedSumTypeDefinitionNodeMatcher.java
@@ -6,6 +6,8 @@ import org.shedlang.compiler.types.Visibility;
import org.shedlang.compiler.util.P;
import org.zwobble.precisely.Matcher;
+import java.util.Optional;
+
import static org.zwobble.precisely.Matchers.equalTo;
import static org.zwobble.precisely.Matchers.has;
@@ -40,6 +42,12 @@ public class UntypedSumTypeDefinitionNodeMatcher extends MatcherBuilder<
return with(has("name", x -> x.name(), equalTo(name)));
}
+ public UntypedSumTypeDefinitionNodeMatcher withTypeLevelParams(
+ Matcher<Optional<? extends Iterable<? extends UntypedTypeLevelParamNode>>> typeLevelParams
+ ) {
+ return with(has("typeLevelParams", x -> x.typeLevelParams(), typeLevelParams));
+ }
+
public UntypedSumTypeDefinitionNodeMatcher withVariants(
Matcher<Iterable<? extends UntypedTypeLevelExpressionNode>> variants
) {
diff --git a/src/test/java/org/shedlang/compiler/parser/ParserSumTypeDefinitionTests.java b/src/test/java/org/shedlang/compiler/parser/ParserSumTypeDefinitionTests.java
index 32392389..eb8285aa 100644
--- a/src/test/java/org/shedlang/compiler/parser/ParserSumTypeDefinitionTests.java
+++ b/src/test/java/org/shedlang/compiler/parser/ParserSumTypeDefinitionTests.java
@@ -3,8 +3,8 @@ 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.isUntypedSumTypeDefinitionNode;
-import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.isUntypedTypeLevelReferenceNode;
+import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.*;
+import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.isUntypedTypeLevelParamNode;
import static org.shedlang.compiler.parser.ParserTesting.parseString;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.*;
@@ -68,4 +68,64 @@ public class ParserSumTypeDefinitionTests {
.withVisibility(Visibility.PUBLIC)
);
}
+
+ @Test
+ public void canParseSumTypeWithNoTypeLevelParams() {
+ var source = "sumtype Option = None;";
+
+ var node = parseString(source, Parser::parseNamespaceStatement);
+
+ assertThat(
+ node,
+ isUntypedSumTypeDefinitionNode()
+ .withTypeLevelParams(isOptionalEmpty())
+ );
+ }
+
+ @Test
+ public void canParseSumTypeWithSingleTypeLevelParam() {
+ var source = "sumtype Option[T1] = None;";
+
+ var node = parseString(source, Parser::parseNamespaceStatement);
+
+ assertThat(
+ node,
+ isUntypedSumTypeDefinitionNode()
+ .withTypeLevelParams(isOptionalOf(isSequence(
+ isUntypedTypeLevelParamNode("T1")
+ )))
+ );
+ }
+
+ @Test
+ public void canParseSumTypeWithMultipleTypeLevelParams() {
+ var source = "sumtype Option[T1, T2, T3] = None;";
+
+ var node = parseString(source, Parser::parseNamespaceStatement);
+
+ assertThat(
+ node,
+ isUntypedSumTypeDefinitionNode()
+ .withTypeLevelParams(isOptionalOf(isSequence(
+ isUntypedTypeLevelParamNode("T1"),
+ isUntypedTypeLevelParamNode("T2"),
+ isUntypedTypeLevelParamNode("T3")
+ )))
+ );
+ }
+
+ @Test
+ public void typeLevelParamsCanHaveTrailingComma() {
+ var source = "sumtype Option[T1,] = None;";
+
+ var node = parseString(source, Parser::parseNamespaceStatement);
+
+ assertThat(
+ node,
+ isUntypedSumTypeDefinitionNode()
+ .withTypeLevelParams(isOptionalOf(isSequence(
+ isUntypedTypeLevelParamNode("T1")
+ )))
+ );
+ }
}