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 {
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().