summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-11 15:36:00 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-11 15:36:00 +0100
commitea5daa6b523813b5d244a8b8aa72943e91e7d3f2 (patch)
tree77a3ae5c2156e32d79beb0affb82cd84b086230e /src/test
parent91c5ecad55a7011ed883b3e06a197938af6dfd23 (diff)
Add if statements to Java AST
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java69
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaIfStatementMatcher.java51
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
+}