From d48c71d4ead46220cfe28119988ede87e284a6fb Mon Sep 17 00:00:00 2001 From: chrisly42 Date: Fri, 2 Oct 2020 19:24:22 +0200 Subject: [PATCH] Added several cases for hasSizeGreaterThan/LessThan/OrEqualTo() for EnumerablesEmpty inspection. --- README.md | 13 +++++++++- build.gradle | 7 +++--- .../inspections/AbstractAssertJInspection.kt | 8 ++++++ .../AssertThatEnumerableIsEmptyInspection.kt | 15 ++++++++--- .../AssertThatEnumerableIsEmpty.html | 2 +- ...sertThatEnumerableIsEmptyInspectionTest.kt | 6 ++++- .../EnumerableIsEmptyAfter.java | 25 +++++++++++++++++++ .../EnumerableIsEmptyBefore.java | 25 +++++++++++++++++++ 8 files changed, 90 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index f29901a..ba6219f 100644 --- a/README.md +++ b/README.md @@ -415,11 +415,21 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the - AssertThatEnumerableIsEmpty - Uses ```isEmpty()``` for ```hasSize(0)``` iterable assertions instead. + Uses ```isEmpty()``` for ```hasSize(0)```, ```hasSizeLessThanOrEqualTo(0)```, and + ```hasSizeLessThan(1)``` iterable (enumerable) assertions instead. + + Also suggests ```isNotEmpty()``` for ```hasSizeGreaterThan(0)``` and + ```hasSizeGreaterThanOrEqualTo(1)```. ``` from: assertThat(enumerable).hasSize(0); + from: assertThat(enumerable).hasSizeLessThanOrEqualTo(0); + from: assertThat(enumerable).hasSizeLessThan(1); to: assertThat(enumerable).isEmpty(); + + from: assertThat(enumerable).hasSizeGreaterThan(0); + from: assertThat(enumerable).hasSizeGreaterThanOrEqualTo(1); + to: assertThat(enumerable).isNotEmpty(); ``` - AssertThatSize @@ -805,6 +815,7 @@ Feel free to use the code (in package ```de.platon42.intellij.jupiter```) for yo - Updated various dependencies and AssertJ 3.17.2. - Fixed the ImplicitAssertionInspection that broke the plugin with IntelliJ 2020.3 EAP as reported by Frédéric Thomas. Thanks! - Added new singleElement() from AssertJ >= 3.17.0 to ImplicitAssertionInspection. +- Added several cases for ```hasSizeGreaterThan/LessThan/OrEqualTo()``` for EnumerablesEmpty inspection. #### V1.10 (31-Jul-20) Friday the 31st Edition - Updated libraries to the latest versions (including AssertJ 3.16.1 and Kotlin 1.40-rc). diff --git a/build.gradle b/build.gradle index 8f0aaf1..d0b61bd 100644 --- a/build.gradle +++ b/build.gradle @@ -8,6 +8,8 @@ plugins { group 'de.platon42' version '1.11' +sourceCompatibility = "1.8" +targetCompatibility = "1.8" repositories { mavenCentral() @@ -30,14 +32,10 @@ dependencies { compileKotlin { kotlinOptions.jvmTarget = "1.8" - sourceCompatibility = "1.8" - targetCompatibility = "1.8" } compileTestKotlin { kotlinOptions.jvmTarget = "1.8" - sourceCompatibility = "1.8" - targetCompatibility = "1.8" } intellij { @@ -55,6 +53,7 @@ patchPluginXml {
  • Updated various dependencies and AssertJ 3.17.2.
  • Fixed the ImplicitAssertionInspection that broke the plugin with IntelliJ 2020.3 EAP as reported by Frédéric Thomas. Thanks!
  • Added new singleElement() from AssertJ >= 3.17.0 to ImplicitAssertionInspection. +
  • Added several cases for hasSizeGreaterThan/LessThan/OrEqualTo() for EnumerablesEmpty inspection.

    Full changelog available at Github project site.

    """ diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AbstractAssertJInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AbstractAssertJInspection.kt index 67b7745..0e38e17 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AbstractAssertJInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AbstractAssertJInspection.kt @@ -126,6 +126,14 @@ abstract class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() .parameterCount(0)!! val HAS_SIZE = CallMatcher.instanceCall(ENUMERABLE_ASSERT_INTERFACE, MethodNames.HAS_SIZE) .parameterTypes("int")!! + val HAS_SIZE_GREATER_THAN_INT = CallMatcher.instanceCall(ENUMERABLE_ASSERT_INTERFACE, MethodNames.HAS_SIZE_GREATER_THAN) + .parameterTypes("int")!! + val HAS_SIZE_GREATER_THAN_OR_EQUAL_TO_INT = CallMatcher.instanceCall(ENUMERABLE_ASSERT_INTERFACE, MethodNames.HAS_SIZE_GREATER_THAN_OR_EQUAL_TO) + .parameterTypes("int")!! + val HAS_SIZE_LESS_THAN_INT = CallMatcher.instanceCall(ENUMERABLE_ASSERT_INTERFACE, MethodNames.HAS_SIZE_LESS_THAN) + .parameterTypes("int")!! + val HAS_SIZE_LESS_THAN_OR_EQUAL_TO_INT = CallMatcher.instanceCall(ENUMERABLE_ASSERT_INTERFACE, MethodNames.HAS_SIZE_LESS_THAN_OR_EQUAL_TO) + .parameterTypes("int")!! val IS_GREATER_THAN_INT = CallMatcher.instanceCall(ABSTRACT_COMPARABLE_ASSERT_CLASSNAME, MethodNames.IS_GREATER_THAN) .parameterTypes("int")!! 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 1842204..88b5643 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 @@ -5,6 +5,7 @@ import com.intellij.psi.JavaElementVisitor import com.intellij.psi.PsiElementVisitor import com.intellij.psi.PsiMethodCallExpression import com.intellij.psi.PsiStatement +import com.siyeh.ig.callMatcher.CallMatcher import de.platon42.intellij.plugins.cajon.MethodNames import de.platon42.intellij.plugins.cajon.calculateConstantParameterValue import de.platon42.intellij.plugins.cajon.hasAssertThat @@ -12,7 +13,7 @@ import de.platon42.intellij.plugins.cajon.hasAssertThat class AssertThatEnumerableIsEmptyInspection : AbstractAssertJInspection() { companion object { - private const val DISPLAY_NAME = "Asserting an empty enumerable" + private const val DISPLAY_NAME = "Asserting an empty or not empty enumerable" } override fun getDisplayName() = DISPLAY_NAME @@ -23,11 +24,17 @@ class AssertThatEnumerableIsEmptyInspection : AbstractAssertJInspection() { super.visitMethodCallExpression(expression) if (!expression.hasAssertThat()) return val isLastExpression = expression.parent is PsiStatement - if (!(HAS_SIZE.test(expression) && isLastExpression)) return - val value = expression.calculateConstantParameterValue(0) ?: return - if (value == 0) { + + val isEmpty = (CallMatcher.anyOf(HAS_SIZE, HAS_SIZE_LESS_THAN_OR_EQUAL_TO_INT).test(expression) && (value == 0)) || + (HAS_SIZE_LESS_THAN_INT.test(expression) && (value == 1)); + val isNotEmpty = (HAS_SIZE_GREATER_THAN_INT.test(expression) && (value == 0)) || + (HAS_SIZE_GREATER_THAN_OR_EQUAL_TO_INT.test(expression) && (value == 1)); + + if (isEmpty && isLastExpression) { registerSimplifyMethod(holder, expression, MethodNames.IS_EMPTY) + } else if (isNotEmpty) { + registerSimplifyMethod(holder, expression, MethodNames.IS_NOT_EMPTY) } } } diff --git a/src/main/resources/inspectionDescriptions/AssertThatEnumerableIsEmpty.html b/src/main/resources/inspectionDescriptions/AssertThatEnumerableIsEmpty.html index ed79f54..e4acf71 100644 --- a/src/main/resources/inspectionDescriptions/AssertThatEnumerableIsEmpty.html +++ b/src/main/resources/inspectionDescriptions/AssertThatEnumerableIsEmpty.html @@ -1,6 +1,6 @@ -Turns assertThat(enumerable).hasSize(0) into assertThat(enumerable).isEmpty(). +Turns assertThat(enumerable).hasSize(0) and similar into assertThat(enumerable).isEmpty() or .isNotEmpty().
    Works with anything that is enumerable such as arrays, iterables, collections, etc. 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 9abd8fd..edd994e 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 @@ -10,10 +10,14 @@ internal class AssertThatEnumerableIsEmptyInspectionTest : AbstractCajonTest() { @Test @TestDataSubPath("inspections/EnumerableIsEmpty") - internal fun assertThat_with_hasSize_zero_can_use_isEmpty(@MyFixture myFixture: JavaCodeInsightTestFixture) { + internal fun assertThat_with_hasSize_zero_and_similar_can_use_isEmpty(@MyFixture myFixture: JavaCodeInsightTestFixture) { myFixture.enableInspections(AssertThatEnumerableIsEmptyInspection::class.java) myFixture.configureByFile("EnumerableIsEmptyBefore.java") executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSize() with isEmpty()"), 5) + executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSizeLessThan() with isEmpty()"), 5) + executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSizeLessThanOrEqualTo() with isEmpty()"), 5) + executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSizeGreaterThan() with isNotEmpty()"), 6) + executeQuickFixes(myFixture, Regex.fromLiteral("Replace hasSizeGreaterThanOrEqualTo() with isNotEmpty()"), 6) myFixture.checkResultByFile("EnumerableIsEmptyAfter.java") } } \ No newline at end of file diff --git a/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyAfter.java b/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyAfter.java index 17ef42c..2c4faf1 100644 --- a/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyAfter.java +++ b/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyAfter.java @@ -16,7 +16,32 @@ public class EnumerableIsEmpty { assertThat(new ArrayList()).as("etc").hasSize(1); assertThat(new Long[1]).as("etc").hasSize(1); + assertThat("string").as("foo").isEmpty(); + assertThat(new StringBuilder()).as("bar").isEmpty(); + assertThat(new ArrayList()).as("etc").isEmpty(); + assertThat(new Long[1]).as("etc").isEmpty(); + + assertThat("string").as("foo").isEmpty(); + assertThat(new StringBuilder()).as("bar").isEmpty(); + assertThat(new ArrayList()).as("etc").isEmpty(); + assertThat(new Long[1]).as("etc").isEmpty(); + + assertThat("string").as("foo").isNotEmpty(); + assertThat(new StringBuilder()).as("bar").isNotEmpty(); + assertThat(new ArrayList()).as("etc").isNotEmpty(); + assertThat(new Long[1]).as("etc").isNotEmpty(); + + assertThat("string").as("foo").isNotEmpty(); + assertThat(new StringBuilder()).as("bar").isNotEmpty(); + assertThat(new ArrayList()).as("etc").isNotEmpty(); + assertThat(new Long[1]).as("etc").isNotEmpty(); + assertThat("string").as("foo").hasSize(0).hasSameSizeAs("foo").isEmpty(); + assertThat("string").as("foo").hasSizeLessThanOrEqualTo(0).hasSameSizeAs("foo").isEmpty(); + assertThat("string").as("foo").hasSizeLessThan(1).hasSameSizeAs("foo").isEmpty(); + + assertThat("string").as("foo").isNotEmpty().hasSameSizeAs("foo").isNotEmpty(); + assertThat("string").as("foo").isNotEmpty().hasSameSizeAs("foo").isNotEmpty(); org.junit.Assert.assertThat("foo", null); fail("oh no!"); diff --git a/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyBefore.java b/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyBefore.java index 51d8de8..e316679 100644 --- a/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyBefore.java +++ b/src/test/resources/inspections/EnumerableIsEmpty/EnumerableIsEmptyBefore.java @@ -16,7 +16,32 @@ public class EnumerableIsEmpty { assertThat(new ArrayList()).as("etc").hasSize(1); assertThat(new Long[1]).as("etc").hasSize(1); + assertThat("string").as("foo").hasSizeLessThanOrEqualTo(0); + assertThat(new StringBuilder()).as("bar").hasSizeLessThanOrEqualTo(0 + 0); + assertThat(new ArrayList()).as("etc").hasSizeLessThanOrEqualTo(10 / 100); + assertThat(new Long[1]).as("etc").hasSizeLessThanOrEqualTo(1 - 1); + + assertThat("string").as("foo").hasSizeLessThan(1); + assertThat(new StringBuilder()).as("bar").hasSizeLessThan(0 + 1); + assertThat(new ArrayList()).as("etc").hasSizeLessThan(10 / 10); + assertThat(new Long[1]).as("etc").hasSizeLessThan(1 - 0); + + assertThat("string").as("foo").hasSizeGreaterThan(0); + assertThat(new StringBuilder()).as("bar").hasSizeGreaterThan(0 + 0); + assertThat(new ArrayList()).as("etc").hasSizeGreaterThan(10 / 100); + assertThat(new Long[1]).as("etc").hasSizeGreaterThan(1 - 1); + + assertThat("string").as("foo").hasSizeGreaterThanOrEqualTo(1); + assertThat(new StringBuilder()).as("bar").hasSizeGreaterThanOrEqualTo(0 + 1); + assertThat(new ArrayList()).as("etc").hasSizeGreaterThanOrEqualTo(10 / 10); + assertThat(new Long[1]).as("etc").hasSizeGreaterThanOrEqualTo(1 - 0); + assertThat("string").as("foo").hasSize(0).hasSameSizeAs("foo").hasSize(0); + assertThat("string").as("foo").hasSizeLessThanOrEqualTo(0).hasSameSizeAs("foo").hasSizeLessThanOrEqualTo(0); + assertThat("string").as("foo").hasSizeLessThan(1).hasSameSizeAs("foo").hasSizeLessThan(1); + + assertThat("string").as("foo").hasSizeGreaterThan(0).hasSameSizeAs("foo").hasSizeGreaterThan(0); + assertThat("string").as("foo").hasSizeGreaterThanOrEqualTo(1).hasSameSizeAs("foo").hasSizeGreaterThanOrEqualTo(1); org.junit.Assert.assertThat("foo", null); fail("oh no!");