diff --git a/README.md b/README.md
index 270ca64..49afc2b 100644
--- a/README.md
+++ b/README.md
@@ -50,6 +50,18 @@ The plugin will report inspections in your opened editor file as warnings.
You can then quick-fix these with your quick-fix hotkey (usually Alt-Return or Opt-Return).
Or, you can use the "Run Inspection by Name..." action to run one inspection on a bigger scope (e.g. the whole project).
+Applying a quick fix might result in further optimization possibilities, so you might need to perform a couple of fixes before you get to the final result.
+
+For example:
+```
+assertFalse(!(array.length() == collection.size()));
+
+assertThat(!(array.length() == collection.size())).isFalse();
+
+assertThat(array.length() == collection.size()).isTrue();
+
+assertThat(array).hasSameSizeAs(collection);
+```
You can toggle the various inspections in the Settings/Editor/Inspections in the AssertJ group.
@@ -123,7 +135,7 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
to: assertThat(array).hasSameSizeAs(anotherArray);
```
- with AssertJ 13.2.0 or higher
+ and additionally with AssertJ 13.2.0 or later
```
from: assertThat(array.length).isLessThanOrEqualTo(expression);
@@ -138,7 +150,15 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
from: assertThat(array.length).isGreaterThanOrEqualTo(expression);
to: assertThat(array).hasSizeGreaterThanOrEqualTo(expression);
```
- and analogously for collections...
+ and analogously for collections, strings and CharSequences, e.g:
+
+ ```
+ from: assertThat("string".length()).isLessThan(1);
+ to: assertThat("string").isEmpty();
+
+ from: assertThat("string".length()).isEqualTo(collection.size())
+ to: assertThat("string").hasSameSizeAs(collection);
+ ```
- AssertThatBinaryExpressionIsTrueOrFalse
```
@@ -289,7 +309,8 @@ Feel free to use the code (in package de.platon42.intellij.jupiter) for your pro
#### V0.6 (unreleased)
- New AssertThatStringExpression that will move ```isEmpty()```, ```equals()```, ```equalsIgnoreCase()```, ```contains()```,
```startsWith()```, and ```endsWith()``` out of actual expression.
-
+- Extended AssertThatSize intention to take ```String```s and ```CharSequences``` into account, too.
+
#### V0.5 (13-Apr-19)
- Fixed incompatibility with IDEA versions < 2018.2 (affected AssertThatSizeInspection). Minimal version is now 2017.3.
- Fixed missing Guava imports (if not already present) for AssertThatGuavaInspection. This was a major PITA to get right.
diff --git a/build.gradle b/build.gradle
index 8802466..2f5cee7 100644
--- a/build.gradle
+++ b/build.gradle
@@ -42,8 +42,9 @@ patchPluginXml {
changeNotes """
V0.6 (xx-Apr-19)
- - New AssertThatStringExpression that will move isEmpty(), equals(), equalsIgnoreCase(), contains(),
+
- New AssertThatStringExpression intention that will move isEmpty(), equals(), equalsIgnoreCase(), contains(),
startsWith(), and endsWith() out of actual expression.
+
- Extended AssertThatSize intention to take strings and CharSequences into account, too.
V0.5 (18-Apr-19)
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 b246116..7aaa07f 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
@@ -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)!!
diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatSizeInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatSizeInspection.kt
index f7532ac..cfef00b 100644
--- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatSizeInspection.kt
+++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatSizeInspection.kt
@@ -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 {
diff --git a/src/main/resources/inspectionDescriptions/AssertThatSize.html b/src/main/resources/inspectionDescriptions/AssertThatSize.html
index dfc607c..495fa63 100644
--- a/src/main/resources/inspectionDescriptions/AssertThatSize.html
+++ b/src/main/resources/inspectionDescriptions/AssertThatSize.html
@@ -1,6 +1,7 @@
-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().
+Several more conversions are available with AssertJ 13.2.0 or later.
\ No newline at end of file
diff --git a/src/test/java/de/platon42/intellij/playground/Playground.java b/src/test/java/de/platon42/intellij/playground/Playground.java
index ca94c35..3572ea8 100644
--- a/src/test/java/de/platon42/intellij/playground/Playground.java
+++ b/src/test/java/de/platon42/intellij/playground/Playground.java
@@ -35,6 +35,7 @@ public class Playground {
assertThat(new ArrayList()).hasSizeGreaterThan(1);
assertThat(new ArrayList()).hasSameSizeAs(new ArrayList<>());
assertThat(new Long[1]).as("etc").hasSameSizeAs(new Long[2]);
+ assertThat(new Long[1]).as("etc").hasSameSizeAs(new Long[2]);
}
private void sizeOfArray() {
@@ -146,6 +147,41 @@ public class Playground {
assertThat(foo).doesNotEndWith("foobar");
assertThat(foo.equalsIgnoreCase("foo")).isFalse();
assertThat(foo).isNotEqualToIgnoringCase("foo");
+
+ ArrayList list = new ArrayList<>();
+ long[] otherArray = new long[4];
+
+ String string = "string";
+ assertThat(string.length()).isEqualTo(0);
+ assertThat(string.length()).isZero();
+ assertThat(string.length()).isNotZero();
+ assertThat(string.length()).as("hi").isGreaterThan(0);
+ assertThat(string.length()).isGreaterThanOrEqualTo(1);
+ assertThat(string.length()).isLessThan(1);
+ assertThat(string.length()).isLessThanOrEqualTo(0);
+ assertThat(string.length()).isEqualTo(list.size());
+ assertThat(string.length()).isEqualTo(otherArray.length);
+ assertThat(string.length()).isEqualTo(1);
+ assertThat(string.length()).isGreaterThan(otherArray.length - 1);
+ assertThat(string.length()).isGreaterThanOrEqualTo(otherArray.length + 1);
+ assertThat(string.length()).isLessThan(otherArray.length - 3);
+ assertThat(string.length()).isLessThanOrEqualTo(1 - otherArray.length);
+
+ StringBuilder stringBuilder = new StringBuilder();
+ assertThat(stringBuilder.length()).isEqualTo(0);
+ assertThat(stringBuilder.length()).isZero();
+ assertThat(stringBuilder.length()).isNotZero();
+ assertThat(stringBuilder.length()).as("hi").isGreaterThan(0);
+ assertThat(stringBuilder.length()).isGreaterThanOrEqualTo(1);
+ assertThat(stringBuilder.length()).isLessThan(1);
+ assertThat(stringBuilder.length()).isLessThanOrEqualTo(0);
+ assertThat(stringBuilder.length()).isEqualTo(list.size());
+ assertThat(stringBuilder.length()).isEqualTo(otherArray.length);
+ assertThat(stringBuilder.length()).isEqualTo(1);
+ assertThat(stringBuilder.length()).isGreaterThan(otherArray.length - 1);
+ assertThat(stringBuilder.length()).isGreaterThanOrEqualTo(otherArray.length + 1);
+ assertThat(stringBuilder.length()).isLessThan(otherArray.length - 3);
+ assertThat(stringBuilder.length()).isLessThanOrEqualTo(1 - otherArray.length);
}
private void java8Optional() {
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 0c0b503..e0b842a 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,19 +14,19 @@ internal class AssertThatSizeInspectionTest : AbstractCajonTest() {
runTest {
myFixture.enableInspections(AssertThatSizeInspection::class.java)
myFixture.configureByFile("AssertThatSizeBefore.java")
- 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)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isEmpty()"), 4)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace isZero() with isEmpty()"), 4)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotZero() with isNotEmpty()"), 4)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThan() with isNotEmpty()"), 4)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThanOrEqualTo() with isNotEmpty()"), 4)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThan() with isEmpty()"), 4)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThanOrEqualTo() with isEmpty()"), 4)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with hasSameSizeAs()"), 12)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with hasSize()"), 8)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThan() with hasSizeGreaterThan()"), 4)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThanOrEqualTo() with hasSizeGreaterThanOrEqualTo()"), 4)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThan() with hasSizeLessThan()"), 4)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThanOrEqualTo() with hasSizeLessThanOrEqualTo()"), 4)
myFixture.checkResultByFile("AssertThatSizeAfter.java")
}
}
diff --git a/src/test/resources/inspections/AssertThatSize/AssertThatSizeAfter.java b/src/test/resources/inspections/AssertThatSize/AssertThatSizeAfter.java
index ddabb49..8c496bd 100644
--- a/src/test/resources/inspections/AssertThatSize/AssertThatSizeAfter.java
+++ b/src/test/resources/inspections/AssertThatSize/AssertThatSizeAfter.java
@@ -9,6 +9,8 @@ public class AssertThatSize {
ArrayList otherList = new ArrayList<>();
long[] array = new long[5];
long[] otherArray = new long[4];
+ String string = "string";
+ StringBuilder stringBuilder = new StringBuilder();
assertThat(list).isEmpty();
assertThat(list).isEmpty();
@@ -19,6 +21,8 @@ public class AssertThatSize {
assertThat(list).isEmpty();
assertThat(list).hasSameSizeAs(otherList);
assertThat(list).hasSameSizeAs(array);
+ assertThat(list).hasSize(string.length());
+ assertThat(list).hasSize(stringBuilder.length());
assertThat(list).hasSize(1);
assertThat(list).hasSizeGreaterThan(list.size() * 2);
assertThat(list).hasSizeGreaterThanOrEqualTo(list.size() * 2);
@@ -34,10 +38,46 @@ public class AssertThatSize {
assertThat(array).isEmpty();
assertThat(array).hasSameSizeAs(list);
assertThat(array).hasSameSizeAs(otherArray);
+ assertThat(array).hasSize(string.length());
+ assertThat(array).hasSize(stringBuilder.length());
assertThat(array).hasSize(1);
assertThat(array).hasSizeGreaterThan(otherArray.length - 1);
assertThat(array).hasSizeGreaterThanOrEqualTo(otherArray.length + 1);
assertThat(array).hasSizeLessThan(otherArray.length - 3);
assertThat(array).hasSizeLessThanOrEqualTo(1 - otherArray.length);
+
+ assertThat(string).isEmpty();
+ assertThat(string).isEmpty();
+ assertThat(string).isNotEmpty();
+ assertThat(string).as("hi").isNotEmpty();
+ assertThat(string).isNotEmpty();
+ assertThat(string).isEmpty();
+ assertThat(string).isEmpty();
+ assertThat(string).hasSameSizeAs(list);
+ assertThat(string).hasSameSizeAs(otherArray);
+ assertThat(string).hasSameSizeAs(string);
+ assertThat(string).hasSameSizeAs(stringBuilder);
+ assertThat(string).hasSize(1);
+ assertThat(string).hasSizeGreaterThan(otherArray.length - 1);
+ assertThat(string).hasSizeGreaterThanOrEqualTo(otherArray.length + 1);
+ assertThat(string).hasSizeLessThan(otherArray.length - 3);
+ assertThat(string).hasSizeLessThanOrEqualTo(1 - otherArray.length);
+
+ assertThat(stringBuilder).isEmpty();
+ assertThat(stringBuilder).isEmpty();
+ assertThat(stringBuilder).isNotEmpty();
+ assertThat(stringBuilder).as("hi").isNotEmpty();
+ assertThat(stringBuilder).isNotEmpty();
+ assertThat(stringBuilder).isEmpty();
+ assertThat(stringBuilder).isEmpty();
+ assertThat(stringBuilder).hasSameSizeAs(list);
+ assertThat(stringBuilder).hasSameSizeAs(otherArray);
+ assertThat(stringBuilder).hasSameSizeAs(string);
+ assertThat(stringBuilder).hasSameSizeAs(stringBuilder);
+ assertThat(stringBuilder).hasSize(1);
+ assertThat(stringBuilder).hasSizeGreaterThan(otherArray.length - 1);
+ assertThat(stringBuilder).hasSizeGreaterThanOrEqualTo(otherArray.length + 1);
+ assertThat(stringBuilder).hasSizeLessThan(otherArray.length - 3);
+ assertThat(stringBuilder).hasSizeLessThanOrEqualTo(1 - otherArray.length);
}
}
diff --git a/src/test/resources/inspections/AssertThatSize/AssertThatSizeBefore.java b/src/test/resources/inspections/AssertThatSize/AssertThatSizeBefore.java
index 808169a..c9f00b4 100644
--- a/src/test/resources/inspections/AssertThatSize/AssertThatSizeBefore.java
+++ b/src/test/resources/inspections/AssertThatSize/AssertThatSizeBefore.java
@@ -9,6 +9,8 @@ public class AssertThatSize {
ArrayList otherList = new ArrayList<>();
long[] array = new long[5];
long[] otherArray = new long[4];
+ String string = "string";
+ StringBuilder stringBuilder = new StringBuilder();
assertThat(list.size()).isEqualTo(0);
assertThat(list.size()).isZero();
@@ -19,6 +21,8 @@ public class AssertThatSize {
assertThat(list.size()).isLessThanOrEqualTo(0);
assertThat(list.size()).isEqualTo(otherList.size());
assertThat(list.size()).isEqualTo(array.length);
+ assertThat(list.size()).isEqualTo(string.length());
+ assertThat(list.size()).isEqualTo(stringBuilder.length());
assertThat(list.size()).isEqualTo(1);
assertThat(list.size()).isGreaterThan(list.size() * 2);
assertThat(list.size()).isGreaterThanOrEqualTo(list.size() * 2);
@@ -34,10 +38,46 @@ public class AssertThatSize {
assertThat(array.length).isLessThanOrEqualTo(0);
assertThat(array.length).isEqualTo(list.size());
assertThat(array.length).isEqualTo(otherArray.length);
+ assertThat(array.length).isEqualTo(string.length());
+ assertThat(array.length).isEqualTo(stringBuilder.length());
assertThat(array.length).isEqualTo(1);
assertThat(array.length).isGreaterThan(otherArray.length - 1);
assertThat(array.length).isGreaterThanOrEqualTo(otherArray.length + 1);
assertThat(array.length).isLessThan(otherArray.length - 3);
assertThat(array.length).isLessThanOrEqualTo(1 - otherArray.length);
+
+ assertThat(string.length()).isEqualTo(0);
+ assertThat(string.length()).isZero();
+ assertThat(string.length()).isNotZero();
+ assertThat(string.length()).as("hi").isGreaterThan(0);
+ assertThat(string.length()).isGreaterThanOrEqualTo(1);
+ assertThat(string.length()).isLessThan(1);
+ assertThat(string.length()).isLessThanOrEqualTo(0);
+ assertThat(string.length()).isEqualTo(list.size());
+ assertThat(string.length()).isEqualTo(otherArray.length);
+ assertThat(string.length()).isEqualTo(string.length());
+ assertThat(string.length()).isEqualTo(stringBuilder.length());
+ assertThat(string.length()).isEqualTo(1);
+ assertThat(string.length()).isGreaterThan(otherArray.length - 1);
+ assertThat(string.length()).isGreaterThanOrEqualTo(otherArray.length + 1);
+ assertThat(string.length()).isLessThan(otherArray.length - 3);
+ assertThat(string.length()).isLessThanOrEqualTo(1 - otherArray.length);
+
+ assertThat(stringBuilder.length()).isEqualTo(0);
+ assertThat(stringBuilder.length()).isZero();
+ assertThat(stringBuilder.length()).isNotZero();
+ assertThat(stringBuilder.length()).as("hi").isGreaterThan(0);
+ assertThat(stringBuilder.length()).isGreaterThanOrEqualTo(1);
+ assertThat(stringBuilder.length()).isLessThan(1);
+ assertThat(stringBuilder.length()).isLessThanOrEqualTo(0);
+ assertThat(stringBuilder.length()).isEqualTo(list.size());
+ assertThat(stringBuilder.length()).isEqualTo(otherArray.length);
+ assertThat(stringBuilder.length()).isEqualTo(string.length());
+ assertThat(stringBuilder.length()).isEqualTo(stringBuilder.length());
+ assertThat(stringBuilder.length()).isEqualTo(1);
+ assertThat(stringBuilder.length()).isGreaterThan(otherArray.length - 1);
+ assertThat(stringBuilder.length()).isGreaterThanOrEqualTo(otherArray.length + 1);
+ assertThat(stringBuilder.length()).isLessThan(otherArray.length - 3);
+ assertThat(stringBuilder.length()).isLessThanOrEqualTo(1 - otherArray.length);
}
}