diff --git a/README.md b/README.md index d366b1c..0a341dd 100644 --- a/README.md +++ b/README.md @@ -78,10 +78,8 @@ You can toggle the various inspections in the Settings/Editor/Inspections in the assertThat(expected).anotherCondition(); to: assertThat(expected).someCondition().anotherCondition(); ``` - Joining will work on actual expressions inside assertThat() that are - - the same variable reference - - textually equal binary expressions - - the same method calls (except for known side-effect methods such as ```Iterator.next()``` -- please notify me about others) + Joining will work on actual expressions inside assertThat() that are equivalent expressions, + except for method calls with known side-effect methods such as ```Iterator.next()``` -- please notify me about others. The comments of the statements will be preserved. When using ```.extracting()``` or similar, the statements will not be merged. @@ -345,6 +343,9 @@ Feel free to use the code (in package de.platon42.intellij.jupiter) for your pro ## Changelog +#### V0.8 (unreleased) +- Fixed missing description for JoinAssertThatStatements and detection of equivalent expressions (sorry, released it too hastily). + #### V0.7 (28-Apr-19) - Another fix for AssertThatGuavaOptional inspection regarding using the same family name for slightly different quick fix executions (really, Jetbrains, this sucks for no reason). diff --git a/build.gradle b/build.gradle index e1bcef4..097d173 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group 'de.platon42' -version '0.7' +version '0.8' repositories { mavenCentral() @@ -40,6 +40,10 @@ intellij { patchPluginXml { changeNotes """ +

V0.8 (unreleased)

+

V0.7 (28-Apr-19)

-

V0.6 (22-Apr-19)

-

Full changelog available at Github project site.

""" } diff --git a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/JoinAssertThatStatementsInspection.kt b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/JoinAssertThatStatementsInspection.kt index 0948727..694331b 100644 --- a/src/main/java/de/platon42/intellij/plugins/cajon/inspections/JoinAssertThatStatementsInspection.kt +++ b/src/main/java/de/platon42/intellij/plugins/cajon/inspections/JoinAssertThatStatementsInspection.kt @@ -4,6 +4,7 @@ import com.intellij.codeInspection.ProblemHighlightType import com.intellij.codeInspection.ProblemsHolder import com.intellij.psi.* import com.intellij.psi.util.PsiTreeUtil +import com.siyeh.ig.psiutils.TrackingEquivalenceChecker import de.platon42.intellij.plugins.cajon.* import de.platon42.intellij.plugins.cajon.quickfixes.JoinStatementsQuickFix @@ -25,6 +26,7 @@ class JoinAssertThatStatementsInspection : AbstractAssertJInspection() { var sameCount = 0 var firstStatement: PsiStatement? = null var lastStatement: PsiStatement? = null + val equivalenceChecker = TrackingEquivalenceChecker() for (statement in statements) { val assertThatCall = isLegitAssertThatCall(statement) var reset = true @@ -34,11 +36,9 @@ class JoinAssertThatStatementsInspection : AbstractAssertJInspection() { actualExpression = assertThatCall.firstArg if (!reset) { val isSame = when (actualExpression) { - is PsiReferenceExpression -> (actualExpression.qualifierExpression == (lastActualExpression as? PsiReferenceExpression)?.qualifierExpression) - is PsiMethodCallExpression -> (actualExpression.text == (lastActualExpression as? PsiMethodCallExpression)?.text) + is PsiMethodCallExpression -> equivalenceChecker.expressionsAreEquivalent(actualExpression, lastActualExpression) && !KNOWN_METHODS_WITH_SIDE_EFFECTS.test(actualExpression) - is PsiPolyadicExpression -> (actualExpression.text == (lastActualExpression as? PsiPolyadicExpression)?.text) - else -> false + else -> equivalenceChecker.expressionsAreEquivalent(actualExpression, lastActualExpression) } if (isSame) { sameCount++ diff --git a/src/main/resources/inspectionDescriptions/JoinAssertThatStatements.html b/src/main/resources/inspectionDescriptions/JoinAssertThatStatements.html new file mode 100644 index 0000000..efdb20b --- /dev/null +++ b/src/main/resources/inspectionDescriptions/JoinAssertThatStatements.html @@ -0,0 +1,7 @@ + + +Joins consecutive assertThat() statements with the same actual expression together. + +
Retains comments during operation. If the AssertThat()-Statement contains .extracting() methods, they will not be joined. + + \ No newline at end of file diff --git a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/JoinAssertThatStatementsInspectionTest.kt b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/JoinAssertThatStatementsInspectionTest.kt index c760ff2..fd25551 100644 --- a/src/test/java/de/platon42/intellij/plugins/cajon/inspections/JoinAssertThatStatementsInspectionTest.kt +++ b/src/test/java/de/platon42/intellij/plugins/cajon/inspections/JoinAssertThatStatementsInspectionTest.kt @@ -10,11 +10,11 @@ internal class JoinAssertThatStatementsInspectionTest : AbstractCajonTest() { @Test @TestDataSubPath("inspections/JoinStatements") - internal fun assertThat_size_of_array_or_collection_can_be_simplified(@MyFixture myFixture: JavaCodeInsightTestFixture) { + internal fun assertThat_statements_can_be_joined_together(@MyFixture myFixture: JavaCodeInsightTestFixture) { runTest { myFixture.enableInspections(JoinAssertThatStatementsInspection::class.java) myFixture.configureByFile("JoinStatementsBefore.java") - executeQuickFixes(myFixture, Regex.fromLiteral("Join assertThat() statements"), 6) + executeQuickFixes(myFixture, Regex.fromLiteral("Join assertThat() statements"), 5) myFixture.checkResultByFile("JoinStatementsAfter.java") } } diff --git a/src/test/resources/inspections/JoinStatements/JoinStatementsAfter.java b/src/test/resources/inspections/JoinStatements/JoinStatementsAfter.java index 3b18060..4d8a8eb 100644 --- a/src/test/resources/inspections/JoinStatements/JoinStatementsAfter.java +++ b/src/test/resources/inspections/JoinStatements/JoinStatementsAfter.java @@ -6,6 +6,8 @@ public class JoinStatements { private void joinStatements() { List list = new ArrayList<>(); + List otherList = new ArrayList<>(); + // the future is always born in pain /* tricky */ assertThat(list).as("foo").hasSize(2) @@ -21,18 +23,26 @@ public class JoinStatements { // moar! .doesNotContain("foobar"); - assertThat("narf").isNotEqualTo("puit").as("bar").contains("barbar").as("foo").hasSize(2); + assertThat("narf").isNotEqualTo("puit"); + assertThat(list).as("bar").contains("barbar").as("foo").hasSize(2); assertThat(list).as("evil").extracting(String::length).contains(2); assertThat(list).as("bar").contains("barbar"); - assertThat("narf").isNotEqualTo("puit").as("foo").hasSize(2); + assertThat(otherList).contains("puit"); + assertThat(list).as("foo").hasSize(2); if (true) { assertThat(list).doesNotContain("narf").as("bar").contains("barbar"); } assertThat(list.get(0)).isNotEmpty().hasSize(3).isEqualTo("bar"); + assertThat(otherList.get(0)).isNotEmpty(); + assertThat(list.get(0)).hasSize(3); + assertThat(list.get(0) + "foo").isEqualTo("bar").doesNotStartWith("foo"); + assertThat(otherList.get(0) + "foo").isEqualTo("bar"); + assertThat(list.get(0) + "foo").doesNotStartWith("foo"); + Iterator iterator = list.iterator(); assertThat(iterator.next()).isEqualTo("foo"); assertThat(iterator.next()).isEqualTo("bar"); diff --git a/src/test/resources/inspections/JoinStatements/JoinStatementsBefore.java b/src/test/resources/inspections/JoinStatements/JoinStatementsBefore.java index 7e924d1..c74379a 100644 --- a/src/test/resources/inspections/JoinStatements/JoinStatementsBefore.java +++ b/src/test/resources/inspections/JoinStatements/JoinStatementsBefore.java @@ -6,6 +6,8 @@ public class JoinStatements { private void joinStatements() { List list = new ArrayList<>(); + List otherList = new ArrayList<>(); + // the future is always born in pain /* tricky */assertThat(list).as("foo").hasSize(2); /* do one */ /* do another */ assertThat(list).as("bar").contains("barbar"); // comment to keep @@ -24,7 +26,7 @@ public class JoinStatements { assertThat(list).as("evil").extracting(String::length).contains(2); assertThat(list).as("bar").contains("barbar"); - assertThat("narf").isNotEqualTo("puit"); + assertThat(otherList).contains("puit"); assertThat(list).as("foo").hasSize(2); if (true) { assertThat(list).doesNotContain("narf"); @@ -34,9 +36,15 @@ public class JoinStatements { assertThat(list.get(0)).hasSize(3); assertThat(list.get(0)).isEqualTo("bar"); + assertThat(otherList.get(0)).isNotEmpty(); + assertThat(list.get(0)).hasSize(3); + assertThat(list.get(0) + "foo").isEqualTo("bar"); assertThat(list.get(0) + "foo").doesNotStartWith("foo"); + assertThat(otherList.get(0) + "foo").isEqualTo("bar"); + assertThat(list.get(0) + "foo").doesNotStartWith("foo"); + Iterator iterator = list.iterator(); assertThat(iterator.next()).isEqualTo("foo"); assertThat(iterator.next()).isEqualTo("bar");