Skip to content

Commit

Permalink
fix(#357): fix the problem with lambda as a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Apr 19, 2024
1 parent 3e74ca9 commit c124fa1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,16 @@ private static Stream<Statement> flatStatements(final Stream<? extends Statement
} else if (statement.isExpressionStmt()) {
final Expression expression = statement.asExpressionStmt().getExpression();
if (expression.isMethodCallExpr()) {
result = Stream.of(statement);
final MethodCallExpr call = expression.asMethodCallExpr();
result = Stream.concat(
Stream.concat(
call.getScope()
.map(JavaParserMethod::flatStatements)
.orElseGet(Stream::empty),
Stream.of(statement)
),
call.getArguments().stream().flatMap(JavaParserMethod::flatStatements)
);
} else {
result = JavaParserMethod.flatStatements(expression);
}
Expand Down Expand Up @@ -165,22 +174,34 @@ private static Stream<Statement> flatStatements(final Expression expression) {
result = JavaParserMethod.flatStatements(
Stream.of(expression.asLambdaExpr().getBody())
);
} else if (expression.isMethodCallExpr()) {
final MethodCallExpr call = expression.asMethodCallExpr();
result = Stream.concat(
call.getScope().map(JavaParserMethod::flatStatements).orElseGet(Stream::empty),
call.getArguments().stream().flatMap(JavaParserMethod::flatStatements)
);
} else {
final List<Statement> statements = new ArrayList<>(0);
final List<VariableDeclarator> declarators = new ArrayList<>(0);
final List<Expression> expressions = new ArrayList<>(0);
for (final Node node : expression.getChildNodes()) {
if (node instanceof Statement) {
statements.add((Statement) node);
} else if (node instanceof VariableDeclarator) {
declarators.add((VariableDeclarator) node);
} else if (node instanceof Expression) {
expressions.add((Expression) node);
}
}
result = Stream.concat(
declarators.stream()
.map(VariableDeclarator::getInitializer)
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(JavaParserMethod::flatStatements),
Stream.concat(
declarators.stream()
.map(VariableDeclarator::getInitializer)
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(JavaParserMethod::flatStatements),
expressions.stream().flatMap(JavaParserMethod::flatStatements)
),
statements.stream()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,25 @@ void parsesAssertionInsideClosure() {
);
}

@Test
@SuppressWarnings("PMD.JUnitTestContainsTooManyAsserts")
void parsesAssertionInsideLambdaAsParam() {
final JavaParserTestClass parser = JavaTestClasses.TEST_WITH_ASSERTIONS.toTestClass();
final String method = "checksTheCaseFrom357issueWithAssertionAsParam";
final TestCase tested = parser.all().stream()
.filter(test -> method.equals(test.name()))
.findFirst()
.orElseThrow(() -> new AssertionError(String.format("Method %s not found", method)));
final Assertion assertion = tested.assertions().stream()
.findFirst()
.orElseThrow(() -> new AssertionError("Assertion not found"));
MatcherAssert.assertThat(
String.format("The '%s' assertion has to contain an explanation", assertion),
assertion.explanation().isPresent(),
Matchers.is(true)
);
}

@Test
void parsesPackageJava() {
final JavaParserTestClass parser = JavaTestClasses.PACKAGE_INFO.toTestClass();
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/TestWithAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.stream.Stream;

class TestWithAssertions {
@Test
Expand Down Expand Up @@ -123,4 +124,15 @@ void checksTheCaseFrom357issueWithAssertionInClosure() {
};
r.run();
}

@Test
void checksTheCaseFrom357issueWithAssertionAsParam() {
Stream.generate(() -> (Runnable) () -> {
MatcherAssert.assertThat(
"Lambda as a parameter assertion",
1,
Matchers.equalTo(1)
);
}).limit(1).forEach(Runnable::run);
}
}

0 comments on commit c124fa1

Please sign in to comment.