diff --git a/src/main/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertions.java b/src/main/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertions.java index 2c04d2a6..bcae691b 100644 --- a/src/main/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertions.java +++ b/src/main/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertions.java @@ -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[]))`." @@ -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)`." @@ -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)`." diff --git a/src/test/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertionsTests.java b/src/test/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertionsTests.java index 90575ded..cabeb5d9 100644 --- a/src/test/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertionsTests.java +++ b/src/test/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertionsTests.java @@ -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 { @@ -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") + )); + } } } @@ -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 { @@ -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") + )); + } } } diff --git a/src/test/java/org/philzen/oss/research/AnnotationsTest.java b/src/test/java/org/philzen/oss/research/AnnotationsTest.java index f95215c2..accb823e 100644 --- a/src/test/java/org/philzen/oss/research/AnnotationsTest.java +++ b/src/test/java/org/philzen/oss/research/AnnotationsTest.java @@ -30,6 +30,25 @@ public class AnnotationsTest { static final Set CBA_set = ImmutableSet.of("c", "b", "a"); static final Set 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") @@ -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) {