diff options
Diffstat (limited to 'src/test/java/org/zwobble')
2 files changed, 120 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 b28f590..508321e 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 @@ -938,6 +938,75 @@ public class JavaWriterTests { assertThat(string, equalTo("42;")); } + // === If statements === + + @Test + public void ifStatementWithNonEmptyTrueAndFalseBodies() throws IOException { + var java = JavaIfStatement.arbitrary() + .withCondition(JavaRef.arbitrary().withName(JavaIdentifier.of("a"))) + .withIfTrue(JavaBlock.arbitrary() + .addStatement(JavaExpressionStatement.arbitrary() + .withExpression(JavaIntegerLiteral.arbitrary().withValue(0)) + ) + ) + .withIfFalse(JavaBlock.arbitrary() + .addStatement(JavaExpressionStatement.arbitrary() + .withExpression(JavaIntegerLiteral.arbitrary().withValue(1)) + ) + ) + .build(); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo(""" + if (a) { + 0; + } else { + 1; + }""")); + } + + @Test + public void ifStatementWithEmptyTrueBody() throws IOException { + var java = JavaIfStatement.arbitrary() + .withCondition(JavaRef.arbitrary().withName(JavaIdentifier.of("a"))) + .withIfTrue(JavaBlock.arbitrary()) + .withIfFalse(JavaBlock.arbitrary() + .addStatement(JavaExpressionStatement.arbitrary() + .withExpression(JavaIntegerLiteral.arbitrary().withValue(1)) + ) + ) + .build(); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo(""" + if (a) { + } else { + 1; + }""")); + } + + @Test + public void ifStatementWithEmptyFalseBody() throws IOException { + var java = JavaIfStatement.arbitrary() + .withCondition(JavaRef.arbitrary().withName(JavaIdentifier.of("a"))) + .withIfTrue(JavaBlock.arbitrary() + .addStatement(JavaExpressionStatement.arbitrary() + .withExpression(JavaIntegerLiteral.arbitrary().withValue(0)) + ) + ) + .withIfFalse(JavaBlock.arbitrary()) + .build(); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo(""" + if (a) { + 0; + }""")); + } + // === Local variable declarations === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaIfStatementMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaIfStatementMatcher.java new file mode 100644 index 0000000..25d10a6 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaIfStatementMatcher.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.JavaIfStatementMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher imports + +public class JavaIfStatementMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher isJavaIfStatement() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement>> submatchers; + + private JavaIfStatementMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement>> 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.JavaIfStatement.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher withCondition(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression> condition) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("condition", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement::condition, condition)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher withIfTrue(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock> ifTrue) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("ifTrue", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement::ifTrue, ifTrue)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher withIfFalse(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock> ifFalse) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("ifFalse", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatement::ifFalse, ifFalse)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIfStatementMatcher body +} |
