Skip to content

Commit

Permalink
BooleanExpressionRule fixes (#1295)
Browse files Browse the repository at this point in the history
### What's done:
* fixed substitution issue in boolean expression rule + preserve order in variables
* supported negative expression and additional substitution issue (replacement of sub-word)

Co-authored-by: Andrey Kuleshov <andrewkuleshov7@gmail.com>
  • Loading branch information
nulls and orchestr7 authored May 25, 2022
1 parent 7f116ac commit 4656b80
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 47 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,22 @@ class BooleanExpressionsRuleFixTest : FixTestBase("test/paragraph3/boolean_expre
fun `check same expressions`() {
fixAndCompare("SameExpressionsInConditionExpected.kt", "SameExpressionsInConditionTest.kt")
}

@Test
@Tag(WarningNames.COMPLEX_BOOLEAN_EXPRESSION)
fun `check substitution works properly`() {
fixAndCompare("SubstitutionIssueExpected.kt", "SubstitutionIssueTest.kt")
}

@Test
@Tag(WarningNames.COMPLEX_BOOLEAN_EXPRESSION)
fun `check ordering is persisted`() {
fixAndCompare("OrderIssueExpected.kt", "OrderIssueTest.kt")
}

@Test
@Tag(WarningNames.COMPLEX_BOOLEAN_EXPRESSION)
fun `check handling of negative expression`() {
fixAndCompare("NegativeExpressionExpected.kt", "NegativeExpressionTest.kt")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class BooleanExpressionsRuleWarnTest : LintTestBase(::BooleanExpressionsRule) {
// nested boolean expressions in lambdas
if (currentProperty.nextSibling { it.elementType == PROPERTY } == nextProperty) {}
if (!(rightSide == null || leftSide == null) &&
if (rightSide != null && leftSide != null &&
rightSide.size == leftSide.size &&
rightSide.zip(leftSide).all { (first, second) -> first.text == second.text }) {}
Expand Down Expand Up @@ -283,10 +283,11 @@ class BooleanExpressionsRuleWarnTest : LintTestBase(::BooleanExpressionsRule) {
val stream = ByteArrayOutputStream()
System.setErr(PrintStream(stream))
val node = KotlinParser().createNode(expression)
val map: java.util.HashMap<String, Char> = HashMap()
val result = BooleanExpressionsRule(emptyList()).formatBooleanExpressionAsString(node, map)
val rule = BooleanExpressionsRule(emptyList())
val map: BooleanExpressionsRule.ExpressionsReplacement = rule.ExpressionsReplacement()
val result = rule.formatBooleanExpressionAsString(node, map)
Assertions.assertEquals(expectedRepresentation, result)
Assertions.assertEquals(expectedMapSize, map.size)
Assertions.assertEquals(expectedMapSize, map.size())
System.setErr(System.err)
val stderr = stream.toString()
Assertions.assertTrue(stderr.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package test.paragraph3.boolean_expressions

fun foo() {
if (bar) {
}
if (bar) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package test.paragraph3.boolean_expressions

fun foo() {
if (bar && (!isEmpty() || isEmpty())) {
}
if (bar && (isEmpty() || !isEmpty())) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package test.paragraph3.boolean_expressions

fun foo() {
if (isB && isA && isC) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package test.paragraph3.boolean_expressions

fun foo() {
if (isB && isA && isC && (isEmpty() || !isEmpty())) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package test.paragraph3.boolean_expressions

fun foo() {
if (isABC_A && isABC_B && isABC_C) {
}
if (isAdd && isAdded()) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package test.paragraph3.boolean_expressions

fun foo() {
if (isABC_A && isABC_B && isABC_C && (isEmpty() || !isEmpty())) {
}
if (isAdd && isAdded() && (isEmpty() || !isEmpty())) {
}
}

0 comments on commit 4656b80

Please sign in to comment.