Extended AssertThatSize intention to take strings and CharSequences into account, too.
This commit is contained in:
parent
533c20906a
commit
a707eee9ad
27
README.md
27
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.
|
||||
|
@ -42,8 +42,9 @@ patchPluginXml {
|
||||
changeNotes """
|
||||
<h4>V0.6 (xx-Apr-19)</h4>
|
||||
<ul>
|
||||
<li>New AssertThatStringExpression that will move isEmpty(), equals(), equalsIgnoreCase(), contains(),
|
||||
<li>New AssertThatStringExpression intention that will move isEmpty(), equals(), equalsIgnoreCase(), contains(),
|
||||
startsWith(), and endsWith() out of actual expression.
|
||||
<li>Extended AssertThatSize intention to take strings and CharSequences into account, too.
|
||||
</ul>
|
||||
<h4>V0.5 (18-Apr-19)</h4>
|
||||
<ul>
|
||||
|
@ -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)!!
|
||||
|
||||
|
@ -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>
|
@ -35,6 +35,7 @@ public class Playground {
|
||||
assertThat(new ArrayList<String>()).hasSizeGreaterThan(1);
|
||||
assertThat(new ArrayList<String>()).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<String> 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() {
|
||||
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ public class AssertThatSize {
|
||||
ArrayList<String> 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);
|
||||
}
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ public class AssertThatSize {
|
||||
ArrayList<String> 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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user