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 14, 2024
1 parent a3b6099 commit bb49971
Show file tree
Hide file tree
Showing 3 changed files with 292 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,70 @@ public static class MigrateAssertEqualsArrayWithMsg {
}
}

@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 MigrateAssertEqualsDoubleDelta {

@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(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#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 MigrateAssertEqualsFloatDelta {

@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(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 = "Migrate `Assert#assertEquals(Iterator<?>, Iterator<?>)`",
description = "Migrates `org.testng.Assert#assertEquals(Iterator<?>, Iterator<?>)` " +
Expand Down Expand Up @@ -158,6 +222,70 @@ public static class MigrateAssertNotEqualsArrayWithMsg {
}
}

@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 MigrateAssertNotEqualsDoubleDelta {

@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(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#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(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 = "Migrate `Assert#assertNotEquals(Iterator<?>, Iterator<?>)`",
description = "Migrates `org.testng.Assert#assertNotEquals(Iterator<?>, Iterator<?>)` " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,37 @@ void testMethod() {
"""
));
}

@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 @@ -320,6 +351,37 @@ void testMethod() {
"""
));
}

@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 @@ -427,6 +489,37 @@ void testMethod() {
"""
));
}

@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 @@ -531,7 +624,38 @@ void testMethod() {
}
"""
));
}
}

@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
39 changes: 39 additions & 0 deletions src/test/java/org/philzen/oss/research/ApiComparisonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ class ApiComparisonTest {
StreamSupport.stream(Spliterators.spliteratorUnknownSize(CBA_list.iterator(), 0), false).toArray()
));
}

@Test void doubleDelta() {
final double actual = 1d;

thisWillPass(() -> Assert.assertEquals(actual, 2d, 1d));
thisWillPass(() -> Assertions.assertEquals(2d, actual, 1d));

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

@Test void floatDelta() {
final float actual = 1f;

thisWillPass(() -> Assert.assertEquals(actual, 2f, 1f));
thisWillPass(() -> Assertions.assertEquals(2f, actual, 1f));

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

@Nested class assertNotEquals {
Expand Down Expand Up @@ -131,6 +151,25 @@ class ApiComparisonTest {
thisWillFail(() -> Assert.assertNotEquals(ABC_set, ABC_set));
thisWillFail(() -> Assertions.assertNotEquals(ABC_set, ABC_set));
}

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

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

@Test void floatDelta() {
final float actual = 1f;

thisWillPass(() -> Assert.assertNotEquals(actual, 2f, .999f));
thisWillPass(() -> Assertions.assertNotEquals(2f, actual, .999f));

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

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

0 comments on commit bb49971

Please sign in to comment.