Fixed missing description for JoinAssertThatStatements and detection of equivalent expressions.
This commit is contained in:
+4
-4
@@ -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++
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<html>
|
||||
<body>
|
||||
Joins consecutive assertThat() statements with the same actual expression together.
|
||||
<!-- tooltip end -->
|
||||
<br>Retains comments during operation. If the AssertThat()-Statement contains .extracting() methods, they will not be joined.
|
||||
</body>
|
||||
</html>
|
||||
+2
-2
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,8 @@ public class JoinStatements {
|
||||
|
||||
private void joinStatements() {
|
||||
List<String> list = new ArrayList<>();
|
||||
List<String> 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<String> iterator = list.iterator();
|
||||
assertThat(iterator.next()).isEqualTo("foo");
|
||||
assertThat(iterator.next()).isEqualTo("bar");
|
||||
|
||||
@@ -6,6 +6,8 @@ public class JoinStatements {
|
||||
|
||||
private void joinStatements() {
|
||||
List<String> list = new ArrayList<>();
|
||||
List<String> 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<String> iterator = list.iterator();
|
||||
assertThat(iterator.next()).isEqualTo("foo");
|
||||
assertThat(iterator.next()).isEqualTo("bar");
|
||||
|
||||
Reference in New Issue
Block a user