Added several cases for hasSizeGreaterThan/LessThan/OrEqualTo() for EnumerablesEmpty inspection.
This commit is contained in:
+8
@@ -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")!!
|
||||
|
||||
+11
-4
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
Turns assertThat(enumerable).hasSize(0) into assertThat(enumerable).isEmpty().
|
||||
Turns assertThat(enumerable).hasSize(0) and similar into assertThat(enumerable).isEmpty() or .isNotEmpty().
|
||||
<!-- tooltip end -->
|
||||
<br>Works with anything that is enumerable such as arrays, iterables, collections, etc.
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user