Skip to content

Commit c7fbb04

Browse files
authored
Only drop a literal zero delta in AssertEqualsIntegralDeltaToAssertEquals (#1212)
The recipe removed the delta from any `assertEquals(int|long, int|long, delta)`, so a tolerance check such as `assertEquals(100, elapsed, 50)` became the exact `assertEquals(100, elapsed)` and started failing. Only a literal zero delta is redundant, so the recipe now leaves any other delta in place, and the description no longer claims the delta is meaningless.
1 parent 6c313e2 commit c7fbb04

3 files changed

Lines changed: 60 additions & 17 deletions

File tree

‎src/main/java/org/openrewrite/java/testing/cleanup/AssertEqualsIntegralDeltaToAssertEquals.java‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ public class AssertEqualsIntegralDeltaToAssertEquals extends Recipe {
4040
final String displayName = "Remove unnecessary `assertEquals` delta argument for integral types";
4141

4242
@Getter
43-
final String description = "Remove the delta argument from `assertEquals()` when both expected and actual are " +
44-
"`int` or `long` types, since the delta is meaningless for exact integer comparison. " +
45-
"Integer arguments get unnecessarily upcasted to `double` when a delta is provided.";
43+
final String description = "Remove a literal zero delta argument from `assertEquals()` when both expected and actual are `int` or `long` types, since a zero delta already asks for an exact comparison. Integer arguments get unnecessarily upcasted to `double` when a delta is provided; a non-zero delta is a tolerance and is kept.";
4644

4745
@Override
4846
public TreeVisitor<?, ExecutionContext> getVisitor() {
@@ -56,7 +54,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
5654
}
5755

5856
List<Expression> args = mi.getArguments();
59-
if (args.size() == 3 && isIntegralType(args.get(0)) && isIntegralType(args.get(1)) && isNumericType(args.get(2))) {
57+
if (args.size() == 3 && isIntegralType(args.get(0)) && isIntegralType(args.get(1)) && isLiteralZero(args.get(2))) {
6058
// assertEquals(expected, actual, delta) -> assertEquals(expected, actual)
6159
StringBuilder sb = new StringBuilder();
6260
if (mi.getSelect() != null) {
@@ -80,7 +78,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)
8078
return t.apply(updateCursor(mi), mi.getCoordinates().replace(), args.get(0), args.get(1));
8179
}
8280

83-
if (args.size() == 4 && isIntegralType(args.get(0)) && isIntegralType(args.get(1)) && isNumericType(args.get(2))) {
81+
if (args.size() == 4 && isIntegralType(args.get(0)) && isIntegralType(args.get(1)) && isLiteralZero(args.get(2))) {
8482
// assertEquals(expected, actual, delta, message) -> assertEquals(expected, actual, message)
8583
StringBuilder sb = new StringBuilder();
8684
if (mi.getSelect() != null) {
@@ -117,18 +115,12 @@ private boolean isIntegralType(Expression expression) {
117115
return p == JavaType.Primitive.Long || p == JavaType.Primitive.Int;
118116
}
119117

120-
private boolean isNumericType(Expression expression) {
121-
JavaType.FullyQualified fq = TypeUtils.asFullyQualified(expression.getType());
122-
if (fq != null) {
123-
String typeName = fq.getFullyQualifiedName();
124-
return "java.lang.Double".equals(typeName) || "java.lang.Float".equals(typeName) ||
125-
"java.lang.Long".equals(typeName) || "java.lang.Integer".equals(typeName) ||
126-
"java.lang.Short".equals(typeName) || "java.lang.Byte".equals(typeName);
118+
private boolean isLiteralZero(Expression expression) {
119+
if (!(expression instanceof J.Literal)) {
120+
return false;
127121
}
128-
JavaType.Primitive p = TypeUtils.asPrimitive(expression.getType());
129-
return p == JavaType.Primitive.Double || p == JavaType.Primitive.Float ||
130-
p == JavaType.Primitive.Long || p == JavaType.Primitive.Int ||
131-
p == JavaType.Primitive.Short || p == JavaType.Primitive.Byte;
122+
Object value = ((J.Literal) expression).getValue();
123+
return value instanceof Number && ((Number) value).doubleValue() == 0;
132124
}
133125
});
134126
}

‎src/main/resources/META-INF/rewrite/recipes.csv‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.tes
8484
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.assertj.StaticImports,Statically import AssertJ's `assertThat`,Consistently use a static import rather than inlining the `Assertions` class name in tests.,3,AssertJ,Testing,Java,,,Basic building blocks for transforming Java code.,,
8585
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.byteman.BytemanJUnit4ToBytemanJUnit5,Use Byteman JUnit 5 dependency,Migrates Byteman JUnit 4 to JUnit 5.,3,Byteman,Testing,Java,Recipes for [Byteman](https://byteman.jboss.org/) bytecode injection in tests.,,Basic building blocks for transforming Java code.,,
8686
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.AssertEqualsBooleanToAssertBoolean,"Replace JUnit `assertEquals(false, <boolean>)` to `assertFalse(<boolean>)` / `assertTrue(<boolean>)`",Using `assertFalse` or `assertTrue` is simpler and more clear.,1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,,
87-
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.AssertEqualsIntegralDeltaToAssertEquals,Remove unnecessary `assertEquals` delta argument for integral types,"Remove the delta argument from `assertEquals()` when both expected and actual are `int` or `long` types, since the delta is meaningless for exact integer comparison. Integer arguments get unnecessarily upcasted to `double` when a delta is provided.",1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,,
87+
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.AssertEqualsIntegralDeltaToAssertEquals,Remove unnecessary `assertEquals` delta argument for integral types,"Remove a literal zero delta argument from `assertEquals()` when both expected and actual are `int` or `long` types, since a zero delta already asks for an exact comparison. Integer arguments get unnecessarily upcasted to `double` when a delta is provided; a non-zero delta is a tolerance and is kept.",1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,,
8888
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.AssertEqualsNullToAssertNull,"`assertEquals(a, null)` to `assertNull(a)`",Using `assertNull(a)` is simpler and more clear.,1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,,
8989
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.AssertFalseEqualsToAssertNotEquals,"Replace JUnit `assertFalse(a.equals(b))` to `assertNotEquals(a,b)`","Using `assertNotEquals(a,b)` is simpler and more clear.",1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,,
9090
maven,org.openrewrite.recipe:rewrite-testing-frameworks,org.openrewrite.java.testing.cleanup.AssertFalseNegationToAssertTrue,Replace JUnit `assertFalse(!<boolean>)` to `assertTrue(<boolean>)`,Using `assertTrue` is simpler and more clear.,1,Test cleanup,Testing,Java,Recipes to simplify assertions and clean up test code.,,Basic building blocks for transforming Java code.,,

‎src/test/java/org/openrewrite/java/testing/cleanup/AssertEqualsIntegralDeltaToAssertEqualsTest.java‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,57 @@ void test() {
185185
);
186186
}
187187

188+
@Test
189+
void noChangeForNonZeroDelta() {
190+
rewriteRun(
191+
java(
192+
"""
193+
import static org.junit.jupiter.api.Assertions.assertEquals;
194+
195+
class Test {
196+
void test(long elapsed) {
197+
assertEquals(100, elapsed, 50);
198+
}
199+
}
200+
"""
201+
)
202+
);
203+
}
204+
205+
@Test
206+
void noChangeForNonZeroDeltaWithMessage() {
207+
rewriteRun(
208+
java(
209+
"""
210+
import static org.junit.jupiter.api.Assertions.assertEquals;
211+
212+
class Test {
213+
void test(long elapsed) {
214+
assertEquals(100L, elapsed, 0.5, "elapsed");
215+
}
216+
}
217+
"""
218+
)
219+
);
220+
}
221+
222+
@Test
223+
void noChangeForDeltaVariable() {
224+
rewriteRun(
225+
java(
226+
"""
227+
import static org.junit.jupiter.api.Assertions.assertEquals;
228+
229+
class Test {
230+
void test(long elapsed, double tolerance) {
231+
assertEquals(100, elapsed, tolerance);
232+
}
233+
}
234+
"""
235+
)
236+
);
237+
}
238+
188239
@Test
189240
void noChangeForMessageArg() {
190241
rewriteRun(

0 commit comments

Comments
 (0)