diff --git a/README.md b/README.md index 10f3391..ab631c5 100644 --- a/README.md +++ b/README.md @@ -130,9 +130,35 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the from: assertThat(null == objActual).isFalse(); to: assertThat(objActual).isNotNull(); + + from: assertThat(objActual.equals(objExpected).isTrue(); + to: assertThat(objActual).isEqualTo(objExpected); ``` ...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 ``` 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(); ``` -- 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 ``` 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. +- 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 ``` @@ -246,29 +272,22 @@ Feel free to use the code (in package de.platon42.intellij.jupiter) for your pro ## TODO - AssertThatNegatedBooleanExpression - AssertThatInstanceOf -- 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 +- AssumeThatInsteadOfReturn +- Join consecutive assertThats - Extraction with property names to lambda with Java 8 ``` from: assertThat(object).extracting("propOne", "propNoGetter", "propTwo.innerProp")... 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 +#### V0.6 (unreleased) +- New AssertThatStringExpression that will move ```isEmpty()```, ```equals()```, ```equalsIgnoreCase()```, ```contains()```, + ```startsWith()```, and ```endsWith()``` out of actual expression. + #### V0.5 (13-Apr-19) - Fixed incompatibility with IDEA versions < 2018.2 (affected AssertThatSizeInspection). Minimal version is now 2017.3. - Fixed missing Guava imports (if not already present) for AssertThatGuavaInspection. This was a major PITA to get right. diff --git a/build.gradle b/build.gradle index fb0cfa0..8802466 100644 --- a/build.gradle +++ b/build.gradle @@ -1,11 +1,11 @@ plugins { 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' } group 'de.platon42' -version '0.5' +version '0.6' repositories { mavenCentral() @@ -33,36 +33,28 @@ compileTestKotlin { kotlinOptions.jvmTarget = "1.8" } intellij { - version '2019.1' + version '2019.1.1' // pluginName 'Concise AssertJ Optimizing Nitpicker (Cajon)' updateSinceUntilBuild false } patchPluginXml { changeNotes """ +

V0.6 (xx-Apr-19)

+

V0.5 (18-Apr-19)

-

V0.4 (11-Apr-19)

- -

V0.3 (07-Apr-19)

- +

Full changelog available at Github project site.

""" } diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/MethodNames.kt b/src/main/java/de/platon42/intellij/plugins/cajon/MethodNames.kt index d028092..c21a9c0 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/MethodNames.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/MethodNames.kt @@ -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" diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AbstractAssertJInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AbstractAssertJInspection.kt index cc77c4c..b246116 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AbstractAssertJInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AbstractAssertJInspection.kt @@ -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( diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBinaryExpressionIsTrueOrFalseInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBinaryExpressionIsTrueOrFalseInspection.kt index 71be627..90c6bf8 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBinaryExpressionIsTrueOrFalseInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBinaryExpressionIsTrueOrFalseInspection.kt @@ -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 } diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringExpressionInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringExpressionInspection.kt new file mode 100644 index 0000000..7e57f40 --- /dev/null +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringExpressionInspection.kt @@ -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 + ) +} \ No newline at end of file diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/SplitEqualsExpressionMethodCallQuickFix.kt b/src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/MoveActualOuterExpressionMethodCallQuickFix.kt similarity index 66% rename from src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/SplitEqualsExpressionMethodCallQuickFix.kt rename to src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/MoveActualOuterExpressionMethodCallQuickFix.kt index 00780d7..d81cd9b 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/SplitEqualsExpressionMethodCallQuickFix.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/MoveActualOuterExpressionMethodCallQuickFix.kt @@ -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) } diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/ReplaceSimpleMethodCallQuickFix.kt b/src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/ReplaceSimpleMethodCallQuickFix.kt index 3319c8b..5bbfe86 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/ReplaceSimpleMethodCallQuickFix.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/ReplaceSimpleMethodCallQuickFix.kt @@ -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 diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 0530208..2e1e802 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -40,6 +40,8 @@ implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatJava8OptionalInspection"/> + diff --git a/src/main/resources/inspectionDescriptions/AssertThatStringExpression.html b/src/main/resources/inspectionDescriptions/AssertThatStringExpression.html new file mode 100644 index 0000000..b22f1d4 --- /dev/null +++ b/src/main/resources/inspectionDescriptions/AssertThatStringExpression.html @@ -0,0 +1,7 @@ + + +Turns assertThat(string.someMethod(arg)).isTrue/isFalse() into assertThat(string).someMethod(arg). + +
someMethod() can be equals(), equalsIgnoreCase(), contentEquals(), contains(), startsWith(), and endsWith(). + + \ No newline at end of file diff --git a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatGuavaOptionalInspectionTest.kt b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatGuavaOptionalInspectionTest.kt index 1b0b6c5..62d0c41 100644 --- a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatGuavaOptionalInspectionTest.kt +++ b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatGuavaOptionalInspectionTest.kt @@ -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 isFalse() with isAbsent()"), 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") } } diff --git a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatJava8OptionalInspectionTest.kt b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatJava8OptionalInspectionTest.kt index f7e3d64..5975a74 100644 --- a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatJava8OptionalInspectionTest.kt +++ b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatJava8OptionalInspectionTest.kt @@ -22,7 +22,7 @@ internal class AssertThatJava8OptionalInspectionTest : AbstractCajonTest() { executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isNotPresent()"), 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 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 isSameAs() with containsSame()"), 1) myFixture.checkResultByFile("AssertThatJava8OptionalAfter.java") diff --git a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringExpressionInspectionTest.kt b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringExpressionInspectionTest.kt new file mode 100644 index 0000000..9f63db4 --- /dev/null +++ b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatStringExpressionInspectionTest.kt @@ -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") + } + } +} \ No newline at end of file diff --git a/src/test/resources/inspections/StringExpression/StringExpressionAfter.java b/src/test/resources/inspections/StringExpression/StringExpressionAfter.java new file mode 100644 index 0000000..7876f75 --- /dev/null +++ b/src/test/resources/inspections/StringExpression/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"); + } +} diff --git a/src/test/resources/inspections/StringExpression/StringExpressionBefore.java b/src/test/resources/inspections/StringExpression/StringExpressionBefore.java new file mode 100644 index 0000000..b8b8e3f --- /dev/null +++ b/src/test/resources/inspections/StringExpression/StringExpressionBefore.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()).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(); + } +}