From deee2d3f92e3f09b711e1fc68a25129bc554df2a Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 12 Jun 2026 15:43:02 +0100 Subject: Add implementsTypes to JavaClassDeclaration --- .../compiler/output/lang/java/JavaWriterTests.java | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) (limited to 'src/test/java/org/zwobble') 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 798de28..53a3e65 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 @@ -23,6 +23,7 @@ public class JavaWriterTests { new JavaClassDeclaration( "Rectangle", List.of(), + List.of(), new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Rectangle"), "body"), DocComment.EMPTY ) @@ -48,6 +49,7 @@ public class JavaWriterTests { var java = new JavaClassDeclaration( "Rectangle", List.of(), + List.of(), new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Rectangle"), "body"), DocComment.EMPTY ); @@ -62,10 +64,56 @@ public class JavaWriterTests { )); } + @Test + public void classDeclarationImplementingSingleInterface() throws IOException { + var java = new JavaClassDeclaration( + "Rectangle", + List.of( + JavaTypeRef.topLevel(List.of("com.example"), "Shape") + ), + List.of(), + new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Rectangle"), "body"), + DocComment.EMPTY + ); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public class Rectangle implements com.example.Shape { + // Custom area start: Rectangle body + // Custom area end: Rectangle body + }""" + )); + } + + @Test + public void classDeclarationImplementingMultipleInterfaces() throws IOException { + var java = new JavaClassDeclaration( + "Rectangle", + List.of( + JavaTypeRef.topLevel(List.of("com.example"), "Shape"), + JavaTypeRef.topLevel(List.of("com.example"), "Drawable") + ), + List.of(), + new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Rectangle"), "body"), + DocComment.EMPTY + ); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public class Rectangle implements com.example.Shape, com.example.Drawable { + // Custom area start: Rectangle body + // Custom area end: Rectangle body + }""" + )); + } + @Test public void classDeclarationWithMethods() throws IOException { var java = new JavaClassDeclaration( "Rectangle", + List.of(), List.of( new JavaMethodDeclaration( "a", @@ -107,6 +155,7 @@ public class JavaWriterTests { var java = new JavaClassDeclaration( "Point", List.of(), + List.of(), new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Point"), "body"), new DocComment("A 2D point.") ); @@ -295,6 +344,53 @@ public class JavaWriterTests { )); } + @Test + public void recordDeclarationImplementingOneInterface() throws IOException { + var java = new JavaRecordDeclaration( + "Rectangle", + List.of(), + List.of( + JavaTypeRef.topLevel(List.of("com.example"), "Shape") + ), + List.of(), + new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Rectangle"), "body"), + DocComment.EMPTY + ); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public record Rectangle() implements com.example.Shape { + // Custom area start: Rectangle body + // Custom area end: Rectangle body + }""" + )); + } + + @Test + public void recordDeclarationImplementingMultipleInterfaces() throws IOException { + var java = new JavaRecordDeclaration( + "Rectangle", + List.of(), + List.of( + JavaTypeRef.topLevel(List.of("com.example"), "Shape"), + JavaTypeRef.topLevel(List.of("com.example"), "Drawable") + ), + List.of(), + new JavaCustomArea(JavaTypeRef.topLevel(List.of(), "Rectangle"), "body"), + DocComment.EMPTY + ); + + var string = write(writer -> writer.writeTypeDeclaration(java)); + + assertThat(string, equalTo(""" + public record Rectangle() implements com.example.Shape, com.example.Drawable { + // Custom area start: Rectangle body + // Custom area end: Rectangle body + }""" + )); + } + @Test public void recordDeclarationWithMethods() throws IOException { var java = new JavaRecordDeclaration( -- cgit v1.2.3