diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-11 15:42:04 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-11 15:42:04 +0100 |
| commit | c80835cf60e3d772152fd1873dde76d41fa6cec2 (patch) | |
| tree | 08e5740b73852c4faf32f5ea765c39449fe2c2ed /src/test/java/org | |
| parent | ea5daa6b523813b5d244a8b8aa72943e91e7d3f2 (diff) | |
Make initializer optional for local variables in Java
Diffstat (limited to 'src/test/java/org')
2 files changed, 26 insertions, 1 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 508321e..f768827 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 @@ -1034,6 +1034,31 @@ public class JavaWriterTests { assertThat(string, equalTo("var count = 42;")); } + @Test + public void localVariableDeclarationWithInitializer() throws IOException { + var java = JavaLocalVariableDeclaration.arbitrary() + .withName(JavaIdentifier.of("count")) + .withInitializer(new JavaIntegerLiteral(42)) + .build(); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo("var count = 42;")); + } + + @Test + public void localVariableDeclarationWithoutInitializer() throws IOException { + var java = JavaLocalVariableDeclaration.arbitrary() + .withType(JavaTypeRef.INT) + .withName(JavaIdentifier.of("count")) + .withInitializer(Optional.empty()) + .build(); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo("int count;")); + } + // === Returns === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaLocalVariableDeclarationMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaLocalVariableDeclarationMatcher.java index d36db60..a39fcd2 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaLocalVariableDeclarationMatcher.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaLocalVariableDeclarationMatcher.java @@ -40,7 +40,7 @@ public class JavaLocalVariableDeclarationMatcher implements org.zwobble.precisel return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLocalVariableDeclarationMatcher(submatchers); } - public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLocalVariableDeclarationMatcher withInitializer(org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression> initializer) { + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLocalVariableDeclarationMatcher withInitializer(org.zwobble.precisely.Matcher<? super java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression>> initializer) { var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLocalVariableDeclaration>>(this.submatchers); submatchers.add(org.zwobble.precisely.Matchers.has("initializer", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLocalVariableDeclaration::initializer, initializer)); return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLocalVariableDeclarationMatcher(submatchers); |
