Skip to content

Commit

Permalink
PreferAssertj supports migration of zero-delta floating point array a…
Browse files Browse the repository at this point in the history
…sserts (#883)

PreferAssertj supports migration of zero-delta floating point array asserts
  • Loading branch information
carterkozak authored and bulldozer-bot[bot] committed Sep 24, 2019
1 parent db07680 commit 6180e53
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ public final class PreferAssertj extends BugChecker implements BugChecker.Method
.onClassAny(LEGACY_ASSERT_CLASSES)
.namedAnyOf("assertEquals", "assertArrayEquals");

private static final Matcher<ExpressionTree> ASSERT_ARRAY_EQUALS_CATCHALL =
MethodMatchers.staticMethod()
.onClassAny(LEGACY_ASSERT_CLASSES)
.named("assertArrayEquals");

private static final Matcher<ExpressionTree> ASSERT_NOT_EQUALS_CATCHALL =
MethodMatchers.staticMethod()
.onClassAny(LEGACY_ASSERT_CLASSES)
Expand Down Expand Up @@ -363,13 +368,22 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
fix.replace(tree, assertThat
+ ".isEqualTo(" + argSource(tree, state, 0) + ")"));
} else if (parameters == 3 && ASTHelpers.isSameType(
ASTHelpers.getType(tree.getArguments().get(0)),
getParameterType(tree, 0),
state.getTypeFromString(String.class.getName()),
state)) {
return withAssertThat(tree, state, 2, (assertThat, fix) ->
fix.replace(tree, assertThat
+ ".describedAs(" + argSource(tree, state, 0) + ").isEqualTo("
+ argSource(tree, state, 1) + ")"));
} else if (parameters == 3 && isFloatingPointArrayEqualsWithZeroDelta(tree, state)) {
return withAssertThat(tree, state, 1, (assertThat, fix) ->
fix.replace(tree, assertThat
+ ".isEqualTo(" + argSource(tree, state, 0) + ")"));
} else if (parameters == 4 && isFloatingPointArrayEqualsWithZeroDelta(tree, state)) {
return withAssertThat(tree, state, 2, (assertThat, fix) ->
fix.replace(tree, assertThat
+ ".describedAs(" + argSource(tree, state, 0) + ").isEqualTo("
+ argSource(tree, state, 1) + ")"));
} else {
// Does not fix assertArrayEquals(double[], double[], double)
// or assertArrayEquals(float[], float[], float)
Expand Down Expand Up @@ -721,4 +735,26 @@ private static boolean isObjectVarArgs(MethodInvocationTree tree, VisitorState s
state.getTypeFromString(Object.class.getName()),
state);
}

private static Type getParameterType(MethodInvocationTree tree, int parameterIndex) {
Symbol.MethodSymbol methodSymbol = checkNotNull(ASTHelpers.getSymbol(tree), "symbol");
List<Symbol.VarSymbol> parameters = methodSymbol.getParameters();
checkArgument(parameterIndex >= 0, "index must be greater than zero, was %s", parameterIndex);
checkArgument(parameterIndex < parameters.size(),
"index '%s' is out of bounds for collection %s", parameterIndex, parameters);
return methodSymbol.getParameters().get(parameterIndex).type;
}

private static boolean isFloatingPointArrayEqualsWithZeroDelta(MethodInvocationTree tree, VisitorState state) {
if (!ASSERT_ARRAY_EQUALS_CATCHALL.matches(tree, state)) {
return false;
}
int parameters = tree.getArguments().size();
Type floatType = state.getTypeFromString("float");
Type doubleType = state.getTypeFromString("double");
int deltaParameterIndex = parameters - 1;
return (ASTHelpers.isSameType(getParameterType(tree, deltaParameterIndex), floatType, state)
|| ASTHelpers.isSameType(getParameterType(tree, deltaParameterIndex), doubleType, state))
&& isConstantZero(tree.getArguments().get(deltaParameterIndex));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -415,18 +415,6 @@ public void fails_assertArrayEqualsDelta_double() {
" void f(double[] param) {",
" // BUG: Diagnostic contains: Prefer AssertJ",
" assertArrayEquals(param, new double[] { 1D }, .1D);",
" }",
"}")
.doTest();
}

@Test
public void fails_assertArrayEqualsDeltaDescription_double() {
CompilationTestHelper.newInstance(PreferAssertj.class, getClass()).addSourceLines(
"Test.java",
"import static org.junit.Assert.assertArrayEquals;",
"class Test {",
" void f(double[] param) {",
" // BUG: Diagnostic contains: Prefer AssertJ",
" assertArrayEquals(\"desc\", param, new double[] { 1D }, .1D);",
" }",
Expand All @@ -443,23 +431,49 @@ public void fails_assertArrayEqualsDelta_float() {
" void f(float[] param) {",
" // BUG: Diagnostic contains: Prefer AssertJ",
" assertArrayEquals(param, new float[] { 1f }, .1f);",
" // BUG: Diagnostic contains: Prefer AssertJ",
" assertArrayEquals(\"desc\", param, new float[] { 1f }, .1f);",
" }",
"}")
.doTest();
}

@Test
public void fails_assertArrayEqualsDeltaDescription_float() {
CompilationTestHelper.newInstance(PreferAssertj.class, getClass()).addSourceLines(
"Test.java",
"import static org.junit.Assert.assertArrayEquals;",
"class Test {",
" void f(float[] param) {",
" // BUG: Diagnostic contains: Prefer AssertJ",
" assertArrayEquals(\"desc\", param, new float[] { 1f }, .1f);",
" }",
"}")
.doTest();
public void fix_assertArrayEqualsDelta() {
test()
.addInputLines(
"Test.java",
"import static org.junit.Assert.assertArrayEquals;",
"class Test {",
" void foo(float[] floatArray, double[] doubleArray) {",
" assertArrayEquals(\"desc\", new float[] { 1f }, floatArray, 0);",
" assertArrayEquals(new float[] { 1f }, floatArray, 0);",
" assertArrayEquals(\"desc\", new double[] { 1D }, doubleArray, 0);",
" assertArrayEquals(new double[] { 1D }, doubleArray, 0);",
// nonzero delta doesn't have a migration path
" assertArrayEquals(\"desc\", new float[] { 1f }, floatArray, .1f);",
" assertArrayEquals(\"desc\", new double[] { 1D }, doubleArray, .1D);",
" assertArrayEquals(new float[] { 1f }, floatArray, .1f);",
" assertArrayEquals(new double[] { 1D }, doubleArray, .1D);",
" }",
"}")
.addOutputLines(
"Test.java",
"import static org.assertj.core.api.Assertions.assertThat;",
"import static org.junit.Assert.assertArrayEquals;",
"class Test {",
" void foo(float[] floatArray, double[] doubleArray) {",
" assertThat(floatArray).describedAs(\"desc\").isEqualTo(new float[] { 1f });",
" assertThat(floatArray).isEqualTo(new float[] { 1f });",
" assertThat(doubleArray).describedAs(\"desc\").isEqualTo(new double[] { 1D });",
" assertThat(doubleArray).isEqualTo(new double[] { 1D });",
" assertArrayEquals(\"desc\", new float[] { 1f }, floatArray, .1f);",
" assertArrayEquals(\"desc\", new double[] { 1D }, doubleArray, .1D);",
" assertArrayEquals(new float[] { 1f }, floatArray, .1f);",
" assertArrayEquals(new double[] { 1D }, doubleArray, .1D);",
" }",
"}")
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
}

@Test
Expand Down
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-883.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: PreferAssertj supports migration of zero-delta floating point array
asserts
links:
- https://github.com/palantir/gradle-baseline/pull/883

0 comments on commit 6180e53

Please sign in to comment.