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:
@@ -65,8 +65,22 @@ class MethodNames {
|
||||
@NonNls
|
||||
const val CONTAINS = "contains"
|
||||
@NonNls
|
||||
const val DOES_NOT_CONTAIN = "doesNotContain"
|
||||
@NonNls
|
||||
const val CONTAINS_EXACTLY = "containsExactly"
|
||||
@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"
|
||||
@NonNls
|
||||
const val IS_PRESENT = "isPresent"
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
|
||||
const val MORE_CONCISE_MESSAGE_TEMPLATE = "%s() would be more concise than %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()"
|
||||
|
||||
val TOKEN_TO_ASSERTJ_FOR_PRIMITIVE_MAP = mapOf<IElementType, String>(
|
||||
|
||||
+2
-2
@@ -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.firstArg
|
||||
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.SplitEqualsExpressionMethodCallQuickFix
|
||||
|
||||
class AssertThatBinaryExpressionIsTrueOrFalseInspection : AbstractAssertJInspection() {
|
||||
|
||||
@@ -37,7 +37,7 @@ class AssertThatBinaryExpressionIsTrueOrFalseInspection : AbstractAssertJInspect
|
||||
val assertThatArgument = expression.firstArg
|
||||
if (assertThatArgument is PsiMethodCallExpression && OBJECT_EQUALS.test(assertThatArgument)) {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
+102
@@ -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
-5
@@ -5,17 +5,17 @@ import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiMethodCallExpression
|
||||
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) {
|
||||
val element = descriptor.startElement
|
||||
val methodCallExpression = element as? PsiMethodCallExpression ?: return
|
||||
val equalsMethodCall = methodCallExpression.firstArg as? PsiMethodCallExpression ?: return
|
||||
val expectedArgument = equalsMethodCall.firstArg.copy()
|
||||
equalsMethodCall.replace(equalsMethodCall.qualifierExpression)
|
||||
val assertExpression = methodCallExpression.firstArg as? PsiMethodCallExpression ?: return
|
||||
val assertExpressionArg = assertExpression.firstArg.copy()
|
||||
assertExpression.replace(assertExpression.qualifierExpression)
|
||||
|
||||
val oldExpectedExpression = element.findOutmostMethodCall() ?: return
|
||||
val expectedExpression = createExpectedMethodCall(element, replacementMethod, expectedArgument)
|
||||
val expectedExpression = createExpectedMethodCall(element, replacementMethod, assertExpressionArg)
|
||||
expectedExpression.replaceQualifierFromMethodCall(oldExpectedExpression)
|
||||
oldExpectedExpression.replace(expectedExpression)
|
||||
}
|
||||
+1
-4
@@ -6,10 +6,7 @@ import com.intellij.psi.PsiMethodCallExpression
|
||||
import de.platon42.intellij.plugins.cajon.createExpectedMethodCall
|
||||
import de.platon42.intellij.plugins.cajon.replaceQualifierFromMethodCall
|
||||
|
||||
class ReplaceSimpleMethodCallQuickFix(
|
||||
description: String,
|
||||
private val replacementMethod: String
|
||||
) : AbstractCommonQuickFix(description) {
|
||||
class ReplaceSimpleMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
|
||||
|
||||
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
|
||||
val element = descriptor.startElement
|
||||
|
||||
@@ -40,6 +40,8 @@
|
||||
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatJava8OptionalInspection"/>
|
||||
<localInspection groupPath="Java" shortName="AssertThatGuavaOptional" enabledByDefault="true" level="WARNING"
|
||||
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"
|
||||
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>
|
||||
Reference in New Issue
Block a user