From 64344428233e3ed4250477b52f5eb560e168c29b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 19 Jun 2026 18:27:41 +0100 Subject: Add Java enum declarations --- .../compiler/output/lang/java/JavaWriterTests.java | 62 ++++++++++++++++++++++ .../lang/java/ast/JavaEnumConstantMatcher.java | 34 ++++++++++++ .../lang/java/ast/JavaEnumDeclarationMatcher.java | 46 ++++++++++++++++ 3 files changed, 142 insertions(+) create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumConstantMatcher.java create mode 100644 src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumDeclarationMatcher.java (limited to 'src/test/java/org') 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 1de864d..78b975d 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 @@ -173,6 +173,68 @@ public class JavaWriterTests { )); } + @Test + public void emptyEnumDeclaration() throws IOException { + var java = JavaEnumDeclaration.arbitrary() + .withName("SmallNumber") + .withCustomArea(JavaCustomArea.forType(JavaTypeRef.topLevel(List.of(), "SmallNumber"), "body")) + .build(); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public enum SmallNumber { + // Custom area start: SmallNumber body + // Custom area end: SmallNumber body + }""" + )); + } + + @Test + public void enumDeclarationWithConstants() throws IOException { + var java = JavaEnumDeclaration.arbitrary() + .withName("SmallNumber") + .withCustomArea(JavaCustomArea.forType(JavaTypeRef.topLevel(List.of(), "SmallNumber"), "body")) + .withConstants(List.of( + JavaEnumConstant.arbitrary().withName("ZERO").build(), + JavaEnumConstant.arbitrary().withName("ONE").build(), + JavaEnumConstant.arbitrary().withName("TWO").build() + )) + .build(); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public enum SmallNumber { + ZERO, + ONE, + TWO + + // Custom area start: SmallNumber body + // Custom area end: SmallNumber body + }""" + )); + } + + @Test + public void enumDeclarationWithDocComment() throws IOException { + var java = JavaEnumDeclaration.arbitrary() + .withName("SmallNumber") + .withDocComment(new DocComment("A small number.")) + .withCustomArea(JavaCustomArea.forType(JavaTypeRef.topLevel(List.of(), "SmallNumber"), "body")) + .build(); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + /// A small number. + public enum SmallNumber { + // Custom area start: SmallNumber body + // Custom area end: SmallNumber body + }""" + )); + } + @Test public void openInterfaceDeclaration() throws IOException { var java = new JavaInterfaceDeclaration( diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumConstantMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumConstantMatcher.java new file mode 100644 index 0000000..58d6f54 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumConstantMatcher.java @@ -0,0 +1,34 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +public class JavaEnumConstantMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstantMatcher isJavaEnumConstant() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstantMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private JavaEnumConstantMatcher(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.JavaEnumConstant.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstantMatcher withName(org.zwobble.precisely.Matcher name) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstantMatcher(java.util.stream.Stream.concat(this.submatchers.stream(), java.util.stream.Stream.of(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstant::name, name))).toList()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstantMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumConstantMatcher body +} diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumDeclarationMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumDeclarationMatcher.java new file mode 100644 index 0000000..0c7be66 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaEnumDeclarationMatcher.java @@ -0,0 +1,46 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +public class JavaEnumDeclarationMatcher implements org.zwobble.precisely.Matcher { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher isJavaEnumDeclaration() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher(java.util.List.of()); + } + + private final java.util.List> submatchers; + + private JavaEnumDeclarationMatcher(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.JavaEnumDeclaration.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher withName(org.zwobble.precisely.Matcher name) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher(java.util.stream.Stream.concat(this.submatchers.stream(), java.util.stream.Stream.of(org.zwobble.precisely.Matchers.has("name", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration::name, name))).toList()); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher withConstants(org.zwobble.precisely.Matcher> constants) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher(java.util.stream.Stream.concat(this.submatchers.stream(), java.util.stream.Stream.of(org.zwobble.precisely.Matchers.has("constants", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration::constants, constants))).toList()); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher withCustomArea(org.zwobble.precisely.Matcher customArea) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher(java.util.stream.Stream.concat(this.submatchers.stream(), java.util.stream.Stream.of(org.zwobble.precisely.Matchers.has("customArea", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration::customArea, customArea))).toList()); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher withDocComment(org.zwobble.precisely.Matcher docComment) { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher(java.util.stream.Stream.concat(this.submatchers.stream(), java.util.stream.Stream.of(org.zwobble.precisely.Matchers.has("docComment", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclaration::docComment, docComment))).toList()); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaEnumDeclarationMatcher body +} -- cgit v1.2.3