Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ReplaceAssertBooleanWithAssertEqualityInspection
- Loading branch information
Showing
24 changed files
with
324 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
idea/resources/inspectionDescriptions/ReplaceAssertBooleanWithAssertEquality.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...org/jetbrains/kotlin/idea/inspections/ReplaceAssertBooleanWithAssertEqualityInspection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* 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? { | ||
var result: String? = null | ||
val referencedName = (calleeExpression as? KtNameReferenceExpression)?.getReferencedName() ?: return null | ||
if (!assertions.contains(referencedName)) { | ||
return result | ||
} | ||
|
||
if (getCallableDescriptor()?.containingDeclaration?.fqNameSafe != FqName("kotlin.test")) { | ||
return result | ||
} | ||
|
||
if (valueArguments.size != 1 && valueArguments.size != 2) return null | ||
val binaryExpression = valueArguments.first().getArgumentExpression() as? KtBinaryExpression ?: return null | ||
val operationToken = binaryExpression.operationToken | ||
|
||
assertionMap.entries.forEach { | ||
val (assertion, token) = it.key | ||
if (referencedName == assertion && operationToken == token) { | ||
result = it.value | ||
return@forEach | ||
} | ||
} | ||
|
||
return result | ||
} | ||
|
||
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 change: 1 addition & 0 deletions
1
idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/.inspection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
org.jetbrains.kotlin.idea.inspections.ReplaceAssertBooleanWithAssertEqualityInspection |
9 changes: 9 additions & 0 deletions
9
idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertFalse.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUNTIME_WITH_KOTLIN_TEST | ||
// PROBLEM: none | ||
|
||
import kotlin.test.* | ||
|
||
fun foo() { | ||
val isA = false | ||
assertFalse<caret>(isA) | ||
} |
9 changes: 9 additions & 0 deletions
9
idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertFalseEQEQ.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUNTIME_WITH_KOTLIN_TEST | ||
|
||
import kotlin.test.* | ||
|
||
fun foo() { | ||
val a = "a" | ||
val b = "b" | ||
assertFalse<caret>(a == b) | ||
} |
9 changes: 9 additions & 0 deletions
9
...testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertFalseEQEQ.kt.after
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUNTIME_WITH_KOTLIN_TEST | ||
|
||
import kotlin.test.* | ||
|
||
fun foo() { | ||
val a = "a" | ||
val b = "b" | ||
assertNotEquals(a, b) | ||
} |
9 changes: 9 additions & 0 deletions
9
idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertFalseEQEQEQ.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUNTIME_WITH_KOTLIN_TEST | ||
|
||
import kotlin.test.* | ||
|
||
fun foo() { | ||
val a = "a" | ||
val b = "b" | ||
assertFalse<caret>(a === b) | ||
} |
9 changes: 9 additions & 0 deletions
9
...stData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertFalseEQEQEQ.kt.after
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUNTIME_WITH_KOTLIN_TEST | ||
|
||
import kotlin.test.* | ||
|
||
fun foo() { | ||
val a = "a" | ||
val b = "b" | ||
assertNotSame(a, b) | ||
} |
9 changes: 9 additions & 0 deletions
9
...estData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertFalseWithMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 changes: 9 additions & 0 deletions
9
...a/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertFalseWithMessage.kt.after
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUNTIME_WITH_KOTLIN_TEST | ||
|
||
import kotlin.test.* | ||
|
||
fun foo() { | ||
val a = "a" | ||
val b = "b" | ||
assertNotEquals(a, b, "message") | ||
} |
9 changes: 9 additions & 0 deletions
9
idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertTrue.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUNTIME_WITH_KOTLIN_TEST | ||
// PROBLEM: none | ||
|
||
import kotlin.test.* | ||
|
||
fun foo() { | ||
val isA = true | ||
assertTrue<caret>(isA) | ||
} |
9 changes: 9 additions & 0 deletions
9
idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertTrueEQEQ.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUNTIME_WITH_KOTLIN_TEST | ||
|
||
import kotlin.test.* | ||
|
||
fun foo() { | ||
val a = "a" | ||
val b = "a" | ||
assertTrue<caret>(a == b) | ||
} |
9 changes: 9 additions & 0 deletions
9
.../testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertTrueEQEQ.kt.after
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUNTIME_WITH_KOTLIN_TEST | ||
|
||
import kotlin.test.* | ||
|
||
fun foo() { | ||
val a = "a" | ||
val b = "a" | ||
assertEquals(a, b) | ||
} |
9 changes: 9 additions & 0 deletions
9
idea/testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertTrueEQEQEQ.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUNTIME_WITH_KOTLIN_TEST | ||
|
||
import kotlin.test.* | ||
|
||
fun foo() { | ||
val a = "a" | ||
val b = "a" | ||
assertTrue(<caret>a === b) | ||
} |
9 changes: 9 additions & 0 deletions
9
...estData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertTrueEQEQEQ.kt.after
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUNTIME_WITH_KOTLIN_TEST | ||
|
||
import kotlin.test.* | ||
|
||
fun foo() { | ||
val a = "a" | ||
val b = "a" | ||
assertSame(a, b) | ||
} |
9 changes: 9 additions & 0 deletions
9
...testData/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertTrueWithMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUNTIME_WITH_KOTLIN_TEST | ||
|
||
import kotlin.test.* | ||
|
||
fun foo() { | ||
val a = "a" | ||
val b = "a" | ||
assertTrue(<caret>a == b, "message") | ||
} |
9 changes: 9 additions & 0 deletions
9
...ta/inspectionsLocal/replaceAssertBooleanWithAssertEquality/assertTrueWithMessage.kt.after
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// RUNTIME_WITH_KOTLIN_TEST | ||
|
||
import kotlin.test.* | ||
|
||
fun foo() { | ||
val a = "a" | ||
val b = "a" | ||
assertEquals(a, b, "message") | ||
} |
Oops, something went wrong.