Internally: Upgraded to AssertJ 13.2.2.

Support for hasSizeLessThan(), hasSizeLessThanOrEqualTo(), hasSizeGreaterThanOrEqualTo(), and hasSizeGreaterThan() for AssertThatSizeInspection (with AssertJ >=13.2.0).
Really fixed highlighting for JUnit conversion. Sorry.
Fixed testing code to work against IDEA 2019.1.
This commit is contained in:
2019-04-06 21:29:13 +02:00
parent 5d91eaf276
commit ba56325299
10 changed files with 155 additions and 18 deletions
@@ -2,10 +2,7 @@ package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.AbstractBaseJavaLocalInspectionTool
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.psi.CommonClassNames
import com.intellij.psi.JavaPsiFacade
import com.intellij.psi.PsiCapturedWildcardType
import com.intellij.psi.PsiMethodCallExpression
import com.intellij.psi.*
import com.intellij.psi.search.GlobalSearchScope
import com.intellij.psi.util.PsiTypesUtil
import com.siyeh.ig.callMatcher.CallMatcher
@@ -133,4 +130,12 @@ open class AbstractAssertJInspection : AbstractBaseJavaLocalInspectionTool() {
val constantEvaluationHelper = JavaPsiFacade.getInstance(expression.project).constantEvaluationHelper
return constantEvaluationHelper.computeConstantExpression(valueExpression)
}
protected fun hasAssertJMethod(element: PsiElement, classAndMethod: String): Boolean {
val classname = "org.assertj.core.api.${classAndMethod.substringBeforeLast(".")}"
val findClass =
JavaPsiFacade.getInstance(element.project).findClass(classname, GlobalSearchScope.allScope(element.project))
?: return false
return findClass.findMethodsByName(classAndMethod.substringAfterLast(".")).isNotEmpty()
}
}
@@ -53,6 +53,19 @@ class AssertThatSizeInspection : AbstractAssertJInspection() {
registerSizeMethod(holder, expression, expectedCallExpression, "isNotEmpty()", noExpectedExpression = true)
return
}
// new stuff in AssertJ 13.2.0
if (hasAssertJMethod(expression, "AbstractIterableAssert.hasSizeLessThan")) {
val matchedMethod = listOf(
Pair(IS_GREATER_THAN_INT, "hasSizeGreaterThan()"),
Pair(IS_GREATER_THAN_OR_EQUAL_TO_INT, "hasSizeGreaterThanOrEqualTo()"),
Pair(IS_LESS_THAN_OR_EQUAL_TO_INT, "hasSizeLessThanOrEqualTo()"),
Pair(IS_LESS_THAN_INT, "hasSizeLessThan()")
).find { it.first.test(expectedCallExpression) }?.second
if (matchedMethod != null) {
registerSizeMethod(holder, expression, expectedCallExpression, matchedMethod)
return
}
}
}
}
}
@@ -1,8 +1,6 @@
package de.platon42.intellij.plugins.cajon.inspections
import com.intellij.codeInspection.ProblemHighlightType
import com.intellij.codeInspection.ProblemsHolder
import com.intellij.openapi.util.TextRange
import com.intellij.psi.CommonClassNames
import com.intellij.psi.JavaElementVisitor
import com.intellij.psi.PsiElementVisitor
@@ -147,8 +145,6 @@ class JUnitAssertToAssertJInspection : AbstractJUnitAssertInspection() {
holder.registerProblem(
expression,
message,
ProblemHighlightType.INFORMATION,
null as TextRange?,
ReplaceJUnitAssertMethodCallQuickFix(description, hasExpected, replacementMethod)
)
}
@@ -164,8 +160,6 @@ class JUnitAssertToAssertJInspection : AbstractJUnitAssertInspection() {
holder.registerProblem(
expression,
message,
ProblemHighlightType.INFORMATION,
null as TextRange?,
ReplaceJUnitDeltaAssertMethodCallQuickFix(description, replacementMethod)
)
}
@@ -131,7 +131,6 @@ public class LightCodeInsightExtension implements ParameterResolver, AfterTestEx
VirtualFile jarFile = LocalFileSystem.getInstance().findFileByIoFile(jarPath.toFile());
myFixture.allowTreeAccessForFile(jarFile);
PsiTestUtil.addLibrary(
myFixture.getModule(),
model,
jarPath.getFileName().toString().replace(".jar", ""),
jarPath.getParent().toString(),
@@ -4,9 +4,11 @@ import org.assertj.core.api.ListAssert;
import org.assertj.core.data.Offset;
import java.util.ArrayList;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.Offset.offset;
import static org.assertj.guava.api.Assertions.assertThat;
import static org.junit.Assert.*;
public class Playground {
@@ -27,6 +29,7 @@ public class Playground {
assertThat(new ArrayList<String>().size()).isEqualTo(1);
assertThat(new ArrayList<String>().size()).isGreaterThanOrEqualTo(1);
assertThat(new ArrayList<String>().size()).isZero();
assertThat(new ArrayList<String>()).hasSizeGreaterThan(1);
assertThat(new ArrayList<String>()).hasSameSizeAs(new ArrayList<>());
assertThat(new Long[1]).as("etc").hasSameSizeAs(new Long[2]);
}
@@ -75,6 +78,30 @@ public class Playground {
assertThat(foo).hasSize(0);
}
private void java8Optional() {
Optional<String> foo = Optional.empty();
assertThat(foo.get()).isEqualTo("bla");
assertThat(foo).contains("bla");
assertThat(foo.isPresent()).isTrue();
assertThat(!foo.isPresent()).isFalse();
assertThat(foo).isPresent();
assertThat(foo.isPresent()).isFalse();
assertThat(!foo.isPresent()).isTrue();
assertThat(foo).isNotPresent();
}
private void guavaOptional() {
com.google.common.base.Optional<String> foo = com.google.common.base.Optional.absent();
assertThat(foo.get()).isEqualTo("bla");
assertThat(foo).contains("bla");
assertThat(foo.isPresent()).isTrue();
assertThat(!foo.isPresent()).isFalse();
assertThat(foo).isPresent();
assertThat(foo.isPresent()).isFalse();
assertThat(!foo.isPresent()).isTrue();
assertThat(foo).isAbsent();
}
private void junitAssertions() {
assertTrue(true);
assertTrue("message", true);
@@ -183,6 +210,8 @@ public class Playground {
assertThat(new double[1]).as("array equals").containsExactly(new double[2], offset(1.0));
assertThat(new float[1]).containsExactly(new float[2], offset(1.0f));
assertThat(new float[1]).as("array equals").containsExactly(new float[2], offset(1.0f));
assertThat(new Object()).extracting(Object::toString, Object::hashCode);
}
}
@@ -14,7 +14,7 @@ internal class AssertThatSizeInspectionTest : AbstractCajonTest() {
runTest {
myFixture.enableInspections(AssertThatSizeInspection::class.java)
myFixture.configureByFile("AssertThatSizeBefore.java")
executeQuickFixes(myFixture, Regex("Replace .*"), 20)
executeQuickFixes(myFixture, Regex("Replace .*"), 28)
myFixture.checkResultByFile("AssertThatSizeAfter.java")
}
}
@@ -2,7 +2,7 @@ import java.util.ArrayList;
import static org.assertj.core.api.Assertions.assertThat;
public class assertThatSize {
public class AssertThatSize {
private void assertThatSize() {
ArrayList<String> list = new ArrayList<>();
@@ -20,6 +20,10 @@ public class assertThatSize {
assertThat(list).hasSameSizeAs(otherList);
assertThat(list).hasSameSizeAs(array);
assertThat(list).hasSize(1);
assertThat(list).hasSizeGreaterThan(list.size() * 2);
assertThat(list).hasSizeGreaterThanOrEqualTo(list.size() * 2);
assertThat(list).hasSizeLessThan(list.size() * 2);
assertThat(list).hasSizeLessThanOrEqualTo(list.size() * 2);
assertThat(array).isEmpty();
assertThat(array).isEmpty();
@@ -31,5 +35,9 @@ public class assertThatSize {
assertThat(array).hasSameSizeAs(list);
assertThat(array).hasSameSizeAs(otherArray);
assertThat(array).hasSize(1);
assertThat(array).hasSizeGreaterThan(otherArray.length - 1);
assertThat(array).hasSizeGreaterThanOrEqualTo(otherArray.length + 1);
assertThat(array).hasSizeLessThan(otherArray.length - 3);
assertThat(array).hasSizeLessThanOrEqualTo(1 - otherArray.length);
}
}
@@ -2,7 +2,7 @@ import java.util.ArrayList;
import static org.assertj.core.api.Assertions.assertThat;
public class assertThatSize {
public class AssertThatSize {
private void assertThatSize() {
ArrayList<String> list = new ArrayList<>();
@@ -20,6 +20,10 @@ public class assertThatSize {
assertThat(list.size()).isEqualTo(otherList.size());
assertThat(list.size()).isEqualTo(array.length);
assertThat(list.size()).isEqualTo(1);
assertThat(list.size()).isGreaterThan(list.size() * 2);
assertThat(list.size()).isGreaterThanOrEqualTo(list.size() * 2);
assertThat(list.size()).isLessThan(list.size() * 2);
assertThat(list.size()).isLessThanOrEqualTo(list.size() * 2);
assertThat(array.length).isEqualTo(0);
assertThat(array.length).isZero();
@@ -31,5 +35,9 @@ public class assertThatSize {
assertThat(array.length).isEqualTo(list.size());
assertThat(array.length).isEqualTo(otherArray.length);
assertThat(array.length).isEqualTo(1);
assertThat(array.length).isGreaterThan(otherArray.length - 1);
assertThat(array.length).isGreaterThanOrEqualTo(otherArray.length + 1);
assertThat(array.length).isLessThan(otherArray.length - 3);
assertThat(array.length).isLessThanOrEqualTo(1 - otherArray.length);
}
}