Fixed isEmpty() for enumerables and strings and isNull() for object conversions to be applied only if it is the terminal method call as isEmpty() and isNull() return void.

This commit is contained in:
Chris Hodges 2019-05-01 13:07:05 +02:00
parent e55acf9c74
commit 362c4210a5
14 changed files with 27 additions and 11 deletions

View File

@ -345,6 +345,7 @@ Feel free to use the code (in package de.platon42.intellij.jupiter) for your pro
#### V0.8 (unreleased) #### V0.8 (unreleased)
- Fixed missing description for JoinAssertThatStatements and detection of equivalent expressions (sorry, released it too hastily). - Fixed missing description for JoinAssertThatStatements and detection of equivalent expressions (sorry, released it too hastily).
- Fixed ```isEmpty()``` for enumerables and strings and ```isNull()``` for object conversions to be applied only if it is the terminal method call as ```isEmpty()``` and ```isNull()``` return void.
#### V0.7 (28-Apr-19) #### V0.7 (28-Apr-19)
- Another fix for AssertThatGuavaOptional inspection regarding using the same family name for slightly different quick fix executions - Another fix for AssertThatGuavaOptional inspection regarding using the same family name for slightly different quick fix executions

View File

@ -43,6 +43,7 @@ patchPluginXml {
<h4>V0.8 (unreleased)</h4> <h4>V0.8 (unreleased)</h4>
<ul> <ul>
<li>Fixed missing description for JoinAssertThatStatements and detection of equivalent expressions (sorry, released it too hastily). <li>Fixed missing description for JoinAssertThatStatements and detection of equivalent expressions (sorry, released it too hastily).
<li>Fixed isEmpty() for enumerables and strings and isNull() for object conversions to be applied only if it is the terminal method call as isEmpty() and isNull() return void.
</ul> </ul>
<h4>V0.7 (28-Apr-19)</h4> <h4>V0.7 (28-Apr-19)</h4>
<ul> <ul>

View File

@ -4,6 +4,7 @@ import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.JavaElementVisitor import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiStatement
import de.platon42.intellij.plugins.cajon.MethodNames import de.platon42.intellij.plugins.cajon.MethodNames
class AssertThatEnumerableIsEmptyInspection : AbstractAssertJInspection() { class AssertThatEnumerableIsEmptyInspection : AbstractAssertJInspection() {
@ -18,7 +19,8 @@ class AssertThatEnumerableIsEmptyInspection : AbstractAssertJInspection() {
return object : JavaElementVisitor() { return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression) super.visitMethodCallExpression(expression)
if (!HAS_SIZE.test(expression)) { val isLastExpression = expression.parent is PsiStatement
if (!(HAS_SIZE.test(expression) && isLastExpression)) {
return return
} }

View File

@ -1,10 +1,7 @@
package de.platon42.intellij.plugins.cajon.inspections package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.JavaElementVisitor import com.intellij.psi.*
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiType
import de.platon42.intellij.plugins.cajon.MethodNames import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.firstArg import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.map import de.platon42.intellij.plugins.cajon.map
@ -23,7 +20,8 @@ class AssertThatObjectIsNullOrNotNullInspection : AbstractAssertJInspection() {
super.visitMethodCallExpression(expression) super.visitMethodCallExpression(expression)
val isNotEqualTo = IS_NOT_EQUAL_TO_OBJECT.test(expression) val isNotEqualTo = IS_NOT_EQUAL_TO_OBJECT.test(expression)
val isEqualTo = IS_EQUAL_TO_OBJECT.test(expression) val isEqualTo = IS_EQUAL_TO_OBJECT.test(expression)
if (!(isEqualTo || isNotEqualTo)) { val isLastExpression = expression.parent is PsiStatement
if (!((isEqualTo && isLastExpression) || isNotEqualTo)) {
return return
} }

View File

@ -4,6 +4,7 @@ import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.JavaElementVisitor import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiStatement
import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME import de.platon42.intellij.plugins.cajon.AssertJClassNames.Companion.ABSTRACT_CHAR_SEQUENCE_ASSERT_CLASSNAME
import de.platon42.intellij.plugins.cajon.MethodNames import de.platon42.intellij.plugins.cajon.MethodNames
@ -21,7 +22,8 @@ class AssertThatStringIsEmptyInspection : AbstractAssertJInspection() {
super.visitMethodCallExpression(expression) super.visitMethodCallExpression(expression)
val isEqual = IS_EQUAL_TO_OBJECT.test(expression) val isEqual = IS_EQUAL_TO_OBJECT.test(expression)
val hasSize = HAS_SIZE.test(expression) val hasSize = HAS_SIZE.test(expression)
if (!(isEqual || hasSize)) { val isLastExpression = expression.parent is PsiStatement
if (!((isEqual || hasSize) && isLastExpression)) {
return return
} }

View File

@ -14,7 +14,7 @@ internal class AssertThatEnumerableIsEmptyInspectionTest : AbstractCajonTest() {
runTest { runTest {
myFixture.enableInspections(AssertThatEnumerableIsEmptyInspection::class.java) myFixture.enableInspections(AssertThatEnumerableIsEmptyInspection::class.java)
myFixture.configureByFile("EnumerableIsEmptyBefore.java") myFixture.configureByFile("EnumerableIsEmptyBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSize() with isEmpty()"), 4) executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSize() with isEmpty()"), 5)
myFixture.checkResultByFile("EnumerableIsEmptyAfter.java") myFixture.checkResultByFile("EnumerableIsEmptyAfter.java")
} }
} }

View File

@ -15,7 +15,7 @@ internal class AssertThatObjectIsNullOrNotNullInspectionTest : AbstractCajonTest
myFixture.enableInspections(AssertThatObjectIsNullOrNotNullInspection::class.java) myFixture.enableInspections(AssertThatObjectIsNullOrNotNullInspection::class.java)
myFixture.configureByFile("ObjectIsNullOrNotNullBefore.java") myFixture.configureByFile("ObjectIsNullOrNotNullBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isNull()"), 4) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isNull()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isNotNull()"), 4) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isNotNull()"), 5)
myFixture.checkResultByFile("ObjectIsNullOrNotNullAfter.java") myFixture.checkResultByFile("ObjectIsNullOrNotNullAfter.java")
} }
} }

View File

@ -14,8 +14,8 @@ internal class AssertThatStringIsEmptyInspectionTest : AbstractCajonTest() {
runTest { runTest {
myFixture.enableInspections(AssertThatStringIsEmptyInspection::class.java) myFixture.enableInspections(AssertThatStringIsEmptyInspection::class.java)
myFixture.configureByFile("StringIsEmptyBefore.java") myFixture.configureByFile("StringIsEmptyBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isEmpty()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isEmpty()"), 3)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSize() with isEmpty()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSize() with isEmpty()"), 3)
myFixture.checkResultByFile("StringIsEmptyAfter.java") myFixture.checkResultByFile("StringIsEmptyAfter.java")
} }
} }

View File

@ -14,5 +14,7 @@ public class EnumerableIsEmpty {
assertThat(new StringBuilder()).as("bar").hasSize(1); assertThat(new StringBuilder()).as("bar").hasSize(1);
assertThat(new ArrayList<Long>()).as("etc").hasSize(1); assertThat(new ArrayList<Long>()).as("etc").hasSize(1);
assertThat(new Long[1]).as("etc").hasSize(1); assertThat(new Long[1]).as("etc").hasSize(1);
assertThat("string").as("foo").hasSize(0).hasSameSizeAs("foo").isEmpty();
} }
} }

View File

@ -14,5 +14,7 @@ public class EnumerableIsEmpty {
assertThat(new StringBuilder()).as("bar").hasSize(1); assertThat(new StringBuilder()).as("bar").hasSize(1);
assertThat(new ArrayList<Long>()).as("etc").hasSize(1); assertThat(new ArrayList<Long>()).as("etc").hasSize(1);
assertThat(new Long[1]).as("etc").hasSize(1); assertThat(new Long[1]).as("etc").hasSize(1);
assertThat("string").as("foo").hasSize(0).hasSameSizeAs("foo").hasSize(0);
} }
} }

View File

@ -12,5 +12,6 @@ public class ObjectIsNull {
assertThat(new Object()).isNotNull(); assertThat(new Object()).isNotNull();
assertThat(new Object()).as("foo").isNotNull().as("bar").isEqualTo(new Object()).as("etc").isNull(); assertThat(new Object()).as("foo").isNotNull().as("bar").isEqualTo(new Object()).as("etc").isNull();
assertThat(new Object()).as("foo").isEqualTo(null).as("bar").isEqualTo(new Object()).as("etc").isNotNull();
} }
} }

View File

@ -12,5 +12,6 @@ public class ObjectIsNull {
assertThat(new Object()).isNotEqualTo(null); assertThat(new Object()).isNotEqualTo(null);
assertThat(new Object()).as("foo").isNotEqualTo(null).as("bar").isEqualTo(new Object()).as("etc").isEqualTo(null); assertThat(new Object()).as("foo").isNotEqualTo(null).as("bar").isEqualTo(new Object()).as("etc").isEqualTo(null);
assertThat(new Object()).as("foo").isEqualTo(null).as("bar").isEqualTo(new Object()).as("etc").isNotEqualTo(null);
} }
} }

View File

@ -15,5 +15,8 @@ public class StringIsEmpty {
assertThat(stringBuilder).as("bar").isEmpty(); assertThat(stringBuilder).as("bar").isEmpty();
assertThat(new Object()).isEqualTo(""); assertThat(new Object()).isEqualTo("");
assertThat(string).as("foo").isEqualTo("").as("bar").hasSize(0).hasSameSizeAs("foo").isEmpty();
assertThat(string).as("foo").isEqualTo("").as("bar").hasSize(0).hasSameSizeAs("foo").isEmpty();
} }
} }

View File

@ -15,5 +15,8 @@ public class StringIsEmpty {
assertThat(stringBuilder).as("bar").hasSize(0); assertThat(stringBuilder).as("bar").hasSize(0);
assertThat(new Object()).isEqualTo(""); assertThat(new Object()).isEqualTo("");
assertThat(string).as("foo").isEqualTo("").as("bar").hasSize(0).hasSameSizeAs("foo").isEqualTo("");
assertThat(string).as("foo").isEqualTo("").as("bar").hasSize(0).hasSameSizeAs("foo").hasSize(0);
} }
} }