|
| 1 | +package org.philzen.oss.testng; |
| 2 | + |
| 3 | +import org.openrewrite.*; |
| 4 | +import org.openrewrite.internal.lang.NonNullApi; |
| 5 | +import org.openrewrite.java.JavaParser; |
| 6 | +import org.openrewrite.java.JavaTemplate; |
| 7 | +import org.openrewrite.java.JavaVisitor; |
| 8 | +import org.openrewrite.java.search.UsesMethod; |
| 9 | +import org.openrewrite.java.search.UsesType; |
| 10 | +import org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor; |
| 11 | +import org.openrewrite.java.tree.J; |
| 12 | +import org.philzen.oss.utils.JupiterJavaParser; |
| 13 | + |
| 14 | +import java.util.function.Function; |
| 15 | + |
| 16 | +import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.SHORTEN_NAMES; |
| 17 | +import static org.openrewrite.java.template.internal.AbstractRefasterJavaVisitor.EmbeddingOption.SIMPLIFY_BOOLEANS; |
| 18 | + |
| 19 | +@NonNullApi |
| 20 | +public class MigrateMismatchedAssertions extends Recipe { |
| 21 | + |
| 22 | + @Override |
| 23 | + public String getDisplayName() { |
| 24 | + return "Replace `Assert#assertEquals(actual[], expected[], delta [, message])` for float and double inputs"; |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public String getDescription() { |
| 29 | + return "Replace `org.testng.Assert#assertEquals(actual[], expected[], delta [, message])` with custom `org.junit.jupiter.api.Assertions#assertAll(() -> {})`."; |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 34 | + JavaVisitor<ExecutionContext> javaVisitor = new AbstractRefasterJavaVisitor() { |
| 35 | + |
| 36 | + final Function<String, JavaTemplate> before = (type) -> JavaTemplate |
| 37 | + .builder("org.testng.Assert.assertEquals(#{actual:anyArray(%s)}, #{expected:anyArray(%s)}, #{delta:any(%s)});".replace("%s", type)) |
| 38 | + .javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath())) |
| 39 | + .build(); |
| 40 | + |
| 41 | + final Function<String, JavaTemplate> beforeWithMsg = (type) -> JavaTemplate |
| 42 | + .builder("org.testng.Assert.assertEquals(#{actual:anyArray(%s)}, #{expected:anyArray(%s)}, #{delta:any(%s)}, #{message:any(java.lang.String)});".replace("%s", type)) |
| 43 | + .javaParser(JavaParser.fromJavaVersion().classpath(JavaParser.runtimeClasspath())) |
| 44 | + .build(); |
| 45 | + |
| 46 | + final JavaTemplate after = JavaTemplate |
| 47 | + .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});") |
| 48 | + .imports("org.junit.jupiter.api.Assertions") |
| 49 | + .javaParser(JupiterJavaParser.get()).build(); |
| 50 | + |
| 51 | + final JavaTemplate afterWithMsg = JavaTemplate |
| 52 | + .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});") |
| 53 | + .imports("org.junit.jupiter.api.Assertions") |
| 54 | + .javaParser(JupiterJavaParser.get()).build(); |
| 55 | + |
| 56 | + @Override |
| 57 | + public J visitMethodInvocation(J.MethodInvocation elem, ExecutionContext ctx) { |
| 58 | + JavaTemplate.Matcher matcher; |
| 59 | + if ((matcher = before.apply("float").matcher(getCursor())).find() |
| 60 | + || (matcher = before.apply("double").matcher(getCursor())).find()) |
| 61 | + { |
| 62 | + return embed(ctx, after.apply( |
| 63 | + getCursor(), |
| 64 | + elem.getCoordinates().replace(), |
| 65 | + matcher.parameter(1), |
| 66 | + matcher.parameter(0), |
| 67 | + matcher.parameter(2)) |
| 68 | + ); |
| 69 | + } |
| 70 | + if ((matcher = beforeWithMsg.apply("float").matcher(getCursor())).find() |
| 71 | + || (matcher = beforeWithMsg.apply("double").matcher(getCursor())).find()) |
| 72 | + { |
| 73 | + return embed(ctx, afterWithMsg.apply( |
| 74 | + getCursor(), |
| 75 | + elem.getCoordinates().replace(), |
| 76 | + matcher.parameter(1), |
| 77 | + matcher.parameter(0), |
| 78 | + matcher.parameter(2), |
| 79 | + matcher.parameter(3) |
| 80 | + )); |
| 81 | + } |
| 82 | + return super.visitMethodInvocation(elem, ctx); |
| 83 | + } |
| 84 | + |
| 85 | + private J embed(ExecutionContext ctx, J j) { |
| 86 | + maybeRemoveImport("org.testng.Assert"); |
| 87 | + maybeAddImport("org.junit.jupiter.api.Assertions"); |
| 88 | + return super.embed(j, getCursor(), ctx, SHORTEN_NAMES, SIMPLIFY_BOOLEANS); |
| 89 | + } |
| 90 | + }; |
| 91 | + |
| 92 | + return Preconditions.check( |
| 93 | + Preconditions.and( |
| 94 | + new UsesType<>("org.testng.Assert", true), |
| 95 | + new UsesMethod<>("org.testng.Assert assertEquals(..)") |
| 96 | + ), |
| 97 | + javaVisitor |
| 98 | + ); |
| 99 | + } |
| 100 | +} |
0 commit comments