Skip to content

Commit

Permalink
Simplify boolean expressions comparing with null and isEmpty (#3489)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Aug 19, 2023
1 parent 1d2415b commit 8893935
Show file tree
Hide file tree
Showing 3 changed files with 283 additions and 140 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package org.openrewrite.java.cleanup;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
import org.openrewrite.test.RecipeSpec;
Expand All @@ -25,7 +27,7 @@
import static org.openrewrite.test.RewriteTest.toRecipe;

@SuppressWarnings("ALL")
class SimplifyBooleanExpressionTest implements RewriteTest {
class SimplifyBooleanExpressionVisitorTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
Expand Down Expand Up @@ -346,4 +348,74 @@ public class A {
)
);
}

@ParameterizedTest
@Issue("https://github.com/openrewrite/rewrite-templating/issues/28")
// Mimic what would be inserted by a Refaster template using two nullable parameters, with the second one a literal
@CsvSource(delimiterString = "//", textBlock = """
a == null || a.isEmpty() // a == null || a.isEmpty()
a == null || !a.isEmpty() // a == null || !a.isEmpty()
a != null && a.isEmpty() // a != null && a.isEmpty()
a != null && !a.isEmpty() // a != null && !a.isEmpty()
"" == null || "".isEmpty() // true
"" == null || !"".isEmpty() // false
"" != null && "".isEmpty() // true
"" != null && !"".isEmpty() // false
"b" == null || "b".isEmpty() // false
"b" == null || !"b".isEmpty() // true
"b" != null && "b".isEmpty() // false
"b" != null && !"b".isEmpty() // true
a == null || a.isEmpty() || "" == null || "".isEmpty() // true
a == null || a.isEmpty() || "" == null || !"".isEmpty() // a == null || a.isEmpty()
a == null || a.isEmpty() || "" != null && "".isEmpty() // true
a == null || a.isEmpty() || "" != null && !"".isEmpty() // a == null || a.isEmpty()
a == null || a.isEmpty() && "" == null || "".isEmpty() // true
a == null || a.isEmpty() && "" == null || !"".isEmpty() // a == null
a == null || a.isEmpty() && "" != null && "".isEmpty() // a == null || a.isEmpty()
a == null || a.isEmpty() && "" != null && !"".isEmpty() // a == null
a == null || !a.isEmpty() || "" == null || "".isEmpty() // true
a == null || !a.isEmpty() || "" == null || !"".isEmpty() // a == null || !a.isEmpty()
a == null || !a.isEmpty() || "" != null && "".isEmpty() // true
a == null || !a.isEmpty() || "" != null && !"".isEmpty() // a == null || !a.isEmpty()
a == null || !a.isEmpty() && "" == null || "".isEmpty() // true
a == null || !a.isEmpty() && "" == null || !"".isEmpty() // a == null
a == null || !a.isEmpty() && "" != null && "".isEmpty() // a == null || !a.isEmpty()
a == null || !a.isEmpty() && "" != null && !"".isEmpty() // a == null
a == null || a.isEmpty() || "b" == null || "b".isEmpty() // a == null || a.isEmpty()
a == null || a.isEmpty() || "b" == null || !"b".isEmpty() // true
a == null || a.isEmpty() || "b" != null && "b".isEmpty() // a == null || a.isEmpty()
a == null || a.isEmpty() || "b" != null && !"b".isEmpty() // true
a == null || a.isEmpty() && "b" == null || "b".isEmpty() // a == null
a == null || a.isEmpty() && "b" == null || !"b".isEmpty() // true
a == null || a.isEmpty() && "b" != null && "b".isEmpty() // a == null
a == null || a.isEmpty() && "b" != null && !"b".isEmpty() // a == null || a.isEmpty()
a == null || !a.isEmpty() || "b" == null || "b".isEmpty() // a == null || !a.isEmpty()
a == null || !a.isEmpty() || "b" == null || !"b".isEmpty() // true
a == null || !a.isEmpty() || "b" != null && "b".isEmpty() // a == null || !a.isEmpty()
a == null || !a.isEmpty() || "b" != null && !"b".isEmpty() // true
a == null || !a.isEmpty() && "b" == null || "b".isEmpty() // a == null
a == null || !a.isEmpty() && "b" == null || !"b".isEmpty() // true
a == null || !a.isEmpty() && "b" != null && "b".isEmpty() // a == null
a == null || !a.isEmpty() && "b" != null && !"b".isEmpty() // a == null || !a.isEmpty()
""")
void simplifyLiteralNull(String before, String after) {
//language=java
String template = """
class A {
void foo(String a) {
boolean c = %s;
}
}
""";
String beforeJava = template.formatted(before);
if (before.equals(after)) {
rewriteRun(java(beforeJava));
} else {
rewriteRun(java(beforeJava, template.formatted(after)));
}
}
}
Loading

0 comments on commit 8893935

Please sign in to comment.