New AssertThatStringExpression that will move isEmpty(), equals(), equalsIgnoreCase(), contains(), startsWith(), and endsWith() out of actual expression. Bumped intellij-plugin to latest version.
This commit is contained in:
parent
faeb509797
commit
da83f7f101
105
README.md
105
README.md
@ -130,9 +130,35 @@ 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).
|
||||||
|
|
||||||
|
- 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()```.
|
||||||
|
|
||||||
- AssertThatJava8Optional
|
- AssertThatJava8Optional
|
||||||
```
|
```
|
||||||
from: assertThat(opt.isPresent()).isEqualTo(true);
|
from: assertThat(opt.isPresent()).isEqualTo(true);
|
||||||
@ -162,34 +188,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,6 +216,34 @@ 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
|
||||||
|
|
||||||
```
|
```
|
||||||
@ -246,29 +272,22 @@ Feel free to use the code (in package de.platon42.intellij.jupiter) for your pro
|
|||||||
## TODO
|
## TODO
|
||||||
- AssertThatNegatedBooleanExpression
|
- AssertThatNegatedBooleanExpression
|
||||||
- AssertThatInstanceOf
|
- AssertThatInstanceOf
|
||||||
- AssertThatStringOps
|
- AssumeThatInsteadOfReturn
|
||||||
```
|
- Join consecutive assertThats
|
||||||
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.6 (unreleased)
|
||||||
|
- New AssertThatStringExpression that will move ```isEmpty()```, ```equals()```, ```equalsIgnoreCase()```, ```contains()```,
|
||||||
|
```startsWith()```, and ```endsWith()``` out of actual expression.
|
||||||
|
|
||||||
#### V0.5 (13-Apr-19)
|
#### V0.5 (13-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.
|
||||||
|
28
build.gradle
28
build.gradle
@ -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,36 +33,28 @@ 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 (xx-Apr-19)</h4>
|
||||||
|
<ul>
|
||||||
|
<li>New AssertThatStringExpression that will move isEmpty(), equals(), equalsIgnoreCase(), contains(),
|
||||||
|
startsWith(), and endsWith() out of actual expression.
|
||||||
|
</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.
|
||||||
<li>Fixed missing Guava imports (if not already present) for AssertThatGuavaInspection. This was a major PITA to get right.
|
<li>Fixed missing Guava imports (if not already present) for AssertThatGuavaInspection. This was a major PITA to get right.
|
||||||
<li>Added support for referencing and refactoring inside .extracting() methods with fields, properties and methods (though
|
<li>Added support for referencing and refactoring inside .extracting() methods with fields, properties and methods (though
|
||||||
getter renaming does not work that perfect, but I'm giving up for now as the IntelliJ SDK docs are seriously lacking).
|
getter renaming does not work that perfect, but I'm giving up for now as the IntelliJ SDK docs are seriously lacking).
|
||||||
<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>
|
|
||||||
"""
|
"""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,8 +65,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>(
|
||||||
|
@ -10,8 +10,8 @@ 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 AssertThatBinaryExpressionIsTrueOrFalseInspection : AbstractAssertJInspection() {
|
||||||
|
|
||||||
@ -37,7 +37,7 @@ 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 = if (expectedResult) MethodNames.IS_EQUAL_TO else MethodNames.IS_NOT_EQUAL_TO
|
||||||
registerSplitMethod(holder, expression, "${MethodNames.EQUALS}()", replacementMethod, ::SplitEqualsExpressionMethodCallQuickFix)
|
registerSplitMethod(holder, expression, "${MethodNames.EQUALS}()", replacementMethod, ::MoveActualOuterExpressionMethodCallQuickFix)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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)
|
||||||
}
|
}
|
@ -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
|
||||||
|
@ -40,6 +40,8 @@
|
|||||||
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatJava8OptionalInspection"/>
|
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatJava8OptionalInspection"/>
|
||||||
<localInspection groupPath="Java" shortName="AssertThatGuavaOptional" enabledByDefault="true" level="WARNING"
|
<localInspection groupPath="Java" shortName="AssertThatGuavaOptional" enabledByDefault="true" level="WARNING"
|
||||||
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatGuavaOptionalInspection"/>
|
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatGuavaOptionalInspection"/>
|
||||||
|
<localInspection groupPath="Java" shortName="AssertThatStringExpression" enabledByDefault="true" level="WARNING"
|
||||||
|
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatStringExpressionInspection"/>
|
||||||
|
|
||||||
<localInspection groupPath="Java" shortName="JUnitAssertToAssertJ" enabledByDefault="true" level="WARNING"
|
<localInspection groupPath="Java" shortName="JUnitAssertToAssertJ" enabledByDefault="true" level="WARNING"
|
||||||
implementationClass="de.platon42.intellij.plugins.cajon.inspections.JUnitAssertToAssertJInspection"/>
|
implementationClass="de.platon42.intellij.plugins.cajon.inspections.JUnitAssertToAssertJInspection"/>
|
||||||
|
@ -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>
|
@ -26,7 +26,7 @@ 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("AssertThatGuavaOptionalAfter.java")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ 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("AssertThatJava8OptionalAfter.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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user