Extended AssertThatSize intention to take strings and CharSequences into account, too.
This commit is contained in:
+2
@@ -118,6 +118,8 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
|
||||
|
||||
val COLLECTION_SIZE = CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_COLLECTION, "size")
|
||||
.parameterCount(0)!!
|
||||
val CHAR_SEQUENCE_LENGTH = CallMatcher.instanceCall("java.lang.CharSequence", "length")
|
||||
.parameterCount(0)!!
|
||||
val OBJECT_EQUALS = CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_OBJECT, "equals")
|
||||
.parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)!!
|
||||
|
||||
|
||||
+9
-3
@@ -12,7 +12,7 @@ import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceSizeMethodCallQuickF
|
||||
class AssertThatSizeInspection : AbstractAssertJInspection() {
|
||||
|
||||
companion object {
|
||||
private const val DISPLAY_NAME = "Asserting the size of an collection or array"
|
||||
private const val DISPLAY_NAME = "Asserting the size of an collection, array or string"
|
||||
|
||||
private val BONUS_EXPRESSIONS_CALL_MATCHER_MAP = listOf(
|
||||
IS_LESS_THAN_INT to MethodNames.HAS_SIZE_LESS_THAN,
|
||||
@@ -33,7 +33,9 @@ class AssertThatSizeInspection : AbstractAssertJInspection() {
|
||||
}
|
||||
val actualExpression = expression.firstArg
|
||||
|
||||
if (isArrayLength(actualExpression) || isCollectionSize(actualExpression)) {
|
||||
val isForArrayOrCollection = isArrayLength(actualExpression) || isCollectionSize(actualExpression)
|
||||
val isForString = isCharSequenceLength(actualExpression)
|
||||
if (isForArrayOrCollection || isForString) {
|
||||
val expectedCallExpression = expression.findOutmostMethodCall() ?: return
|
||||
val constValue = calculateConstantParameterValue(expectedCallExpression, 0)
|
||||
if (IS_EQUAL_TO_INT.test(expectedCallExpression)) {
|
||||
@@ -43,7 +45,9 @@ class AssertThatSizeInspection : AbstractAssertJInspection() {
|
||||
}
|
||||
} else {
|
||||
val equalToExpression = expectedCallExpression.firstArg
|
||||
if (isCollectionSize(equalToExpression) || isArrayLength(equalToExpression)) {
|
||||
if (isForArrayOrCollection && (isCollectionSize(equalToExpression) || isArrayLength(equalToExpression)) ||
|
||||
isForString && (isCollectionSize(equalToExpression) || isArrayLength(equalToExpression) || isCharSequenceLength(equalToExpression))
|
||||
) {
|
||||
registerReplaceMethod(holder, expression, expectedCallExpression, MethodNames.HAS_SAME_SIZE_AS) { desc, method ->
|
||||
ReplaceSizeMethodCallQuickFix(desc, method, expectedIsCollection = true)
|
||||
}
|
||||
@@ -76,6 +80,8 @@ class AssertThatSizeInspection : AbstractAssertJInspection() {
|
||||
}
|
||||
}
|
||||
|
||||
private fun isCharSequenceLength(expression: PsiExpression) = (expression is PsiMethodCallExpression) && CHAR_SEQUENCE_LENGTH.test(expression)
|
||||
|
||||
private fun isCollectionSize(expression: PsiExpression) = (expression is PsiMethodCallExpression) && COLLECTION_SIZE.test(expression)
|
||||
|
||||
private fun isArrayLength(expression: PsiExpression): Boolean {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<html>
|
||||
<body>
|
||||
Makes assertions on sizes of arrays or collections more concise by replacing them with isEmpty(), isNotEmpty(), hasSize(), or hasSameSizeAs().
|
||||
Makes assertions on sizes of arrays, collections, strings, or CharSequences more concise by replacing them with isEmpty(), isNotEmpty(), hasSize(), or hasSameSizeAs().
|
||||
<!-- tooltip end -->
|
||||
Several more conversions are available with AssertJ 13.2.0 or later.
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user