Skip to content

Commit

Permalink
Merge branch 'main' into datafaker-migration
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Mar 12, 2024
2 parents adb8eba + 71a71c8 commit fad1832
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public String getDisplayName() {

@Override
public String getDescription() {
return "Assertions such as `org.junit.Assert.assertEquals` expect the first argument to be the expected value and the second argument to be the actual value; for `org.testng.Assert`, it’s the other way around. This recipe detects `J.Literal`, `J.NewArray`, and `java.util.Iterable` arguments swapping them if necessary so that the error messages will be confusing.";
return "Assertions such as `org.junit.Assert.assertEquals` expect the first argument to be the expected value and the second argument to be the actual value; for `org.testng.Assert`, it’s the other way around. This recipe detects `J.Literal`, `J.NewArray`, and `java.util.Iterable` arguments swapping them if necessary so that the error messages won't be confusing.";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public String getDescription() {
return "Migration of JUnit4 (or potentially JUnit5) test case in form of assertTrue(x instanceof y) to assertInstanceOf(y.class, x).";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {

Expand All @@ -56,7 +57,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
reason = null;
} else if (mi.getArguments().size() == 2) {
reason = mi.getArguments().get(1);
} else return mi;
} else {
return mi;
}

if (argument instanceof J.InstanceOf) {
J.InstanceOf instanceOf = (J.InstanceOf) argument;
Expand All @@ -74,7 +77,9 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
} else if (mi.getArguments().size() == 2) {
reason = mi.getArguments().get(0);
argument = mi.getArguments().get(1);
} else return mi;
} else {
return mi;
}

if (argument instanceof J.InstanceOf) {
J.InstanceOf instanceOf = (J.InstanceOf) argument;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ public class RemoveDuplicateTestTemplates extends Recipe {
private static final AnnotationMatcher TEST_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api.Test");
private static final AnnotationMatcher REPEATED_TEST_ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api.RepeatedTest");

@Override
public String getDisplayName() {
return "Remove duplicates uses of @TestTemplate implementations for a single method";
}

@Override
public String getDescription() {
return "Remove duplicates uses of @TestTemplate implementations for a single method.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
new UsesType<>("org.junit.jupiter.api.RepeatedTest", false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void test() {

String formattedAssertBefore = assertBefore.formatted(chainedAssertion, firstArg, assertToReplace, secondArg);

String finalArgument = firstArg.isEmpty() && !secondArg.equals("0") ? secondArg : firstArg;
String finalArgument = firstArg.isEmpty() && !"0".equals(secondArg) ? secondArg : firstArg;
finalArgument = finalArgument.contains(".") ? finalArgument.split("\\.")[0] : finalArgument;

String before = String.format(template, formattedAssertBefore);
Expand Down Expand Up @@ -180,7 +180,7 @@ void test() {

String formattedAssertBefore = assertBefore.formatted(chainedAssertion, firstArg, assertToReplace, secondArg);

String finalArgument = firstArg.equals("") && !secondArg.equals("0") ? secondArg : firstArg;
String finalArgument = "".equals(firstArg) && !"0".equals(secondArg) ? secondArg : firstArg;
finalArgument = finalArgument.contains(".") ? finalArgument.split("\\.")[0] : finalArgument;

String before = String.format(template, formattedAssertBefore);
Expand Down Expand Up @@ -226,7 +226,7 @@ void test() {

String formattedAssertBefore = assertBefore.formatted(chainedAssertion, firstArg, assertToReplace, secondArg);

String finalArgument = firstArg.equals("") && !secondArg.equals("0") ? secondArg : firstArg;
String finalArgument = "".equals(firstArg) && !"0".equals(secondArg) ? secondArg : firstArg;
finalArgument = finalArgument.contains(".") ? finalArgument.split("\\.")[0] : finalArgument;

String before = String.format(template, formattedAssertBefore);
Expand Down Expand Up @@ -273,7 +273,7 @@ void test(Collection<String> collection, Collection<String> otherCollection) {

String formattedAssertBefore = assertBefore.formatted(chainedAssertion, firstArg, assertToReplace, secondArg);

String finalArgument = firstArg.equals("") ? secondArg : firstArg;
String finalArgument = "".equals(firstArg) ? secondArg : firstArg;

String before = String.format(template, formattedAssertBefore);
String after = String.format(template, assertAfter.formatted(dedicatedAssertion, finalArgument));
Expand Down Expand Up @@ -318,14 +318,14 @@ void test() {
}
""";
String assertBefore = "assertThat(map.%s(%s)).%s(%s);";
String assertAfter = !firstArg.equals("") && !secondArg.equals("") ? "assertThat(map).%s(%s, %s);" : "assertThat(map).%s(%s);";
String assertAfter = !"".equals(firstArg) && !"".equals(secondArg) ? "assertThat(map).%s(%s, %s);" : "assertThat(map).%s(%s);";

String formattedAssertBefore = assertBefore.formatted(chainedAssertion, firstArg, assertToReplace, secondArg);
String before = String.format(template, formattedAssertBefore);

String finalArgument = firstArg.equals("") ? secondArg : firstArg;
String finalArgument = "".equals(firstArg) ? secondArg : firstArg;
List<String> formattedArgs = new ArrayList<>(Arrays.asList(dedicatedAssertion, finalArgument));
if (!firstArg.equals("") && !secondArg.equals("")) {
if (!"".equals(firstArg) && !"".equals(secondArg)) {
formattedArgs.add(secondArg);
}
String after = String.format(template, assertAfter.formatted(formattedArgs.toArray()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

@SuppressWarnings({"NumericOverflow", "divzero", "TryWithIdenticalCatches"})
class RemoveTryCatchFailBlocksTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion()
Expand Down

0 comments on commit fad1832

Please sign in to comment.