summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-05-03 12:00:38 +0100
committerMichael Williamson <mike@zwobble.org>2026-05-03 12:00:38 +0100
commit3f8039bff09578fc3219503ca34a4d99833a7323 (patch)
treefbbfd3622e0ab81c992e07c31a151e781a3fb86f /src/test
parentbd7d0b6335238a465090af626a50df5fdfd425be (diff)
Parse constructed types
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserConstructedTypeTests.java76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserConstructedTypeTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserConstructedTypeTests.java
new file mode 100644
index 0000000..1444cf0
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/parser/ParserConstructedTypeTests.java
@@ -0,0 +1,76 @@
+package org.zwobble.hobgoblin.compiler.parser;
+
+import org.junit.jupiter.api.Test;
+import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedConstructedTypeNode;
+import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelExpressionNode;
+import org.zwobble.hobgoblin.compiler.ast.untyped.UntypedTypeLevelReferenceNode;
+import org.zwobble.precisely.Matcher;
+
+import static org.zwobble.hobgoblin.compiler.parser.ParserTesting.parseString;
+import static org.zwobble.precisely.AssertThat.assertThat;
+import static org.zwobble.precisely.Matchers.*;
+
+public class ParserConstructedTypeTests {
+ @Test
+ public void canParseConstructedTypeWithOneArg() {
+ var source = "List[String]";
+
+ var node = parseString(source, Parser::parseTypeLevelExpression);
+
+ assertThat(node, isUntypedConstructedTypeNode(
+ isUntypedTypeLevelReferenceNode("List"),
+ isSequence(
+ isUntypedTypeLevelReferenceNode("String")
+ )
+ ));
+ }
+
+ @Test
+ public void canParseConstructedTypeWithMultipleArgs() {
+ var source = "X[T1, T2, T3]";
+
+ var node = parseString(source, Parser::parseTypeLevelExpression);
+
+ assertThat(node, isUntypedConstructedTypeNode(
+ isUntypedTypeLevelReferenceNode("X"),
+ isSequence(
+ isUntypedTypeLevelReferenceNode("T1"),
+ isUntypedTypeLevelReferenceNode("T2"),
+ isUntypedTypeLevelReferenceNode("T3")
+ )
+ ));
+ }
+
+ @Test
+ public void argsCanHaveTrailingComma() {
+ var source = "List[String,]";
+
+ var node = parseString(source, Parser::parseTypeLevelExpression);
+
+ assertThat(node, isUntypedConstructedTypeNode(
+ isUntypedTypeLevelReferenceNode("List"),
+ isSequence(
+ isUntypedTypeLevelReferenceNode("String")
+ )
+ ));
+ }
+
+ private static Matcher<UntypedTypeLevelExpressionNode> isUntypedConstructedTypeNode(
+ Matcher<UntypedTypeLevelExpressionNode> receiver,
+ Matcher<Iterable<? extends UntypedTypeLevelExpressionNode>> args
+ ) {
+ return instanceOf(
+ UntypedConstructedTypeNode.class,
+ has("receiver", UntypedConstructedTypeNode::receiver, receiver),
+ has("args", UntypedConstructedTypeNode::args, args)
+ );
+ }
+
+ private static Matcher<UntypedTypeLevelExpressionNode> isUntypedTypeLevelReferenceNode(String name) {
+ return instanceOf(
+ UntypedTypeLevelReferenceNode.class,
+ has("name", UntypedTypeLevelReferenceNode::name, equalTo(name))
+ );
+ }
+}
+