diff --git a/README.md b/README.md index f938c5b..716bc32 100644 --- a/README.md +++ b/README.md @@ -34,13 +34,11 @@ The plugin also supports the conversion of the most common JUnit 4 assertions to ## Implemented inspections -- AssertThatObjectIsNull +- AssertThatObjectIsNullOrNotNull ``` from: assertThat(object).isEqualTo(null); to: assertThat(object).isNull(); - ``` -- AssertThatObjectIsNotNull - ``` + from: assertThat(object).isNotEqualTo(null); to: assertThat(object).isNotNull(); ``` diff --git a/build.gradle b/build.gradle index 650a3b0..c5200b6 100644 --- a/build.gradle +++ b/build.gradle @@ -43,7 +43,7 @@ patchPluginXml { changeNotes """

V0.3 (xx-Apr-19)

diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullInspection.kt deleted file mode 100644 index b27f9fb..0000000 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullInspection.kt +++ /dev/null @@ -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()") - } - } - } - } -} \ No newline at end of file diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNotNullInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspection.kt similarity index 64% rename from src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNotNullInspection.kt rename to src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspection.kt index 1ad68d6..0e92e32 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNotNullInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspection.kt @@ -6,10 +6,10 @@ import com.intellij.psi.PsiElementVisitor import com.intellij.psi.PsiMethodCallExpression import com.intellij.psi.PsiType -class AssertThatObjectIsNotNullInspection : AbstractAssertJInspection() { +class AssertThatObjectIsNullOrNotNullInspection : AbstractAssertJInspection() { 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 @@ -18,12 +18,14 @@ class AssertThatObjectIsNotNullInspection : AbstractAssertJInspection() { return object : JavaElementVisitor() { override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { 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 } if (expression.argumentList.expressions[0].type == PsiType.NULL) { - registerSimplifyMethod(holder, expression, "isNotNull()") + registerSimplifyMethod(holder, expression, if (isEqualTo) "isNull()" else "isNotNull()") } } } diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 4ab41ea..cc1f669 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -19,10 +19,8 @@ com.intellij.modules.java - - + diff --git a/src/main/resources/inspectionDescriptions/AssertThatObjectIsNotNull.html b/src/main/resources/inspectionDescriptions/AssertThatObjectIsNotNull.html deleted file mode 100644 index d1d02f6..0000000 --- a/src/main/resources/inspectionDescriptions/AssertThatObjectIsNotNull.html +++ /dev/null @@ -1,6 +0,0 @@ - - -Turns assertThat(object).isNotEqualTo(null) into assertThat(object).isNotNull(). - - - \ No newline at end of file diff --git a/src/main/resources/inspectionDescriptions/AssertThatObjectIsNull.html b/src/main/resources/inspectionDescriptions/AssertThatObjectIsNull.html deleted file mode 100644 index 428f1b1..0000000 --- a/src/main/resources/inspectionDescriptions/AssertThatObjectIsNull.html +++ /dev/null @@ -1,6 +0,0 @@ - - -Turns assertThat(object).isEqualTo(null) into assertThat(object).isNull(). - - - \ No newline at end of file diff --git a/src/main/resources/inspectionDescriptions/AssertThatObjectIsNullOrNotNull.html b/src/main/resources/inspectionDescriptions/AssertThatObjectIsNullOrNotNull.html new file mode 100644 index 0000000..8ae765d --- /dev/null +++ b/src/main/resources/inspectionDescriptions/AssertThatObjectIsNullOrNotNull.html @@ -0,0 +1,6 @@ + + +Turns assertThat(object).is(Not)EqualTo(null) into assertThat(object).is(Not)Null(). + + + \ No newline at end of file diff --git a/src/test/java/de/platon42/intellij/plugins/cajon/AbstractCajonTest.kt b/src/test/java/de/platon42/intellij/plugins/cajon/AbstractCajonTest.kt index cb14ed9..19c70d3 100644 --- a/src/test/java/de/platon42/intellij/plugins/cajon/AbstractCajonTest.kt +++ b/src/test/java/de/platon42/intellij/plugins/cajon/AbstractCajonTest.kt @@ -61,7 +61,7 @@ abstract class AbstractCajonTest { protected fun executeQuickFixes(myFixture: JavaCodeInsightTestFixture, regex: Regex, expectedFixes: Int) { 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) } } \ No newline at end of file diff --git a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBooleanIsTrueOrFalseInspectionTest.kt b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBooleanIsTrueOrFalseInspectionTest.kt index b0c0114..5762e42 100644 --- a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBooleanIsTrueOrFalseInspectionTest.kt +++ b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBooleanIsTrueOrFalseInspectionTest.kt @@ -14,7 +14,10 @@ internal class AssertThatBooleanIsTrueOrFalseInspectionTest : AbstractCajonTest( runTest { myFixture.enableInspections(AssertThatBooleanIsTrueOrFalseInspection::class.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") } } 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 793f19c..e350fb9 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("Replace hasSize.*"), 4) + executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSize() with isEmpty()"), 4) myFixture.checkResultByFile("EnumerableIsEmptyAfter.java") } } diff --git a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNotNullInspectionTest.kt b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNotNullInspectionTest.kt deleted file mode 100644 index 9b39b9a..0000000 --- a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNotNullInspectionTest.kt +++ /dev/null @@ -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") - } - } -} \ No newline at end of file diff --git a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullInspectionTest.kt b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspectionTest.kt similarity index 53% rename from src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullInspectionTest.kt rename to src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspectionTest.kt index 0a29ee8..1cb29b7 100644 --- a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullInspectionTest.kt +++ b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatObjectIsNullOrNotNullInspectionTest.kt @@ -6,16 +6,17 @@ import de.platon42.intellij.jupiter.TestDataSubPath import de.platon42.intellij.plugins.cajon.AbstractCajonTest import org.junit.jupiter.api.Test -internal class AssertThatObjectIsNullInspectionTest : AbstractCajonTest() { +internal class AssertThatObjectIsNullOrNotNullInspectionTest : AbstractCajonTest() { @Test - @TestDataSubPath("inspections/ObjectIsNull") + @TestDataSubPath("inspections/ObjectIsNullOrNotNull") internal fun assertThat_with_isEqualTo_null_can_use_isNull(@MyFixture myFixture: JavaCodeInsightTestFixture) { runTest { - myFixture.enableInspections(AssertThatObjectIsNullInspection::class.java) - myFixture.configureByFile("ObjectIsNullBefore.java") + myFixture.enableInspections(AssertThatObjectIsNullOrNotNullInspection::class.java) + myFixture.configureByFile("ObjectIsNullOrNotNullBefore.java") 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") } } } \ No newline at end of file diff --git a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatSizeInspectionTest.kt b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatSizeInspectionTest.kt index bf894dc..0c0b503 100644 --- a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatSizeInspectionTest.kt +++ b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatSizeInspectionTest.kt @@ -14,7 +14,19 @@ internal class AssertThatSizeInspectionTest : AbstractCajonTest() { runTest { myFixture.enableInspections(AssertThatSizeInspection::class.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") } } 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 b621a7d..1bc6803 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,7 +14,8 @@ internal class AssertThatStringIsEmptyInspectionTest : AbstractCajonTest() { runTest { myFixture.enableInspections(AssertThatStringIsEmptyInspection::class.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") } } diff --git a/src/test/resources/inspections/ObjectIsNotNull/ObjectIsNotNullAfter.java b/src/test/resources/inspections/ObjectIsNotNull/ObjectIsNotNullAfter.java deleted file mode 100644 index e8f4c88..0000000 --- a/src/test/resources/inspections/ObjectIsNotNull/ObjectIsNotNullAfter.java +++ /dev/null @@ -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(); - } -} diff --git a/src/test/resources/inspections/ObjectIsNotNull/ObjectIsNotNullBefore.java b/src/test/resources/inspections/ObjectIsNotNull/ObjectIsNotNullBefore.java deleted file mode 100644 index 791c189..0000000 --- a/src/test/resources/inspections/ObjectIsNotNull/ObjectIsNotNullBefore.java +++ /dev/null @@ -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); - } -} diff --git a/src/test/resources/inspections/ObjectIsNull/ObjectIsNullAfter.java b/src/test/resources/inspections/ObjectIsNullOrNotNull/ObjectIsNullOrNotNullAfter.java similarity index 54% rename from src/test/resources/inspections/ObjectIsNull/ObjectIsNullAfter.java rename to src/test/resources/inspections/ObjectIsNullOrNotNull/ObjectIsNullOrNotNullAfter.java index e35d587..88de037 100644 --- a/src/test/resources/inspections/ObjectIsNull/ObjectIsNullAfter.java +++ b/src/test/resources/inspections/ObjectIsNullOrNotNull/ObjectIsNullOrNotNullAfter.java @@ -5,6 +5,10 @@ public class ObjectIsNull { private void objectIsNull() { assertThat("").isNull(); assertThat("").as("nah").isNull(); - assertThat(new Object).isNull(); + assertThat(new Object()).isNull(); + + assertThat("").isNotNull(); + assertThat("").as("nah").isNotNull(); + assertThat(new Object()).isNotNull(); } } diff --git a/src/test/resources/inspections/ObjectIsNull/ObjectIsNullBefore.java b/src/test/resources/inspections/ObjectIsNullOrNotNull/ObjectIsNullOrNotNullBefore.java similarity index 52% rename from src/test/resources/inspections/ObjectIsNull/ObjectIsNullBefore.java rename to src/test/resources/inspections/ObjectIsNullOrNotNull/ObjectIsNullOrNotNullBefore.java index 0956150..697f7e5 100644 --- a/src/test/resources/inspections/ObjectIsNull/ObjectIsNullBefore.java +++ b/src/test/resources/inspections/ObjectIsNullOrNotNull/ObjectIsNullOrNotNullBefore.java @@ -5,6 +5,10 @@ public class ObjectIsNull { private void objectIsNull() { assertThat("").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); } }