diff --git a/README.md b/README.md
index 0a341dd..bf58809 100644
--- a/README.md
+++ b/README.md
@@ -345,6 +345,7 @@ Feel free to use the code (in package de.platon42.intellij.jupiter) for your pro
#### V0.8 (unreleased)
- 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)
- Another fix for AssertThatGuavaOptional inspection regarding using the same family name for slightly different quick fix executions
diff --git a/build.gradle b/build.gradle
index 097d173..8fc348a 100644
--- a/build.gradle
+++ b/build.gradle
@@ -43,6 +43,7 @@ patchPluginXml {
V0.8 (unreleased)
- 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)
diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatEnumerableIsEmptyInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatEnumerableIsEmptyInspection.kt
index 8d14403..594e16f 100644
--- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatEnumerableIsEmptyInspection.kt
+++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatEnumerableIsEmptyInspection.kt
@@ -4,6 +4,7 @@ import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression
+import com.intellij.psi.PsiStatement
import de.platon42.intellij.plugins.cajon.MethodNames
class AssertThatEnumerableIsEmptyInspection : AbstractAssertJInspection() {
@@ -18,7 +19,8 @@ class AssertThatEnumerableIsEmptyInspection : AbstractAssertJInspection() {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
- if (!HAS_SIZE.test(expression)) {
+ val isLastExpression = expression.parent is PsiStatement
+ if (!(HAS_SIZE.test(expression) && isLastExpression)) {
return
}
diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspection.kt
index fb96191..30296e8 100644
--- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspection.kt
+++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspection.kt
@@ -1,10 +1,7 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemsHolder
-import com.intellij.psi.JavaElementVisitor
-import com.intellij.psi.PsiElementVisitor
-import com.intellij.psi.PsiMethodCallExpression
-import com.intellij.psi.PsiType
+import com.intellij.psi.*
import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.map
@@ -23,7 +20,8 @@ class AssertThatObjectIsNullOrNotNullInspection : AbstractAssertJInspection() {
super.visitMethodCallExpression(expression)
val isNotEqualTo = IS_NOT_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
}
diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringIsEmptyInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringIsEmptyInspection.kt
index 48176dd..ea53105 100644
--- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringIsEmptyInspection.kt
+++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringIsEmptyInspection.kt
@@ -4,6 +4,7 @@ import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
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.MethodNames
@@ -21,7 +22,8 @@ class AssertThatStringIsEmptyInspection : AbstractAssertJInspection() {
super.visitMethodCallExpression(expression)
val isEqual = IS_EQUAL_TO_OBJECT.test(expression)
val hasSize = HAS_SIZE.test(expression)
- if (!(isEqual || hasSize)) {
+ val isLastExpression = expression.parent is PsiStatement
+ if (!((isEqual || hasSize) && isLastExpression)) {
return
}
diff --git a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatEnumerableIsEmptyInspectionTest.kt b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatEnumerableIsEmptyInspectionTest.kt
index e350fb9..83c2efa 100644
--- a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatEnumerableIsEmptyInspectionTest.kt
+++ b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatEnumerableIsEmptyInspectionTest.kt
@@ -14,7 +14,7 @@ internal class AssertThatEnumerableIsEmptyInspectionTest : AbstractCajonTest() {
runTest {
myFixture.enableInspections(AssertThatEnumerableIsEmptyInspection::class.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")
}
}
diff --git a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspectionTest.kt b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspectionTest.kt
index 5219eaf..f15b99f 100644
--- a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspectionTest.kt
+++ b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspectionTest.kt
@@ -15,7 +15,7 @@ internal class AssertThatObjectIsNullOrNotNullInspectionTest : AbstractCajonTest
myFixture.enableInspections(AssertThatObjectIsNullOrNotNullInspection::class.java)
myFixture.configureByFile("ObjectIsNullOrNotNullBefore.java")
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")
}
}
diff --git a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringIsEmptyInspectionTest.kt b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringIsEmptyInspectionTest.kt
index 1bc6803..0653423 100644
--- a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringIsEmptyInspectionTest.kt
+++ b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringIsEmptyInspectionTest.kt
@@ -14,8 +14,8 @@ internal class AssertThatStringIsEmptyInspectionTest : AbstractCajonTest() {
runTest {
myFixture.enableInspections(AssertThatStringIsEmptyInspection::class.java)
myFixture.configureByFile("StringIsEmptyBefore.java")
- executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isEmpty()"), 2)
- executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSize() with isEmpty()"), 2)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isEmpty()"), 3)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSize() with isEmpty()"), 3)
myFixture.checkResultByFile("StringIsEmptyAfter.java")
}
}
diff --git a/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyAfter.java b/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyAfter.java
index 607af6b..3531e5a 100644
--- a/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyAfter.java
+++ b/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyAfter.java
@@ -14,5 +14,7 @@ public class EnumerableIsEmpty {
assertThat(new StringBuilder()).as("bar").hasSize(1);
assertThat(new ArrayList()).as("etc").hasSize(1);
assertThat(new Long[1]).as("etc").hasSize(1);
+
+ assertThat("string").as("foo").hasSize(0).hasSameSizeAs("foo").isEmpty();
}
}
diff --git a/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyBefore.java b/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyBefore.java
index 3c91aca..e7fe848 100644
--- a/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyBefore.java
+++ b/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyBefore.java
@@ -14,5 +14,7 @@ public class EnumerableIsEmpty {
assertThat(new StringBuilder()).as("bar").hasSize(1);
assertThat(new ArrayList()).as("etc").hasSize(1);
assertThat(new Long[1]).as("etc").hasSize(1);
+
+ assertThat("string").as("foo").hasSize(0).hasSameSizeAs("foo").hasSize(0);
}
}
diff --git a/src/test/resources/inspections/ObjectIsNullOrNotNull/ObjectIsNullOrNotNullAfter.java b/src/test/resources/inspections/ObjectIsNullOrNotNull/ObjectIsNullOrNotNullAfter.java
index ae5598d..bf5de76 100644
--- a/src/test/resources/inspections/ObjectIsNullOrNotNull/ObjectIsNullOrNotNullAfter.java
+++ b/src/test/resources/inspections/ObjectIsNullOrNotNull/ObjectIsNullOrNotNullAfter.java
@@ -12,5 +12,6 @@ public class ObjectIsNull {
assertThat(new Object()).isNotNull();
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();
}
}
diff --git a/src/test/resources/inspections/ObjectIsNullOrNotNull/ObjectIsNullOrNotNullBefore.java b/src/test/resources/inspections/ObjectIsNullOrNotNull/ObjectIsNullOrNotNullBefore.java
index da35a8c..ec274b3 100644
--- a/src/test/resources/inspections/ObjectIsNullOrNotNull/ObjectIsNullOrNotNullBefore.java
+++ b/src/test/resources/inspections/ObjectIsNullOrNotNull/ObjectIsNullOrNotNullBefore.java
@@ -12,5 +12,6 @@ public class ObjectIsNull {
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").isEqualTo(null).as("bar").isEqualTo(new Object()).as("etc").isNotEqualTo(null);
}
}
diff --git a/src/test/resources/inspections/StringIsEmpty/StringIsEmptyAfter.java b/src/test/resources/inspections/StringIsEmpty/StringIsEmptyAfter.java
index 441fec0..bc12c9a 100644
--- a/src/test/resources/inspections/StringIsEmpty/StringIsEmptyAfter.java
+++ b/src/test/resources/inspections/StringIsEmpty/StringIsEmptyAfter.java
@@ -15,5 +15,8 @@ public class StringIsEmpty {
assertThat(stringBuilder).as("bar").isEmpty();
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();
}
}
diff --git a/src/test/resources/inspections/StringIsEmpty/StringIsEmptyBefore.java b/src/test/resources/inspections/StringIsEmpty/StringIsEmptyBefore.java
index 2fc9489..959452b 100644
--- a/src/test/resources/inspections/StringIsEmpty/StringIsEmptyBefore.java
+++ b/src/test/resources/inspections/StringIsEmpty/StringIsEmptyBefore.java
@@ -15,5 +15,8 @@ public class StringIsEmpty {
assertThat(stringBuilder).as("bar").hasSize(0);
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);
}
}