Add "Replace assertBoolean() with assertEquals() inspection"
#KT-23445 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
e92d3998de
commit
443b1834bc
@@ -0,0 +1,6 @@
|
||||
<html>
|
||||
<body>
|
||||
This inspection reports assert boolean function calls replaceable with assert equality function.
|
||||
Example: <b>assertTrue(a == b)</b> can be replaced by <b>assertEquals(a, b)</b>.
|
||||
</body>
|
||||
</html>
|
||||
@@ -2997,6 +2997,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplaceAssertBooleanWithAssertEqualityInspection"
|
||||
displayName="Replace assert boolean with assert equality"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2996,6 +2996,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplaceAssertBooleanWithAssertEqualityInspection"
|
||||
displayName="Replace assert boolean with assert equality"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2996,6 +2996,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplaceAssertBooleanWithAssertEqualityInspection"
|
||||
displayName="Replace assert boolean with assert equality"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2997,6 +2997,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplaceAssertBooleanWithAssertEqualityInspection"
|
||||
displayName="Replace assert boolean with assert equality"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2996,6 +2996,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplaceAssertBooleanWithAssertEqualityInspection"
|
||||
displayName="Replace assert boolean with assert equality"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2996,6 +2996,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplaceAssertBooleanWithAssertEqualityInspection"
|
||||
displayName="Replace assert boolean with assert equality"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
@@ -2998,6 +2998,15 @@ The Kotlin plugin provides language support in IntelliJ IDEA and Android Studio.
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.ReplaceAssertBooleanWithAssertEqualityInspection"
|
||||
displayName="Replace assert boolean with assert equality"
|
||||
groupPath="Kotlin"
|
||||
groupName="Style issues"
|
||||
enabledByDefault="true"
|
||||
level="WEAK WARNING"
|
||||
language="kotlin"
|
||||
/>
|
||||
|
||||
<referenceImporter implementation="org.jetbrains.kotlin.idea.quickfix.KotlinReferenceImporter"/>
|
||||
|
||||
<fileType.fileViewProviderFactory filetype="KJSM" implementationClass="com.intellij.psi.ClassFileViewProviderFactory"/>
|
||||
|
||||
+75
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.idea.inspections
|
||||
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.idea.intentions.getCallableDescriptor
|
||||
import org.jetbrains.kotlin.lexer.KtTokens
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
|
||||
|
||||
class ReplaceAssertBooleanWithAssertEqualityInspection : AbstractApplicabilityBasedInspection<KtCallExpression>(KtCallExpression::class.java) {
|
||||
|
||||
override fun inspectionText(element: KtCallExpression) = "Replace assert boolean with assert equality"
|
||||
|
||||
override val defaultFixText = "Replace assert boolean with assert equality"
|
||||
|
||||
override fun fixText(element: KtCallExpression): String {
|
||||
val assertion = element.replaceableAssertion() ?: return defaultFixText
|
||||
return "Replace with '$assertion'"
|
||||
}
|
||||
|
||||
override fun isApplicable(element: KtCallExpression): Boolean {
|
||||
return (element.replaceableAssertion() != null)
|
||||
}
|
||||
|
||||
override fun applyTo(element: PsiElement, project: Project, editor: Editor?) {
|
||||
val expression = element as? KtCallExpression ?: return
|
||||
val condition = expression.valueArguments.first().getArgumentExpression() as? KtBinaryExpression ?: return
|
||||
val left = condition.left ?: return
|
||||
val right = condition.right ?: return
|
||||
val assertion = expression.replaceableAssertion() ?: return
|
||||
val factory = KtPsiFactory(project)
|
||||
|
||||
if (expression.valueArguments.size == 1) {
|
||||
expression.replace(factory.createExpressionByPattern("$assertion($0, $1)", left, right))
|
||||
} else if (expression.valueArguments.size == 2) {
|
||||
val message = expression.valueArguments[1].getArgumentExpression() ?: return
|
||||
expression.replace(factory.createExpressionByPattern("$assertion($0, $1, $2)", left, right, message))
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtCallExpression.replaceableAssertion(): String? {
|
||||
val referencedName = (calleeExpression as? KtNameReferenceExpression)?.getReferencedName() ?: return null
|
||||
if (referencedName !in assertions) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (getCallableDescriptor()?.containingDeclaration?.fqNameSafe != FqName("kotlin.test")) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (valueArguments.size != 1 && valueArguments.size != 2) return null
|
||||
val binaryExpression = valueArguments.first().getArgumentExpression() as? KtBinaryExpression ?: return null
|
||||
val operationToken = binaryExpression.operationToken
|
||||
|
||||
return assertionMap[Pair(referencedName, operationToken)]
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val assertions = setOf("assertTrue", "assertFalse")
|
||||
|
||||
private val assertionMap = mapOf(
|
||||
Pair("assertTrue", KtTokens.EQEQ) to "assertEquals",
|
||||
Pair("assertTrue", KtTokens.EQEQEQ) to "assertSame",
|
||||
Pair("assertFalse", KtTokens.EQEQ) to "assertNotEquals",
|
||||
Pair("assertFalse", KtTokens.EQEQEQ) to "assertNotSame"
|
||||
)
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
org.jetbrains.kotlin.idea.inspections.ReplaceAssertBooleanWithAssertEqualityInspection
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// RUNTIME_WITH_KOTLIN_TEST
|
||||
// PROBLEM: none
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
val isA = false
|
||||
assertFalse<caret>(isA)
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// RUNTIME_WITH_KOTLIN_TEST
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
val a = "a"
|
||||
val b = "b"
|
||||
assertFalse<caret>(a == b)
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// RUNTIME_WITH_KOTLIN_TEST
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
val a = "a"
|
||||
val b = "b"
|
||||
assertNotEquals(a, b)
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// RUNTIME_WITH_KOTLIN_TEST
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
val a = "a"
|
||||
val b = "b"
|
||||
assertFalse<caret>(a === b)
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// RUNTIME_WITH_KOTLIN_TEST
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
val a = "a"
|
||||
val b = "b"
|
||||
assertNotSame(a, b)
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// RUNTIME_WITH_KOTLIN_TEST
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
val a = "a"
|
||||
val b = "b"
|
||||
assertFalse(<caret>a == b, "message")
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// RUNTIME_WITH_KOTLIN_TEST
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
val a = "a"
|
||||
val b = "b"
|
||||
assertNotEquals(a, b, "message")
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// RUNTIME_WITH_KOTLIN_TEST
|
||||
// PROBLEM: none
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
val isA = true
|
||||
assertTrue<caret>(isA)
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// RUNTIME_WITH_KOTLIN_TEST
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
val a = "a"
|
||||
val b = "a"
|
||||
assertTrue<caret>(a == b)
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// RUNTIME_WITH_KOTLIN_TEST
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
val a = "a"
|
||||
val b = "a"
|
||||
assertEquals(a, b)
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
// RUNTIME_WITH_KOTLIN_TEST
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
val a = "a"
|
||||
val b = "a"
|
||||
assertTrue(<caret>a === b)
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// RUNTIME_WITH_KOTLIN_TEST
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
val a = "a"
|
||||
val b = "a"
|
||||
assertSame(a, b)
|
||||
}
|
||||
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// RUNTIME_WITH_KOTLIN_TEST
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
val a = "a"
|
||||
val b = "a"
|
||||
assertTrue(<caret>a == b, "message")
|
||||
}
|
||||
idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertTrueWithMessage.kt.after
Vendored
+9
@@ -0,0 +1,9 @@
|
||||
// RUNTIME_WITH_KOTLIN_TEST
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
fun foo() {
|
||||
val a = "a"
|
||||
val b = "a"
|
||||
assertEquals(a, b, "message")
|
||||
}
|
||||
+53
@@ -4649,6 +4649,59 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class ReplaceAssertBooleanWithAssertEquality extends AbstractLocalInspectionTest {
|
||||
private void runTest(String testDataFilePath) throws Exception {
|
||||
KotlinTestUtils.runTest(this::doTest, TargetBackend.ANY, testDataFilePath);
|
||||
}
|
||||
|
||||
public void testAllFilesPresentInReplaceAssertBooleanWithAssertEquality() throws Exception {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("assertFalse.kt")
|
||||
public void testAssertFalse() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertFalse.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("assertFalseEQEQ.kt")
|
||||
public void testAssertFalseEQEQ() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertFalseEQEQ.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("assertFalseEQEQEQ.kt")
|
||||
public void testAssertFalseEQEQEQ() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertFalseEQEQEQ.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("assertFalseWithMessage.kt")
|
||||
public void testAssertFalseWithMessage() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertFalseWithMessage.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("assertTrue.kt")
|
||||
public void testAssertTrue() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertTrue.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("assertTrueEQEQ.kt")
|
||||
public void testAssertTrueEQEQ() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertTrueEQEQ.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("assertTrueEQEQEQ.kt")
|
||||
public void testAssertTrueEQEQEQ() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertTrueEQEQEQ.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("assertTrueWithMessage.kt")
|
||||
public void testAssertTrueWithMessage() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertTrueWithMessage.kt");
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("idea/testData/inspectionsLocal/replacePutWithAssignment")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user