summaryrefslogtreecommitdiff
path: root/src/test/java/org
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-10 20:31:25 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-10 20:31:25 +0100
commit215ead6190a3c54969e93dd180632ca1e3574de2 (patch)
treeedb3aaebb4410643413290b4b712a08a223922b1 /src/test/java/org
parent9bc1704339c846de19f68543b05901ee18d1befc (diff)
Add enhanced for statement to Java AST
Diffstat (limited to 'src/test/java/org')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java50
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnhancedForStatementMatcher.java57
2 files changed, 107 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 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<java.lang.Object> {
+ 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<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement>> submatchers;
+
+ private JavaEnhancedForStatementMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement>> 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.JavaEnhancedForStatement.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatementMatcher withElementType(org.zwobble.precisely.Matcher<? super java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef>> elementType) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement>>(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<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaIdentifier> elementName) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement>>(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<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression> elements) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement>>(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<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBlock> body) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnhancedForStatement>>(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
+}