diff options
Diffstat (limited to 'src/test')
4 files changed, 128 insertions, 13 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 eab48ae..b8bfe5c 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 @@ -1073,20 +1073,57 @@ public class JavaWriterTests { // === Switch statements === @Test - public void switchStatement() throws IOException { + public void switchStatementWithCaseConstants() 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 JavaCaseConstant(new JavaIntegerLiteral(0)), + new JavaBlock(List.of( + new JavaReturn(new JavaIntegerLiteral(10)) + )) + ), + new JavaSwitchRule( + new JavaCaseConstant(new JavaIntegerLiteral(1)), + new JavaBlock(List.of( + new JavaReturn(new JavaIntegerLiteral(11)) + )) + ) + ) + ); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo(""" + switch (x) { + case 0 -> { + return 10; + } + case 1 -> { + return 11; + } + }""")); + } + + @Test + public void switchStatementWithCasePatterns() throws IOException { + var java = new JavaSwitchStatement( + new JavaRef(JavaIdentifier.of("x")), + List.of( + new JavaSwitchRule( + new JavaCasePattern( + 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 JavaCasePattern( + JavaTypeRef.topLevel(JavaPackageName.of("org", "example"), JavaIdentifier.of("B")), + JavaIdentifier.of("b") + ), new JavaBlock(List.of( new JavaReturn(new JavaIntegerLiteral(1)) )) diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCaseConstantMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCaseConstantMatcher.java new file mode 100644 index 0000000..d6f2807 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCaseConstantMatcher.java @@ -0,0 +1,39 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstantMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstantMatcher imports + +public class JavaCaseConstantMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstantMatcher isJavaCaseConstant() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstantMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant>> submatchers; + + private JavaCaseConstantMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant>> 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.JavaCaseConstant.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstantMatcher withValue(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression> value) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("value", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstant::value, value)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstantMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstantMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCaseConstantMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePatternMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePatternMatcher.java new file mode 100644 index 0000000..b923926 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaCasePatternMatcher.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.JavaCasePatternMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePatternMatcher imports + +public class JavaCasePatternMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePatternMatcher isJavaCasePattern() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePatternMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern>> submatchers; + + private JavaCasePatternMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern>> 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.JavaCasePattern.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePatternMatcher 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.JavaCasePattern>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("type", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern::type, type)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePatternMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePatternMatcher 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.JavaCasePattern>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("variableName", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePattern::variableName, variableName)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePatternMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePatternMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaCasePatternMatcher body +} 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 index fc6f33d..a505382 100644 --- 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 @@ -28,15 +28,9 @@ public class JavaSwitchRuleMatcher implements org.zwobble.precisely.Matcher<java 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) { + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher withLabel(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchLabel> label) { 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)); + submatchers.add(org.zwobble.precisely.Matchers.has("label", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRule::label, label)); return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaSwitchRuleMatcher(submatchers); } |
