diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-07-13 20:57:33 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-07-13 20:57:33 +0100 |
| commit | 11634a5c5463d10f4d6be2dce0fa8b2b04339d43 (patch) | |
| tree | a7e217eede6cfb75434231e6488e8ee50a153955 /src/test/java/org/zwobble | |
| parent | 3b79d116965efc20e1065cbb24e7ad32ad038a0a (diff) | |
Add field initializers to Java AST
Diffstat (limited to 'src/test/java/org/zwobble')
2 files changed, 35 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 cd4fdfc..5cc5613 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 @@ -616,6 +616,35 @@ public class JavaWriterTests { assertThat(string, equalTo("private static final int count;")); } + @Test + public void fieldDeclarationWithoutInitializer() throws IOException { + var java = JavaFieldDeclaration.arbitrary() + .withVisibility(JavaVisibility.PRIVATE) + .withKind(JavaMemberKind.INSTANCE) + .withType(JavaTypeRef.INT) + .withName(JavaIdentifier.of("count")) + .build(); + + var string = write(writer -> writer.writeClassBodyDeclaration(java)); + + assertThat(string, equalTo("private final int count;")); + } + + @Test + public void fieldDeclarationWithInitializer() throws IOException { + var java = JavaFieldDeclaration.arbitrary() + .withVisibility(JavaVisibility.PRIVATE) + .withKind(JavaMemberKind.INSTANCE) + .withType(JavaTypeRef.INT) + .withName(JavaIdentifier.of("count")) + .withInitializer(JavaIntegerLiteral.arbitrary().withValue(2)) + .build(); + + var string = write(writer -> writer.writeClassBodyDeclaration(java)); + + assertThat(string, equalTo("private final int count = 2;")); + } + // === Method declarations === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaFieldDeclarationMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaFieldDeclarationMatcher.java index 1eac6ef..62ddbe1 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaFieldDeclarationMatcher.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaFieldDeclarationMatcher.java @@ -52,6 +52,12 @@ public class JavaFieldDeclarationMatcher implements org.zwobble.precisely.Matche return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaFieldDeclarationMatcher(submatchers); } + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaFieldDeclarationMatcher 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.JavaFieldDeclaration>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("initializer", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaFieldDeclaration::initializer, initializer)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaFieldDeclarationMatcher(submatchers); + } + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaFieldDeclarationMatcher body // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaFieldDeclarationMatcher body } |
