Skip to content

Commit

Permalink
Also swap assertNull and assertNotNull argument order (#440)
Browse files Browse the repository at this point in the history
* Also swap assertNull and assertNotNull argument order

* Use wildcard

* Fix argument matcher
  • Loading branch information
timtebeek authored Dec 8, 2023
1 parent 89e1355 commit 76fe8e9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class AssertionsArgumentOrder extends Recipe {
};
private static final MethodMatcher jupiterAssertIterableEqualsMatcher = new MethodMatcher("org.junit.jupiter.api.Assertions assertIterableEquals(..)");

// `assertNull("message", result())` should be `assertNull(result(), "message")`
private static final MethodMatcher jupiterAssertNullMatcher = new MethodMatcher("org.junit.jupiter.api.Assertions assert*Null(Object, String)");

private static final MethodMatcher[] testNgMatcher = new MethodMatcher[]{
new MethodMatcher("org.testng.Assert assertSame(..)"),
new MethodMatcher("org.testng.Assert assertNotSame(..)"),
Expand All @@ -54,6 +57,7 @@ public class AssertionsArgumentOrder extends Recipe {
static {
List<MethodMatcher> matchers = new ArrayList<>(Arrays.asList(jupiterAssertionMatchers));
matchers.add(jupiterAssertIterableEqualsMatcher);
matchers.add(jupiterAssertNullMatcher);
matchers.addAll(Arrays.asList(testNgMatcher));
//noinspection unchecked
precondition = Preconditions.or(matchers.stream().map(UsesMethod::new).toArray(TreeVisitor[]::new));
Expand Down Expand Up @@ -117,6 +121,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
}

private boolean isCorrectOrder(Expression expected, Expression actual, J.MethodInvocation mi) {
if (jupiterAssertNullMatcher.matches(mi)) {
return isConstant(actual, mi) || !isConstant(expected, mi);
}
return isConstant(expected, mi) || !isConstant(actual, mi);
}

Expand Down Expand Up @@ -156,7 +163,7 @@ private boolean isJupiterAssertion(J.MethodInvocation mi) {
return true;
}
}
return jupiterAssertIterableEqualsMatcher.matches(mi);
return jupiterAssertIterableEqualsMatcher.matches(mi) || jupiterAssertNullMatcher.matches(mi);
}

private boolean isTestNgAssertion(J.MethodInvocation mi) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import static org.openrewrite.java.Assertions.java;

public class AssertionsArgumentOrderTest implements RewriteTest {
class AssertionsArgumentOrderTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new AssertionsArgumentOrder())
Expand Down Expand Up @@ -71,6 +71,47 @@ String result() {
);
}

@Test
void junitAssertNullAndAssertNotNull() {
rewriteRun(
//language=java
java(
"""
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
class MyTest {
void someMethod() {
assertNull(result(), "message");
assertNull("message", result());
assertNotNull(result(), "message");
assertNotNull("message", result());
}
String result() {
return "result";
}
}
""",
"""
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
class MyTest {
void someMethod() {
assertNull(result(), "message");
assertNull(result(), "message");
assertNotNull(result(), "message");
assertNotNull(result(), "message");
}
String result() {
return "result";
}
}
"""
)
);
}

@Test
void jupiterAssertSameNotSame() {
rewriteRun(
Expand Down

0 comments on commit 76fe8e9

Please sign in to comment.