mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-05 01:50:56 +07:00
[jvm] Take extracting call into account while checking assertEquals types
IDEA-311934 Fixed GitOrigin-RevId: c856b24a687e8e5aab4c68621510f3f56c22c688
This commit is contained in:
committed by
intellij-monorepo-bot
parent
24a8be2e87
commit
d4c43b6aca
@@ -87,19 +87,22 @@ private class AssertEqualsBetweenInconvertibleTypesVisitor(private val holder: P
|
||||
}
|
||||
|
||||
private fun processAssertJ(call: UCallExpression) {
|
||||
if (!assertjIsEqualMatcher.uCallMatches(call)) return
|
||||
if (!ASSERTJ_IS_EQUALS_MATCHER.uCallMatches(call)) return
|
||||
var qualifier = getQualifier(call)
|
||||
if (qualifier == null) return
|
||||
val chain = qualifier.getOutermostQualified().getQualifiedChain()
|
||||
val lastDescribed = chain.lastOrNull { expr ->
|
||||
expr is UCallExpression && assertjDescribedMatcher.uCallMatches(expr)
|
||||
expr is UCallExpression && ASSERTJ_DESCRIBED_MATCHER.uCallMatches(expr)
|
||||
}
|
||||
if (lastDescribed != null) qualifier = getQualifier(lastDescribed as UCallExpression)
|
||||
if (qualifier == null || !assertJAssertThatMatcher.uCallMatches(qualifier)) return
|
||||
val callValueArguments = call.valueArguments
|
||||
if (qualifier == null || !ASSERTJ_ASSERT_THAT_MATCHER.uCallMatches(qualifier)) return
|
||||
val lastExtracting = chain.lastOrNull { expr ->
|
||||
expr is UCallExpression && ASSERTJ_EXTRACTING_MATCHER.uCallMatches(expr)
|
||||
} as UCallExpression?
|
||||
val callValueArguments = lastExtracting?.valueArguments ?: call.valueArguments
|
||||
val qualValueArguments = qualifier.valueArguments
|
||||
if (callValueArguments.isEmpty() || qualValueArguments.isEmpty()) return
|
||||
checkConvertibleTypes(call, callValueArguments[0], qualValueArguments[0], holder)
|
||||
checkConvertibleTypes(call, callValueArguments.first(), qualValueArguments[0], holder)
|
||||
}
|
||||
|
||||
|
||||
@@ -123,15 +126,19 @@ private class AssertEqualsBetweenInconvertibleTypesVisitor(private val holder: P
|
||||
private fun isAssertNotSameMethod(methodName: String): Boolean = "assertNotSame" == methodName || "isNotSameAs" == methodName
|
||||
|
||||
private companion object {
|
||||
private val assertjIsEqualMatcher: CallMatcher = CallMatcher.instanceCall(
|
||||
private val ASSERTJ_IS_EQUALS_MATCHER: CallMatcher = CallMatcher.instanceCall(
|
||||
"org.assertj.core.api.Assert", "isEqualTo", "isSameAs", "isNotEqualTo", "isNotSameAs"
|
||||
).parameterTypes(CommonClassNames.JAVA_LANG_OBJECT)
|
||||
|
||||
private val assertjDescribedMatcher: CallMatcher = CallMatcher.instanceCall(
|
||||
private val ASSERTJ_DESCRIBED_MATCHER: CallMatcher = CallMatcher.instanceCall(
|
||||
"org.assertj.core.api.Descriptable", "describedAs", "as"
|
||||
)
|
||||
|
||||
private val assertJAssertThatMatcher: CallMatcher = CallMatcher.staticCall(
|
||||
private val ASSERTJ_EXTRACTING_MATCHER: CallMatcher = CallMatcher.instanceCall(
|
||||
"org.assertj.core.api.AbstractObjectAssert", "extracting"
|
||||
)
|
||||
|
||||
private val ASSERTJ_ASSERT_THAT_MATCHER: CallMatcher = CallMatcher.staticCall(
|
||||
"org.assertj.core.api.Assertions", "assertThat"
|
||||
).parameterCount(1)
|
||||
}
|
||||
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.siyeh.igtest.junit.assert_equals_between_inconvertible_types;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class AssertEqualsBetweenInconvertibleTypes {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Double d = 1.0;
|
||||
assertEquals(1.0, d , 0.0); // fine.
|
||||
assertEquals(1.0, d , 0); // 'assertEquals()' between inconvertible types 'Double' and 'int'
|
||||
assertEquals(1, d , 0.0); // Doesn't complain even though perhaps it should.
|
||||
}
|
||||
|
||||
public void testFoo() {
|
||||
org.junit.Assert.<warning descr="'assertEquals()' between objects of inconvertible types 'String' and 'double'">assertEquals</warning>("java", 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollection() {
|
||||
Collection<A> c1 = null;
|
||||
Collection<B> c2 = null;
|
||||
assertEquals(c1, c2);
|
||||
assertEquals(new ArrayList<String>(){}, new ArrayList<String>());
|
||||
assertEquals(new TreeSet<String>(){}, new HashSet<String>());
|
||||
<warning descr="'assertEquals()' between objects of inconvertible types 'TreeSet<Integer>' and 'HashSet<String>'">assertEquals</warning>(new TreeSet<Integer>(), new HashSet<String>());
|
||||
}
|
||||
|
||||
interface A {}
|
||||
interface B extends A {}
|
||||
|
||||
private static class GenericClass<T> {}
|
||||
|
||||
public static boolean areEqual(Object a, GenericClass<String> b) {
|
||||
return a.equals(b);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.siyeh.igtest.junit.assert_equals_between_inconvertible_types;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
Assertions.assertThat("foo").<warning descr="'isEqualTo()' between objects of inconvertible types 'int' and 'String'">isEqualTo</warning>(2);
|
||||
Assertions.assertThat("foo").isEqualTo("bar");
|
||||
Assertions.assertThat("foo").describedAs("foo").<warning descr="'isEqualTo()' between objects of inconvertible types 'int' and 'String'">isEqualTo</warning>(2);
|
||||
Assertions.assertThat("foo").as("foo").<warning descr="'isEqualTo()' between objects of inconvertible types 'int' and 'String'">isEqualTo</warning>(2);
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.siyeh.igtest.junit.assert_equals_between_inconvertible_types;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
<warning descr="'assertEquals()' between objects of inconvertible types 'int' and 'String'">assertEquals</warning>(1, "", "error message");
|
||||
<warning descr="'assertEquals()' between objects of inconvertible types 'int' and 'String'">assertEquals</warning>(1, "", () -> "error message in supplier");
|
||||
assertEquals(1, 42, "message");
|
||||
assertEquals(1, 42, () -> "message");
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.siyeh.igtest.junit.assert_equals_between_inconvertible_types;
|
||||
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class AssertNotEqualsBetweenInconvertibleTypes {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
<weak_warning descr="Possibly redundant assertion: incompatible types are compared 'String' and 'int'">assertNotEquals</weak_warning>("java", 1);
|
||||
<weak_warning descr="Possibly redundant assertion: incompatible types are compared 'int[]' and 'double'">assertNotEquals</weak_warning>(new int[0], 1.0);
|
||||
assertNotEquals(new int[0], new int[1]); //ok
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.siyeh.igtest.junit.assert_equals_between_inconvertible_types;
|
||||
|
||||
import org.assertj.core.api.Assertions;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
Assertions.assertThat("java").as("test").<weak_warning descr="Possibly redundant assertion: incompatible types are compared 'int' and 'String'">isNotEqualTo</weak_warning>(1);
|
||||
Assertions.assertThat(new int[0]).describedAs("test").<weak_warning descr="Possibly redundant assertion: incompatible types are compared 'double' and 'int[]'">isNotEqualTo</weak_warning>(1.0);
|
||||
Assertions.assertThat(new int[0]).isNotEqualTo(new int[1]); //ok
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.siyeh.igtest.junit.assert_equals_between_inconvertible_types;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
<weak_warning descr="Possible redundant assertion: incompatible types are compared 'String' and 'int'">assertNotEquals</weak_warning>("java", 1, "message");
|
||||
<weak_warning descr="Possible redundant assertion: incompatible types are compared 'int[]' and 'double'">assertNotEquals</weak_warning>(new int[0], 1.0, "message");
|
||||
assertNotEquals(new int[0], new int[1]); //ok
|
||||
}
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.siyeh.igtest.junit.assert_equals_between_inconvertible_types;
|
||||
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class AssertNotSameBetweenInconvertibleTypes {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
<warning descr="Redundant assertion: incompatible types are compared 'String' and 'int'">assertNotSame</warning>("java", 1);
|
||||
<warning descr="Redundant assertion: incompatible types are compared 'int[]' and 'double'">assertNotSame</warning>(new int[0], 1.0);
|
||||
assertNotSame(new int[0], new int[1]); //ok
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.siyeh.igtest.junit.assert_equals_between_inconvertible_types;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
assertThat("java").as("test").<warning descr="Redundant assertion: incompatible types are compared 'int' and 'String'">isNotSameAs</warning>(1);
|
||||
assertThat(new int[0]).describedAs("test").<warning descr="Redundant assertion: incompatible types are compared 'double' and 'int[]'">isNotSameAs</warning>(1.0);
|
||||
assertThat(new int[0]).isNotSameAs(new int[1]); //ok
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.siyeh.igtest.junit.assert_equals_between_inconvertible_types;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
<warning descr="Redundant assertion: incompatible types are compared 'String' and 'int'">assertNotSame</warning>("java", 1, "message");
|
||||
<warning descr="Redundant assertion: incompatible types are compared 'int[]' and 'double'">assertNotSame</warning>(new int[0], 1.0, "message");
|
||||
assertNotSame(new int[0], new int[1]); //ok
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.siyeh.igtest.junit.assert_equals_between_inconvertible_types;
|
||||
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
<warning descr="'assertSame()' between objects of inconvertible types 'String' and 'int'">assertSame</warning>("foo", 2);
|
||||
<warning descr="'assertSame()' between objects of inconvertible types 'int[]' and 'int'">assertSame</warning>(new int[2], 2);
|
||||
assertSame(1, 2); // ok
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.siyeh.igtest.junit.assert_equals_between_inconvertible_types;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
assertThat(1).<warning descr="'isSameAs()' between objects of inconvertible types 'String' and 'int'">isSameAs</warning>("foo");
|
||||
assertThat("foo").describedAs("foo").<warning descr="'isSameAs()' between objects of inconvertible types 'int' and 'String'">isSameAs</warning>(2);
|
||||
assertThat(new int[2]).as("array").<warning descr="'isSameAs()' between objects of inconvertible types 'int' and 'int[]'">isSameAs</warning>(2);
|
||||
assertThat(1).isSameAs(2); // ok
|
||||
}
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.siyeh.igtest.junit.assert_equals_between_inconvertible_types;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
<warning descr="'assertSame()' between objects of inconvertible types 'int' and 'String'">assertSame</warning>(1, "", "Foo");
|
||||
<warning descr="'assertSame()' between objects of inconvertible types 'int' and 'int[]'">assertSame</warning>(1, new int[2], () -> "Foo in supplier");
|
||||
assertSame(1, 2, "message"); // ok
|
||||
assertSame(1, 2, () -> "message"); // ok
|
||||
}
|
||||
}
|
||||
@@ -1,57 +1,240 @@
|
||||
package com.intellij.codeInspection.tests.java.test
|
||||
|
||||
import com.intellij.codeInspection.tests.JvmLanguage
|
||||
import com.intellij.codeInspection.tests.test.AssertEqualsBetweenInconvertibleTypesInspectionTestBase
|
||||
import com.intellij.jvm.analysis.JavaJvmAnalysisTestUtil
|
||||
|
||||
class JavaAssertEqualsBetweenInconvertibleTypesInspectionTest : AssertEqualsBetweenInconvertibleTypesInspectionTestBase() {
|
||||
fun `test JUnit 4 assertEquals`() {
|
||||
myFixture.testHighlighting(JvmLanguage.JAVA, """
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import org.junit.Test;
|
||||
|
||||
override fun getBasePath() = JavaJvmAnalysisTestUtil.TEST_DATA_PROJECT_RELATIVE_BASE_PATH + "/codeInspection/assert_equals_between_inconvertible_types"
|
||||
import java.util.*;
|
||||
|
||||
interface A { }
|
||||
|
||||
interface B extends A { }
|
||||
|
||||
fun `test AssertEqualsBetweenInconvertibleTypes`() {
|
||||
myFixture.testHighlighting("AssertEqualsBetweenInconvertibleTypes.java")
|
||||
class GenericClass<T> { }
|
||||
|
||||
public class AssertEqualsBetweenInconvertibleTypes {
|
||||
public static boolean areEqual(Object a, GenericClass<String> b) {
|
||||
return a.equals(b);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Double d = 1.0;
|
||||
assertEquals(1.0, d , 0.0); // fine.
|
||||
assertEquals(1.0, d , 0); // 'assertEquals()' between inconvertible types 'Double' and 'int'
|
||||
assertEquals(1, d , 0.0); // Doesn't complain even though perhaps it should.
|
||||
}
|
||||
|
||||
public void testFoo() {
|
||||
org.junit.Assert.<warning descr="'assertEquals()' between objects of inconvertible types 'String' and 'double'">assertEquals</warning>("java", 1.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCollection() {
|
||||
Collection<A> c1 = null;
|
||||
Collection<B> c2 = null;
|
||||
assertEquals(c1, c2);
|
||||
assertEquals(new ArrayList<String>(){}, new ArrayList<String>());
|
||||
assertEquals(new TreeSet<String>(){}, new HashSet<String>());
|
||||
<warning descr="'assertEquals()' between objects of inconvertible types 'TreeSet<Integer>' and 'HashSet<String>'">assertEquals</warning>(new TreeSet<Integer>(), new HashSet<String>());
|
||||
}
|
||||
}
|
||||
""".trimIndent(), fileName = "AssertEqualsBetweenInconvertibleTypes")
|
||||
}
|
||||
|
||||
fun `test AssertEqualsBetweenInconvertibleTypesAssertJ`() {
|
||||
myFixture.testHighlighting("AssertEqualsBetweenInconvertibleTypesAssertJ.java")
|
||||
fun `test JUnit 5 assertEquals`() {
|
||||
myFixture.testHighlighting(JvmLanguage.JAVA, """
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
<warning descr="'assertEquals()' between objects of inconvertible types 'int' and 'String'">assertEquals</warning>(1, "", "error message");
|
||||
<warning descr="'assertEquals()' between objects of inconvertible types 'int' and 'String'">assertEquals</warning>(1, "", () -> "error message in supplier");
|
||||
assertEquals(1, 42, "message");
|
||||
assertEquals(1, 42, () -> "message");
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun `test AssertEqualsBetweenInconvertibleTypesJUnit5`() {
|
||||
myFixture.testHighlighting("AssertEqualsBetweenInconvertibleTypesJUnit5.java")
|
||||
fun `test AssertJ assertEquals`() {
|
||||
myFixture.testHighlighting(JvmLanguage.JAVA, """
|
||||
import org.assertj.core.api.Assertions;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
Assertions.assertThat("foo").<warning descr="'isEqualTo()' between objects of inconvertible types 'int' and 'String'">isEqualTo</warning>(2);
|
||||
Assertions.assertThat("foo").isEqualTo("bar");
|
||||
Assertions.assertThat("foo").describedAs("foo").<warning descr="'isEqualTo()' between objects of inconvertible types 'int' and 'String'">isEqualTo</warning>(2);
|
||||
Assertions.assertThat("foo").as("foo").<warning descr="'isEqualTo()' between objects of inconvertible types 'int' and 'String'">isEqualTo</warning>(2);
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun `test AssertNotEqualsBetweenInconvertibleTypes`() {
|
||||
myFixture.testHighlighting("AssertNotEqualsBetweenInconvertibleTypes.java")
|
||||
fun `test JUnit 4 assertNotEquals`() {
|
||||
myFixture.testHighlighting(JvmLanguage.JAVA, """
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class AssertNotEqualsBetweenInconvertibleTypes {
|
||||
@Test
|
||||
public void test() {
|
||||
<weak_warning descr="Possibly redundant assertion: incompatible types are compared 'String' and 'int'">assertNotEquals</weak_warning>("java", 1);
|
||||
<weak_warning descr="Possibly redundant assertion: incompatible types are compared 'int[]' and 'double'">assertNotEquals</weak_warning>(new int[0], 1.0);
|
||||
assertNotEquals(new int[0], new int[1]); //ok
|
||||
}
|
||||
}
|
||||
""".trimIndent(), fileName = "AssertNotEqualsBetweenInconvertibleTypes")
|
||||
}
|
||||
|
||||
fun `test AssertNotEqualsBetweenInconvertibleTypesAssertJ`() {
|
||||
myFixture.testHighlighting("AssertNotEqualsBetweenInconvertibleTypesAssertJ.java")
|
||||
fun `test JUnit 5 assertNotEquals`() {
|
||||
myFixture.testHighlighting(JvmLanguage.JAVA, """
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
<weak_warning descr="Possibly redundant assertion: incompatible types are compared 'String' and 'int'">assertNotEquals</weak_warning>("java", 1, "message");
|
||||
<weak_warning descr="Possibly redundant assertion: incompatible types are compared 'int[]' and 'double'">assertNotEquals</weak_warning>(new int[0], 1.0, "message");
|
||||
assertNotEquals(new int[0], new int[1]); //ok
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun `test AssertNotEqualsBetweenInconvertibleTypesJUnit5`() {
|
||||
myFixture.testHighlighting("AssertEqualsBetweenInconvertibleTypesJUnit5.java")
|
||||
fun `test AssertJ assertNotEquals`() {
|
||||
myFixture.testHighlighting(JvmLanguage.JAVA, """
|
||||
import org.assertj.core.api.Assertions;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
Assertions.assertThat("java").as("test").<weak_warning descr="Possibly redundant assertion: incompatible types are compared 'int' and 'String'">isNotEqualTo</weak_warning>(1);
|
||||
Assertions.assertThat(new int[0]).describedAs("test").<weak_warning descr="Possibly redundant assertion: incompatible types are compared 'double' and 'int[]'">isNotEqualTo</weak_warning>(1.0);
|
||||
Assertions.assertThat(new int[0]).isNotEqualTo(new int[1]); //ok
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun `test AssertNotSameBetweenInconvertibleTypes`() {
|
||||
myFixture.testHighlighting("AssertNotEqualsBetweenInconvertibleTypes.java")
|
||||
fun `test JUnit 4 assertNotSame`() {
|
||||
myFixture.testHighlighting(JvmLanguage.JAVA, """
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class AssertNotSameBetweenInconvertibleTypes {
|
||||
@Test
|
||||
public void test() {
|
||||
<warning descr="Redundant assertion: incompatible types are compared 'String' and 'int'">assertNotSame</warning>("java", 1);
|
||||
<warning descr="Redundant assertion: incompatible types are compared 'int[]' and 'double'">assertNotSame</warning>(new int[0], 1.0);
|
||||
assertNotSame(new int[0], new int[1]); //ok
|
||||
}
|
||||
}
|
||||
""".trimIndent(), fileName = "AssertNotSameBetweenInconvertibleTypes")
|
||||
}
|
||||
|
||||
fun `test AssertNotSameBetweenInconvertibleTypesAssertJ`() {
|
||||
myFixture.testHighlighting("AssertNotEqualsBetweenInconvertibleTypesAssertJ.java")
|
||||
fun `test JUnit 5 assertNotSame`() {
|
||||
myFixture.testHighlighting(JvmLanguage.JAVA, """
|
||||
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
<warning descr="Redundant assertion: incompatible types are compared 'String' and 'int'">assertNotSame</warning>("java", 1, "message");
|
||||
<warning descr="Redundant assertion: incompatible types are compared 'int[]' and 'double'">assertNotSame</warning>(new int[0], 1.0, "message");
|
||||
assertNotSame(new int[0], new int[1]); //ok
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun `test AssertNotSameBetweenInconvertibleTypesJUnit5`() {
|
||||
myFixture.testHighlighting("AssertEqualsBetweenInconvertibleTypesJUnit5.java")
|
||||
fun `test AssertJ assertNotSame`() {
|
||||
myFixture.testHighlighting(JvmLanguage.JAVA, """
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
assertThat("java").as("test").<warning descr="Redundant assertion: incompatible types are compared 'int' and 'String'">isNotSameAs</warning>(1);
|
||||
assertThat(new int[0]).describedAs("test").<warning descr="Redundant assertion: incompatible types are compared 'double' and 'int[]'">isNotSameAs</warning>(1.0);
|
||||
assertThat(new int[0]).isNotSameAs(new int[1]); //ok
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun `test AssertSameBetweenInconvertibleTypes`() {
|
||||
myFixture.testHighlighting("AssertNotEqualsBetweenInconvertibleTypes.java")
|
||||
fun `test JUnit 4 assertSame`() {
|
||||
myFixture.testHighlighting(JvmLanguage.JAVA, """
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
<warning descr="'assertSame()' between objects of inconvertible types 'String' and 'int'">assertSame</warning>("foo", 2);
|
||||
<warning descr="'assertSame()' between objects of inconvertible types 'int[]' and 'int'">assertSame</warning>(new int[2], 2);
|
||||
assertSame(1, 2); // ok
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun `test AssertSameBetweenInconvertibleTypesAssertJ`() {
|
||||
myFixture.testHighlighting("AssertNotEqualsBetweenInconvertibleTypesAssertJ.java")
|
||||
fun `test JUnit 5 assertSame`() {
|
||||
myFixture.testHighlighting(JvmLanguage.JAVA, """
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
<warning descr="'assertSame()' between objects of inconvertible types 'int' and 'String'">assertSame</warning>(1, "", "Foo");
|
||||
<warning descr="'assertSame()' between objects of inconvertible types 'int' and 'int[]'">assertSame</warning>(1, new int[2], () -> "Foo in supplier");
|
||||
assertSame(1, 2, "message"); // ok
|
||||
assertSame(1, 2, () -> "message"); // ok
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun `test AssertSameBetweenInconvertibleTypesJUnit5`() {
|
||||
myFixture.testHighlighting("AssertNotEqualsBetweenInconvertibleTypesAssertJ.java")
|
||||
fun `test AssertJ assertSame`() {
|
||||
myFixture.testHighlighting(JvmLanguage.JAVA, """
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void myTest() {
|
||||
assertThat(1).<warning descr="'isSameAs()' between objects of inconvertible types 'String' and 'int'">isSameAs</warning>("foo");
|
||||
assertThat("foo").describedAs("foo").<warning descr="'isSameAs()' between objects of inconvertible types 'int' and 'String'">isSameAs</warning>(2);
|
||||
assertThat(new int[2]).as("array").<warning descr="'isSameAs()' between objects of inconvertible types 'int' and 'int[]'">isSameAs</warning>(2);
|
||||
assertThat(1).isSameAs(2); // ok
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
|
||||
fun `test Assertj extract to correct type`() {
|
||||
myFixture.testHighlighting(JvmLanguage.JAVA, """
|
||||
import org.assertj.core.api.Assertions;
|
||||
|
||||
class MyTest {
|
||||
@org.junit.jupiter.api.Test
|
||||
void testExtractingNoHighlight() {
|
||||
Assertions.assertThat(Integer.valueOf(1))
|
||||
.as("Mapping to String")
|
||||
.extracting(Object::toString)
|
||||
.isEqualTo("1");
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class AssertEqualsBetweenInconvertibleTypes {
|
||||
@Test
|
||||
fun myTest() {
|
||||
assertThat(1).<warning descr="'isSameAs()' between objects of inconvertible types 'String' and 'int'">isSameAs</warning>("foo")
|
||||
Assertions.assertThat("foo").<warning descr="'isEqualTo()' between objects of inconvertible types 'int' and 'String'">isEqualTo</warning>(2);
|
||||
Assertions.assertThat("foo").isEqualTo("bar"); //ok
|
||||
assertThat("foo").describedAs("foo").<warning descr="'isEqualTo()' between objects of inconvertible types 'int' and 'String'">isEqualTo</warning>(2);
|
||||
Assertions.assertThat("foo").<warning descr="'isEqualTo()' between objects of inconvertible types 'int' and 'String'">isEqualTo</warning>(2);
|
||||
Assertions.assertThat(1).<warning descr="'isSameAs()' between objects of inconvertible types 'String' and 'int'">isSameAs</warning>("foo")
|
||||
Assertions.assertThat("foo").describedAs("foo").<warning descr="'isSameAs()' between objects of inconvertible types 'int' and 'String'">isSameAs</warning>(2)
|
||||
assertThat(IntArray(2)).`as`("array").<warning descr="'isSameAs()' between objects of inconvertible types 'int' and 'int[]'">isSameAs</warning>(2)
|
||||
Assertions.assertThat("foo").`as`("foo").<warning descr="'isEqualTo()' between objects of inconvertible types 'int' and 'String'">isEqualTo</warning>(2);
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,29 @@
|
||||
package com.intellij.codeInspection.tests.kotlin.test
|
||||
|
||||
import com.intellij.codeInspection.tests.JvmLanguage
|
||||
import com.intellij.codeInspection.tests.test.AssertEqualsBetweenInconvertibleTypesInspectionTestBase
|
||||
import com.intellij.jvm.analysis.KotlinJvmAnalysisTestUtil
|
||||
import com.intellij.testFramework.TestDataPath
|
||||
|
||||
private const val inspectionPath = "/codeInspection/assertEqualsBetweenInconvertibleTypes"
|
||||
|
||||
@TestDataPath("\$CONTENT_ROOT/testData$inspectionPath")
|
||||
class KotlinAssertEqualsBetweenInconvertibleTypesTest : AssertEqualsBetweenInconvertibleTypesInspectionTestBase() {
|
||||
override fun getBasePath() = KotlinJvmAnalysisTestUtil.TEST_DATA_PROJECT_RELATIVE_BASE_PATH + inspectionPath
|
||||
fun `test AssertJ incompatible types`() {
|
||||
myFixture.testHighlighting(JvmLanguage.KOTLIN, """
|
||||
import org.assertj.core.api.Assertions
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
fun `test AssertEqualsBetweenInconvertibleTypes`() {
|
||||
myFixture.testHighlighting("AssertEqualsBetweenInconvertibleTypes.kt")
|
||||
class AssertEqualsBetweenInconvertibleTypes {
|
||||
@Test
|
||||
fun myTest() {
|
||||
assertThat(1).<warning descr="'isSameAs()' between objects of inconvertible types 'String' and 'int'">isSameAs</warning>("foo")
|
||||
Assertions.assertThat("foo").<warning descr="'isEqualTo()' between objects of inconvertible types 'int' and 'String'">isEqualTo</warning>(2);
|
||||
Assertions.assertThat("foo").isEqualTo("bar"); //ok
|
||||
assertThat("foo").describedAs("foo").<warning descr="'isEqualTo()' between objects of inconvertible types 'int' and 'String'">isEqualTo</warning>(2);
|
||||
Assertions.assertThat("foo").<warning descr="'isEqualTo()' between objects of inconvertible types 'int' and 'String'">isEqualTo</warning>(2);
|
||||
Assertions.assertThat(1).<warning descr="'isSameAs()' between objects of inconvertible types 'String' and 'int'">isSameAs</warning>("foo")
|
||||
Assertions.assertThat("foo").describedAs("foo").<warning descr="'isSameAs()' between objects of inconvertible types 'int' and 'String'">isSameAs</warning>(2)
|
||||
assertThat(IntArray(2)).`as`("array").<warning descr="'isSameAs()' between objects of inconvertible types 'int' and 'int[]'">isSameAs</warning>(2)
|
||||
Assertions.assertThat("foo").`as`("foo").<warning descr="'isEqualTo()' between objects of inconvertible types 'int' and 'String'">isEqualTo</warning>(2);
|
||||
}
|
||||
}
|
||||
""".trimIndent())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user