Merged AssertThatObjectIsNull and AssertThatObjectIsNotNull to AssertThatObjectIsNullOrNotNull.

Extended tests to be more explicit regarding the expected quick fix messages.
This commit is contained in:
2019-04-06 21:29:18 +02:00
parent ba56325299
commit 51703e8499
19 changed files with 54 additions and 109 deletions
+2 -4
View File
@@ -34,13 +34,11 @@ The plugin also supports the conversion of the most common JUnit 4 assertions to
## Implemented inspections ## Implemented inspections
- AssertThatObjectIsNull - AssertThatObjectIsNullOrNotNull
``` ```
from: assertThat(object).isEqualTo(null); from: assertThat(object).isEqualTo(null);
to: assertThat(object).isNull(); to: assertThat(object).isNull();
```
- AssertThatObjectIsNotNull
```
from: assertThat(object).isNotEqualTo(null); from: assertThat(object).isNotEqualTo(null);
to: assertThat(object).isNotNull(); to: assertThat(object).isNotNull();
``` ```
+1 -1
View File
@@ -43,7 +43,7 @@ patchPluginXml {
changeNotes """ changeNotes """
<h4>V0.3 (xx-Apr-19)</h4> <h4>V0.3 (xx-Apr-19)</h4>
<ul> <ul>
<li>Internally: Upgraded to AssertJ 13.2.2. <li>Merged AssertThatObjectIsNull and AssertThatObjectIsNotNull to AssertThatObjectIsNullOrNotNull.
<li>Support for hasSizeLessThan(), hasSizeLessThanOrEqualTo(), hasSizeGreaterThanOrEqualTo(), and hasSizeGreaterThan() for AssertThatSizeInspection (with AssertJ >=13.2.0). <li>Support for hasSizeLessThan(), hasSizeLessThanOrEqualTo(), hasSizeGreaterThanOrEqualTo(), and hasSizeGreaterThan() for AssertThatSizeInspection (with AssertJ >=13.2.0).
<li>Really fixed highlighting for JUnit conversion. Sorry. <li>Really fixed highlighting for JUnit conversion. Sorry.
</ul> </ul>
@@ -1,31 +0,0 @@
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
class AssertThatObjectIsNullInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting null"
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
if (!IS_EQUAL_TO_OBJECT.test(expression)) {
return
}
if (expression.argumentList.expressions[0].type == PsiType.NULL) {
registerSimplifyMethod(holder, expression, "isNull()")
}
}
}
}
}
@@ -6,10 +6,10 @@ import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiType import com.intellij.psi.PsiType
class AssertThatObjectIsNotNullInspection : AbstractAssertJInspection() { class AssertThatObjectIsNullOrNotNullInspection : AbstractAssertJInspection() {
companion object { companion object {
private const val DISPLAY_NAME = "Asserting non-null" private const val DISPLAY_NAME = "Asserting null or not-null"
} }
override fun getDisplayName() = DISPLAY_NAME override fun getDisplayName() = DISPLAY_NAME
@@ -18,12 +18,14 @@ class AssertThatObjectIsNotNullInspection : AbstractAssertJInspection() {
return object : JavaElementVisitor() { return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression) super.visitMethodCallExpression(expression)
if (!IS_NOT_EQUAL_TO_OBJECT.test(expression)) { val isNotEqualTo = IS_NOT_EQUAL_TO_OBJECT.test(expression)
val isEqualTo = IS_EQUAL_TO_OBJECT.test(expression)
if (!(isEqualTo || isNotEqualTo)) {
return return
} }
if (expression.argumentList.expressions[0].type == PsiType.NULL) { if (expression.argumentList.expressions[0].type == PsiType.NULL) {
registerSimplifyMethod(holder, expression, "isNotNull()") registerSimplifyMethod(holder, expression, if (isEqualTo) "isNull()" else "isNotNull()")
} }
} }
} }
+2 -4
View File
@@ -19,10 +19,8 @@
<depends>com.intellij.modules.java</depends> <depends>com.intellij.modules.java</depends>
<extensions defaultExtensionNs="com.intellij"> <extensions defaultExtensionNs="com.intellij">
<localInspection groupPath="Java" shortName="AssertThatObjectIsNull" enabledByDefault="true" level="WARNING" <localInspection groupPath="Java" shortName="AssertThatObjectIsNullOrNotNull" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatObjectIsNullInspection"/> implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatObjectIsNullOrNotNullInspection"/>
<localInspection groupPath="Java" shortName="AssertThatObjectIsNotNull" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatObjectIsNotNullInspection"/>
<localInspection groupPath="Java" shortName="AssertThatBooleanIsTrueOrFalse" enabledByDefault="true" <localInspection groupPath="Java" shortName="AssertThatBooleanIsTrueOrFalse" enabledByDefault="true"
level="WARNING" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBooleanIsTrueOrFalseInspection"/> implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBooleanIsTrueOrFalseInspection"/>
@@ -1,6 +0,0 @@
<html>
<body>
Turns assertThat(object).isNotEqualTo(null) into assertThat(object).isNotNull().
<!-- tooltip end -->
</body>
</html>
@@ -1,6 +0,0 @@
<html>
<body>
Turns assertThat(object).isEqualTo(null) into assertThat(object).isNull().
<!-- tooltip end -->
</body>
</html>
@@ -0,0 +1,6 @@
<html>
<body>
Turns assertThat(object).is(Not)EqualTo(null) into assertThat(object).is(Not)Null().
<!-- tooltip end -->
</body>
</html>
@@ -61,7 +61,7 @@ abstract class AbstractCajonTest {
protected fun executeQuickFixes(myFixture: JavaCodeInsightTestFixture, regex: Regex, expectedFixes: Int) { protected fun executeQuickFixes(myFixture: JavaCodeInsightTestFixture, regex: Regex, expectedFixes: Int) {
val quickfixes = myFixture.getAllQuickFixes().filter { it.familyName.matches(regex) } val quickfixes = myFixture.getAllQuickFixes().filter { it.familyName.matches(regex) }
assertThat(quickfixes).hasSize(expectedFixes) assertThat(quickfixes).`as`("Fixes matched by $regex: ${myFixture.getAllQuickFixes().map { it.familyName }}").hasSize(expectedFixes)
quickfixes.forEach(myFixture::launchAction) quickfixes.forEach(myFixture::launchAction)
} }
} }
@@ -14,7 +14,10 @@ internal class AssertThatBooleanIsTrueOrFalseInspectionTest : AbstractCajonTest(
runTest { runTest {
myFixture.enableInspections(AssertThatBooleanIsTrueOrFalseInspection::class.java) myFixture.enableInspections(AssertThatBooleanIsTrueOrFalseInspection::class.java)
myFixture.configureByFile("BooleanIsTrueOrFalseBefore.java") myFixture.configureByFile("BooleanIsTrueOrFalseBefore.java")
executeQuickFixes(myFixture, Regex("Replace is.*"), 17) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isTrue()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isFalse()"), 5)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isTrue()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isFalse()"), 4)
myFixture.checkResultByFile("BooleanIsTrueOrFalseAfter.java") myFixture.checkResultByFile("BooleanIsTrueOrFalseAfter.java")
} }
} }
@@ -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("Replace hasSize.*"), 4) executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSize() with isEmpty()"), 4)
myFixture.checkResultByFile("EnumerableIsEmptyAfter.java") myFixture.checkResultByFile("EnumerableIsEmptyAfter.java")
} }
} }
@@ -1,21 +0,0 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import de.platon42.intellij.jupiter.MyFixture
import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test
internal class AssertThatObjectIsNotNullInspectionTest : AbstractCajonTest() {
@Test
@TestDataSubPath("inspections/ObjectIsNotNull")
internal fun assertThat_with_isNotEqualTo_null_can_use_isNotNull(@MyFixture myFixture: JavaCodeInsightTestFixture) {
runTest {
myFixture.enableInspections(AssertThatObjectIsNotNullInspection::class.java)
myFixture.configureByFile("ObjectIsNotNullBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isNotNull()"), 3)
myFixture.checkResultByFile("ObjectIsNotNullAfter.java")
}
}
}
@@ -6,16 +6,17 @@ import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
internal class AssertThatObjectIsNullInspectionTest : AbstractCajonTest() { internal class AssertThatObjectIsNullOrNotNullInspectionTest : AbstractCajonTest() {
@Test @Test
@TestDataSubPath("inspections/ObjectIsNull") @TestDataSubPath("inspections/ObjectIsNullOrNotNull")
internal fun assertThat_with_isEqualTo_null_can_use_isNull(@MyFixture myFixture: JavaCodeInsightTestFixture) { internal fun assertThat_with_isEqualTo_null_can_use_isNull(@MyFixture myFixture: JavaCodeInsightTestFixture) {
runTest { runTest {
myFixture.enableInspections(AssertThatObjectIsNullInspection::class.java) myFixture.enableInspections(AssertThatObjectIsNullOrNotNullInspection::class.java)
myFixture.configureByFile("ObjectIsNullBefore.java") myFixture.configureByFile("ObjectIsNullOrNotNullBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isNull()"), 3) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isNull()"), 3)
myFixture.checkResultByFile("ObjectIsNullAfter.java") executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isNotNull()"), 3)
myFixture.checkResultByFile("ObjectIsNullOrNotNullAfter.java")
} }
} }
} }
@@ -14,7 +14,19 @@ internal class AssertThatSizeInspectionTest : AbstractCajonTest() {
runTest { runTest {
myFixture.enableInspections(AssertThatSizeInspection::class.java) myFixture.enableInspections(AssertThatSizeInspection::class.java)
myFixture.configureByFile("AssertThatSizeBefore.java") myFixture.configureByFile("AssertThatSizeBefore.java")
executeQuickFixes(myFixture, Regex("Replace .*"), 28) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isEmpty()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isZero() with isEmpty()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotZero() with isNotEmpty()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThan() with isNotEmpty()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThanOrEqualTo() with isNotEmpty()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThan() with isEmpty()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThanOrEqualTo() with isEmpty()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with hasSameSizeAs()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with hasSize()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThan() with hasSizeGreaterThan()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThanOrEqualTo() with hasSizeGreaterThanOrEqualTo()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThan() with hasSizeLessThan()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThanOrEqualTo() with hasSizeLessThanOrEqualTo()"), 2)
myFixture.checkResultByFile("AssertThatSizeAfter.java") myFixture.checkResultByFile("AssertThatSizeAfter.java")
} }
} }
@@ -14,7 +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("Replace .*"), 4) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isEmpty()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSize() with isEmpty()"), 2)
myFixture.checkResultByFile("StringIsEmptyAfter.java") myFixture.checkResultByFile("StringIsEmptyAfter.java")
} }
} }
@@ -1,10 +0,0 @@
import static org.assertj.core.api.Assertions.assertThat;
public class ObjectIsNotNull {
private void objectIsNotNull() {
assertThat("").isNotNull();
assertThat("").as("nah").isNotNull();
assertThat(new Object).isNotNull();
}
}
@@ -1,10 +0,0 @@
import static org.assertj.core.api.Assertions.assertThat;
public class ObjectIsNotNull {
private void objectIsNotNull() {
assertThat("").isNotEqualTo(null);
assertThat("").as("nah").isNotEqualTo(null);
assertThat(new Object).isNotEqualTo(null);
}
}
@@ -5,6 +5,10 @@ public class ObjectIsNull {
private void objectIsNull() { private void objectIsNull() {
assertThat("").isNull(); assertThat("").isNull();
assertThat("").as("nah").isNull(); assertThat("").as("nah").isNull();
assertThat(new Object).isNull(); assertThat(new Object()).isNull();
assertThat("").isNotNull();
assertThat("").as("nah").isNotNull();
assertThat(new Object()).isNotNull();
} }
} }
@@ -5,6 +5,10 @@ public class ObjectIsNull {
private void objectIsNull() { private void objectIsNull() {
assertThat("").isEqualTo(null); assertThat("").isEqualTo(null);
assertThat("").as("nah").isEqualTo(null); assertThat("").as("nah").isEqualTo(null);
assertThat(new Object).isEqualTo(null); assertThat(new Object()).isEqualTo(null);
assertThat("").isNotEqualTo(null);
assertThat("").as("nah").isNotEqualTo(null);
assertThat(new Object()).isNotEqualTo(null);
} }
} }