summaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-08-01 11:48:04 +0100
committerMichael Williamson <mike@zwobble.org>2026-08-01 11:48:04 +0100
commit6f5f31a4024eaf56454e4cb81c56e3c20f25dd60 (patch)
tree74bbb1ba129f5621982c4e54b18bc00a2e4729c5 /src/test/java
parentb9f4f31a1ebb8197bef31e44d876d6fc82cdaf78 (diff)
Add switch expression to Java AST
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java48
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchExpressionMatcher.java47
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaYieldStatementMatcher.java41
3 files changed, 136 insertions, 0 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
index 50f581e..269b490 100644
--- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java
@@ -1265,6 +1265,17 @@ public class JavaWriterTests {
assertThat(string, equalTo("throw exception;"));
}
+ // === Yield statements ===
+
+ @Test
+ public void yieldStatement() throws IOException {
+ var java = new JavaYieldStatement(new JavaIntegerLiteral(42));
+
+ var string = write(writer -> writer.writeBlockStatement(java));
+
+ assertThat(string, equalTo("yield 42;"));
+ }
+
// == Expressions ==
// === Array accesses ===
@@ -1615,6 +1626,43 @@ public class JavaWriterTests {
assertThat(string, equalTo("one.Two.three"));
}
+ // === Switch expressions ===
+
+ @Test
+ public void switchExpression() throws IOException {
+ // We assume that the various switch rules are tested by the tests for
+ // switch statements.
+ var java = new JavaSwitchExpression(
+ new JavaRef(JavaIdentifier.of("x")),
+ List.of(
+ new JavaSwitchRule(
+ new JavaCaseConstant(new JavaIntegerLiteral(0)),
+ new JavaBlock(List.of(
+ new JavaYieldStatement(new JavaIntegerLiteral(10))
+ ))
+ ),
+ new JavaSwitchRule(
+ new JavaCaseConstant(new JavaIntegerLiteral(1)),
+ new JavaBlock(List.of(
+ new JavaYieldStatement(new JavaIntegerLiteral(11))
+ ))
+ )
+ )
+ );
+
+ var string = write(writer -> writer.writeTopLevelExpression(java));
+
+ assertThat(string, equalTo("""
+ switch (x) {
+ case 0 -> {
+ yield 10;
+ }
+ case 1 -> {
+ yield 11;
+ }
+ }"""));
+ }
+
// === Ternary conditionals ===
@Test
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchExpressionMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchExpressionMatcher.java
new file mode 100644
index 0000000..cc022df
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchExpressionMatcher.java
@@ -0,0 +1,47 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpressionMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpressionMatcher imports
+
+public final class JavaSwitchExpressionMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpressionMatcher isJavaSwitchExpression() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpressionMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpression>> submatchers;
+
+ private JavaSwitchExpressionMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpression>> submatchers
+ ) {
+ this.submatchers = submatchers;
+ }
+
+ public org.zwobble.precisely.MatchResult match(java.lang.Object actual) {
+ return this.toMatcher().match(actual);
+ }
+
+ public org.zwobble.precisely.TextTree describe() {
+ return this.toMatcher().describe();
+ }
+
+ private org.zwobble.precisely.Matcher<java.lang.Object> toMatcher() {
+ return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpression.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpressionMatcher withSelectorExpression(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression> selectorExpression) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpression>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("selectorExpression", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpression::selectorExpression, selectorExpression));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpressionMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpressionMatcher withRules(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule>> rules) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpression>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("rules", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpression::rules, rules));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpressionMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpressionMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchExpressionMatcher body
+}
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaYieldStatementMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaYieldStatementMatcher.java
new file mode 100644
index 0000000..21afad7
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaYieldStatementMatcher.java
@@ -0,0 +1,41 @@
+// Generated by hobgoblin.
+
+package org.zwobble.hobgoblin.compiler.output.lang.java.ast;
+
+// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaYieldStatementMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaYieldStatementMatcher imports
+
+public final class JavaYieldStatementMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaYieldStatementMatcher isJavaYieldStatement() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaYieldStatementMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaYieldStatement>> submatchers;
+
+ private JavaYieldStatementMatcher(
+ java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaYieldStatement>> submatchers
+ ) {
+ this.submatchers = submatchers;
+ }
+
+ public org.zwobble.precisely.MatchResult match(java.lang.Object actual) {
+ return this.toMatcher().match(actual);
+ }
+
+ public org.zwobble.precisely.TextTree describe() {
+ return this.toMatcher().describe();
+ }
+
+ private org.zwobble.precisely.Matcher<java.lang.Object> toMatcher() {
+ return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaYieldStatement.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaYieldStatementMatcher withExpression(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression> expression) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaYieldStatement>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("expression", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaYieldStatement::expression, expression));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaYieldStatementMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaYieldStatementMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaYieldStatementMatcher body
+}