diff options
Diffstat (limited to 'src/test/java/org')
2 files changed, 99 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 7158062..75a57a6 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 @@ -1560,6 +1560,58 @@ public class JavaWriterTests { assertThat(string, equalTo("42")); } + // === Lambda expressions === + + @Test + public void lambdaExpressionWithNoParams() throws IOException { + var java = JavaLambdaExpression.arbitrary() + .build(); + + var string = write(writer -> writer.writeTopLevelExpression(java)); + + assertThat(string, equalTo(""" + () -> { + }""")); + } + + @Test + public void lambdaExpressionWithParams() throws IOException { + var java = JavaLambdaExpression.arbitrary() + .addParam(JavaParam.arbitrary() + .withName(JavaIdentifier.of("a")) + .withType(JavaTypeRef.local(JavaIdentifier.of("A"))) + ) + .addParam(JavaParam.arbitrary() + .withName(JavaIdentifier.of("b")) + .withType(JavaTypeRef.local(JavaIdentifier.of("B"))) + ) + .build(); + + var string = write(writer -> writer.writeTopLevelExpression(java)); + + assertThat(string, equalTo(""" + (A a, B b) -> { + }""")); + } + + @Test + public void lambdaExpressionWithBody() throws IOException { + var java = JavaLambdaExpression.arbitrary() + .withBody(JavaBlock.arbitrary() + .addStatement(JavaReturn.arbitrary() + .withValue(JavaIntegerLiteral.arbitrary().withValue(42)) + ) + ) + .build(); + + var string = write(writer -> writer.writeTopLevelExpression(java)); + + assertThat(string, equalTo(""" + () -> { + return 42; + }""")); + } + // === Method refs === @Test diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaLambdaExpressionMatcher.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaLambdaExpressionMatcher.java new file mode 100644 index 0000000..1a9e79a --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/lang/java/ast/JavaLambdaExpressionMatcher.java @@ -0,0 +1,47 @@ +// Generated by hobgoblin. + +package org.zwobble.hobgoblin.compiler.output.lang.java.ast; + +// Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpressionMatcher imports +// Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpressionMatcher imports + +public final class JavaLambdaExpressionMatcher implements org.zwobble.precisely.Matcher<java.lang.Object> { + public static org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpressionMatcher isJavaLambdaExpression() { + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpressionMatcher(java.util.List.of()); + } + + private final java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpression>> submatchers; + + private JavaLambdaExpressionMatcher( + java.util.List<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpression>> 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.JavaLambdaExpression.class, this.submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpressionMatcher withParams(org.zwobble.precisely.Matcher<? super java.util.List<org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaParam>> params) { + var submatchers = new java.util.ArrayList<org.zwobble.precisely.Matcher<? super org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpression>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("params", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpression::params, params)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpressionMatcher(submatchers); + } + + public org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpressionMatcher 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.JavaLambdaExpression>>(this.submatchers); + submatchers.add(org.zwobble.precisely.Matchers.has("body", org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpression::body, body)); + return new org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpressionMatcher(submatchers); + } + + // Custom area start: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpressionMatcher body + // Custom area end: org.zwobble.hobgoblin.compiler.output.lang.java.ast.JavaLambdaExpressionMatcher body +} |
