diff options
| author | Michael Williamson <mike@zwobble.org> | 2026-05-03 11:24:39 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2026-05-03 11:24:39 +0100 |
| commit | 19d9169d58ce2f2581073fd0af6e6439604f591c (patch) | |
| tree | e1a63375cef7c76862e30359c5d51e912bc563d6 | |
| parent | f0a8f1b93c12506f40903093f90a70d2a8a99f4f (diff) | |
Add tests for writing Java interface declarations
| -rw-r--r-- | src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java | 18 | ||||
| -rw-r--r-- | src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java | 61 |
2 files changed, 71 insertions, 8 deletions
diff --git a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java index 664c322..59de6b7 100644 --- a/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java +++ b/src/main/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriter.java @@ -27,7 +27,7 @@ public class JavaWriter implements AutoCloseable { writeTypeDeclaration(compilationUnit.typeDeclaration()); } - private void writeTypeDeclaration(JavaTypeDeclaration typeDeclaration) throws IOException { + void writeTypeDeclaration(JavaTypeDeclaration typeDeclaration) throws IOException { switch (typeDeclaration) { case JavaInterfaceDeclaration interfaceDeclaration -> { writeInterfaceDeclaration(interfaceDeclaration); @@ -44,13 +44,15 @@ public class JavaWriter implements AutoCloseable { this.writer.write(interfaceDeclaration.name()); this.writer.write(" "); - writer.write("permits "); - writeWithSeparator( - interfaceDeclaration.permitsTypes(), - this::writeTypeRef, - () -> writer.write(", ") - ); - writer.write(" "); + if (!interfaceDeclaration.permitsTypes().isEmpty()) { + writer.write("permits "); + writeWithSeparator( + interfaceDeclaration.permitsTypes(), + this::writeTypeRef, + () -> writer.write(", ") + ); + writer.write(" "); + } this.writer.write("{"); this.writer.newLine(); 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 new file mode 100644 index 0000000..0a614f4 --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/JavaWriterTests.java @@ -0,0 +1,61 @@ +package org.zwobble.hobgoblin.compiler.output.lang.java; + +import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.output.CodeWriter; +import org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaInterfaceDeclaration; +import org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.List; + +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; + +public class JavaWriterTests { + @Test + public void emptyInterfaceDeclaration() throws IOException { + var java = new JavaInterfaceDeclaration( + "Shape", + List.of() + ); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public sealed interface Shape { + } + """ + )); + } + + @Test + public void sealedInterfaceDeclarationWithPermitsTypes() throws IOException { + var java = new JavaInterfaceDeclaration( + "Shape", + List.of( + new JavaTypeRef(List.of("abc", "def"), "One"), + new JavaTypeRef(List.of("abc", "def"), "Two") + ) + ); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public sealed interface Shape permits abc.def.One, abc.def.Two { + } + """ + )); + } + + private String write(Write write) throws IOException { + var stringWriter = new StringWriter(); + var javaWriter = new JavaWriter(new CodeWriter(stringWriter)); + write.write(javaWriter); + return stringWriter.toString(); + } + + private interface Write { + void write(JavaWriter writer) throws IOException; + } +} |
