Skip to content

Commit

Permalink
Implement assertEquals & assertNotEquals float/double delta migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Philzen committed Jun 12, 2024
1 parent 25a7d94 commit 704ae43
Show file tree
Hide file tree
Showing 3 changed files with 287 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,70 @@ void after(Object actual, Object expected, String msg) {
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertEquals(float, float, float)`",
description = "Replace `org.testng.Assert#assertEquals(float, float, float)` with `org.junit.jupiter.api.Assertions#assertEquals(float, float, float)`."
)
public static class MigrateAssertEqualsFloatWithinDelta {

@BeforeTemplate void before(float actual, float expected, float delta) {
Assert.assertEquals(actual, expected, delta);
}

@AfterTemplate
void after(float actual, float expected, float delta) {
Assertions.assertEquals(expected, actual, delta);
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertEquals(double, double, double)`",
description = "Replace `org.testng.Assert#assertEquals(double, double, double)` with `org.junit.jupiter.api.Assertions#assertEquals(double, double, double)`."
)
public static class MigrateAssertEqualsDoubleWithinDelta {

@BeforeTemplate void before(double actual, double expected, double delta) {
Assert.assertEquals(actual, expected, delta);
}

@AfterTemplate
void after(double actual, double expected, double delta) {
Assertions.assertEquals(expected, actual, delta);
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertEquals(float, float, float, String)`",
description = "Replace `org.testng.Assert#assertEquals(float, float, float, String)` with `org.junit.jupiter.api.Assertions#assertEquals(float, float, float, String)`."
)
public static class MigrateAssertEqualsFloatDeltaWithMsg {

@BeforeTemplate void before(float actual, float expected, float delta, String msg) {
Assert.assertEquals(actual, expected, delta, msg);
}

@AfterTemplate
void after(float actual, float expected, float delta, String msg) {
Assertions.assertEquals(expected, actual, delta, msg);
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertEquals(double, double, double, String)`",
description = "Replace `org.testng.Assert#assertEquals(double, double, double, String)` with `org.junit.jupiter.api.Assertions#assertEquals(double, double, double, String)`."
)
public static class MigrateAssertEqualsDoubleDeltaWithMsg {

@BeforeTemplate void before(double actual, double expected, double delta, String msg) {
Assert.assertEquals(actual, expected, delta, msg);
}

@AfterTemplate
void after(double actual, double expected, double delta, String msg) {
Assertions.assertEquals(expected, actual, delta, msg);
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertNotEquals(Object[], Object[])`",
description = "Replace `org.testng.Assert#assertNotEquals(Object[], Object[])` with `org.junit.jupiter.api.Assertions#assertNotEquals(Arrays.toString(Object[], Arrays.toString(Object[]))`."
Expand Down Expand Up @@ -117,6 +181,38 @@ public static class MigrateAssertNotEqual {
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertNotEquals(float, float, float)`",
description = "Replace `org.testng.Assert#assertNotEquals(float, float, float)` with `org.junit.jupiter.api.Assertions#assertNotEquals(float, float, float)`."
)
public static class MigrateAssertNotEqualsFloatDelta {

@BeforeTemplate void before(float actual, float expected, float delta) {
Assert.assertNotEquals(actual, expected, delta);
}

@AfterTemplate
void after(float actual, float expected, float delta) {
Assertions.assertNotEquals(expected, actual, delta);
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertNotEquals(double, double, double)`",
description = "Replace `org.testng.Assert#assertNotEquals(double, double, double)` with `org.junit.jupiter.api.Assertions#assertNotEquals(double, double, double)`."
)
public static class MigrateAssertNotEqualsDoubleWithinDelta {

@BeforeTemplate void before(double actual, double expected, double delta) {
Assert.assertNotEquals(actual, expected, delta);
}

@AfterTemplate
void after(double actual, double expected, double delta) {
Assertions.assertNotEquals(expected, actual, delta);
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertNotEquals(Object[], Object[], String)`",
description = "Replace `org.testng.Assert#assertNotEquals(Object[], Object[])` with `org.junit.jupiter.api.Assertions#assertNotEquals(Arrays.toString(Object[], Arrays.toString(Object[]), String)`."
Expand Down Expand Up @@ -148,6 +244,38 @@ public static class MigrateAssertNotEqualWithMsg {
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertNotEquals(float, float, float, String)`",
description = "Replace `org.testng.Assert#assertNotEquals(float, float, float, String)` with `org.junit.jupiter.api.Assertions#assertNotEquals(float, float, float, String)`."
)
public static class MigrateAssertNotEqualsFloatDeltaWithMsg {

@BeforeTemplate void before(float actual, float expected, float delta, String msg) {
Assert.assertNotEquals(actual, expected, delta, msg);
}

@AfterTemplate
void after(float actual, float expected, float delta, String msg) {
Assertions.assertNotEquals(expected, actual, delta, msg);
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertNotEquals(double, double, double, String)`",
description = "Replace `org.testng.Assert#assertNotEquals(double, double, double, String)` with `org.junit.jupiter.api.Assertions#assertNotEquals(double, double, double, String)`."
)
public static class MigrateAssertNotEqualsDoubleDeltaWithMsg {

@BeforeTemplate void before(double actual, double expected, double delta, String msg) {
Assert.assertNotEquals(actual, expected, delta, msg);
}

@AfterTemplate
void after(double actual, double expected, double delta, String msg) {
Assertions.assertNotEquals(expected, actual, delta, msg);
}
}

@RecipeDescriptor(
name = "Replace `Assert#assertFalse(boolean)`",
description = "Replace `org.testng.Assert#assertFalse(boolean)` with `org.junit.jupiter.api.Assertions#assertFalse(boolean)`."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,37 @@ void testMethod() {
""".formatted(actual, expected)
));
}

@ValueSource(strings = {"float", "double"})
@ParameterizedTest void deltaFunctionIsMigrated(String type) {
//language=java
rewriteRun(java(
"""
import org.testng.Assert;
class MyTest {
void testMethod() {
%s actual;
%s expected;
Assert.assertEquals(actual, expected, %s, "Test failed badly");
}
}
""".formatted(type, type, type.equals("float") ? "0.1f" : "0.2d"),
"""
import org.junit.jupiter.api.Assertions;
class MyTest {
void testMethod() {
%s actual;
%s expected;
Assertions.assertEquals(expected, actual, %s, "Test failed badly");
}
}
""".formatted(type, type, type.equals("float") ? "0.1f" : "0.2d")
));
}
}

@Nested class WithoutErrorMessage {
Expand Down Expand Up @@ -246,6 +277,37 @@ void testMethod() {
""".formatted(actual, expected)
));
}

@ValueSource(strings = {"float", "double"})
@ParameterizedTest void deltaFunctionIsMigrated(String type) {
//language=java
rewriteRun(java(
"""
import org.testng.Assert;
class MyTest {
void testMethod() {
%s actual;
%s expected;
Assert.assertEquals(actual, expected, %s);
}
}
""".formatted(type, type, type.equals("float") ? "0.1f" : "0.2d"),
"""
import org.junit.jupiter.api.Assertions;
class MyTest {
void testMethod() {
%s actual;
%s expected;
Assertions.assertEquals(expected, actual, %s);
}
}
""".formatted(type, type, type.equals("float") ? "0.1f" : "0.2d")
));
}
}
}

Expand Down Expand Up @@ -315,6 +377,37 @@ void testMethod() {
""".formatted(actual, expected)
));
}

@ValueSource(strings = {"float", "double"})
@ParameterizedTest void deltaFunctionIsMigrated(String type) {
//language=java
rewriteRun(java(
"""
import org.testng.Assert;
class MyTest {
void testMethod() {
%s actual;
%s expected;
Assert.assertNotEquals(actual, expected, %s, "Test failed badly");
}
}
""".formatted(type, type, type.equals("float") ? "0.1f" : "0.2d"),
"""
import org.junit.jupiter.api.Assertions;
class MyTest {
void testMethod() {
%s actual;
%s expected;
Assertions.assertNotEquals(expected, actual, %s, "Test failed badly");
}
}
""".formatted(type, type, type.equals("float") ? "0.1f" : "0.2d")
));
}
}

@Nested class WithoutErrorMessage {
Expand Down Expand Up @@ -382,6 +475,37 @@ void testMethod() {
""".formatted(actual, expected)
));
}

@ValueSource(strings = {"float", "double"})
@ParameterizedTest void deltaFunctionIsMigrated(String type) {
//language=java
rewriteRun(java(
"""
import org.testng.Assert;
class MyTest {
void testMethod() {
%s actual;
%s expected;
Assert.assertNotEquals(actual, expected, %s);
}
}
""".formatted(type, type, type.equals("float") ? "0.1f" : "0.2d"),
"""
import org.junit.jupiter.api.Assertions;
class MyTest {
void testMethod() {
%s actual;
%s expected;
Assertions.assertNotEquals(expected, actual, %s);
}
}
""".formatted(type, type, type.equals("float") ? "0.1f" : "0.2d")
));
}
}
}

Expand Down
35 changes: 35 additions & 0 deletions src/test/java/org/philzen/oss/research/AnnotationsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ public class AnnotationsTest {
static final Set<String> CBA_set = ImmutableSet.of("c", "b", "a");
static final Set<String> XYZ_set = ImmutableSet.of("x", "y", "z");

@Nested class assertEquals {

@Test void deltaDouble() {
thisWillPass(() -> Assert.assertEquals(1d, 2d, 1d));
thisWillPass(() -> Assertions.assertEquals(1d, 2d, 1d));

thisWillFail(() -> Assert.assertEquals(1d, 2d, .999d));
thisWillFail(() -> Assertions.assertEquals(1d, 2d, .999d));
}

@Test void deltaFloat() {
thisWillPass(() -> Assert.assertEquals(1f, 2f, 1f));
thisWillPass(() -> Assertions.assertEquals(1f, 2f, 1f));

thisWillFail(() -> Assert.assertEquals(1f, 2f, .999f));
thisWillFail(() -> Assertions.assertEquals(1f, 2f, .999f));
}
}

@Nested class assertNotEquals {

@Tag("mismatch")
Expand Down Expand Up @@ -96,6 +115,22 @@ public class AnnotationsTest {
thisWillFail(() -> Assert.assertNotEquals(ABC_set, ABC_set));
thisWillFail(() -> Assertions.assertNotEquals(ABC_set, ABC_set));
}

@Test void doubleDelta() {
thisWillPass(() -> Assert.assertNotEquals(1d, 2d, .999d));
thisWillPass(() -> Assertions.assertNotEquals(1d, 2d, .999d));

thisWillFail(() -> Assert.assertNotEquals(1d, 2d, 1d));
thisWillFail(() -> Assertions.assertNotEquals(1d, 2d, 1d));
}

@Test void floatDelta() {
thisWillPass(() -> Assert.assertNotEquals(1f, 2f, .999f));
thisWillPass(() -> Assertions.assertNotEquals(1f, 2f, .999f));

thisWillFail(() -> Assert.assertNotEquals(1f, 2f, 1f));
thisWillFail(() -> Assertions.assertNotEquals(1f, 2f, 1f));
}
}

void thisWillPass(final ThrowableAssert.ThrowingCallable code) {
Expand Down

0 comments on commit 704ae43

Please sign in to comment.