diff --git a/README.md b/README.md
index 716bc32..060194f 100644
--- a/README.md
+++ b/README.md
@@ -93,6 +93,22 @@ The plugin also supports the conversion of the most common JUnit 4 assertions to
and analogously for collections...
+- AssertThatBinaryExpressionIsTrueOrFalse
+ ```
+ from: assertThat(primActual == primExpected).isTrue();
+ to: assertThat(primActual).isEqualTo(primExpected);
+
+ from: assertThat(10 < primActual).isNotEqualTo(false);
+ to: assertThat(primActual).isGreaterThan(primExpected);
+
+ from: assertThat(objActual != objExpected).isEqualTo(true);
+ to: assertThat(objActual).isNotSameAs(objExpected);
+
+ from: assertThat(null == objActual).isFalse();
+ to: assertThat(objActual).isNotNull();
+ ```
+ and many, many more combinations (more than 150).
+
- JUnitAssertToAssertJ
```
assertTrue(condition);
@@ -182,3 +198,4 @@ Feel free to use the code (in package de.platon42.intellij.jupiter) for your pro
from: assertThat(object).extracting("propOne", "propNoGetter", "propTwo.innerProp")...
to: assertThat(object).extracting(type::getPropOne, it -> it.propNoGetter, it -> it.getPropTwo().getInnerProp())...
```
+- Kotlin support
diff --git a/build.gradle b/build.gradle
index c5200b6..ef2c297 100644
--- a/build.gradle
+++ b/build.gradle
@@ -41,8 +41,9 @@ intellij {
patchPluginXml {
changeNotes """
-
V0.3 (xx-Apr-19)
+ V0.3 (07-Apr-19)
+ - New inspection AssertThatBinaryExpressionIsTrueOrFalse that will find and fix common binary expressions and equals() statements (more than 150 combinations) inside assertThat().
- Merged AssertThatObjectIsNull and AssertThatObjectIsNotNull to AssertThatObjectIsNullOrNotNull.
- Support for hasSizeLessThan(), hasSizeLessThanOrEqualTo(), hasSizeGreaterThanOrEqualTo(), and hasSizeGreaterThan() for AssertThatSizeInspection (with AssertJ >=13.2.0).
- Really fixed highlighting for JUnit conversion. Sorry.
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 7d561c7..9ccbb7f 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
@@ -53,11 +53,18 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
@NonNls
const val IS_NOT_ZERO_METHOD = "isNotZero"
@NonNls
+ const val IS_TRUE_METHOD = "isTrue"
+ @NonNls
+ const val IS_FALSE_METHOD = "isFalse"
+ @NonNls
const val HAS_SIZE_METHOD = "hasSize"
val ASSERT_THAT_INT = CallMatcher.staticCall(ASSERTIONS_CLASSNAME, ASSERT_THAT_METHOD)
.parameterTypes("int")!!
+ val ASSERT_THAT_BOOLEAN = CallMatcher.staticCall(ASSERTIONS_CLASSNAME, ASSERT_THAT_METHOD)
+ .parameterTypes("boolean")!!
+
val IS_EQUAL_TO_OBJECT = CallMatcher.instanceCall(ABSTRACT_ASSERT_CLASSNAME, IS_EQUAL_TO_METHOD)
.parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)!!
val IS_NOT_EQUAL_TO_OBJECT = CallMatcher.instanceCall(ABSTRACT_ASSERT_CLASSNAME, IS_NOT_EQUAL_TO_METHOD)
@@ -87,8 +94,15 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
val IS_NOT_ZERO = CallMatcher.instanceCall(ABSTRACT_INTEGER_ASSERT_CLASSNAME, IS_NOT_ZERO_METHOD)
.parameterCount(0)!!
+ val IS_TRUE = CallMatcher.instanceCall(ABSTRACT_BOOLEAN_ASSERT_CLASSNAME, IS_TRUE_METHOD)
+ .parameterCount(0)!!
+ val IS_FALSE = CallMatcher.instanceCall(ABSTRACT_BOOLEAN_ASSERT_CLASSNAME, IS_FALSE_METHOD)
+ .parameterCount(0)!!
+
val COLLECTION_SIZE = CallMatcher.instanceCall(CommonClassNames.JAVA_UTIL_COLLECTION, "size")
.parameterCount(0)!!
+ val OBJECT_EQUALS = CallMatcher.instanceCall(CommonClassNames.JAVA_LANG_OBJECT, "equals")
+ .parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)!!
}
override fun getGroupDisplayName(): String {
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
new file mode 100644
index 0000000..defaeba
--- /dev/null
+++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBinaryExpressionIsTrueOrFalseInspection.kt
@@ -0,0 +1,144 @@
+package de.platon42.intellij.plugins.cajon.inspections
+
+import com.intellij.codeInspection.ProblemsHolder
+import com.intellij.psi.*
+import com.intellij.psi.tree.IElementType
+import com.intellij.psi.util.PsiTreeUtil
+import com.intellij.psi.util.TypeConversionUtil
+import de.platon42.intellij.plugins.cajon.quickfixes.SplitBinaryExpressionMethodCallQuickFix
+import de.platon42.intellij.plugins.cajon.quickfixes.SplitEqualsExpressionMethodCallQuickFix
+
+class AssertThatBinaryExpressionIsTrueOrFalseInspection : AbstractAssertJInspection() {
+
+ companion object {
+ private const val DISPLAY_NAME = "Asserting a binary expression"
+ private const val SPLIT_BINARY_EXPRESSION_DESCRIPTION = "Split binary expression out of assertThat()"
+ private const val SPLIT_EQUALS_EXPRESSION_DESCRIPTION = "Split equals() expression out of assertThat()"
+ private const val BINARY_MORE_MEANINGFUL_MESSAGE = "Moving binary expression out of assertThat() would be more meaningful"
+ private const val EQUALS_MORE_MEANINGFUL_MESSAGE = "Moving equals() expression out of assertThat() would be more meaningful"
+
+ private val PRIMITIVE_MAPPINGS = listOf(
+ Mapping(JavaTokenType.EQEQ, "isEqualTo()", "isNotEqualTo()"),
+ Mapping(JavaTokenType.NE, "isNotEqualTo()", "isEqualTo()"),
+ Mapping(JavaTokenType.GT, "isGreaterThan()", "isLessThanOrEqualTo()"),
+ Mapping(JavaTokenType.GE, "isGreaterThanOrEqualTo()", "isLessThan()"),
+ Mapping(JavaTokenType.LT, "isLessThan()", "isGreaterThanOrEqualTo()"),
+ Mapping(JavaTokenType.LE, "isLessThanOrEqualTo()", "isGreaterThan()")
+ )
+
+ private val PRIMITIVE_MAPPINGS_SWAPPED = listOf(
+ Mapping(JavaTokenType.EQEQ, "isEqualTo()", "isNotEqualTo()"),
+ Mapping(JavaTokenType.NE, "isNotEqualTo()", "isEqualTo()"),
+ Mapping(JavaTokenType.GT, "isLessThan()", "isGreaterThanOrEqualTo()"),
+ Mapping(JavaTokenType.GE, "isLessThanOrEqualTo()", "isGreaterThan()"),
+ Mapping(JavaTokenType.LT, "isGreaterThan()", "isLessThanOrEqualTo()"),
+ Mapping(JavaTokenType.LE, "isGreaterThanOrEqualTo()", "isLessThan()")
+ )
+
+ private val OBJECT_MAPPINGS = listOf(
+ Mapping(JavaTokenType.EQEQ, "isSameAs()", "isNotSameAs()"),
+ Mapping(JavaTokenType.NE, "isNotSameAs()", "isSameAs()")
+ )
+ }
+
+ 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 statement = PsiTreeUtil.getParentOfType(expression, PsiStatement::class.java) ?: return
+ val expectedCallExpression = PsiTreeUtil.findChildOfType(statement, PsiMethodCallExpression::class.java) ?: return
+ val isInverted = getExpectedResult(expectedCallExpression) ?: return
+
+ val assertThatArgument = expression.argumentList.expressions[0] ?: return
+ if (assertThatArgument is PsiMethodCallExpression && OBJECT_EQUALS.test(assertThatArgument)) {
+ val replacementMethod = if (isInverted) "isNotEqualTo()" else "isEqualTo()"
+ holder.registerProblem(
+ expression,
+ EQUALS_MORE_MEANINGFUL_MESSAGE,
+ SplitEqualsExpressionMethodCallQuickFix(SPLIT_EQUALS_EXPRESSION_DESCRIPTION, replacementMethod)
+ )
+ return
+ }
+
+ val binaryExpression = assertThatArgument as? PsiBinaryExpression ?: return
+
+ val leftType = binaryExpression.lOperand.type ?: return
+ val rightType = binaryExpression.rOperand?.type ?: return
+
+ val isLeftNull = TypeConversionUtil.isNullType(leftType)
+ val isRightNull = TypeConversionUtil.isNullType(rightType)
+ if (isLeftNull && isRightNull) {
+ return
+ } else if (isLeftNull || isRightNull) {
+ registerSplitBinaryExpressionMethod(
+ holder,
+ expression,
+ if (isInverted) "isNotNull()" else "isNull()",
+ pickRightOperand = isLeftNull,
+ noExpectedExpression = true
+ )
+ return
+ }
+
+ val isPrimitive = TypeConversionUtil.isPrimitiveAndNotNull(leftType) && TypeConversionUtil.isPrimitiveAndNotNull(rightType)
+ val isNumericType = TypeConversionUtil.isNumericType(leftType) && TypeConversionUtil.isNumericType(rightType)
+ val constantEvaluationHelper = JavaPsiFacade.getInstance(expression.project).constantEvaluationHelper
+ val swapExpectedAndActual = constantEvaluationHelper.computeConstantExpression(binaryExpression.lOperand) != null
+
+ val tokenType = binaryExpression.operationSign.tokenType
+ val mappingToUse =
+ if (isPrimitive || isNumericType) {
+ if (swapExpectedAndActual) PRIMITIVE_MAPPINGS_SWAPPED else PRIMITIVE_MAPPINGS
+ } else {
+ OBJECT_MAPPINGS
+ }
+ val mapping = mappingToUse.find { it.tokenType == tokenType } ?: return
+ val replacementMethod = if (isInverted) mapping.replacementInverted else mapping.replacement
+
+ registerSplitBinaryExpressionMethod(holder, expression, replacementMethod, pickRightOperand = swapExpectedAndActual)
+ }
+
+ private fun getExpectedResult(expectedCallExpression: PsiMethodCallExpression): Boolean? {
+ val isTrue = IS_TRUE.test(expectedCallExpression)
+ val isFalse = IS_FALSE.test(expectedCallExpression)
+ if (isTrue || isFalse) {
+ return isFalse
+ } else {
+ val isEqualTo = IS_EQUAL_TO_BOOLEAN.test(expectedCallExpression)
+ val isNotEqualTo = IS_NOT_EQUAL_TO_BOOLEAN.test(expectedCallExpression)
+ if (isEqualTo || isNotEqualTo) {
+ val constValue = calculateConstantParameterValue(expectedCallExpression, 0) as? Boolean ?: return null
+ return isEqualTo xor constValue
+ }
+ }
+ return null
+ }
+
+ private fun registerSplitBinaryExpressionMethod(
+ holder: ProblemsHolder,
+ expression: PsiMethodCallExpression,
+ replacementMethod: String,
+ pickRightOperand: Boolean = false,
+ noExpectedExpression: Boolean = false
+ ) {
+ holder.registerProblem(
+ expression,
+ BINARY_MORE_MEANINGFUL_MESSAGE,
+ SplitBinaryExpressionMethodCallQuickFix(SPLIT_BINARY_EXPRESSION_DESCRIPTION, replacementMethod, pickRightOperand, noExpectedExpression)
+ )
+ }
+ }
+ }
+
+ private class Mapping(
+ val tokenType: IElementType,
+ val replacement: String,
+ val replacementInverted: String
+ )
+}
\ No newline at end of file
diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/SplitBinaryExpressionMethodCallQuickFix.kt b/src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/SplitBinaryExpressionMethodCallQuickFix.kt
new file mode 100644
index 0000000..02e7b7e
--- /dev/null
+++ b/src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/SplitBinaryExpressionMethodCallQuickFix.kt
@@ -0,0 +1,36 @@
+package de.platon42.intellij.plugins.cajon.quickfixes
+
+import com.intellij.codeInspection.ProblemDescriptor
+import com.intellij.openapi.project.Project
+import com.intellij.psi.JavaPsiFacade
+import com.intellij.psi.PsiBinaryExpression
+import com.intellij.psi.PsiMethodCallExpression
+import com.intellij.psi.PsiStatement
+import com.intellij.psi.util.PsiTreeUtil
+
+class SplitBinaryExpressionMethodCallQuickFix(
+ description: String,
+ private val replacementMethod: String,
+ private val pickRightOperand: Boolean,
+ private val noExpectedExpression: Boolean
+) : AbstractCommonQuickFix(description) {
+
+ override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
+ val element = descriptor.startElement
+ val factory = JavaPsiFacade.getElementFactory(element.project)
+ val methodCallExpression = element as? PsiMethodCallExpression ?: return
+ val binaryExpression = methodCallExpression.argumentList.expressions[0] as? PsiBinaryExpression ?: return
+ val expectedArgument = (if (pickRightOperand) binaryExpression.lOperand else binaryExpression.rOperand)?.copy() ?: return
+ binaryExpression.replace(if (pickRightOperand) binaryExpression.rOperand!! else binaryExpression.lOperand)
+
+ val statement = PsiTreeUtil.getParentOfType(element, PsiStatement::class.java) ?: return
+ val oldExpectedExpression = PsiTreeUtil.findChildOfType(statement, PsiMethodCallExpression::class.java) ?: return
+ val expectedExpression =
+ factory.createExpressionFromText("a.${if (noExpectedExpression) replacementMethod else replacementMethod.replace("()", "(e)")}", element) as PsiMethodCallExpression
+ if (!noExpectedExpression) {
+ expectedExpression.argumentList.expressions[0].replace(expectedArgument)
+ }
+ expectedExpression.methodExpression.qualifierExpression!!.replace(oldExpectedExpression.methodExpression.qualifierExpression!!)
+ oldExpectedExpression.replace(expectedExpression)
+ }
+}
\ 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/SplitEqualsExpressionMethodCallQuickFix.kt
new file mode 100644
index 0000000..737b4c1
--- /dev/null
+++ b/src/main/java/de/platon42/intellij/plugins/cajon/quickfixes/SplitEqualsExpressionMethodCallQuickFix.kt
@@ -0,0 +1,28 @@
+package de.platon42.intellij.plugins.cajon.quickfixes
+
+import com.intellij.codeInspection.ProblemDescriptor
+import com.intellij.openapi.project.Project
+import com.intellij.psi.JavaPsiFacade
+import com.intellij.psi.PsiMethodCallExpression
+import com.intellij.psi.PsiStatement
+import com.intellij.psi.util.PsiTreeUtil
+
+class SplitEqualsExpressionMethodCallQuickFix(description: String, private val replacementMethod: String) : AbstractCommonQuickFix(description) {
+
+ override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
+ val element = descriptor.startElement
+ val factory = JavaPsiFacade.getElementFactory(element.project)
+ val methodCallExpression = element as? PsiMethodCallExpression ?: return
+ val equalsMethodCall = methodCallExpression.argumentList.expressions[0] as? PsiMethodCallExpression ?: return
+ val expectedArgument = equalsMethodCall.argumentList.expressions[0].copy()
+ equalsMethodCall.replace(equalsMethodCall.methodExpression.qualifierExpression!!)
+
+ val statement = PsiTreeUtil.getParentOfType(element, PsiStatement::class.java) ?: return
+ val oldExpectedExpression = PsiTreeUtil.findChildOfType(statement, PsiMethodCallExpression::class.java) ?: return
+ val expectedExpression =
+ factory.createExpressionFromText("a.${replacementMethod.replace("()", "(e)")}", element) as PsiMethodCallExpression
+ expectedExpression.argumentList.expressions[0].replace(expectedArgument)
+ expectedExpression.methodExpression.qualifierExpression!!.replace(oldExpectedExpression.methodExpression.qualifierExpression!!)
+ oldExpectedExpression.replace(expectedExpression)
+ }
+}
\ No newline at end of file
diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml
index cc1f669..2822fe7 100644
--- a/src/main/resources/META-INF/plugin.xml
+++ b/src/main/resources/META-INF/plugin.xml
@@ -21,21 +21,20 @@
-
-
-
-
+
+
diff --git a/src/main/resources/inspectionDescriptions/AssertThatBinaryExpressionIsTrueOrFalse.html b/src/main/resources/inspectionDescriptions/AssertThatBinaryExpressionIsTrueOrFalse.html
new file mode 100644
index 0000000..19c5434
--- /dev/null
+++ b/src/main/resources/inspectionDescriptions/AssertThatBinaryExpressionIsTrueOrFalse.html
@@ -0,0 +1,11 @@
+
+
+Turns a binary expression in the kind of assertThat(actual <operator> expected) with .isTrue()/isFalse()/isEqualTo()/isNotEqualTo()
+into assertThat(actual).is<operator>(expected).
+
+There are over 150 combinations that are found with this inspections.
+It also detects assertThat(actual.equals(expected)) assertions.
+Also works with constant expressions on the expected side.
+Swaps actual and expected when actual is a constant expression (correctly transforming the used operator).
+
+
\ No newline at end of file
diff --git a/src/test/java/de/platon42/intellij/playground/Playground.java b/src/test/java/de/platon42/intellij/playground/Playground.java
index fc5fcac..2bcde24 100644
--- a/src/test/java/de/platon42/intellij/playground/Playground.java
+++ b/src/test/java/de/platon42/intellij/playground/Playground.java
@@ -72,6 +72,56 @@ public class Playground {
assertThat("").isEqualTo(Boolean.TRUE);
}
+ private void binaryExpression() {
+ int primExp = 42;
+ int primAct = 1337;
+ Double numberObjExp = 42.0;
+ Double numberObjAct = 1337.0;
+ String stringExp = "foo";
+ String stringAct = "bar";
+
+ assertThat(primAct == primExp).isTrue();
+ assertThat(primAct == 1).isTrue();
+ assertThat(primAct == primExp).isEqualTo(false);
+ assertThat(primAct != primExp).isEqualTo(true);
+ assertThat(1 != primAct).isTrue();
+ assertThat(primAct != primExp).isNotEqualTo(true);
+ assertThat(primAct > primExp).isNotEqualTo(false);
+ assertThat(primAct > primExp).isFalse();
+ assertThat(primAct > 1).isFalse();
+ assertThat(primAct >= 1).isTrue();
+ assertThat(primAct >= primExp).isEqualTo(false);
+ assertThat(1 <= primAct).isFalse();
+ assertThat(1 > primAct).isTrue();
+ assertThat(primAct < primExp).isNotEqualTo(true);
+ assertThat(primAct <= primExp).isNotEqualTo(false);
+ assertThat(primAct <= primExp).isFalse();
+ assertThat(primAct <= 1).isFalse();
+ assertThat(numberObjAct == 1).isTrue();
+ assertThat(numberObjAct == numberObjExp).isEqualTo(false);
+ assertThat(numberObjAct != numberObjExp).isEqualTo(true);
+ assertThat(1 != numberObjAct).isTrue();
+ assertThat(numberObjAct != numberObjExp).isNotEqualTo(true);
+ assertThat(numberObjAct > numberObjExp).isNotEqualTo(false);
+ assertThat(numberObjAct > 1).isFalse();
+ assertThat(numberObjAct >= numberObjExp).isTrue();
+ assertThat(numberObjAct >= numberObjExp).isEqualTo(false);
+ assertThat(1 <= numberObjAct).isFalse();
+ assertThat(numberObjAct < numberObjExp).isEqualTo(true);
+ assertThat(numberObjAct < numberObjExp).isNotEqualTo(true);
+ assertThat(numberObjAct <= numberObjExp).isNotEqualTo(false);
+ assertThat(numberObjAct <= 1).isFalse();
+ assertThat(numberObjAct.equals(numberObjExp)).isFalse();
+ assertThat(stringAct == stringExp).isNotEqualTo(false);
+ assertThat(stringAct.equals(stringExp)).isEqualTo(true);
+ assertThat(stringAct != stringExp).isFalse();
+ assertThat(stringAct == null).isNotEqualTo(true);
+ assertThat(null == stringAct).isEqualTo(false);
+
+ assertThat(null == null).isTrue();
+ assertThat(!false).isTrue();
+ }
+
private void stringIsEmpty() {
String foo = "bar";
assertThat(foo).isEqualTo("");
@@ -138,13 +188,9 @@ public class Playground {
assertNotSame(bar, foo);
assertNotSame("same", bar, foo);
- assertEquals(1.0, 2.0);
assertEquals(1.0, 2.0, 0.1);
- assertEquals("equals", 1.0, 2.0);
assertEquals("equals", 1.0, 2.0, 0.1);
- assertEquals(1.0f, 2.0f);
assertEquals(1.0f, 2.0f, 0.1f);
- assertEquals("equals", 1.0f, 2.0f);
assertEquals("equals", 1.0f, 2.0f, 0.1f);
assertNotEquals(1.0, 2.0);
@@ -213,5 +259,4 @@ public class Playground {
assertThat(new Object()).extracting(Object::toString, Object::hashCode);
}
-
-}
+}
\ No newline at end of file
diff --git a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBinaryExpressionIsTrueOrFalseInspectionTest.kt b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBinaryExpressionIsTrueOrFalseInspectionTest.kt
new file mode 100644
index 0000000..d84f1bb
--- /dev/null
+++ b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/AssertThatBinaryExpressionIsTrueOrFalseInspectionTest.kt
@@ -0,0 +1,22 @@
+package de.platon42.intellij.plugins.cajon.inspections
+
+import com.intellij.testFramework.fixtures.JavaCodeInsightTestFixture
+import de.platon42.intellij.jupiter.MyFixture
+import de.platon42.intellij.jupiter.TestDataSubPath
+import de.platon42.intellij.plugins.cajon.AbstractCajonTest
+import org.junit.jupiter.api.Test
+
+internal class AssertThatBinaryExpressionIsTrueOrFalseInspectionTest : AbstractCajonTest() {
+
+ @Test
+ @TestDataSubPath("inspections/BinaryExpression")
+ internal fun assertThat_of_binary_expression_can_be_moved_out(@MyFixture myFixture: JavaCodeInsightTestFixture) {
+ runTest {
+ myFixture.enableInspections(AssertThatBinaryExpressionIsTrueOrFalseInspection::class.java)
+ myFixture.configureByFile("BinaryExpressionBefore.java")
+ executeQuickFixes(myFixture, Regex.fromLiteral("Split binary expression out of assertThat()"), 144)
+ executeQuickFixes(myFixture, Regex.fromLiteral("Split equals() expression out of assertThat()"), 12)
+ myFixture.checkResultByFile("BinaryExpressionAfter.java")
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/test/resources/inspections/BinaryExpression/BinaryExpressionAfter.java b/src/test/resources/inspections/BinaryExpression/BinaryExpressionAfter.java
new file mode 100644
index 0000000..1a1bfa4
--- /dev/null
+++ b/src/test/resources/inspections/BinaryExpression/BinaryExpressionAfter.java
@@ -0,0 +1,190 @@
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class BinaryExpression {
+
+ private void binaryExpression() {
+ int primExp = 42;
+ int primAct = 1337;
+ Double numberObjExp = 42.0;
+ Double numberObjAct = 1337.0;
+ String stringExp = "foo";
+ String stringAct = "bar";
+
+ assertThat(primAct).isEqualTo(primExp);
+ assertThat(primAct).isEqualTo(primExp);
+ assertThat(primAct).isEqualTo(primExp);
+ assertThat(primAct).isEqualTo(1);
+ assertThat(primAct).isEqualTo(1);
+ assertThat(primAct).isNotEqualTo(primExp);
+ assertThat(primAct).isNotEqualTo(primExp);
+ assertThat(primAct).isNotEqualTo(primExp);
+ assertThat(primAct).isNotEqualTo(1);
+ assertThat(primAct).isNotEqualTo(1);
+
+ assertThat(primAct).isNotEqualTo(primExp);
+ assertThat(primAct).isNotEqualTo(primExp);
+ assertThat(primAct).isNotEqualTo(primExp);
+ assertThat(primAct).isNotEqualTo(1);
+ assertThat(primAct).isNotEqualTo(1);
+ assertThat(primAct).isEqualTo(primExp);
+ assertThat(primAct).isEqualTo(primExp);
+ assertThat(primAct).isEqualTo(primExp);
+ assertThat(primAct).isEqualTo(1);
+ assertThat(primAct).isEqualTo(1);
+
+ assertThat(primAct).isGreaterThan(primExp);
+ assertThat(primAct).isGreaterThan(primExp);
+ assertThat(primAct).isGreaterThan(primExp);
+ assertThat(primAct).isGreaterThan(1);
+ assertThat(primAct).isGreaterThan(1);
+ assertThat(primAct).isLessThanOrEqualTo(primExp);
+ assertThat(primAct).isLessThanOrEqualTo(primExp);
+ assertThat(primAct).isLessThanOrEqualTo(primExp);
+ assertThat(primAct).isLessThanOrEqualTo(1);
+ assertThat(primAct).isLessThanOrEqualTo(1);
+
+ assertThat(primAct).isGreaterThanOrEqualTo(primExp);
+ assertThat(primAct).isGreaterThanOrEqualTo(primExp);
+ assertThat(primAct).isGreaterThanOrEqualTo(primExp);
+ assertThat(primAct).isGreaterThanOrEqualTo(1);
+ assertThat(primAct).isGreaterThanOrEqualTo(1);
+ assertThat(primAct).isLessThan(primExp);
+ assertThat(primAct).isLessThan(primExp);
+ assertThat(primAct).isLessThan(primExp);
+ assertThat(primAct).isLessThan(1);
+ assertThat(primAct).isLessThan(1);
+
+ assertThat(primAct).isLessThan(primExp);
+ assertThat(primAct).isLessThan(primExp);
+ assertThat(primAct).isLessThan(primExp);
+ assertThat(primAct).isLessThan(1);
+ assertThat(primAct).isLessThan(1);
+ assertThat(primAct).isGreaterThanOrEqualTo(primExp);
+ assertThat(primAct).isGreaterThanOrEqualTo(primExp);
+ assertThat(primAct).isGreaterThanOrEqualTo(primExp);
+ assertThat(primAct).isGreaterThanOrEqualTo(1);
+ assertThat(primAct).isGreaterThanOrEqualTo(1);
+
+ assertThat(primAct).isLessThanOrEqualTo(primExp);
+ assertThat(primAct).isLessThanOrEqualTo(primExp);
+ assertThat(primAct).isLessThanOrEqualTo(primExp);
+ assertThat(primAct).isLessThanOrEqualTo(1);
+ assertThat(primAct).isLessThanOrEqualTo(1);
+ assertThat(primAct).isGreaterThan(primExp);
+ assertThat(primAct).isGreaterThan(primExp);
+ assertThat(primAct).isGreaterThan(primExp);
+ assertThat(primAct).isGreaterThan(1);
+ assertThat(primAct).isGreaterThan(1);
+
+ assertThat(numberObjAct).isEqualTo(numberObjExp);
+ assertThat(numberObjAct).isEqualTo(numberObjExp);
+ assertThat(numberObjAct).isEqualTo(numberObjExp);
+ assertThat(numberObjAct).isEqualTo(1);
+ assertThat(numberObjAct).isEqualTo(1);
+ assertThat(numberObjAct).isNotEqualTo(numberObjExp);
+ assertThat(numberObjAct).isNotEqualTo(numberObjExp);
+ assertThat(numberObjAct).isNotEqualTo(numberObjExp);
+ assertThat(numberObjAct).isNotEqualTo(1);
+ assertThat(numberObjAct).isNotEqualTo(1);
+
+ assertThat(numberObjAct).isNotEqualTo(numberObjExp);
+ assertThat(numberObjAct).isNotEqualTo(numberObjExp);
+ assertThat(numberObjAct).isNotEqualTo(numberObjExp);
+ assertThat(numberObjAct).isNotEqualTo(1);
+ assertThat(numberObjAct).isNotEqualTo(1);
+ assertThat(numberObjAct).isEqualTo(numberObjExp);
+ assertThat(numberObjAct).isEqualTo(numberObjExp);
+ assertThat(numberObjAct).isEqualTo(numberObjExp);
+ assertThat(numberObjAct).isEqualTo(1);
+ assertThat(numberObjAct).isEqualTo(1);
+
+ assertThat(numberObjAct).isGreaterThan(numberObjExp);
+ assertThat(numberObjAct).isGreaterThan(numberObjExp);
+ assertThat(numberObjAct).isGreaterThan(numberObjExp);
+ assertThat(numberObjAct).isGreaterThan(1);
+ assertThat(numberObjAct).isGreaterThan(1);
+ assertThat(numberObjAct).isLessThanOrEqualTo(numberObjExp);
+ assertThat(numberObjAct).isLessThanOrEqualTo(numberObjExp);
+ assertThat(numberObjAct).isLessThanOrEqualTo(numberObjExp);
+ assertThat(numberObjAct).isLessThanOrEqualTo(1);
+ assertThat(numberObjAct).isLessThanOrEqualTo(1);
+
+ assertThat(numberObjAct).isGreaterThanOrEqualTo(numberObjExp);
+ assertThat(numberObjAct).isGreaterThanOrEqualTo(numberObjExp);
+ assertThat(numberObjAct).isGreaterThanOrEqualTo(numberObjExp);
+ assertThat(numberObjAct).isGreaterThanOrEqualTo(1);
+ assertThat(numberObjAct).isGreaterThanOrEqualTo(1);
+ assertThat(numberObjAct).isLessThan(numberObjExp);
+ assertThat(numberObjAct).isLessThan(numberObjExp);
+ assertThat(numberObjAct).isLessThan(numberObjExp);
+ assertThat(numberObjAct).isLessThan(1);
+ assertThat(numberObjAct).isLessThan(1);
+
+ assertThat(numberObjAct).isLessThan(numberObjExp);
+ assertThat(numberObjAct).isLessThan(numberObjExp);
+ assertThat(numberObjAct).isLessThan(numberObjExp);
+ assertThat(numberObjAct).isLessThan(1);
+ assertThat(numberObjAct).isLessThan(1);
+ assertThat(numberObjAct).isGreaterThanOrEqualTo(numberObjExp);
+ assertThat(numberObjAct).isGreaterThanOrEqualTo(numberObjExp);
+ assertThat(numberObjAct).isGreaterThanOrEqualTo(numberObjExp);
+ assertThat(numberObjAct).isGreaterThanOrEqualTo(1);
+ assertThat(numberObjAct).isGreaterThanOrEqualTo(1);
+
+ assertThat(numberObjAct).isLessThanOrEqualTo(numberObjExp);
+ assertThat(numberObjAct).isLessThanOrEqualTo(numberObjExp);
+ assertThat(numberObjAct).isLessThanOrEqualTo(numberObjExp);
+ assertThat(numberObjAct).isLessThanOrEqualTo(1);
+ assertThat(numberObjAct).isLessThanOrEqualTo(1);
+ assertThat(numberObjAct).isGreaterThan(numberObjExp);
+ assertThat(numberObjAct).isGreaterThan(numberObjExp);
+ assertThat(numberObjAct).isGreaterThan(numberObjExp);
+ assertThat(numberObjAct).isGreaterThan(1);
+ assertThat(numberObjAct).isGreaterThan(1);
+
+ assertThat(numberObjAct).isEqualTo(numberObjExp);
+ assertThat(numberObjAct).isEqualTo(numberObjExp);
+ assertThat(numberObjAct).isEqualTo(numberObjExp);
+ assertThat(numberObjAct).isNotEqualTo(numberObjExp);
+ assertThat(numberObjAct).isNotEqualTo(numberObjExp);
+ assertThat(numberObjAct).isNotEqualTo(numberObjExp);
+
+ assertThat(stringAct).isSameAs(stringExp);
+ assertThat(stringAct).isSameAs(stringExp);
+ assertThat(stringAct).isSameAs(stringExp);
+ assertThat(stringAct).isNotSameAs(stringExp);
+ assertThat(stringAct).isNotSameAs(stringExp);
+ assertThat(stringAct).isNotSameAs(stringExp);
+
+ assertThat(stringAct).isEqualTo(stringExp);
+ assertThat(stringAct).isEqualTo(stringExp);
+ assertThat(stringAct).isEqualTo(stringExp);
+ assertThat(stringAct).isNotEqualTo(stringExp);
+ assertThat(stringAct).isNotEqualTo(stringExp);
+ assertThat(stringAct).isNotEqualTo(stringExp);
+
+ assertThat(stringAct).isNotSameAs(stringExp);
+ assertThat(stringAct).isNotSameAs(stringExp);
+ assertThat(stringAct).isNotSameAs(stringExp);
+ assertThat(stringAct).isSameAs(stringExp);
+ assertThat(stringAct).isSameAs(stringExp);
+ assertThat(stringAct).isSameAs(stringExp);
+
+ assertThat(stringAct).isNull();
+ assertThat(stringAct).isNull();
+ assertThat(stringAct).isNull();
+ assertThat(stringAct).isNotNull();
+ assertThat(stringAct).isNotNull();
+ assertThat(stringAct).isNotNull();
+
+ assertThat(stringAct).isNull();
+ assertThat(stringAct).isNull();
+ assertThat(stringAct).isNull();
+ assertThat(stringAct).isNotNull();
+ assertThat(stringAct).isNotNull();
+ assertThat(stringAct).isNotNull();
+
+ assertThat(null == null).isTrue();
+ assertThat(!false).isTrue();
+ }
+}
diff --git a/src/test/resources/inspections/BinaryExpression/BinaryExpressionBefore.java b/src/test/resources/inspections/BinaryExpression/BinaryExpressionBefore.java
new file mode 100644
index 0000000..b040f49
--- /dev/null
+++ b/src/test/resources/inspections/BinaryExpression/BinaryExpressionBefore.java
@@ -0,0 +1,190 @@
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class BinaryExpression {
+
+ private void binaryExpression() {
+ int primExp = 42;
+ int primAct = 1337;
+ Double numberObjExp = 42.0;
+ Double numberObjAct = 1337.0;
+ String stringExp = "foo";
+ String stringAct = "bar";
+
+ assertThat(primAct == primExp).isTrue();
+ assertThat(primAct == primExp).isEqualTo(true);
+ assertThat(primAct == primExp).isNotEqualTo(false);
+ assertThat(primAct == 1).isTrue();
+ assertThat(1 == primAct).isTrue();
+ assertThat(primAct == primExp).isFalse();
+ assertThat(primAct == primExp).isEqualTo(false);
+ assertThat(primAct == primExp).isNotEqualTo(true);
+ assertThat(primAct == 1).isFalse();
+ assertThat(1 == primAct).isFalse();
+
+ assertThat(primAct != primExp).isTrue();
+ assertThat(primAct != primExp).isEqualTo(true);
+ assertThat(primAct != primExp).isNotEqualTo(false);
+ assertThat(primAct != 1).isTrue();
+ assertThat(1 != primAct).isTrue();
+ assertThat(primAct != primExp).isFalse();
+ assertThat(primAct != primExp).isEqualTo(false);
+ assertThat(primAct != primExp).isNotEqualTo(true);
+ assertThat(primAct != 1).isFalse();
+ assertThat(1 != primAct).isFalse();
+
+ assertThat(primAct > primExp).isTrue();
+ assertThat(primAct > primExp).isEqualTo(true);
+ assertThat(primAct > primExp).isNotEqualTo(false);
+ assertThat(primAct > 1).isTrue();
+ assertThat(1 < primAct).isTrue();
+ assertThat(primAct > primExp).isFalse();
+ assertThat(primAct > primExp).isEqualTo(false);
+ assertThat(primAct > primExp).isNotEqualTo(true);
+ assertThat(primAct > 1).isFalse();
+ assertThat(1 < primAct).isFalse();
+
+ assertThat(primAct >= primExp).isTrue();
+ assertThat(primAct >= primExp).isEqualTo(true);
+ assertThat(primAct >= primExp).isNotEqualTo(false);
+ assertThat(primAct >= 1).isTrue();
+ assertThat(1 <= primAct).isTrue();
+ assertThat(primAct >= primExp).isFalse();
+ assertThat(primAct >= primExp).isEqualTo(false);
+ assertThat(primAct >= primExp).isNotEqualTo(true);
+ assertThat(primAct >= 1).isFalse();
+ assertThat(1 <= primAct).isFalse();
+
+ assertThat(primAct < primExp).isTrue();
+ assertThat(primAct < primExp).isEqualTo(true);
+ assertThat(primAct < primExp).isNotEqualTo(false);
+ assertThat(primAct < 1).isTrue();
+ assertThat(1 > primAct).isTrue();
+ assertThat(primAct < primExp).isFalse();
+ assertThat(primAct < primExp).isEqualTo(false);
+ assertThat(primAct < primExp).isNotEqualTo(true);
+ assertThat(primAct < 1).isFalse();
+ assertThat(1 > primAct).isFalse();
+
+ assertThat(primAct <= primExp).isTrue();
+ assertThat(primAct <= primExp).isEqualTo(true);
+ assertThat(primAct <= primExp).isNotEqualTo(false);
+ assertThat(primAct <= 1).isTrue();
+ assertThat(1 >= primAct).isTrue();
+ assertThat(primAct <= primExp).isFalse();
+ assertThat(primAct <= primExp).isEqualTo(false);
+ assertThat(primAct <= primExp).isNotEqualTo(true);
+ assertThat(primAct <= 1).isFalse();
+ assertThat(1 >= primAct).isFalse();
+
+ assertThat(numberObjAct == numberObjExp).isTrue();
+ assertThat(numberObjAct == numberObjExp).isEqualTo(true);
+ assertThat(numberObjAct == numberObjExp).isNotEqualTo(false);
+ assertThat(numberObjAct == 1).isTrue();
+ assertThat(1 == numberObjAct).isTrue();
+ assertThat(numberObjAct == numberObjExp).isFalse();
+ assertThat(numberObjAct == numberObjExp).isEqualTo(false);
+ assertThat(numberObjAct == numberObjExp).isNotEqualTo(true);
+ assertThat(numberObjAct == 1).isFalse();
+ assertThat(1 == numberObjAct).isFalse();
+
+ assertThat(numberObjAct != numberObjExp).isTrue();
+ assertThat(numberObjAct != numberObjExp).isEqualTo(true);
+ assertThat(numberObjAct != numberObjExp).isNotEqualTo(false);
+ assertThat(numberObjAct != 1).isTrue();
+ assertThat(1 != numberObjAct).isTrue();
+ assertThat(numberObjAct != numberObjExp).isFalse();
+ assertThat(numberObjAct != numberObjExp).isEqualTo(false);
+ assertThat(numberObjAct != numberObjExp).isNotEqualTo(true);
+ assertThat(numberObjAct != 1).isFalse();
+ assertThat(1 != numberObjAct).isFalse();
+
+ assertThat(numberObjAct > numberObjExp).isTrue();
+ assertThat(numberObjAct > numberObjExp).isEqualTo(true);
+ assertThat(numberObjAct > numberObjExp).isNotEqualTo(false);
+ assertThat(numberObjAct > 1).isTrue();
+ assertThat(1 < numberObjAct).isTrue();
+ assertThat(numberObjAct > numberObjExp).isFalse();
+ assertThat(numberObjAct > numberObjExp).isEqualTo(false);
+ assertThat(numberObjAct > numberObjExp).isNotEqualTo(true);
+ assertThat(numberObjAct > 1).isFalse();
+ assertThat(1 < numberObjAct).isFalse();
+
+ assertThat(numberObjAct >= numberObjExp).isTrue();
+ assertThat(numberObjAct >= numberObjExp).isEqualTo(true);
+ assertThat(numberObjAct >= numberObjExp).isNotEqualTo(false);
+ assertThat(numberObjAct >= 1).isTrue();
+ assertThat(1 <= numberObjAct).isTrue();
+ assertThat(numberObjAct >= numberObjExp).isFalse();
+ assertThat(numberObjAct >= numberObjExp).isEqualTo(false);
+ assertThat(numberObjAct >= numberObjExp).isNotEqualTo(true);
+ assertThat(numberObjAct >= 1).isFalse();
+ assertThat(1 <= numberObjAct).isFalse();
+
+ assertThat(numberObjAct < numberObjExp).isTrue();
+ assertThat(numberObjAct < numberObjExp).isEqualTo(true);
+ assertThat(numberObjAct < numberObjExp).isNotEqualTo(false);
+ assertThat(numberObjAct < 1).isTrue();
+ assertThat(1 > numberObjAct).isTrue();
+ assertThat(numberObjAct < numberObjExp).isFalse();
+ assertThat(numberObjAct < numberObjExp).isEqualTo(false);
+ assertThat(numberObjAct < numberObjExp).isNotEqualTo(true);
+ assertThat(numberObjAct < 1).isFalse();
+ assertThat(1 > numberObjAct).isFalse();
+
+ assertThat(numberObjAct <= numberObjExp).isTrue();
+ assertThat(numberObjAct <= numberObjExp).isEqualTo(true);
+ assertThat(numberObjAct <= numberObjExp).isNotEqualTo(false);
+ assertThat(numberObjAct <= 1).isTrue();
+ assertThat(1 >= numberObjAct).isTrue();
+ assertThat(numberObjAct <= numberObjExp).isFalse();
+ assertThat(numberObjAct <= numberObjExp).isEqualTo(false);
+ assertThat(numberObjAct <= numberObjExp).isNotEqualTo(true);
+ assertThat(numberObjAct <= 1).isFalse();
+ assertThat(1 >= numberObjAct).isFalse();
+
+ assertThat(numberObjAct.equals(numberObjExp)).isTrue();
+ assertThat(numberObjAct.equals(numberObjExp)).isEqualTo(true);
+ assertThat(numberObjAct.equals(numberObjExp)).isNotEqualTo(false);
+ assertThat(numberObjAct.equals(numberObjExp)).isFalse();
+ assertThat(numberObjAct.equals(numberObjExp)).isEqualTo(false);
+ assertThat(numberObjAct.equals(numberObjExp)).isNotEqualTo(true);
+
+ assertThat(stringAct == stringExp).isTrue();
+ assertThat(stringAct == stringExp).isEqualTo(true);
+ assertThat(stringAct == stringExp).isNotEqualTo(false);
+ assertThat(stringAct == stringExp).isFalse();
+ assertThat(stringAct == stringExp).isEqualTo(false);
+ assertThat(stringAct == stringExp).isNotEqualTo(true);
+
+ assertThat(stringAct.equals(stringExp)).isTrue();
+ assertThat(stringAct.equals(stringExp)).isEqualTo(true);
+ assertThat(stringAct.equals(stringExp)).isNotEqualTo(false);
+ assertThat(stringAct.equals(stringExp)).isFalse();
+ assertThat(stringAct.equals(stringExp)).isEqualTo(false);
+ assertThat(stringAct.equals(stringExp)).isNotEqualTo(true);
+
+ assertThat(stringAct != stringExp).isTrue();
+ assertThat(stringAct != stringExp).isEqualTo(true);
+ assertThat(stringAct != stringExp).isNotEqualTo(false);
+ assertThat(stringAct != stringExp).isFalse();
+ assertThat(stringAct != stringExp).isEqualTo(false);
+ assertThat(stringAct != stringExp).isNotEqualTo(true);
+
+ assertThat(stringAct == null).isTrue();
+ assertThat(stringAct == null).isEqualTo(true);
+ assertThat(stringAct == null).isNotEqualTo(false);
+ assertThat(stringAct == null).isFalse();
+ assertThat(stringAct == null).isEqualTo(false);
+ assertThat(stringAct == null).isNotEqualTo(true);
+
+ assertThat(null == stringAct).isTrue();
+ assertThat(null == stringAct).isEqualTo(true);
+ assertThat(null == stringAct).isNotEqualTo(false);
+ assertThat(null == stringAct).isFalse();
+ assertThat(null == stringAct).isEqualTo(false);
+ assertThat(null == stringAct).isNotEqualTo(true);
+
+ assertThat(null == null).isTrue();
+ assertThat(!false).isTrue();
+ }
+}