From 215ead6190a3c54969e93dd180632ca1e3574de2 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 10 Jul 2026 20:31:25 +0100 Subject: Add enhanced for statement to Java AST --- .../compiler/output/lang/java/JavaWriterTests.java | 50 +++++++++++++++++++ .../java/ast/JavaEnhancedForStatementMatcher.java | 57 ++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnhancedForStatementMatcher.java (limited to 'src/test/java') 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 5464116..96dfe16 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 @@ -836,6 +836,56 @@ public class JavaWriterTests { assertThat(string, equalTo("return 42;\nreturn 47;")); } + // === Enhanced for statements === + + @Test + public void enhancedForStatementUsingExplicitType() throws IOException { + var java = JavaEnhancedForStatement.arbitrary() + .withElementType(JavaTypeRef.topLevel(JavaPackageName.EMPTY, JavaIdentifier.of("Fruit"))) + .withElementName(JavaIdentifier.of("fruit")) + .withElements(JavaRef.arbitrary().withName(JavaIdentifier.of("fruits"))) + .withBody(JavaBlock.arbitrary() + .addStatement(JavaExpressionStatement.arbitrary().withExpression( + JavaStaticMethodCall.arbitrary() + .withType(JavaTypeRef.topLevel(JavaPackageName.EMPTY, JavaIdentifier.of("System"))) + .withMethodName(JavaIdentifier.of("println")) + .addArg(JavaRef.arbitrary().withName(JavaIdentifier.of("fruit"))) + )) + ) + .build(); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo(""" + for (Fruit fruit : fruits) { + System.println(fruit); + }""")); + } + + @Test + public void enhancedForStatementUsingVar() throws IOException { + var java = JavaEnhancedForStatement.arbitrary() + .withElementType(Optional.empty()) + .withElementName(JavaIdentifier.of("fruit")) + .withElements(JavaRef.arbitrary().withName(JavaIdentifier.of("fruits"))) + .withBody(JavaBlock.arbitrary() + .addStatement(JavaExpressionStatement.arbitrary().withExpression( + JavaStaticMethodCall.arbitrary() + .withType(JavaTypeRef.topLevel(JavaPackageName.EMPTY, JavaIdentifier.of("System"))) + .withMethodName(JavaIdentifier.of("println")) + .addArg(JavaRef.arbitrary().withName(JavaIdentifier.of("fruit"))) + )) + ) + .build(); + + var string = write(writer -> writer.writeBlockStatement(java)); + + assertThat(string, equalTo(""" + for (var fruit : fruits) { + System.println(fruit); + }""")); + } + // === Expression statements === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnhancedForStatementMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnhancedForStatementMatcher.java new file mode 100644 index 0000000..ab98443 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnhancedForStatementMatcher.java @@ -0,0 +1,57 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher imports + +public class JavaEnhancedForStatementMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher isJavaEnhancedForStatement() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private JavaEnhancedForStatementMatcher(java.util.List> 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 toMatcher() { + return org.zwobble.precisely.Matchers.instanceOf(org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher withElementType(org.zwobble.precisely.Matcher> elementType) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("elementType", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement::elementType, elementType)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher withElementName(org.zwobble.precisely.Matcher elementName) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("elementName", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement::elementName, elementName)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher withElements(org.zwobble.precisely.Matcher elements) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("elements", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement::elements, elements)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher withBody(org.zwobble.precisely.Matcher body) { + var submatchers = new java.util.ArrayList>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("body", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement::body, body)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher body +} -- cgit v1.2.3