New AssertThatInvertedBooleanCondition inspection that will remove inverted boolean expressions inside assertThat().
Renamed a few inspections to better/shorter names (and fixed file and directory names accordingly).
This commit is contained in:
parent
a707eee9ad
commit
db02f7fb93
26
README.md
26
README.md
@ -54,11 +54,13 @@ Applying a quick fix might result in further optimization possibilities, so you
|
|||||||
|
|
||||||
For example:
|
For example:
|
||||||
```
|
```
|
||||||
assertFalse(!(array.length() == collection.size()));
|
assertFalse(!(array.length == collection.size()));
|
||||||
|
|
||||||
assertThat(!(array.length() == collection.size())).isFalse();
|
assertThat(!(array.length == collection.size())).isFalse();
|
||||||
|
|
||||||
assertThat(array.length() == collection.size()).isTrue();
|
assertThat(array.length == collection.size()).isTrue();
|
||||||
|
|
||||||
|
assertThat(array.length).isEqualTo(collection.size());
|
||||||
|
|
||||||
assertThat(array).hasSameSizeAs(collection);
|
assertThat(array).hasSameSizeAs(collection);
|
||||||
```
|
```
|
||||||
@ -76,12 +78,19 @@ 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();
|
||||||
|
```
|
||||||
|
|
||||||
- AssertThatStringIsEmpty
|
- AssertThatStringIsEmpty
|
||||||
```
|
```
|
||||||
from: assertThat(charSequence/string).isEqualTo("");
|
from: assertThat(charSequence/string).isEqualTo("");
|
||||||
@ -160,7 +169,7 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the
|
|||||||
to: assertThat("string").hasSameSizeAs(collection);
|
to: assertThat("string").hasSameSizeAs(collection);
|
||||||
```
|
```
|
||||||
|
|
||||||
- AssertThatBinaryExpressionIsTrueOrFalse
|
- AssertThatBinaryExpression
|
||||||
```
|
```
|
||||||
from: assertThat(primActual == primExpected).isTrue();
|
from: assertThat(primActual == primExpected).isTrue();
|
||||||
to: assertThat(primActual).isEqualTo(primExpected);
|
to: assertThat(primActual).isEqualTo(primExpected);
|
||||||
@ -292,7 +301,6 @@ The IntelliJ framework actually uses the JUnit 3 TestCase for plugin testing and
|
|||||||
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
|
|
||||||
- AssertThatInstanceOf
|
- AssertThatInstanceOf
|
||||||
- AssumeThatInsteadOfReturn
|
- AssumeThatInsteadOfReturn
|
||||||
- Join consecutive assertThats
|
- Join consecutive assertThats
|
||||||
@ -307,9 +315,11 @@ Feel free to use the code (in package de.platon42.intellij.jupiter) for your pro
|
|||||||
## Changelog
|
## Changelog
|
||||||
|
|
||||||
#### V0.6 (unreleased)
|
#### V0.6 (unreleased)
|
||||||
- New AssertThatStringExpression that will move ```isEmpty()```, ```equals()```, ```equalsIgnoreCase()```, ```contains()```,
|
- New AssertThatStringExpression inspection that will move ```isEmpty()```, ```equals()```, ```equalsIgnoreCase()```, ```contains()```,
|
||||||
```startsWith()```, and ```endsWith()``` out of actual expression.
|
```startsWith()```, and ```endsWith()``` out of actual expression.
|
||||||
- Extended AssertThatSize intention to take ```String```s and ```CharSequences``` into account, too.
|
- 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.
|
||||||
|
|
||||||
#### 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.
|
||||||
|
@ -42,9 +42,11 @@ patchPluginXml {
|
|||||||
changeNotes """
|
changeNotes """
|
||||||
<h4>V0.6 (xx-Apr-19)</h4>
|
<h4>V0.6 (xx-Apr-19)</h4>
|
||||||
<ul>
|
<ul>
|
||||||
<li>New AssertThatStringExpression intention that will move isEmpty(), equals(), equalsIgnoreCase(), contains(),
|
<li>New AssertThatStringExpression inspection that will move isEmpty(), equals(), equalsIgnoreCase(), contains(),
|
||||||
startsWith(), and endsWith() out of actual expression.
|
startsWith(), and endsWith() out of actual expression.
|
||||||
<li>Extended AssertThatSize intention to take strings and CharSequences into account, too.
|
<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.
|
||||||
</ul>
|
</ul>
|
||||||
<h4>V0.5 (18-Apr-19)</h4>
|
<h4>V0.5 (18-Apr-19)</h4>
|
||||||
<ul>
|
<ul>
|
||||||
|
@ -13,7 +13,7 @@ import de.platon42.intellij.plugins.cajon.map
|
|||||||
import de.platon42.intellij.plugins.cajon.quickfixes.MoveActualOuterExpressionMethodCallQuickFix
|
import de.platon42.intellij.plugins.cajon.quickfixes.MoveActualOuterExpressionMethodCallQuickFix
|
||||||
import de.platon42.intellij.plugins.cajon.quickfixes.SplitBinaryExpressionMethodCallQuickFix
|
import de.platon42.intellij.plugins.cajon.quickfixes.SplitBinaryExpressionMethodCallQuickFix
|
||||||
|
|
||||||
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"
|
||||||
@ -63,13 +63,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,50 @@
|
|||||||
|
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.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 = if (expectedResult) MethodNames.IS_FALSE else 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -11,7 +11,7 @@
|
|||||||
]]></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 -->
|
||||||
@ -24,7 +24,7 @@
|
|||||||
<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="AssertThatBooleanIsTrueOrFalse" enabledByDefault="true" level="WARNING"
|
||||||
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBooleanIsTrueOrFalseInspection"/>
|
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBooleanConditionInspection"/>
|
||||||
<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"
|
||||||
@ -34,7 +34,9 @@
|
|||||||
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="AssertThatBinaryExpressionIsTrueOrFalse" enabledByDefault="true" level="WARNING"
|
||||||
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBinaryExpressionIsTrueOrFalseInspection"/>
|
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatBinaryExpressionInspection"/>
|
||||||
|
<localInspection groupPath="Java" shortName="AssertThatNotPrefixExpression" enabledByDefault="true" level="WARNING"
|
||||||
|
implementationClass="de.platon42.intellij.plugins.cajon.inspections.AssertThatInvertedBooleanConditionInspection"/>
|
||||||
|
|
||||||
<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(!condition).isEqualTo(true/false) into assertThat(condition).isFalse()/isTrue().
|
||||||
|
<!-- tooltip end -->
|
||||||
|
Also works with constant expressions and Boolean.TRUE/FALSE.
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -243,6 +243,12 @@ public class Playground {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
@ -351,7 +357,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)
|
||||||
@ -27,7 +27,7 @@ internal class AssertThatGuavaOptionalInspectionTest : AbstractCajonTest() {
|
|||||||
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("Remove unwrapping of 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,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)
|
||||||
@ -25,7 +25,7 @@ internal class AssertThatJava8OptionalInspectionTest : AbstractCajonTest() {
|
|||||||
executeQuickFixes(myFixture, Regex.fromLiteral("Remove unwrapping of 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,11 +9,11 @@ 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()"), 4)
|
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isEqualTo() with isEmpty()"), 4)
|
||||||
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isZero() with isEmpty()"), 4)
|
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isZero() with isEmpty()"), 4)
|
||||||
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotZero() with isNotEmpty()"), 4)
|
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isNotZero() with isNotEmpty()"), 4)
|
||||||
@ -27,7 +27,7 @@ internal class AssertThatSizeInspectionTest : AbstractCajonTest() {
|
|||||||
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThanOrEqualTo() with hasSizeGreaterThanOrEqualTo()"), 4)
|
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isGreaterThanOrEqualTo() with hasSizeGreaterThanOrEqualTo()"), 4)
|
||||||
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThan() with hasSizeLessThan()"), 4)
|
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThan() with hasSizeLessThan()"), 4)
|
||||||
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThanOrEqualTo() with hasSizeLessThanOrEqualTo()"), 4)
|
executeQuickFixes(myFixture, Regex.fromLiteral("Replace isLessThanOrEqualTo() with hasSizeLessThanOrEqualTo()"), 4)
|
||||||
myFixture.checkResultByFile("AssertThatSizeAfter.java")
|
myFixture.checkResultByFile("SizeAfter.java")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -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,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);
|
@ -2,9 +2,9 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class AssertThatSize {
|
public class Size {
|
||||||
|
|
||||||
private void assertThatSize() {
|
private void size() {
|
||||||
ArrayList<String> list = new ArrayList<>();
|
ArrayList<String> list = new ArrayList<>();
|
||||||
ArrayList<String> otherList = new ArrayList<>();
|
ArrayList<String> otherList = new ArrayList<>();
|
||||||
long[] array = new long[5];
|
long[] array = new long[5];
|
@ -2,9 +2,9 @@ import java.util.ArrayList;
|
|||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
public class AssertThatSize {
|
public class Size {
|
||||||
|
|
||||||
private void assertThatSize() {
|
private void size() {
|
||||||
ArrayList<String> list = new ArrayList<>();
|
ArrayList<String> list = new ArrayList<>();
|
||||||
ArrayList<String> otherList = new ArrayList<>();
|
ArrayList<String> otherList = new ArrayList<>();
|
||||||
long[] array = new long[5];
|
long[] array = new long[5];
|
Loading…
Reference in New Issue
Block a user