generated from moderneinc/rewrite-recipe-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement special migration for float/double-array delta assertion
Cannot be implemented in Refaster due to openrewrite/rewrite-templating#90
- Loading branch information
Showing
4 changed files
with
237 additions
and
0 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
src/main/java/org/philzen/oss/testng/MigrateMismatchedAssertions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package org.philzen.oss.testng; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.internal.lang.NonNullApi; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.search.UsesMethod; | ||
import org.openrewrite.java.search.UsesType; | ||
import org.openrewrite.java.tree.J; | ||
import org.philzen.oss.utils.Parser; | ||
|
||
import java.util.function.Function; | ||
|
||
@NonNullApi | ||
public class MigrateMismatchedAssertions extends Recipe { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Replace `Assert#assertEquals(actual[], expected[], delta [, message])` for float and double inputs"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Replaces `org.testng.Assert#assertEquals(actual[], expected[], delta [, message])` with custom `org.junit.jupiter.api.Assertions#assertAll(() -> {})`."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
JavaVisitor<ExecutionContext> javaVisitor = new JavaVisitor<ExecutionContext>() { | ||
|
||
final Function<String, JavaTemplate> before = (type) -> JavaTemplate | ||
.builder("org.testng.Assert.assertEquals(#{actual:anyArray(%s)}, #{expected:anyArray(%s)}, #{delta:any(%s)});".replace("%s", type)) | ||
.javaParser(Parser.runtime()).build(); | ||
|
||
final Function<String, JavaTemplate> beforeWithMsg = (type) -> JavaTemplate | ||
.builder("org.testng.Assert.assertEquals(#{actual:anyArray(%s)}, #{expected:anyArray(%s)}, #{delta:any(%s)}, #{message:any(java.lang.String)});".replace("%s", type)) | ||
.javaParser(Parser.runtime()).build(); | ||
|
||
final JavaTemplate after = JavaTemplate | ||
.builder("Assertions.assertAll(()->{\n Assertions.assertEquals(#{expected:anyArray(float)}.length, #{actual:anyArray(float)}.length, \"Arrays don't have the same size.\");\n for (int i = 0; i < #{actual}.length; i++) {\n Assertions.assertEquals(#{expected}[i], #{actual}[i], #{delta:any(float)});\n }\n});") | ||
.imports("org.junit.jupiter.api.Assertions") | ||
.javaParser(Parser.jupiter()).build(); | ||
|
||
final JavaTemplate afterWithMsg = JavaTemplate | ||
.builder("Assertions.assertAll(()->{\n Assertions.assertEquals(#{expected:anyArray(float)}.length, #{actual:anyArray(float)}.length, \"Arrays don't have the same size.\");\n for (int i = 0; i < #{actual}.length; i++) {\n Assertions.assertEquals(#{expected}[i], #{actual}[i], #{delta:any(float)}, #{message:any(String)});\n }\n});") | ||
.imports("org.junit.jupiter.api.Assertions") | ||
.javaParser(Parser.jupiter()).build(); | ||
|
||
@Override | ||
public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { | ||
JavaTemplate.Matcher matcher; | ||
if ((matcher = before.apply("float").matcher(getCursor())).find() | ||
|| (matcher = before.apply("double").matcher(getCursor())).find()) | ||
{ | ||
imports(); | ||
return after.apply( | ||
getCursor(), | ||
elem.getCoordinates().replace(), | ||
matcher.parameter(1), | ||
matcher.parameter(0), | ||
matcher.parameter(2) | ||
); | ||
} else if ((matcher = beforeWithMsg.apply("float").matcher(getCursor())).find() | ||
|| (matcher = beforeWithMsg.apply("double").matcher(getCursor())).find()) | ||
{ | ||
imports(); | ||
return afterWithMsg.apply( | ||
getCursor(), | ||
elem.getCoordinates().replace(), | ||
matcher.parameter(1), | ||
matcher.parameter(0), | ||
matcher.parameter(2), | ||
matcher.parameter(3) | ||
); | ||
} | ||
|
||
return super.visitMethodInvocation(elem, ctx); | ||
} | ||
|
||
private void imports() { | ||
maybeRemoveImport("org.testng.Assert"); | ||
maybeAddImport("org.junit.jupiter.api.Assertions"); | ||
} | ||
}; | ||
|
||
return Preconditions.check( | ||
Preconditions.and( | ||
new UsesType<>("org.testng.Assert", true), | ||
new UsesMethod<>("org.testng.Assert assertEquals(..)") | ||
), | ||
javaVisitor | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/test/java/org/philzen/oss/testng/MigrateMismatchedAssertionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package org.philzen.oss.testng; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class MigrateMismatchedAssertionsTest implements RewriteTest { | ||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new MigrateMismatchedAssertions()); | ||
} | ||
|
||
@ValueSource(strings = {"float[]", "double[]"}) | ||
@ParameterizedTest | ||
void deltaFunctionForArraysIsMigrated(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.assertAll(() -> { | ||
Assertions.assertEquals(expected.length, actual.length, "Arrays don't have the same size."); | ||
for (int i = 0; i < actual.length; i++) { | ||
Assertions.assertEquals(expected[i], actual[i], %s); | ||
} | ||
}); | ||
} | ||
} | ||
""".formatted(type, type, type.equals("float[]") ? "0.1f" : "0.2d") | ||
)); | ||
} | ||
|
||
@ValueSource(strings = {"float[]", "double[]"}) | ||
@ParameterizedTest | ||
void deltaFunctionForArraysIsMigratedWithMessage(String type) { | ||
//language=java | ||
rewriteRun(java( | ||
""" | ||
import org.testng.Assert; | ||
class MyTest { | ||
void testMethod() { | ||
%s actual; | ||
%s expected; | ||
Assert.assertEquals(actual, expected, %s, "Those values are way off."); | ||
} | ||
} | ||
""".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.assertAll(() -> { | ||
Assertions.assertEquals(expected.length, actual.length, "Arrays don't have the same size."); | ||
for (int i = 0; i < actual.length; i++) { | ||
Assertions.assertEquals(expected[i], actual[i], %s, "Those values are way off."); | ||
} | ||
}); | ||
} | ||
} | ||
""".formatted(type, type, type.equals("float[]") ? "0.1f" : "0.2d") | ||
)); | ||
} | ||
} |