summaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2026-07-10 20:51:25 +0100
committerMichael Williamson <mike@zwobble.org>2026-07-10 20:51:25 +0100
commitf0644a3c66265f75580fa6df28f9993e608d4c98 (patch)
tree605acff506e0972b2010dc1068251247fb7d1cce /src/test/java/org/zwobble
parent3fef371e57cf6b943457f2896712193445f60c64 (diff)
Add basic for statement to Java AST
Diffstat (limited to 'src/test/java/org/zwobble')
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java41
-rw-r--r--src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBasicForStatementMatcher.java57
2 files changed, 98 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 96dfe16..02f471c 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,47 @@ public class JavaWriterTests {
assertThat(string, equalTo("return 42;\nreturn 47;"));
}
+ // === Basic for statements ===
+
+ @Test
+ public void basicForStatement() throws IOException {
+ var java = JavaBasicForStatement.arbitrary()
+ .withInit(JavaLocalVariableDeclaration.arbitrary()
+ .withName(JavaIdentifier.of("index"))
+ .withInitializer(JavaIntegerLiteral.arbitrary().withValue(0))
+ )
+ .withCondition(JavaBinaryOperation.arbitrary()
+ .withOperator(JavaBinaryOperator.NOT_EQUAL_TO)
+ .withLeft(JavaRef.arbitrary().withName(JavaIdentifier.of("index")))
+ .withRight(JavaIntegerLiteral.arbitrary().withValue(4))
+ )
+ .withUpdate(JavaBinaryOperation.arbitrary()
+ .withOperator(JavaBinaryOperator.ASSIGN)
+ .withLeft(JavaRef.arbitrary().withName(JavaIdentifier.of("index")))
+ .withRight(JavaBinaryOperation.arbitrary()
+ .withOperator(JavaBinaryOperator.LEFT_SHIFT)
+ .withLeft(JavaRef.arbitrary().withName(JavaIdentifier.of("index")))
+ .withRight(JavaIntegerLiteral.arbitrary().withValue(1))
+ )
+ )
+ .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("index")))
+ ))
+ )
+ .build();
+
+ var string = write(writer -> writer.writeBlockStatement(java));
+
+ assertThat(string, equalTo("""
+ for (var index = 0; index != 4; index = index << 1) {
+ System.println(index);
+ }"""));
+ }
+
// === Enhanced for statements ===
@Test
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBasicForStatementMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBasicForStatementMatcher.java
new file mode 100644
index 0000000..6683952
--- /dev/null
+++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaBasicForStatementMatcher.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.JavaBasicForStatementMatcher imports
+// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher imports
+
+public class JavaBasicForStatementMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> {
+ public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher isJavaBasicForStatement() {
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher(java.util.List.of());
+ }
+
+ private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement>> submatchers;
+
+ private JavaBasicForStatementMatcher(java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement>> 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.JavaBasicForStatement.class, this.submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher withInit(org.zwobble.precisely.Matcher<? super java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLocalVariableDeclaration>> init) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("init", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement::init, init));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher withCondition(org.zwobble.precisely.Matcher<? super java.util.Optional<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.JavaBasicForStatement>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("condition", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement::condition, condition));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher withUpdate(org.zwobble.precisely.Matcher<? super java.util.Optional<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaExpression>> update) {
+ var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("update", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement::update, update));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher(submatchers);
+ }
+
+ public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher 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.JavaBasicForStatement>>(this.submatchers);
+ submatchers.add(org.zwobble.precisely.Matchers.has("body", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatement::body, body));
+ return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher(submatchers);
+ }
+
+ // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher body
+ // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaBasicForStatementMatcher body
+}