diff options
Diffstat (limited to 'src/test/java/org/zwobble')
2 files changed, 122 insertions, 106 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 c487515..53e946f 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 @@ -120,22 +120,20 @@ public class JavaWriterTests { JavaIdentifier.of("Rectangle"), List.of(), List.of( - new JavaMethodDeclaration( - JavaIdentifier.of("a"), - JavaVisibility.PUBLIC, - JavaMethodKind.INSTANCE, - JavaTypeRef.VOID, - List.of(), - JavaBlock.EMPTY - ), - new JavaMethodDeclaration( - JavaIdentifier.of("b"), - JavaVisibility.PUBLIC, - JavaMethodKind.INSTANCE, - JavaTypeRef.VOID, - List.of(), - JavaBlock.EMPTY - ) + JavaMethodDeclaration.arbitrary() + .withName(JavaIdentifier.of("a")) + .withVisibility(JavaVisibility.PUBLIC) + .withKind(JavaMethodKind.INSTANCE) + .withReturnType(JavaTypeRef.VOID) + .withBody(JavaBlock.EMPTY) + .build(), + JavaMethodDeclaration.arbitrary() + .withName(JavaIdentifier.of("b")) + .withVisibility(JavaVisibility.PUBLIC) + .withKind(JavaMethodKind.INSTANCE) + .withReturnType(JavaTypeRef.VOID) + .withBody(JavaBlock.EMPTY) + .build() ), new JavaCustomArea(JavaTypeRef.topLevel(JavaPackageName.EMPTY, JavaIdentifier.of("Rectangle")), "body"), DocComment.EMPTY @@ -335,22 +333,20 @@ public class JavaWriterTests { JavaInterfaceOpenness.OPEN, List.of(), List.of( - new JavaMethodDeclaration( - JavaIdentifier.of("a"), - JavaVisibility.PUBLIC, - JavaMethodKind.STATIC, - JavaTypeRef.VOID, - List.of(), - JavaBlock.EMPTY - ), - new JavaMethodDeclaration( - JavaIdentifier.of("b"), - JavaVisibility.PUBLIC, - JavaMethodKind.STATIC, - JavaTypeRef.VOID, - List.of(), - JavaBlock.EMPTY - ) + JavaMethodDeclaration.arbitrary() + .withName(JavaIdentifier.of("a")) + .withVisibility(JavaVisibility.PUBLIC) + .withKind(JavaMethodKind.STATIC) + .withReturnType(JavaTypeRef.VOID) + .withBody(JavaBlock.EMPTY) + .build(), + JavaMethodDeclaration.arbitrary() + .withName(JavaIdentifier.of("b")) + .withVisibility(JavaVisibility.PUBLIC) + .withKind(JavaMethodKind.STATIC) + .withReturnType(JavaTypeRef.VOID) + .withBody(JavaBlock.EMPTY) + .build() ), new JavaCustomArea(JavaTypeRef.topLevel(JavaPackageName.EMPTY, JavaIdentifier.of("Shape")), "body"), DocComment.EMPTY @@ -469,22 +465,20 @@ public class JavaWriterTests { List.of(), List.of(), List.of( - new JavaMethodDeclaration( - JavaIdentifier.of("a"), - JavaVisibility.PUBLIC, - JavaMethodKind.INSTANCE, - JavaTypeRef.VOID, - List.of(), - JavaBlock.EMPTY - ), - new JavaMethodDeclaration( - JavaIdentifier.of("b"), - JavaVisibility.PUBLIC, - JavaMethodKind.INSTANCE, - JavaTypeRef.VOID, - List.of(), - JavaBlock.EMPTY - ) + JavaMethodDeclaration.arbitrary() + .withName(JavaIdentifier.of("a")) + .withVisibility(JavaVisibility.PUBLIC) + .withKind(JavaMethodKind.INSTANCE) + .withReturnType(JavaTypeRef.VOID) + .withBody(JavaBlock.EMPTY) + .build(), + JavaMethodDeclaration.arbitrary() + .withName(JavaIdentifier.of("b")) + .withVisibility(JavaVisibility.PUBLIC) + .withKind(JavaMethodKind.INSTANCE) + .withReturnType(JavaTypeRef.VOID) + .withBody(JavaBlock.EMPTY) + .build() ), new JavaCustomArea(JavaTypeRef.topLevel(JavaPackageName.EMPTY, JavaIdentifier.of("Rectangle")), "body"), DocComment.EMPTY @@ -508,14 +502,13 @@ public class JavaWriterTests { @Test public void emptyMethodWithoutBody() throws IOException { - var java = new JavaMethodDeclaration( - JavaIdentifier.of("a"), - JavaVisibility.PUBLIC, - JavaMethodKind.INSTANCE, - JavaTypeRef.VOID, - List.of(), - Optional.empty() - ); + var java = JavaMethodDeclaration.arbitrary() + .withName(JavaIdentifier.of("a")) + .withVisibility(JavaVisibility.PUBLIC) + .withKind(JavaMethodKind.INSTANCE) + .withReturnType(JavaTypeRef.VOID) + .withBody(Optional.empty()) + .build(); var string = write(writer -> writer.writeMethodDeclaration(java)); @@ -526,14 +519,13 @@ public class JavaWriterTests { @Test public void emptyInstanceMethod() throws IOException { - var java = new JavaMethodDeclaration( - JavaIdentifier.of("a"), - JavaVisibility.PUBLIC, - JavaMethodKind.INSTANCE, - JavaTypeRef.VOID, - List.of(), - JavaBlock.EMPTY - ); + var java = JavaMethodDeclaration.arbitrary() + .withName(JavaIdentifier.of("a")) + .withVisibility(JavaVisibility.PUBLIC) + .withKind(JavaMethodKind.INSTANCE) + .withReturnType(JavaTypeRef.VOID) + .withBody(JavaBlock.EMPTY) + .build(); var string = write(writer -> writer.writeMethodDeclaration(java)); @@ -545,14 +537,13 @@ public class JavaWriterTests { @Test public void publicInstanceMethod() throws IOException { - var java = new JavaMethodDeclaration( - JavaIdentifier.of("a"), - JavaVisibility.PUBLIC, - JavaMethodKind.INSTANCE, - JavaTypeRef.VOID, - List.of(), - JavaBlock.EMPTY - ); + var java = JavaMethodDeclaration.arbitrary() + .withName(JavaIdentifier.of("a")) + .withVisibility(JavaVisibility.PUBLIC) + .withKind(JavaMethodKind.INSTANCE) + .withReturnType(JavaTypeRef.VOID) + .withBody(JavaBlock.EMPTY) + .build(); var string = write(writer -> writer.writeMethodDeclaration(java)); @@ -564,14 +555,13 @@ public class JavaWriterTests { @Test public void privateInstanceMethod() throws IOException { - var java = new JavaMethodDeclaration( - JavaIdentifier.of("a"), - JavaVisibility.PRIVATE, - JavaMethodKind.INSTANCE, - JavaTypeRef.VOID, - List.of(), - JavaBlock.EMPTY - ); + var java = JavaMethodDeclaration.arbitrary() + .withName(JavaIdentifier.of("a")) + .withVisibility(JavaVisibility.PRIVATE) + .withKind(JavaMethodKind.INSTANCE) + .withReturnType(JavaTypeRef.VOID) + .withBody(JavaBlock.EMPTY) + .build(); var string = write(writer -> writer.writeMethodDeclaration(java)); @@ -583,14 +573,13 @@ public class JavaWriterTests { @Test public void emptyStaticMethod() throws IOException { - var java = new JavaMethodDeclaration( - JavaIdentifier.of("a"), - JavaVisibility.PUBLIC, - JavaMethodKind.STATIC, - JavaTypeRef.VOID, - List.of(), - JavaBlock.EMPTY - ); + var java = JavaMethodDeclaration.arbitrary() + .withName(JavaIdentifier.of("a")) + .withVisibility(JavaVisibility.PUBLIC) + .withKind(JavaMethodKind.STATIC) + .withReturnType(JavaTypeRef.VOID) + .withBody(JavaBlock.EMPTY) + .build(); var string = write(writer -> writer.writeMethodDeclaration(java)); @@ -602,17 +591,17 @@ public class JavaWriterTests { @Test public void methodWithParams() throws IOException { - var java = new JavaMethodDeclaration( - JavaIdentifier.of("a"), - JavaVisibility.PUBLIC, - JavaMethodKind.INSTANCE, - JavaTypeRef.VOID, - List.of( + var java = JavaMethodDeclaration.arbitrary() + .withName(JavaIdentifier.of("a")) + .withVisibility(JavaVisibility.PUBLIC) + .withKind(JavaMethodKind.INSTANCE) + .withReturnType(JavaTypeRef.VOID) + .withParams(List.of( new JavaParam(JavaTypeRef.INT, JavaIdentifier.of("b")), new JavaParam(JavaTypeRef.LONG, JavaIdentifier.of("c")) - ), - JavaBlock.EMPTY - ); + )) + .withBody(JavaBlock.EMPTY) + .build(); var string = write(writer -> writer.writeMethodDeclaration(java)); @@ -623,17 +612,38 @@ public class JavaWriterTests { } @Test + public void methodWithThrows() throws IOException { + var java = JavaMethodDeclaration.arbitrary() + .withName(JavaIdentifier.of("a")) + .withVisibility(JavaVisibility.PUBLIC) + .withKind(JavaMethodKind.INSTANCE) + .withReturnType(JavaTypeRef.VOID) + .withThrows(List.of( + JavaTypeRef.topLevel(JavaPackageName.of("java", "io"), JavaIdentifier.of("IOException")), + JavaTypeRef.topLevel(JavaPackageName.of("java", "lang"), JavaIdentifier.of("InterruptedException")) + )) + .withBody(JavaBlock.EMPTY) + .build(); + + var string = write(writer -> writer.writeMethodDeclaration(java)); + + assertThat(string, equalTo(""" + public void a() throws java.io.IOException, java.lang.InterruptedException { + }""" + )); + } + + @Test public void methodWithBody() throws IOException { - var java = new JavaMethodDeclaration( - JavaIdentifier.of("a"), - JavaVisibility.PUBLIC, - JavaMethodKind.INSTANCE, - JavaTypeRef.INT, - List.of(), - new JavaBlock(List.of( + var java = JavaMethodDeclaration.arbitrary() + .withName(JavaIdentifier.of("a")) + .withVisibility(JavaVisibility.PUBLIC) + .withKind(JavaMethodKind.INSTANCE) + .withReturnType(JavaTypeRef.INT) + .withBody(new JavaBlock(List.of( new JavaReturn(new JavaIntegerLiteral(42)) - )) - ); + ))) + .build(); var string = write(writer -> writer.writeMethodDeclaration(java)); diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclarationMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclarationMatcher.java index 4a9e202..08633d6 100644 --- a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclarationMatcher.java +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaMethodDeclarationMatcher.java @@ -58,6 +58,12 @@ public class JavaMethodDeclarationMatcher implements org.zwobble.precisely.Match return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclarationMatcher(submatchers); } + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclarationMatcher withThrows(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaTypeRef>> throws_) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("throws", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration::throws_, throws_)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclarationMatcher(submatchers); + } + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclarationMatcher withBody(org.zwobble.precisely.Matcher<? super java.util.Optional<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.JavaMethodDeclaration>>(this.submatchers); submatchers.add(org.zwobble.precisely.Matchers.has("body", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaMethodDeclaration::body, body)); |
