Compare commits

...
6 Commits
49 changed files with 1045 additions and 244 deletions
+121 -55
View File
@@ -1,15 +1,17 @@
# Cajon - Concise AssertJ Optimizing Nitpicker # Cajon - Concise AssertJ Optimizing Nitpicker
Cajon is an IntelliJ IDEA Plugin for shortening and optimizing AssertJ assertions. Cajon is an IntelliJ IDEA Plugin for shortening and optimizing [AssertJ](https://assertj.github.io/doc/) assertions.
## Why? ## Purpose
First, code is easier to read, when it is concise and reflects the intention clearly. First, code is easier to read, when it is concise and reflects the intention clearly.
AssertJ has plenty of different convenience methods that describing various intentions precisely. AssertJ has plenty of different convenience methods that describing various intentions precisely.
Why write longer, more complex code that can be expressed in brevity? Why write longer, more complex code that can be expressed in brevity?
Second, AssertJ is able to output more meaningful descriptions when an assertion fails. Second, when using the available special assertion methods of AssertJ, a failure of a condition
can be expressed in better detail and with more meaningful descriptions.
This makes finding bugs and fixing failed tests more efficient. This makes finding bugs and fixing failed tests more efficient.
Nobody likes to read failures of the kind "failed because true is not false".
For example: For example:
@@ -50,6 +52,21 @@ 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). 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). 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.
Check out this example where every line represents the result after a Cajon quickfix:
```
assertFalse(!(array.length == collection.size()));
assertThat(!(array.length == collection.size())).isFalse();
assertThat(array.length == collection.size()).isTrue();
assertThat(array.length).isEqualTo(collection.size());
assertThat(array).hasSameSizeAs(collection);
```
You can toggle the various inspections in the Settings/Editor/Inspections in the AssertJ group. You can toggle the various inspections in the Settings/Editor/Inspections in the AssertJ group.
@@ -64,12 +81,30 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
to: assertThat(object).isNotNull(); to: assertThat(object).isNotNull();
``` ```
- AssertThatBooleanIsTrueOrFalse - AssertThatBooleanCondition
``` ```
from: assertThat(booleanValue).isEqualTo(true/false/Boolean.TRUE/Boolean.FALSE); from: assertThat(booleanValue).isEqualTo(true/false/Boolean.TRUE/Boolean.FALSE);
to: assertThat(booleanValue).isTrue()/isFalse(); to: assertThat(booleanValue).isTrue()/isFalse();
``` ```
- AssertThatInvertedBooleanCondition
```
from: assertThat(!booleanValue).isEqualTo(true/false/Boolean.TRUE/Boolean.FALSE);
from: assertThat(!booleanValue).isTrue()/isFalse();
to: assertThat(booleanValue).isFalse()/isTrue();
```
- AssertThatInstanceOf
```
from: assertThat(object instanceof classname).isEqualTo(true);
from: assertThat(object instanceof classname).isTrue();
to: assertThat(object).isInstanceOf(classname.class);
from: assertThat(object instanceof classname).isEqualTo(false);
from: assertThat(object instanceof classname).isFalse();
to: assertThat(object).isNotInstanceOf(classname.class);
```
- AssertThatStringIsEmpty - AssertThatStringIsEmpty
``` ```
from: assertThat(charSequence/string).isEqualTo(""); from: assertThat(charSequence/string).isEqualTo("");
@@ -77,6 +112,29 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
to: assertThat(charSequence/string).isEmpty(); to: assertThat(charSequence/string).isEmpty();
``` ```
- AssertThatStringExpression
```
from: assertThat(stringActual.isEmpty()).isTrue();
to: assertThat(stringActual).isEmpty();
from: assertThat(stringActual.equals(stringExpected)).isTrue();
from: assertThat(stringActual.contentEquals(charSeqExpected)).isTrue();
to: assertThat(stringActual).isEqualTo(stringExpected);
from: assertThat(stringActual.equalsIgnoreCase(stringExpected)).isTrue();
to: assertThat(stringActual).isEqualToIgnoringCase(stringExpected);
from: assertThat(stringActual.contains(stringExpected)).isTrue();
to: assertThat(stringActual).contains(stringExpected);
from: assertThat(stringActual.startsWith(stringExpected)).isTrue();
to: assertThat(stringActual).startsWith(stringExpected);
from: assertThat(stringActual.endsWith(stringExpected)).isTrue();
to: assertThat(stringActual).endsWith(stringExpected);
```
Analogously with ```isFalse()```.
- AssertThatEnumerableIsEmpty - AssertThatEnumerableIsEmpty
``` ```
from: assertThat(enumerable).hasSize(0); from: assertThat(enumerable).hasSize(0);
@@ -100,7 +158,7 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
to: assertThat(array).hasSameSizeAs(anotherArray); 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); from: assertThat(array.length).isLessThanOrEqualTo(expression);
@@ -115,9 +173,17 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
from: assertThat(array.length).isGreaterThanOrEqualTo(expression); from: assertThat(array.length).isGreaterThanOrEqualTo(expression);
to: assertThat(array).hasSizeGreaterThanOrEqualTo(expression); to: assertThat(array).hasSizeGreaterThanOrEqualTo(expression);
``` ```
and analogously for collections... and analogously for collections, strings and CharSequences, e.g:
- AssertThatBinaryExpressionIsTrueOrFalse ```
from: assertThat("string".length()).isLessThan(1);
to: assertThat("string").isEmpty();
from: assertThat("string".length()).isEqualTo(collection.size())
to: assertThat("string").hasSameSizeAs(collection);
```
- AssertThatBinaryExpression
``` ```
from: assertThat(primActual == primExpected).isTrue(); from: assertThat(primActual == primExpected).isTrue();
to: assertThat(primActual).isEqualTo(primExpected); to: assertThat(primActual).isEqualTo(primExpected);
@@ -130,6 +196,9 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
from: assertThat(null == objActual).isFalse(); from: assertThat(null == objActual).isFalse();
to: assertThat(objActual).isNotNull(); to: assertThat(objActual).isNotNull();
from: assertThat(objActual.equals(objExpected).isTrue();
to: assertThat(objActual).isEqualTo(objExpected);
``` ```
...and many, many more combinations (more than 150). ...and many, many more combinations (more than 150).
@@ -162,34 +231,6 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
to: assertThat(opt).isPresent(); to: assertThat(opt).isPresent();
``` ```
- JUnitAssertToAssertJ
```
assertTrue(condition);
assertTrue(message, condition);
assertFalse(condition);
assertFalse(message, condition);
assertNull(object);
assertNull(message, object);
assertNonNull(object);
assertNonNull(message, object);
assertEquals(expected, actual);
assertEquals(message, expected, actual);
assertEquals(expectedDoubleOrFloat, actualDoubleOrFloat, delta);
assertEquals(message, expectedDoubleOrFloat, actualDoubleOrFloat, delta);
assertNotEquals(unexpected, actual);
assertNotEquals(message, unexpected, actual);
assertNotEquals(unexpectedDoubleOrFloat, actualDoubleOrFloat, delta);
assertNotEquals(message, unexpectedDoubleOrFloat, actualDoubleOrFloat, delta);
assertSame(expected, actual);
assertSame(message, expected, actual);
assertNotSame(unexpected, actual);
assertNotSame(message, unexpected, actual);
assertArrayEquals(expected, actual);
assertArrayEquals(message, expectedArray, actualArray);
assertArrayEquals(expectedDoubleOrFloatArray, actualDoubleOrFloatArray, delta);
assertArrayEquals(message, expectedDoubleOrFloatArray, actualDoubleOrFloatArray, delta);
```
- AssertThatGuavaOptional - AssertThatGuavaOptional
``` ```
from: assertThat(opt.isPresent()).isEqualTo(true); from: assertThat(opt.isPresent()).isEqualTo(true);
@@ -218,11 +259,39 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
AssertJ for Guava needs to be available in the classpath. AssertJ for Guava needs to be available in the classpath.
- JUnitAssertToAssertJ
```
assertTrue(condition);
assertTrue(message, condition);
assertFalse(condition);
assertFalse(message, condition);
assertNull(object);
assertNull(message, object);
assertNonNull(object);
assertNonNull(message, object);
assertEquals(expected, actual);
assertEquals(message, expected, actual);
assertEquals(expectedDoubleOrFloat, actualDoubleOrFloat, delta);
assertEquals(message, expectedDoubleOrFloat, actualDoubleOrFloat, delta);
assertNotEquals(unexpected, actual);
assertNotEquals(message, unexpected, actual);
assertNotEquals(unexpectedDoubleOrFloat, actualDoubleOrFloat, delta);
assertNotEquals(message, unexpectedDoubleOrFloat, actualDoubleOrFloat, delta);
assertSame(expected, actual);
assertSame(message, expected, actual);
assertNotSame(unexpected, actual);
assertNotSame(message, unexpected, actual);
assertArrayEquals(expected, actual);
assertArrayEquals(message, expectedArray, actualArray);
assertArrayEquals(expectedDoubleOrFloatArray, actualDoubleOrFloatArray, delta);
assertArrayEquals(message, expectedDoubleOrFloatArray, actualDoubleOrFloatArray, delta);
```
### Implemented referencing ### Implemented referencing
``` ```
.extracting("field") .extracting("field")
.extracting("outerfield.fieldInsideObjectTypeOfOuterfield.andSoOn") .extracting("outerField.fieldInsideObjectTypeOfOuterField.andSoOn")
.extracting("property") // where the class has a getProperty() (or isProperty() for boolean) method .extracting("property") // where the class has a getProperty() (or isProperty() for boolean) method
.extracting("bareMethod") // supported with AssertJ 13.12.0 .extracting("bareMethod") // supported with AssertJ 13.12.0
.extracting(Extractors.byName("fieldOrPropertyOrBareMethod") .extracting(Extractors.byName("fieldOrPropertyOrBareMethod")
@@ -234,42 +303,39 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
.flatExtracting(Extractors.resultOf("bareMethod") .flatExtracting(Extractors.resultOf("bareMethod")
``` ```
Works on both POJOs and ```Iterable```s/```Array```s. Works on both POJOs and ```Iterable```s/```Array```s.
Implementation is very basic though and does not work with fancy cascaded .extracting() sequences.
If there's demand, I will add it.
## Development notice ## Development notice
Cajon is written in Kotlin 1.3. Cajon is written in Kotlin 1.3.
Cajon is probably the only plugin that uses JUnit 5 Jupiter for unit testing so far (or at least the only one that I'm aware of ;) ). Cajon is probably the only plugin that uses JUnit 5 Jupiter for unit testing so far (or at least the only one that I'm aware of ;) ).
The IntelliJ framework actually uses the JUnit 3 TestCase for plugin testing and I took me quite a while to make it work with JUnit 5. The IntelliJ framework actually uses the JUnit 3 TestCase for plugin testing and it took me quite a while to make it work with JUnit 5.
Feel free to use the code (in package de.platon42.intellij.jupiter) for your projects (with attribution). Feel free to use the code (in package de.platon42.intellij.jupiter) for your projects (with attribution).
## TODO ## TODO
- AssertThatNegatedBooleanExpression - AssumeThatInsteadOfReturn
- AssertThatInstanceOf - Join consecutive assertThats
- AssertThatStringOps
```
from: assertThat(string.contains(foobar)).isTrue();
to: assertThat(string).contains(foobar);
from: assertThat(string.startsWith(foobar)).isTrue();
to: assertThat(string).startsWith(foobar);
from: assertThat(string.endsWith(foobar)).isTrue();
to: assertThat(string).endsWith(foobar);
from: assertThat(string.equalsIgnoreCase(foobar)).isTrue();
to: assertThat(string).isEqualToIgnoringCase(foobar);
```
Analogously with ```isFalse()```.
- AssumeInsteadOfReturn
- Extraction with property names to lambda with Java 8 - Extraction with property names to lambda with Java 8
``` ```
from: assertThat(object).extracting("propOne", "propNoGetter", "propTwo.innerProp")... from: assertThat(object).extracting("propOne", "propNoGetter", "propTwo.innerProp")...
to: assertThat(object).extracting(type::getPropOne, it -> it.propNoGetter, it -> it.getPropTwo().getInnerProp())... to: assertThat(object).extracting(type::getPropOne, it -> it.propNoGetter, it -> it.getPropTwo().getInnerProp())...
``` ```
- Kotlin support
- Kotlin support (right now, however, with less than 100 downloads after a month, this is unlikely to happen)
## Changelog ## Changelog
#### V0.5 (13-Apr-19) #### V0.6 (22-Apr-19)
- New AssertThatStringExpression inspection that will move ```isEmpty()```, ```equals()```, ```equalsIgnoreCase()```, ```contains()```,
```startsWith()```, and ```endsWith()``` out of actual expression.
- Extended AssertThatSize inspection to take ```String```s and ```CharSequences``` into account, too.
- New AssertThatInvertedBooleanCondition inspection that will remove inverted boolean expressions inside ```assertThat()```.
- Renamed a few inspections to better/shorter names.
- New AssertThatInstanceOf inspection that moves instanceof expressions out of ```assertThat()```.
#### V0.5 (18-Apr-19)
- Fixed incompatibility with IDEA versions < 2018.2 (affected AssertThatSizeInspection). Minimal version is now 2017.3. - 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. - Fixed missing Guava imports (if not already present) for AssertThatGuavaInspection. This was a major PITA to get right.
- Added support for referencing and refactoring inside ```.extracting()``` methods with fields, properties and methods (though - Added support for referencing and refactoring inside ```.extracting()``` methods with fields, properties and methods (though
+13 -17
View File
@@ -1,11 +1,11 @@
plugins { plugins {
id 'java' id 'java'
id 'org.jetbrains.intellij' version '0.4.3' id 'org.jetbrains.intellij' version '0.4.8'
id 'org.jetbrains.kotlin.jvm' version '1.3.30' id 'org.jetbrains.kotlin.jvm' version '1.3.30'
} }
group 'de.platon42' group 'de.platon42'
version '0.5' version '0.6'
repositories { repositories {
mavenCentral() mavenCentral()
@@ -33,13 +33,22 @@ compileTestKotlin {
kotlinOptions.jvmTarget = "1.8" kotlinOptions.jvmTarget = "1.8"
} }
intellij { intellij {
version '2019.1' version '2019.1.1'
// pluginName 'Concise AssertJ Optimizing Nitpicker (Cajon)' // pluginName 'Concise AssertJ Optimizing Nitpicker (Cajon)'
updateSinceUntilBuild false updateSinceUntilBuild false
} }
patchPluginXml { patchPluginXml {
changeNotes """ changeNotes """
<h4>V0.6 (22-Apr-19)</h4>
<ul>
<li>New AssertThatStringExpression inspection that will move isEmpty(), equals(), equalsIgnoreCase(), contains(),
startsWith(), and endsWith() out of actual expression.
<li>Extended AssertThatSize inspection to take strings and CharSequences into account, too.
<li>New AssertThatInvertedBooleanCondition inspection that will remove inverted boolean expressions inside assertThat().
<li>Renamed a few inspections to better/shorter names.
<li>New AssertThatInstanceOf inspection that moves instanceof expressions out of assertThat().
</ul>
<h4>V0.5 (18-Apr-19)</h4> <h4>V0.5 (18-Apr-19)</h4>
<ul> <ul>
<li>Fixed incompatibility with IDEA versions < 2018.2 (affected AssertThatSizeInspection). Minimal version is now 2017.3. <li>Fixed incompatibility with IDEA versions < 2018.2 (affected AssertThatSizeInspection). Minimal version is now 2017.3.
@@ -49,20 +58,7 @@ patchPluginXml {
<li>Fixed an exception in batch mode if the description string was the same but for different fixes. <li>Fixed an exception in batch mode if the description string was the same but for different fixes.
Now descriptions are different for quick fixes triggered by AssertThatJava8OptionalInspection and AssertThatGuavaOptionalInspection. Now descriptions are different for quick fixes triggered by AssertThatJava8OptionalInspection and AssertThatGuavaOptionalInspection.
</ul> </ul>
<h4>V0.4 (11-Apr-19)</h4> <p>Full changelog available at <a href="https://github.com/chrisly42/cajon-plugin#changelog">Github project site</a>.</p>
<ul>
<li>Reduced minimal supported IDEA version from 2018.2 to 2017.2.
<li>New inspection AssertThatJava8Optional that operates on Java 8 Optional objects and tries to use contains(), containsSame(), isPresent(), and isNotPresent() instead.
<li>New inspection AssertThatGuavaOptional that operates on Guava Optional objects and tries to use contains(), isPresent(), and isAbsent() instead.
<li>Added support in AssertThatBinaryExpressionIsTrueOrFalse for is(Not)EqualTo(Boolean.TRUE/FALSE).
</ul>
<h4>V0.3 (07-Apr-19)</h4>
<ul>
<li>New inspection AssertThatBinaryExpressionIsTrueOrFalse that will find and fix common binary expressions and equals() statements (more than 150 combinations) inside assertThat().
<li>Merged AssertThatObjectIsNull and AssertThatObjectIsNotNull to AssertThatObjectIsNullOrNotNull.
<li>Support for hasSizeLessThan(), hasSizeLessThanOrEqualTo(), hasSizeGreaterThanOrEqualTo(), and hasSizeGreaterThan() for AssertThatSizeInspection (with AssertJ >=13.2.0).
<li>Really fixed highlighting for JUnit conversion. Sorry.
</ul>
""" """
} }
@@ -45,6 +45,10 @@ class MethodNames {
const val IS_CLOSE_TO = "isCloseTo" const val IS_CLOSE_TO = "isCloseTo"
@NonNls @NonNls
const val IS_NOT_CLOSE_TO = "isNotCloseTo" const val IS_NOT_CLOSE_TO = "isNotCloseTo"
@NonNls
const val IS_INSTANCE_OF = "isInstanceOf"
@NonNls
const val IS_NOT_INSTANCE_OF = "isNotInstanceOf"
@NonNls @NonNls
const val IS_EMPTY = "isEmpty" const val IS_EMPTY = "isEmpty"
@@ -65,8 +69,22 @@ class MethodNames {
@NonNls @NonNls
const val CONTAINS = "contains" const val CONTAINS = "contains"
@NonNls @NonNls
const val DOES_NOT_CONTAIN = "doesNotContain"
@NonNls
const val CONTAINS_EXACTLY = "containsExactly" const val CONTAINS_EXACTLY = "containsExactly"
@NonNls @NonNls
const val IS_EQUAL_TO_IC = "isEqualToIgnoringCase"
@NonNls
const val IS_NOT_EQUAL_TO_IC = "isNotEqualToIgnoringCase"
@NonNls
const val STARTS_WITH = "startsWith"
@NonNls
const val ENDS_WITH = "endsWith"
@NonNls
const val DOES_NOT_START_WITH = "doesNotStartWith"
@NonNls
const val DOES_NOT_END_WITH = "doesNotEndWith"
@NonNls
const val CONTAINS_SAME = "containsSame" const val CONTAINS_SAME = "containsSame"
@NonNls @NonNls
const val IS_PRESENT = "isPresent" const val IS_PRESENT = "isPresent"
@@ -28,7 +28,7 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
const val MORE_CONCISE_MESSAGE_TEMPLATE = "%s() would be more concise than %s()" const val MORE_CONCISE_MESSAGE_TEMPLATE = "%s() would be more concise than %s()"
const val REPLACE_DESCRIPTION_TEMPLATE = "Replace %s() with %s()" const val REPLACE_DESCRIPTION_TEMPLATE = "Replace %s() with %s()"
const val REMOVE_EXPECTED_OUTMOST_DESCRIPTION_TEMPLATE = "Unwrap expected expression and replace %s() with %s()" const val REMOVE_EXPECTED_OUTMOST_DESCRIPTION_TEMPLATE = "Remove unwrapping of expected expression and replace %s() with %s()"
const val REMOVE_ACTUAL_OUTMOST_DESCRIPTION_TEMPLATE = "Unwrap actual expression and replace %s() with %s()" const val REMOVE_ACTUAL_OUTMOST_DESCRIPTION_TEMPLATE = "Unwrap actual expression and replace %s() with %s()"
val TOKEN_TO_ASSERTJ_FOR_PRIMITIVE_MAP = mapOf<IElementType, String>( val TOKEN_TO_ASSERTJ_FOR_PRIMITIVE_MAP = mapOf<IElementType, String>(
@@ -118,6 +118,8 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
val COLLECTION_SIZE = CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_COLLECTION, "size") val COLLECTION_SIZE = CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_COLLECTION, "size")
.parameterCount(0)!! .parameterCount(0)!!
val CHAR_SEQUENCE_LENGTH = CallMatcher.instanceCall("java.lang.CharSequence", "length")
.parameterCount(0)!!
val OBJECT_EQUALS = CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_OBJECT, "equals") val OBJECT_EQUALS = CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_OBJECT, "equals")
.parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)!! .parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)!!
@@ -5,15 +5,13 @@ import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.* import com.intellij.psi.*
import com.intellij.psi.util.TypeConversionUtil import com.intellij.psi.util.TypeConversionUtil
import de.platon42.intellij.plugins.cajon.MethodNames import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.MethodNames.Companion.IS_NOT_NULL
import de.platon42.intellij.plugins.cajon.MethodNames.Companion.IS_NULL
import de.platon42.intellij.plugins.cajon.findOutmostMethodCall import de.platon42.intellij.plugins.cajon.findOutmostMethodCall
import de.platon42.intellij.plugins.cajon.firstArg import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.map import de.platon42.intellij.plugins.cajon.map
import de.platon42.intellij.plugins.cajon.quickfixes.MoveActualOuterExpressionMethodCallQuickFix
import de.platon42.intellij.plugins.cajon.quickfixes.SplitBinaryExpressionMethodCallQuickFix import de.platon42.intellij.plugins.cajon.quickfixes.SplitBinaryExpressionMethodCallQuickFix
import de.platon42.intellij.plugins.cajon.quickfixes.SplitEqualsExpressionMethodCallQuickFix
class AssertThatBinaryExpressionIsTrueOrFalseInspection : AbstractAssertJInspection() { class AssertThatBinaryExpressionInspection : AbstractAssertJInspection() {
companion object { companion object {
private const val DISPLAY_NAME = "Asserting a binary expression" private const val DISPLAY_NAME = "Asserting a binary expression"
@@ -36,8 +34,8 @@ class AssertThatBinaryExpressionIsTrueOrFalseInspection : AbstractAssertJInspect
val assertThatArgument = expression.firstArg val assertThatArgument = expression.firstArg
if (assertThatArgument is PsiMethodCallExpression && OBJECT_EQUALS.test(assertThatArgument)) { if (assertThatArgument is PsiMethodCallExpression && OBJECT_EQUALS.test(assertThatArgument)) {
val replacementMethod = if (expectedResult) MethodNames.IS_EQUAL_TO else MethodNames.IS_NOT_EQUAL_TO val replacementMethod = expectedResult.map(MethodNames.IS_EQUAL_TO, MethodNames.IS_NOT_EQUAL_TO)
registerSplitMethod(holder, expression, "${MethodNames.EQUALS}()", replacementMethod, ::SplitEqualsExpressionMethodCallQuickFix) registerSplitMethod(holder, expression, "${MethodNames.EQUALS}()", replacementMethod, ::MoveActualOuterExpressionMethodCallQuickFix)
return return
} }
@@ -51,7 +49,7 @@ class AssertThatBinaryExpressionIsTrueOrFalseInspection : AbstractAssertJInspect
if (isLeftNull && isRightNull) { if (isLeftNull && isRightNull) {
return return
} else if (isLeftNull || isRightNull) { } else if (isLeftNull || isRightNull) {
val replacementMethod = if (expectedResult) IS_NULL else IS_NOT_NULL val replacementMethod = expectedResult.map(MethodNames.IS_NULL, MethodNames.IS_NOT_NULL)
registerSplitMethod(holder, expression, "binary", replacementMethod) { desc, method -> registerSplitMethod(holder, expression, "binary", replacementMethod) { desc, method ->
SplitBinaryExpressionMethodCallQuickFix(desc, method, pickRightOperand = isLeftNull, noExpectedExpression = true) SplitBinaryExpressionMethodCallQuickFix(desc, method, pickRightOperand = isLeftNull, noExpectedExpression = true)
} }
@@ -63,13 +61,13 @@ class AssertThatBinaryExpressionIsTrueOrFalseInspection : AbstractAssertJInspect
val constantEvaluationHelper = JavaPsiFacade.getInstance(expression.project).constantEvaluationHelper val constantEvaluationHelper = JavaPsiFacade.getInstance(expression.project).constantEvaluationHelper
val swapExpectedAndActual = constantEvaluationHelper.computeConstantExpression(binaryExpression.lOperand) != null val swapExpectedAndActual = constantEvaluationHelper.computeConstantExpression(binaryExpression.lOperand) != null
val tokenType = binaryExpression.operationSign.tokenType val tokenType = binaryExpression.operationTokenType
.let { .let {
if (swapExpectedAndActual) SWAP_SIDE_OF_BINARY_OPERATOR.getOrDefault(it, it) else it if (swapExpectedAndActual) SWAP_SIDE_OF_BINARY_OPERATOR.getOrDefault(it, it) else it
} }
.let { .let {
if (expectedResult) it else INVERT_BINARY_OPERATOR.getOrDefault(it, it) if (expectedResult) it else INVERT_BINARY_OPERATOR.getOrDefault(it, it)
} ?: return }
val mappingToUse = val mappingToUse =
(isPrimitive || isNumericType).map(TOKEN_TO_ASSERTJ_FOR_PRIMITIVE_MAP, TOKEN_TO_ASSERTJ_FOR_OBJECT_MAPPINGS) (isPrimitive || isNumericType).map(TOKEN_TO_ASSERTJ_FOR_PRIMITIVE_MAP, TOKEN_TO_ASSERTJ_FOR_OBJECT_MAPPINGS)
val replacementMethod = mappingToUse[tokenType] ?: return val replacementMethod = mappingToUse[tokenType] ?: return
@@ -10,10 +10,10 @@ import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.firstArg import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.map import de.platon42.intellij.plugins.cajon.map
class AssertThatBooleanIsTrueOrFalseInspection : AbstractAssertJInspection() { class AssertThatBooleanConditionInspection : AbstractAssertJInspection() {
companion object { companion object {
private const val DISPLAY_NAME = "Asserting true or false" private const val DISPLAY_NAME = "Asserting a boolean condition"
} }
override fun getDisplayName() = DISPLAY_NAME override fun getDisplayName() = DISPLAY_NAME
@@ -0,0 +1,54 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiInstanceOfExpression
import com.intellij.psi.PsiMethodCallExpression
import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.findOutmostMethodCall
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.map
import de.platon42.intellij.plugins.cajon.quickfixes.RemoveInstanceOfExpressionQuickFix
class AssertThatInstanceOfInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting a class instance"
private const val REPLACE_INSTANCEOF_DESCRIPTION_TEMPLATE = "Replace instanceof expression by assertThat().%s()"
private const val MOVE_OUT_INSTANCEOF_MESSAGE = "instanceof expression could be moved out of assertThat()"
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
if (!ASSERT_THAT_BOOLEAN.test(expression)) {
return
}
val expectedCallExpression = expression.findOutmostMethodCall() ?: return
val expectedResult = getExpectedBooleanResult(expectedCallExpression) ?: return
if (expression.firstArg is PsiInstanceOfExpression) {
val replacementMethod = expectedResult.map(MethodNames.IS_INSTANCE_OF, MethodNames.IS_NOT_INSTANCE_OF)
registerRemoveInstanceOfMethod(holder, expression, replacementMethod, ::RemoveInstanceOfExpressionQuickFix)
}
}
private fun registerRemoveInstanceOfMethod(
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
replacementMethod: String,
quickFixSupplier: (String, String) -> LocalQuickFix
) {
val description = REPLACE_INSTANCEOF_DESCRIPTION_TEMPLATE.format(replacementMethod)
val quickfix = quickFixSupplier(description, replacementMethod)
holder.registerProblem(expression, MOVE_OUT_INSTANCEOF_MESSAGE, quickfix)
}
}
}
}
@@ -0,0 +1,51 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.*
import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.findOutmostMethodCall
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.map
import de.platon42.intellij.plugins.cajon.quickfixes.RemoveUnaryExpressionQuickFix
class AssertThatInvertedBooleanConditionInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting an inverted boolean condition"
private const val INVERT_CONDITION_DESCRIPTION = "Invert condition in assertThat()"
private const val INVERT_CONDITION_MESSAGE = "Condition inside assertThat() could be inverted"
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
if (!ASSERT_THAT_BOOLEAN.test(expression)) {
return
}
val expectedCallExpression = expression.findOutmostMethodCall() ?: return
val expectedResult = getExpectedBooleanResult(expectedCallExpression) ?: return
val prefixExpression = expression.firstArg as? PsiPrefixExpression ?: return
if (prefixExpression.operationTokenType == JavaTokenType.EXCL) {
val replacementMethod = expectedResult.map(MethodNames.IS_FALSE, MethodNames.IS_TRUE)
registerInvertMethod(holder, expression, replacementMethod, ::RemoveUnaryExpressionQuickFix)
}
}
private fun registerInvertMethod(
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
replacementMethod: String,
quickFixSupplier: (String, String) -> LocalQuickFix
) {
val quickfix = quickFixSupplier(INVERT_CONDITION_DESCRIPTION, replacementMethod)
holder.registerProblem(expression, INVERT_CONDITION_MESSAGE, quickfix)
}
}
}
}
@@ -12,7 +12,7 @@ import de.platon42.intellij.plugins.cajon.quickfixes.ReplaceSizeMethodCallQuickF
class AssertThatSizeInspection : AbstractAssertJInspection() { class AssertThatSizeInspection : AbstractAssertJInspection() {
companion object { 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( private val BONUS_EXPRESSIONS_CALL_MATCHER_MAP = listOf(
IS_LESS_THAN_INT to MethodNames.HAS_SIZE_LESS_THAN, IS_LESS_THAN_INT to MethodNames.HAS_SIZE_LESS_THAN,
@@ -33,7 +33,9 @@ class AssertThatSizeInspection : AbstractAssertJInspection() {
} }
val actualExpression = expression.firstArg 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 expectedCallExpression = expression.findOutmostMethodCall() ?: return
val constValue = calculateConstantParameterValue(expectedCallExpression, 0) val constValue = calculateConstantParameterValue(expectedCallExpression, 0)
if (IS_EQUAL_TO_INT.test(expectedCallExpression)) { if (IS_EQUAL_TO_INT.test(expectedCallExpression)) {
@@ -43,7 +45,9 @@ class AssertThatSizeInspection : AbstractAssertJInspection() {
} }
} else { } else {
val equalToExpression = expectedCallExpression.firstArg 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 -> registerReplaceMethod(holder, expression, expectedCallExpression, MethodNames.HAS_SAME_SIZE_AS) { desc, method ->
ReplaceSizeMethodCallQuickFix(desc, method, expectedIsCollection = true) 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 isCollectionSize(expression: PsiExpression) = (expression is PsiMethodCallExpression) && COLLECTION_SIZE.test(expression)
private fun isArrayLength(expression: PsiExpression): Boolean { private fun isArrayLength(expression: PsiExpression): Boolean {
@@ -0,0 +1,102 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.LocalQuickFix
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.CommonClassNames
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMethodCallExpression
import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.MethodNames
import de.platon42.intellij.plugins.cajon.findOutmostMethodCall
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.quickfixes.MoveActualOuterExpressionMethodCallQuickFix
import de.platon42.intellij.plugins.cajon.quickfixes.RemoveActualOutmostMethodCallQuickFix
class AssertThatStringExpressionInspection : AbstractAssertJInspection() {
companion object {
private const val DISPLAY_NAME = "Asserting a string specific expression"
private const val MOVE_EXPECTED_EXPRESSION_DESCRIPTION_TEMPLATE = "Remove %s() of expected expression and use assertThat().%s() instead"
private const val MOVING_OUT_MESSAGE_TEMPLATE = "Moving %s() expression out of assertThat() would be more concise"
private val MAPPINGS = listOf(
Mapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "isEmpty").parameterCount(0)!!,
MethodNames.IS_EMPTY, MethodNames.IS_NOT_EMPTY, hasExpected = false
),
Mapping(
CallMatcher.anyOf(
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "equals").parameterCount(1)!!,
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "contentEquals").parameterCount(1)!!
),
MethodNames.IS_EQUAL_TO, MethodNames.IS_NOT_EQUAL_TO
),
Mapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "equalsIgnoreCase").parameterTypes(CommonClassNames.JAVA_LANG_STRING)!!,
MethodNames.IS_EQUAL_TO_IC, MethodNames.IS_NOT_EQUAL_TO_IC
),
Mapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "contains").parameterCount(1)!!,
MethodNames.CONTAINS, MethodNames.DOES_NOT_CONTAIN
),
Mapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "startsWith").parameterTypes(CommonClassNames.JAVA_LANG_STRING)!!,
MethodNames.STARTS_WITH, MethodNames.DOES_NOT_START_WITH
),
Mapping(
CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_STRING, "endsWith").parameterTypes(CommonClassNames.JAVA_LANG_STRING)!!,
MethodNames.ENDS_WITH, MethodNames.DOES_NOT_END_WITH
)
)
}
override fun getDisplayName() = DISPLAY_NAME
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean): PsiElementVisitor {
return object : JavaElementVisitor() {
override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
super.visitMethodCallExpression(expression)
if (!ASSERT_THAT_BOOLEAN.test(expression)) {
return
}
val assertThatArgument = expression.firstArg as? PsiMethodCallExpression ?: return
val mapping = MAPPINGS.firstOrNull { it.callMatcher.test(assertThatArgument) } ?: return
val expectedCallExpression = expression.findOutmostMethodCall() ?: return
val expectedResult = getExpectedBooleanResult(expectedCallExpression) ?: return
val replacementMethod = if (expectedResult) mapping.replacementForTrue else mapping.replacementForFalse
if (mapping.hasExpected) {
registerMoveOutMethod(holder, expression, assertThatArgument, replacementMethod, ::MoveActualOuterExpressionMethodCallQuickFix)
} else {
registerMoveOutMethod(holder, expression, assertThatArgument, replacementMethod) { desc, method ->
RemoveActualOutmostMethodCallQuickFix(desc, method, noExpectedExpression = true)
}
}
}
private fun registerMoveOutMethod(
holder: ProblemsHolder,
expression: PsiMethodCallExpression,
oldActualExpression: PsiMethodCallExpression,
replacementMethod: String,
quickFixSupplier: (String, String) -> LocalQuickFix
) {
val originalMethod = getOriginalMethodName(oldActualExpression) ?: return
val description = MOVE_EXPECTED_EXPRESSION_DESCRIPTION_TEMPLATE.format(originalMethod, replacementMethod)
val message = MOVING_OUT_MESSAGE_TEMPLATE.format(originalMethod)
val quickfix = quickFixSupplier(description, replacementMethod)
holder.registerProblem(expression, message, quickfix)
}
}
}
private class Mapping(
val callMatcher: CallMatcher,
val replacementForTrue: String,
val replacementForFalse: String,
val hasExpected: Boolean = true
)
}
@@ -5,17 +5,17 @@ import com.intellij.openapi.project.Project
import com.intellij.psi.PsiMethodCallExpression import com.intellij.psi.PsiMethodCallExpression
import de.platon42.intellij.plugins.cajon.* import de.platon42.intellij.plugins.cajon.*
class SplitEqualsExpressionMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) { class MoveActualOuterExpressionMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement val element = descriptor.startElement
val methodCallExpression = element as? PsiMethodCallExpression ?: return val methodCallExpression = element as? PsiMethodCallExpression ?: return
val equalsMethodCall = methodCallExpression.firstArg as? PsiMethodCallExpression ?: return val assertExpression = methodCallExpression.firstArg as? PsiMethodCallExpression ?: return
val expectedArgument = equalsMethodCall.firstArg.copy() val assertExpressionArg = assertExpression.firstArg.copy()
equalsMethodCall.replace(equalsMethodCall.qualifierExpression) assertExpression.replace(assertExpression.qualifierExpression)
val oldExpectedExpression = element.findOutmostMethodCall() ?: return val oldExpectedExpression = element.findOutmostMethodCall() ?: return
val expectedExpression = createExpectedMethodCall(element, replacementMethod, expectedArgument) val expectedExpression = createExpectedMethodCall(element, replacementMethod, assertExpressionArg)
expectedExpression.replaceQualifierFromMethodCall(oldExpectedExpression) expectedExpression.replaceQualifierFromMethodCall(oldExpectedExpression)
oldExpectedExpression.replace(expectedExpression) oldExpectedExpression.replace(expectedExpression)
} }
@@ -0,0 +1,36 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiInstanceOfExpression
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiParenthesizedExpression
import de.platon42.intellij.plugins.cajon.createExpectedMethodCall
import de.platon42.intellij.plugins.cajon.findOutmostMethodCall
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.replaceQualifierFromMethodCall
class RemoveInstanceOfExpressionQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement
val methodCallExpression = element as? PsiMethodCallExpression ?: return
val assertExpression = methodCallExpression.firstArg as? PsiInstanceOfExpression ?: return
val expectedClass = assertExpression.checkType ?: return
val factory = JavaPsiFacade.getElementFactory(project)
val classObjectAccess = factory.createExpressionFromText("${expectedClass.type.canonicalText}.class", null)
var operand = assertExpression.operand
while (operand is PsiParenthesizedExpression) {
operand = operand.expression ?: return
}
assertExpression.replace(operand)
val oldExpectedExpression = element.findOutmostMethodCall() ?: return
val expectedExpression = createExpectedMethodCall(element, replacementMethod, classObjectAccess)
expectedExpression.replaceQualifierFromMethodCall(oldExpectedExpression)
oldExpectedExpression.replace(expectedExpression)
}
}
@@ -0,0 +1,30 @@
package de.platon42.intellij.plugins.cajon.quickfixes
import com.intellij.codeInspection.ProblemDescriptor
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.PsiParenthesizedExpression
import com.intellij.psi.PsiUnaryExpression
import de.platon42.intellij.plugins.cajon.createExpectedMethodCall
import de.platon42.intellij.plugins.cajon.findOutmostMethodCall
import de.platon42.intellij.plugins.cajon.firstArg
import de.platon42.intellij.plugins.cajon.replaceQualifierFromMethodCall
class RemoveUnaryExpressionQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement
val methodCallExpression = element as? PsiMethodCallExpression ?: return
val assertExpression = methodCallExpression.firstArg as? PsiUnaryExpression ?: return
var operand = assertExpression.operand ?: return
while (operand is PsiParenthesizedExpression) {
operand = operand.expression ?: return
}
assertExpression.replace(operand)
val oldExpectedExpression = element.findOutmostMethodCall() ?: return
val expectedExpression = createExpectedMethodCall(element, replacementMethod)
expectedExpression.replaceQualifierFromMethodCall(oldExpectedExpression)
oldExpectedExpression.replace(expectedExpression)
}
}
@@ -6,10 +6,7 @@ import com.intellij.psi.PsiMethodCallExpression
import de.platon42.intellij.plugins.cajon.createExpectedMethodCall import de.platon42.intellij.plugins.cajon.createExpectedMethodCall
import de.platon42.intellij.plugins.cajon.replaceQualifierFromMethodCall import de.platon42.intellij.plugins.cajon.replaceQualifierFromMethodCall
class ReplaceSimpleMethodCallQuickFix( class ReplaceSimpleMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
description: String,
private val replacementMethod: String
) : AbstractCommonQuickFix(description) {
override fun applyFix(project: Project, descriptor: ProblemDescriptor) { override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val element = descriptor.startElement val element = descriptor.startElement
@@ -7,6 +7,7 @@ import com.intellij.psi.*
import com.intellij.psi.util.PropertyUtilBase import com.intellij.psi.util.PropertyUtilBase
import com.intellij.psi.util.PsiTreeUtil import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.PsiTypesUtil import com.intellij.psi.util.PsiTypesUtil
import com.intellij.util.ArrayUtil
import com.intellij.util.ProcessingContext import com.intellij.util.ProcessingContext
import com.siyeh.ig.callMatcher.CallMatcher import com.siyeh.ig.callMatcher.CallMatcher
import de.platon42.intellij.plugins.cajon.AssertJClassNames import de.platon42.intellij.plugins.cajon.AssertJClassNames
@@ -81,6 +82,10 @@ class ExtractorReferenceContributor : PsiReferenceContributor() {
class ExtractorReference(literal: PsiLiteralExpression, range: TextRange, private val targets: List<PsiElement>) : class ExtractorReference(literal: PsiLiteralExpression, range: TextRange, private val targets: List<PsiElement>) :
PsiPolyVariantReferenceBase<PsiLiteralExpression>(literal, range, true) { PsiPolyVariantReferenceBase<PsiLiteralExpression>(literal, range, true) {
override fun getVariants(): Array<Any> {
return ArrayUtil.EMPTY_OBJECT_ARRAY
}
override fun resolve(): PsiElement? { override fun resolve(): PsiElement? {
return multiResolve(false).map(ResolveResult::getElement).firstOrNull() return multiResolve(false).map(ResolveResult::getElement).firstOrNull()
} }
+14 -7
View File
@@ -5,13 +5,14 @@
<description><![CDATA[ <description><![CDATA[
Cajon is an IntelliJ IDEA Plugin for shortening and optimizing AssertJ assertions. Cajon is an IntelliJ IDEA Plugin for shortening and optimizing AssertJ assertions.
It adds inspections and quick fixes to fully make use of the AssertJ methods It adds several inspections and quick fixes to fully use the fluent assertion methods
to make the intention clear and concise. It can also convert JUnit 4 assertions to AssertJ. and thus makes the intention clear and concise, also generating better messages on test failures.
It can also be used to convert JUnit 4 assertions to AssertJ.
It supports referencing inside extracting()-methods with strings, adding refactoring safety. It supports referencing inside extracting()-methods with strings, adding refactoring safety.
]]></description> ]]></description>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description --> <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/build_number_ranges.html for description -->
<idea-version since-build="173.2290.1"/> <idea-version since-build="173.2696.26"/>
<!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html <!-- please see http://www.jetbrains.org/intellij/sdk/docs/basics/getting_started/plugin_compatibility.html
on how to target different products --> on how to target different products -->
@@ -23,8 +24,12 @@
<psi.referenceContributor implementation="de.platon42.intellij.plugins.cajon.references.ExtractorReferenceContributor"/> <psi.referenceContributor implementation="de.platon42.intellij.plugins.cajon.references.ExtractorReferenceContributor"/>
<localInspection groupPath="Java" shortName="AssertThatObjectIsNullOrNotNull" enabledByDefault="true" level="WARNING" <localInspection groupPath="Java" shortName="AssertThatObjectIsNullOrNotNull" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatObjectIsNullOrNotNullInspection"/> implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatObjectIsNullOrNotNullInspection"/>
<localInspection groupPath="Java" shortName="AssertThatBooleanIsTrueOrFalse" enabledByDefault="true" level="WARNING" <localInspection groupPath="Java" shortName="AssertThatBooleanCondition" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBooleanIsTrueOrFalseInspection"/> implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBooleanConditionInspection"/>
<localInspection groupPath="Java" shortName="AssertThatInvertedBooleanCondition" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatInvertedBooleanConditionInspection"/>
<localInspection groupPath="Java" shortName="AssertThatInstanceOf" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatInstanceOfInspection"/>
<localInspection groupPath="Java" shortName="AssertThatStringIsEmpty" enabledByDefault="true" level="WARNING" <localInspection groupPath="Java" shortName="AssertThatStringIsEmpty" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatStringIsEmptyInspection"/> implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatStringIsEmptyInspection"/>
<localInspection groupPath="Java" shortName="AssertThatEnumerableIsEmpty" enabledByDefault="true" level="WARNING" <localInspection groupPath="Java" shortName="AssertThatEnumerableIsEmpty" enabledByDefault="true" level="WARNING"
@@ -33,8 +38,10 @@
<localInspection groupPath="Java" shortName="AssertThatSize" enabledByDefault="true" level="WARNING" <localInspection groupPath="Java" shortName="AssertThatSize" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatSizeInspection"/> implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatSizeInspection"/>
<localInspection groupPath="Java" shortName="AssertThatBinaryExpressionIsTrueOrFalse" enabledByDefault="true" level="WARNING" <localInspection groupPath="Java" shortName="AssertThatBinaryExpression" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBinaryExpressionIsTrueOrFalseInspection"/> implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBinaryExpressionInspection"/>
<localInspection groupPath="Java" shortName="AssertThatStringExpression" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatStringExpressionInspection"/>
<localInspection groupPath="Java" shortName="AssertThatJava8Optional" enabledByDefault="true" level="WARNING" <localInspection groupPath="Java" shortName="AssertThatJava8Optional" enabledByDefault="true" level="WARNING"
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatJava8OptionalInspection"/> implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatJava8OptionalInspection"/>
@@ -0,0 +1,7 @@
<html>
<body>
Turns assertThat(object instanceof classname).isEqualTo(true/false) into assertThat(object).is(Not)InstanceOf(classname.class).
<!-- tooltip end -->
Also works with constant expressions and Boolean.TRUE/FALSE.
</body>
</html>
@@ -0,0 +1,7 @@
<html>
<body>
Turns assertThat(!condition).isEqualTo(true/false) into assertThat(condition).isFalse()/isTrue().
<!-- tooltip end -->
Also works with constant expressions and Boolean.TRUE/FALSE.
</body>
</html>
@@ -1,6 +1,7 @@
<html> <html>
<body> <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 --> <!-- tooltip end -->
Several more conversions are available with AssertJ 13.2.0 or later.
</body> </body>
</html> </html>
@@ -0,0 +1,7 @@
<html>
<body>
Turns assertThat(string.someMethod(arg)).isTrue/isFalse() into assertThat(string).someMethod(arg).
<!-- tooltip end -->
<br>someMethod() can be equals(), equalsIgnoreCase(), contentEquals(), contains(), startsWith(), and endsWith().
</body>
</html>
@@ -35,6 +35,7 @@ public class Playground {
assertThat(new ArrayList<String>()).hasSizeGreaterThan(1); assertThat(new ArrayList<String>()).hasSizeGreaterThan(1);
assertThat(new ArrayList<String>()).hasSameSizeAs(new ArrayList<>()); 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]);
assertThat(new Long[1]).as("etc").hasSameSizeAs(new Long[2]);
} }
private void sizeOfArray() { private void sizeOfArray() {
@@ -146,6 +147,41 @@ public class Playground {
assertThat(foo).doesNotEndWith("foobar"); assertThat(foo).doesNotEndWith("foobar");
assertThat(foo.equalsIgnoreCase("foo")).isFalse(); assertThat(foo.equalsIgnoreCase("foo")).isFalse();
assertThat(foo).isNotEqualToIgnoringCase("foo"); 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() { private void java8Optional() {
@@ -206,7 +242,20 @@ public class Playground {
assertThat(opt).isAbsent(); assertThat(opt).isAbsent();
} }
private void assertThatInstance() {
String foo = "foo";
assertThat(foo instanceof String).isTrue();
assertThat(foo).isInstanceOf(String.class);
assertThat(foo).isNotInstanceOf(String.class);
}
private void junitAssertions() { private void junitAssertions() {
assertFalse(!(new int[3].length == new ArrayList<Integer>().size()));
assertThat(!(new int[3].length == new ArrayList<Integer>().size())).isFalse();
assertThat((new int[3].length == new ArrayList<Integer>().size())).isTrue();
assertThat(new int[3].length).isEqualTo(new ArrayList<Integer>().size());
assertThat(new int[3]).hasSameSizeAs(new ArrayList<Integer>());
assertTrue(true); assertTrue(true);
assertTrue("message", true); assertTrue("message", true);
assertFalse(true); assertFalse(true);
@@ -315,7 +364,6 @@ public class Playground {
assertThat(new Object()).extracting(Object::toString, Object::hashCode); assertThat(new Object()).extracting(Object::toString, Object::hashCode);
} }
private void findReferences() { private void findReferences() {
Contact contact = new Contact(); Contact contact = new Contact();
List<Contact> contactList = Collections.emptyList(); List<Contact> contactList = Collections.emptyList();
@@ -6,13 +6,13 @@ import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
internal class AssertThatBinaryExpressionIsTrueOrFalseInspectionTest : AbstractCajonTest() { internal class AssertThatBinaryExpressionInspectionTest : AbstractCajonTest() {
@Test @Test
@TestDataSubPath("inspections/BinaryExpression") @TestDataSubPath("inspections/BinaryExpression")
internal fun assertThat_of_binary_expression_can_be_moved_out(@MyFixture myFixture: JavaCodeInsightTestFixture) { internal fun assertThat_of_binary_expression_can_be_moved_out(@MyFixture myFixture: JavaCodeInsightTestFixture) {
runTest { runTest {
myFixture.enableInspections(AssertThatBinaryExpressionIsTrueOrFalseInspection::class.java) myFixture.enableInspections(AssertThatBinaryExpressionInspection::class.java)
myFixture.configureByFile("BinaryExpressionBefore.java") myFixture.configureByFile("BinaryExpressionBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Split binary expression out of assertThat()"), 148) executeQuickFixes(myFixture, Regex.fromLiteral("Split binary expression out of assertThat()"), 148)
executeQuickFixes(myFixture, Regex.fromLiteral("Split equals() expression out of assertThat()"), 12) executeQuickFixes(myFixture, Regex.fromLiteral("Split equals() expression out of assertThat()"), 12)
@@ -6,19 +6,19 @@ import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
internal class AssertThatBooleanIsTrueOrFalseInspectionTest : AbstractCajonTest() { internal class AssertThatBooleanConditionInspectionTest : AbstractCajonTest() {
@Test @Test
@TestDataSubPath("inspections/BooleanIsTrueOrFalse") @TestDataSubPath("inspections/BooleanCondition")
internal fun assertThat_with_isEqualTo_true_or_false_can_use_isTrue_or_isFalse(@MyFixture myFixture: JavaCodeInsightTestFixture) { internal fun assertThat_with_isEqualTo_true_or_false_can_use_isTrue_or_isFalse(@MyFixture myFixture: JavaCodeInsightTestFixture) {
runTest { runTest {
myFixture.enableInspections(AssertThatBooleanIsTrueOrFalseInspection::class.java) myFixture.enableInspections(AssertThatBooleanConditionInspection::class.java)
myFixture.configureByFile("BooleanIsTrueOrFalseBefore.java") myFixture.configureByFile("BooleanConditionBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isTrue()"), 4) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isTrue()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isFalse()"), 5) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isFalse()"), 5)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isTrue()"), 4) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isTrue()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isFalse()"), 4) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isFalse()"), 4)
myFixture.checkResultByFile("BooleanIsTrueOrFalseAfter.java") myFixture.checkResultByFile("BooleanConditionAfter.java")
} }
} }
} }
@@ -9,14 +9,14 @@ import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
@AddLocalJarToModule(com.google.common.base.Optional::class, org.assertj.guava.api.Assertions::class, Assertions::class) @AddLocalJarToModule(com.google.common.base.Optional::class, org.assertj.guava.api.Assertions::class, Assertions::class)
@TestDataSubPath("inspections/AssertThatGuavaOptional") @TestDataSubPath("inspections/GuavaOptional")
internal class AssertThatGuavaOptionalInspectionTest : AbstractCajonTest() { internal class AssertThatGuavaOptionalInspectionTest : AbstractCajonTest() {
@Test @Test
internal fun assertThat_get_or_isPresent_for_Guava_Optional_can_be_simplified(@MyFixture myFixture: JavaCodeInsightTestFixture) { internal fun assertThat_get_or_isPresent_for_Guava_Optional_can_be_simplified(@MyFixture myFixture: JavaCodeInsightTestFixture) {
runTest { runTest {
myFixture.enableInspections(AssertThatGuavaOptionalInspection::class.java) myFixture.enableInspections(AssertThatGuavaOptionalInspection::class.java)
myFixture.configureByFile("AssertThatGuavaOptionalBefore.java") myFixture.configureByFile("GuavaOptionalBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isEqualTo() with isPresent()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isEqualTo() with isPresent()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isNotEqualTo() with isPresent()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isNotEqualTo() with isPresent()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isPresent()"), 3) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isPresent()"), 3)
@@ -26,8 +26,8 @@ internal class AssertThatGuavaOptionalInspectionTest : AbstractCajonTest() {
executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isTrue() with isPresent()"), 1) executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isTrue() with isPresent()"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isFalse() with isAbsent()"), 1) executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isFalse() with isAbsent()"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isEqualTo() with contains()"), 1) executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isEqualTo() with contains()"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap expected expression and replace isEqualTo() with contains()"), 6) executeQuickFixes(myFixture, Regex.fromLiteral("Remove unwrapping of expected expression and replace isEqualTo() with contains()"), 6)
myFixture.checkResultByFile("AssertThatGuavaOptionalAfter.java") myFixture.checkResultByFile("GuavaOptionalAfter.java")
} }
} }
@@ -0,0 +1,22 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import de.platon42.intellij.jupiter.MyFixture
import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test
internal class AssertThatInstanceOfInspectionTest : AbstractCajonTest() {
@Test
@TestDataSubPath("inspections/InstanceOf")
internal fun assertThat_with_instanceof_can_be_moved_out(@MyFixture myFixture: JavaCodeInsightTestFixture) {
runTest {
myFixture.enableInspections(AssertThatInstanceOfInspection::class.java)
myFixture.configureByFile("InstanceOfBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Replace instanceof expression by assertThat().isInstanceOf()"), 5)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace instanceof expression by assertThat().isNotInstanceOf()"), 6)
myFixture.checkResultByFile("InstanceOfAfter.java")
}
}
}
@@ -0,0 +1,21 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import de.platon42.intellij.jupiter.MyFixture
import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test
internal class AssertThatInvertedBooleanConditionInspectionTest : AbstractCajonTest() {
@Test
@TestDataSubPath("inspections/InvertedBooleanCondition")
internal fun assertThat_with_inverted_boolean_condition_can_be_inverted(@MyFixture myFixture: JavaCodeInsightTestFixture) {
runTest {
myFixture.enableInspections(AssertThatInvertedBooleanConditionInspection::class.java)
myFixture.configureByFile("InvertedBooleanConditionBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Invert condition in assertThat()"), 21)
myFixture.checkResultByFile("InvertedBooleanConditionAfter.java")
}
}
}
@@ -9,11 +9,11 @@ import org.junit.jupiter.api.Test
internal class AssertThatJava8OptionalInspectionTest : AbstractCajonTest() { internal class AssertThatJava8OptionalInspectionTest : AbstractCajonTest() {
@Test @Test
@TestDataSubPath("inspections/AssertThatJava8Optional") @TestDataSubPath("inspections/Java8Optional")
internal fun assertThat_get_or_isPresent_for_Java8_Optional_can_be_simplified(@MyFixture myFixture: JavaCodeInsightTestFixture) { internal fun assertThat_get_or_isPresent_for_Java8_Optional_can_be_simplified(@MyFixture myFixture: JavaCodeInsightTestFixture) {
runTest { runTest {
myFixture.enableInspections(AssertThatJava8OptionalInspection::class.java) myFixture.enableInspections(AssertThatJava8OptionalInspection::class.java)
myFixture.configureByFile("AssertThatJava8OptionalBefore.java") myFixture.configureByFile("Java8OptionalBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isEqualTo() with isPresent()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isEqualTo() with isPresent()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isNotEqualTo() with isPresent()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isNotEqualTo() with isPresent()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isEqualTo() with isNotPresent()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isEqualTo() with isNotPresent()"), 2)
@@ -22,10 +22,10 @@ internal class AssertThatJava8OptionalInspectionTest : AbstractCajonTest() {
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isNotPresent()"), 1) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isNotPresent()"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isPresent()"), 1) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotEqualTo() with isPresent()"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isFalse() with isNotPresent()"), 1) executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isFalse() with isNotPresent()"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap expected expression and replace isEqualTo() with contains()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Remove unwrapping of expected expression and replace isEqualTo() with contains()"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isEqualTo() with contains()"), 1) executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isEqualTo() with contains()"), 1)
executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isSameAs() with containsSame()"), 1) executeQuickFixes(myFixture, Regex.fromLiteral("Unwrap actual expression and replace isSameAs() with containsSame()"), 1)
myFixture.checkResultByFile("AssertThatJava8OptionalAfter.java") myFixture.checkResultByFile("Java8OptionalAfter.java")
} }
} }
} }
@@ -9,25 +9,25 @@ import org.junit.jupiter.api.Test
internal class AssertThatSizeInspectionTest : AbstractCajonTest() { internal class AssertThatSizeInspectionTest : AbstractCajonTest() {
@Test @Test
@TestDataSubPath("inspections/AssertThatSize") @TestDataSubPath("inspections/Size")
internal fun assertThat_size_of_array_or_collection_can_be_simplified(@MyFixture myFixture: JavaCodeInsightTestFixture) { internal fun assertThat_size_of_array_or_collection_can_be_simplified(@MyFixture myFixture: JavaCodeInsightTestFixture) {
runTest { runTest {
myFixture.enableInspections(AssertThatSizeInspection::class.java) myFixture.enableInspections(AssertThatSizeInspection::class.java)
myFixture.configureByFile("AssertThatSizeBefore.java") myFixture.configureByFile("SizeBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isEmpty()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isEmpty()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isZero() with isEmpty()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isZero() with isEmpty()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotZero() with isNotEmpty()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotZero() with isNotEmpty()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThan() with isNotEmpty()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThan() with isNotEmpty()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThanOrEqualTo() with isNotEmpty()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThanOrEqualTo() with isNotEmpty()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThan() with isEmpty()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThan() with isEmpty()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThanOrEqualTo() with isEmpty()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThanOrEqualTo() with isEmpty()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with hasSameSizeAs()"), 4) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with hasSameSizeAs()"), 12)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with hasSize()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with hasSize()"), 8)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThan() with hasSizeGreaterThan()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThan() with hasSizeGreaterThan()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThanOrEqualTo() with hasSizeGreaterThanOrEqualTo()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThanOrEqualTo() with hasSizeGreaterThanOrEqualTo()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThan() with hasSizeLessThan()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThan() with hasSizeLessThan()"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThanOrEqualTo() with hasSizeLessThanOrEqualTo()"), 2) executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThanOrEqualTo() with hasSizeLessThanOrEqualTo()"), 4)
myFixture.checkResultByFile("AssertThatSizeAfter.java") myFixture.checkResultByFile("SizeAfter.java")
} }
} }
} }
@@ -0,0 +1,34 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
import de.platon42.intellij.jupiter.MyFixture
import de.platon42.intellij.jupiter.TestDataSubPath
import de.platon42.intellij.plugins.cajon.AbstractCajonTest
import org.junit.jupiter.api.Test
internal class AssertThatStringExpressionInspectionTest : AbstractCajonTest() {
@Test
@TestDataSubPath("inspections/StringExpression")
internal fun assertThat_with_certain_String_methods(@MyFixture myFixture: JavaCodeInsightTestFixture) {
runTest {
myFixture.enableInspections(AssertThatStringExpressionInspection::class.java)
myFixture.configureByFile("StringExpressionBefore.java")
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isEmpty() of expected expression and use assertThat().isEmpty() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove equals() of expected expression and use assertThat().isEqualTo() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove equalsIgnoreCase() of expected expression and use assertThat().isEqualToIgnoringCase() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove contentEquals() of expected expression and use assertThat().isEqualTo() instead"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove contains() of expected expression and use assertThat().contains() instead"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove startsWith() of expected expression and use assertThat().startsWith() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove endsWith() of expected expression and use assertThat().endsWith() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove isEmpty() of expected expression and use assertThat().isNotEmpty() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove equals() of expected expression and use assertThat().isNotEqualTo() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove equalsIgnoreCase() of expected expression and use assertThat().isNotEqualToIgnoringCase() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove contentEquals() of expected expression and use assertThat().isNotEqualTo() instead"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove contains() of expected expression and use assertThat().doesNotContain() instead"), 4)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove startsWith() of expected expression and use assertThat().doesNotStartWith() instead"), 2)
executeQuickFixes(myFixture, Regex.fromLiteral("Remove endsWith() of expected expression and use assertThat().doesNotEndWith() instead"), 2)
myFixture.checkResultByFile("StringExpressionAfter.java")
}
}
}
@@ -1,43 +0,0 @@
import java.util.ArrayList;
import static org.assertj.core.api.Assertions.assertThat;
public class AssertThatSize {
private void assertThatSize() {
ArrayList<String> list = new ArrayList<>();
ArrayList<String> otherList = new ArrayList<>();
long[] array = new long[5];
long[] otherArray = new long[4];
assertThat(list).isEmpty();
assertThat(list).isEmpty();
assertThat(list).isNotEmpty();
assertThat(list).as("hi").isNotEmpty();
assertThat(list).isNotEmpty();
assertThat(list).isEmpty();
assertThat(list).isEmpty();
assertThat(list).hasSameSizeAs(otherList);
assertThat(list).hasSameSizeAs(array);
assertThat(list).hasSize(1);
assertThat(list).hasSizeGreaterThan(list.size() * 2);
assertThat(list).hasSizeGreaterThanOrEqualTo(list.size() * 2);
assertThat(list).hasSizeLessThan(list.size() * 2);
assertThat(list).hasSizeLessThanOrEqualTo(list.size() * 2);
assertThat(array).isEmpty();
assertThat(array).isEmpty();
assertThat(array).isNotEmpty();
assertThat(array).as("hi").isNotEmpty();
assertThat(array).isNotEmpty();
assertThat(array).isEmpty();
assertThat(array).isEmpty();
assertThat(array).hasSameSizeAs(list);
assertThat(array).hasSameSizeAs(otherArray);
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);
}
}
@@ -1,43 +0,0 @@
import java.util.ArrayList;
import static org.assertj.core.api.Assertions.assertThat;
public class AssertThatSize {
private void assertThatSize() {
ArrayList<String> list = new ArrayList<>();
ArrayList<String> otherList = new ArrayList<>();
long[] array = new long[5];
long[] otherArray = new long[4];
assertThat(list.size()).isEqualTo(0);
assertThat(list.size()).isZero();
assertThat(list.size()).isNotZero();
assertThat(list.size()).as("hi").isGreaterThan(0);
assertThat(list.size()).isGreaterThanOrEqualTo(1);
assertThat(list.size()).isLessThan(1);
assertThat(list.size()).isLessThanOrEqualTo(0);
assertThat(list.size()).isEqualTo(otherList.size());
assertThat(list.size()).isEqualTo(array.length);
assertThat(list.size()).isEqualTo(1);
assertThat(list.size()).isGreaterThan(list.size() * 2);
assertThat(list.size()).isGreaterThanOrEqualTo(list.size() * 2);
assertThat(list.size()).isLessThan(list.size() * 2);
assertThat(list.size()).isLessThanOrEqualTo(list.size() * 2);
assertThat(array.length).isEqualTo(0);
assertThat(array.length).isZero();
assertThat(array.length).isNotZero();
assertThat(array.length).as("hi").isGreaterThan(0);
assertThat(array.length).isGreaterThanOrEqualTo(1);
assertThat(array.length).isLessThan(1);
assertThat(array.length).isLessThanOrEqualTo(0);
assertThat(array.length).isEqualTo(list.size());
assertThat(array.length).isEqualTo(otherArray.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);
}
}
@@ -1,8 +1,8 @@
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public class BooleanIsTrueOrFalse { public class BooleanCondition {
private void booleanIsTrueOrFalse() { private void booleanCondition() {
boolean primitive = false; boolean primitive = false;
Boolean object = Boolean.TRUE; Boolean object = Boolean.TRUE;
@@ -1,8 +1,8 @@
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public class BooleanIsTrueOrFalse { public class BooleanCondition {
private void booleanIsTrueOrFalse() { private void booleanCondition() {
boolean primitive = false; boolean primitive = false;
Boolean object = Boolean.TRUE; Boolean object = Boolean.TRUE;
@@ -3,9 +3,9 @@ import com.google.common.base.Optional;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat; import static org.assertj.guava.api.Assertions.assertThat;
public class AssertThatGuavaOptional { public class GuavaOptional {
private void assertThatGuavaOptional() { private void guavaOptional() {
Optional<String> opt = Optional.absent(); Optional<String> opt = Optional.absent();
assertThat(opt).isPresent(); assertThat(opt).isPresent();
@@ -3,9 +3,9 @@ import com.google.common.base.Optional;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat; import static org.assertj.guava.api.Assertions.assertThat;
public class AssertThatGuavaOptional { public class GuavaOptional {
private void assertThatGuavaOptional() { private void guavaOptional() {
Optional<String> opt = Optional.absent(); Optional<String> opt = Optional.absent();
assertThat(opt.isPresent()).isEqualTo(true); assertThat(opt.isPresent()).isEqualTo(true);
@@ -3,9 +3,9 @@ import com.google.common.base.Optional;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat; import static org.assertj.guava.api.Assertions.assertThat;
public class AssertThatGuavaOptional { public class GuavaOptional {
private void assertThatGuavaOptional() { private void guavaOptional() {
Optional<String> opt = Optional.absent(); Optional<String> opt = Optional.absent();
assertThat(opt).contains("foo"); assertThat(opt).contains("foo");
@@ -2,9 +2,9 @@ import com.google.common.base.Optional;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public class AssertThatGuavaOptional { public class GuavaOptional {
private void assertThatGuavaOptional() { private void guavaOptional() {
Optional<String> opt = Optional.absent(); Optional<String> opt = Optional.absent();
assertThat(opt).isEqualTo(Optional.of("foo")); assertThat(opt).isEqualTo(Optional.of("foo"));
@@ -0,0 +1,22 @@
import static org.assertj.core.api.Assertions.assertThat;
public class InstanceOf {
private void instanceOf() {
Boolean object = Boolean.TRUE;
assertThat(object).isInstanceOf(Boolean.class);
assertThat(object).isInstanceOf(Boolean.class);
assertThat(object).isInstanceOf(Boolean.class);
assertThat(object).isInstanceOf(Boolean.class);
assertThat(object).isInstanceOf(Boolean.class);
assertThat(object).isNotInstanceOf(Boolean.class);
assertThat(object).isNotInstanceOf(Boolean.class);
assertThat(object).isNotInstanceOf(Boolean.class);
assertThat(object).isNotInstanceOf(Boolean.class);
assertThat(object).isNotInstanceOf(Boolean.class);
assertThat(object).as("nah").isNotInstanceOf(Boolean.class);
}
}
@@ -0,0 +1,22 @@
import static org.assertj.core.api.Assertions.assertThat;
public class InstanceOf {
private void instanceOf() {
Boolean object = Boolean.TRUE;
assertThat(object instanceof Boolean).isEqualTo(Boolean.TRUE);
assertThat(object instanceof Boolean).isEqualTo(true);
assertThat(object instanceof Boolean).isNotEqualTo(Boolean.FALSE);
assertThat(object instanceof Boolean).isNotEqualTo(false);
assertThat(object instanceof Boolean).isTrue();
assertThat(object instanceof Boolean).isEqualTo(Boolean.FALSE);
assertThat(object instanceof Boolean).isEqualTo(false);
assertThat(object instanceof Boolean).isNotEqualTo(Boolean.TRUE);
assertThat(object instanceof Boolean).isNotEqualTo(true);
assertThat(object instanceof Boolean).isFalse();
assertThat(((object)) instanceof Boolean).as("nah").isEqualTo(true && !true);
}
}
@@ -0,0 +1,34 @@
import static org.assertj.core.api.Assertions.assertThat;
public class InvertedBooleanCondition {
private void invertedBooleanCondition() {
boolean primitive = false;
Boolean object = Boolean.TRUE;
assertThat(primitive).isFalse();
assertThat(primitive).isFalse();
assertThat(primitive).isFalse();
assertThat(primitive).isFalse();
assertThat(primitive).isFalse();
assertThat(object).isFalse();
assertThat(object).isFalse();
assertThat(object).isFalse();
assertThat(object).isFalse();
assertThat(object).isFalse();
assertThat(primitive).isTrue();
assertThat(primitive).isTrue();
assertThat(primitive).isTrue();
assertThat(primitive).isTrue();
assertThat(primitive).isTrue();
assertThat(object).isTrue();
assertThat(object).isTrue();
assertThat(object).isTrue();
assertThat(object).isTrue();
assertThat(object).isTrue();
assertThat(!((primitive))).as("nah").isTrue();
assertThat(!object).isEqualTo(Boolean.TRUE && !Boolean.TRUE);
}
}
@@ -0,0 +1,34 @@
import static org.assertj.core.api.Assertions.assertThat;
public class InvertedBooleanCondition {
private void invertedBooleanCondition() {
boolean primitive = false;
Boolean object = Boolean.TRUE;
assertThat(!primitive).isEqualTo(Boolean.TRUE);
assertThat(!primitive).isEqualTo(true);
assertThat(!primitive).isNotEqualTo(Boolean.FALSE);
assertThat(!primitive).isNotEqualTo(false);
assertThat(!primitive).isTrue();
assertThat(!object).isEqualTo(Boolean.TRUE);
assertThat(!object).isEqualTo(true);
assertThat(!object).isNotEqualTo(Boolean.FALSE);
assertThat(!object).isNotEqualTo(false);
assertThat(!object).isTrue();
assertThat(!primitive).isEqualTo(Boolean.FALSE);
assertThat(!primitive).isEqualTo(false);
assertThat(!primitive).isNotEqualTo(Boolean.TRUE);
assertThat(!primitive).isNotEqualTo(true);
assertThat(!primitive).isFalse();
assertThat(!object).isEqualTo(Boolean.FALSE);
assertThat(!object).isEqualTo(false);
assertThat(!object).isNotEqualTo(Boolean.TRUE);
assertThat(!object).isNotEqualTo(true);
assertThat(!object).isFalse();
assertThat(!(((!((primitive)))))).as("nah").isEqualTo(true && !true);
assertThat(!object).isEqualTo(Boolean.TRUE && !Boolean.TRUE);
}
}
@@ -2,9 +2,9 @@ import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public class AssertThatJava8Optional { public class Java8Optional {
private void assertThatJava8Optional() { private void java8Optional() {
Optional<String> opt = Optional.empty(); Optional<String> opt = Optional.empty();
assertThat(opt).isPresent(); assertThat(opt).isPresent();
@@ -2,9 +2,9 @@ import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
public class AssertThatJava8Optional { public class Java8Optional {
private void assertThatJava8Optional() { private void java8Optional() {
Optional<String> opt = Optional.empty(); Optional<String> opt = Optional.empty();
assertThat(opt.isPresent()).isEqualTo(true); assertThat(opt.isPresent()).isEqualTo(true);
@@ -0,0 +1,83 @@
import java.util.ArrayList;
import static org.assertj.core.api.Assertions.assertThat;
public class Size {
private void size() {
ArrayList<String> list = new ArrayList<>();
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();
assertThat(list).isNotEmpty();
assertThat(list).as("hi").isNotEmpty();
assertThat(list).isNotEmpty();
assertThat(list).isEmpty();
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);
assertThat(list).hasSizeLessThan(list.size() * 2);
assertThat(list).hasSizeLessThanOrEqualTo(list.size() * 2);
assertThat(array).isEmpty();
assertThat(array).isEmpty();
assertThat(array).isNotEmpty();
assertThat(array).as("hi").isNotEmpty();
assertThat(array).isNotEmpty();
assertThat(array).isEmpty();
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);
}
}
@@ -0,0 +1,83 @@
import java.util.ArrayList;
import static org.assertj.core.api.Assertions.assertThat;
public class Size {
private void size() {
ArrayList<String> list = new ArrayList<>();
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();
assertThat(list.size()).isNotZero();
assertThat(list.size()).as("hi").isGreaterThan(0);
assertThat(list.size()).isGreaterThanOrEqualTo(1);
assertThat(list.size()).isLessThan(1);
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);
assertThat(list.size()).isLessThan(list.size() * 2);
assertThat(list.size()).isLessThanOrEqualTo(list.size() * 2);
assertThat(array.length).isEqualTo(0);
assertThat(array.length).isZero();
assertThat(array.length).isNotZero();
assertThat(array.length).as("hi").isGreaterThan(0);
assertThat(array.length).isGreaterThanOrEqualTo(1);
assertThat(array.length).isLessThan(1);
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);
}
}
@@ -0,0 +1,47 @@
import static org.assertj.core.api.Assertions.assertThat;
public class StringExpression {
private void stringExpression() {
String string = "string";
StringBuilder stringBuilder = new StringBuilder();
assertThat(string).isEmpty();
assertThat(string).isEmpty();
assertThat(string).isEqualTo("foo");
assertThat(string).isEqualTo("foo");
assertThat(string).isEqualToIgnoringCase("foo");
assertThat(string).isEqualToIgnoringCase("foo");
assertThat(string).isEqualTo("foo");
assertThat(string).isEqualTo("foo");
assertThat(string).isEqualTo(stringBuilder);
assertThat(string).isEqualTo(stringBuilder);
assertThat(string).contains("foo");
assertThat(string).contains("foo");
assertThat(string).contains(stringBuilder);
assertThat(string).contains(stringBuilder);
assertThat(string).startsWith("foo");
assertThat(string).startsWith("foo");
assertThat(string).endsWith("foo");
assertThat(string).endsWith("foo");
assertThat(string).isNotEmpty();
assertThat(string).isNotEmpty();
assertThat(string).isNotEqualTo("foo");
assertThat(string).isNotEqualTo("foo");
assertThat(string).isNotEqualToIgnoringCase("foo");
assertThat(string).isNotEqualToIgnoringCase("foo");
assertThat(string).isNotEqualTo("foo");
assertThat(string).isNotEqualTo("foo");
assertThat(string).isNotEqualTo(stringBuilder);
assertThat(string).isNotEqualTo(stringBuilder);
assertThat(string).doesNotContain("foo");
assertThat(string).doesNotContain("foo");
assertThat(string).doesNotContain(stringBuilder);
assertThat(string).doesNotContain(stringBuilder);
assertThat(string).doesNotStartWith("foo");
assertThat(string).doesNotStartWith("foo");
assertThat(string).doesNotEndWith("foo");
assertThat(string).doesNotEndWith("foo");
}
}
@@ -0,0 +1,47 @@
import static org.assertj.core.api.Assertions.assertThat;
public class StringExpression {
private void stringExpression() {
String string = "string";
StringBuilder stringBuilder = new StringBuilder();
assertThat(string.isEmpty()).isEqualTo(true);
assertThat(string.isEmpty()).isTrue();
assertThat(string.equals("foo")).isEqualTo(true);
assertThat(string.equals("foo")).isTrue();
assertThat(string.equalsIgnoreCase("foo")).isEqualTo(true);
assertThat(string.equalsIgnoreCase("foo")).isTrue();
assertThat(string.contentEquals("foo")).isEqualTo(true);
assertThat(string.contentEquals("foo")).isTrue();
assertThat(string.contentEquals(stringBuilder)).isTrue();
assertThat(string.contentEquals(stringBuilder)).isEqualTo(true);
assertThat(string.contains("foo")).isEqualTo(true);
assertThat(string.contains("foo")).isTrue();
assertThat(string.contains(stringBuilder)).isEqualTo(true);
assertThat(string.contains(stringBuilder)).isTrue();
assertThat(string.startsWith("foo")).isEqualTo(true);
assertThat(string.startsWith("foo")).isTrue();
assertThat(string.endsWith("foo")).isEqualTo(true);
assertThat(string.endsWith("foo")).isTrue();
assertThat(string.isEmpty()).isEqualTo(false);
assertThat(string.isEmpty()).isFalse();
assertThat(string.equals("foo")).isEqualTo(false);
assertThat(string.equals("foo")).isFalse();
assertThat(string.equalsIgnoreCase("foo")).isEqualTo(false);
assertThat(string.equalsIgnoreCase("foo")).isFalse();
assertThat(string.contentEquals("foo")).isEqualTo(false);
assertThat(string.contentEquals("foo")).isFalse();
assertThat(string.contentEquals(stringBuilder)).isFalse();
assertThat(string.contentEquals(stringBuilder)).isEqualTo(false);
assertThat(string.contains("foo")).isEqualTo(false);
assertThat(string.contains("foo")).isFalse();
assertThat(string.contains(stringBuilder)).isEqualTo(false);
assertThat(string.contains(stringBuilder)).isFalse();
assertThat(string.startsWith("foo")).isEqualTo(false);
assertThat(string.startsWith("foo")).isFalse();
assertThat(string.endsWith("foo")).isEqualTo(false);
assertThat(string.endsWith("foo")).isFalse();
}
}