diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-11 17:11:54 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-11 17:11:54 +0100 |
| commit | eb4ce8b7ebceac4bbf68b779b9f6d6f4439ddbba (patch) | |
| tree | b90df6f69f2a8b25f4d43e525b6351e0c3e6b4e4 /src/test/java/org | |
| parent | 10317b0c2841591a85530a5c4c38cac0c4bf5fe8 (diff) | |
Add switch statement to Java AST
Diffstat (limited to 'src/test/java/org')
3 files changed, 133 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 b26b8f5..eab48ae 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 @@ -1070,6 +1070,43 @@ public class JavaWriterTests { assertThat(string, equalTo("return 42;")); } + // === Switch statements === + + @Test + public void switchStatement() throws IOException { + var java = new JavaSwitchStatement( + new JavaRef(JavaIdentifier.of("x")), + List.of( + new JavaSwitchRule( + JavaTypeRef.topLevel(JavaPackageName.of("org", "example"), JavaIdentifier.of("A")), + JavaIdentifier.of("a"), + new JavaBlock(List.of( + new JavaReturn(new JavaIntegerLiteral(0)) + )) + ), + new JavaSwitchRule( + JavaTypeRef.topLevel(JavaPackageName.of("org", "example"), JavaIdentifier.of("B")), + JavaIdentifier.of("b"), + new JavaBlock(List.of( + new JavaReturn(new JavaIntegerLiteral(1)) + )) + ) + ) + ); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo(""" + switch (x) { + case org.example.A a -> { + return 0; + } + case org.example.B b -> { + return 1; + } + }""")); + } + // == Expressions == // === Binary operations === diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchRuleMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchRuleMatcher.java new file mode 100644 index 0000000..fc6f33d --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchRuleMatcher.java @@ -0,0 +1,51 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher imports + +public class JavaSwitchRuleMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher isJavaSwitchRule() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule>> submatchers; + + private JavaSwitchRuleMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule>> 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.JavaSwitchRule.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher withType(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef> type) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule::type, type)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher withVariableName(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier> variableName) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("variableName", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule::variableName, variableName)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher withBody(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock> body) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("body", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule::body, body)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchStatementMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchStatementMatcher.java new file mode 100644 index 0000000..5cf7d28 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaSwitchStatementMatcher.java @@ -0,0 +1,45 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher imports + +public class JavaSwitchStatementMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher isJavaSwitchStatement() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement>> submatchers; + + private JavaSwitchStatementMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement>> 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.JavaSwitchStatement.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher 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.JavaSwitchStatement>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("selectorExpression", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement::selectorExpression, selectorExpression)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher 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.JavaSwitchStatement>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("rules", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatement::rules, rules)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchStatementMatcher body +} |
