[coverage] IDEA-340739 Fix Kotlin elvis operator for non-primitive classes

GitOrigin-RevId: b05076c9aa28c00526b1c503af53725679b711ab
This commit is contained in:
Maksim Zuev
2024-01-19 13:39:40 +01:00
committed by intellij-monorepo-bot
parent b6af3d1b29
commit 4542d99f76
13 changed files with 56 additions and 33 deletions

View File

@@ -1,7 +1,7 @@
Line 1 coverage: FULL
Hits: 2
Line 2 coverage: FULL
Hits: 4
Hits: 8
Line 3 coverage: FULL
Hits: 2
Line 5 coverage: PARTIAL
@@ -14,33 +14,43 @@ Hits: 2
a != null
true hits: 0
false hits: 2
Line 8 coverage: PARTIAL
Line 7 coverage: PARTIAL
Hits: 2
a != null
true hits: 0
false hits: 2
a ?: b != null
true hits: 2
false hits: 0
Line 9 coverage: PARTIAL
Hits: 2
a != null
true hits: 0
false hits: 2
a ?: b != null
true hits: 2
false hits: 0
Line 10 coverage: PARTIAL
Hits: 2
a != null
true hits: 0
false hits: 2
Line 11 coverage: PARTIAL
a ?: b != null
true hits: 0
false hits: 2
Line 12 coverage: PARTIAL
Hits: 2
a != null
true hits: 2
false hits: 0
Line 12 coverage: PARTIAL
Line 13 coverage: PARTIAL
Hits: 2
a != null
true hits: 0
false hits: 2
Line 14 coverage: PARTIAL
Hits: 2
a != null
true hits: 2
false hits: 0
Line 16 coverage: PARTIAL
Hits: 2
a != null
true hits: 2
@@ -48,7 +58,7 @@ Hits: 2
a?.f() != null
true hits: 2
false hits: 0
Line 15 coverage: PARTIAL
Line 17 coverage: PARTIAL
Hits: 2
a != null
true hits: 2

View File

@@ -10,21 +10,27 @@ Covered 1/2 branches
Line 6 coverage: PARTIAL
Hits: 1
Covered 1/2 branches
Line 8 coverage: PARTIAL
Line 7 coverage: PARTIAL
Hits: 1
Covered 2/4 branches
Covered 1/2 branches
Line 9 coverage: PARTIAL
Hits: 1
Covered 2/4 branches
Line 11 coverage: PARTIAL
Line 10 coverage: PARTIAL
Hits: 1
Covered 2/4 branches
Line 12 coverage: PARTIAL
Hits: 1
Covered 1/2 branches
Line 12 coverage: PARTIAL
Line 13 coverage: PARTIAL
Hits: 1
Covered 1/2 branches
Line 14 coverage: PARTIAL
Hits: 1
Covered 2/4 branches
Line 15 coverage: PARTIAL
Covered 1/2 branches
Line 16 coverage: PARTIAL
Hits: 1
Covered 2/4 branches
Line 17 coverage: PARTIAL
Hits: 1
Covered 2/4 branches

View File

@@ -90,21 +90,21 @@ class KtLineBreaks {
private fun <T> T?.g(): T? = this
private fun <T> h(x: T?): T? = x
fun testNullCheckVariables(a: Int?, b: Int?) = a
fun testNullCheckVariables(a: String?, b: String?) = a
?: b
?: 42
fun testNullCheckMethods(a: Int?, b: Int?) = h(a)
fun testNullCheckMethods(a: String?, b: String?) = h(a)
?: h(b)
?: 42
fun testSafeCallSequence(a: Int?) = a
fun testSafeCallSequence(a: String?) = a
?.g()
?.g()
private fun <T> T?.veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethodName(): T? = this
fun testSafeCallSequenceLongNames(a: Int?) = a
fun testSafeCallSequenceLongNames(a: String?) = a
?.veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethodName()
?.veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethodName()
?.veryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongMethodName()

View File

@@ -2,15 +2,17 @@ class KtNullability {
private fun <T> T?.f(): T? = this
private fun <T> T?.g(): T? = null
fun testNullCheckOperator1(a: Int?) = a ?: 42
fun testNullCheckOperator2(a: Int?) = a ?: 42
fun testNullCheckOperator1(a: String?): String = a ?: "42"
fun testNullCheckOperator2(a: String?): String = a ?: "42"
fun testPrimitiveNullCheckOperator(a: Int?) = a ?: 42
fun testNullCheckSequence1(a: Int?, b: Int?) = a ?: b ?: 42
fun testNullCheckSequence2(a: Int?, b: Int?) = a ?: b ?: 42
fun testNullCheckSequence1(a: String?, b: String?) = a ?: b ?: "42"
fun testNullCheckSequence2(a: String?, b: String?) = a ?: b ?: "42"
fun testSafeCall1(a: Int?) = a?.toString()
fun testSafeCall2(a: Int?) = a?.toString()
fun testSafeCall1(a: String?) = a?.f()
fun testSafeCall2(a: String?) = a?.f()
fun testPrimitiveSafeCall1(a: Int?) = a?.f()
fun testSafeCallSequence1(a: Int?) = a?.f()?.f()
fun testSafeCallSequence2(a: Int?) = a?.g()?.f()
fun testSafeCallSequence1(a: String?) = a?.f()?.f()
fun testSafeCallSequence2(a: String?) = a?.g()?.f()
}

View File

@@ -2,14 +2,16 @@ import junit.framework.TestCase
class KtNullabilityTest : TestCase() {
fun testKotlinNullability(): Unit = KtNullability().run {
testNullCheckOperator1(1)
testNullCheckOperator1("1")
testNullCheckOperator2(null)
testNullCheckSequence1(null, 1)
testPrimitiveNullCheckOperator(1)
testNullCheckSequence1(null, "1")
testNullCheckSequence2(null, null)
testSafeCall1(1)
testSafeCall1("1")
testSafeCall2(null)
testSafeCallSequence1(1)
testSafeCallSequence2(1)
testPrimitiveSafeCall1(1)
testSafeCallSequence1("1")
testSafeCallSequence2("1")
}
}

View File

@@ -33,7 +33,7 @@ public class LineBreaksTest extends TestCase {
l.testNullCheckVariables(null, null);
l.testNullCheckMethods(null, null);
l.testSafeCallSequence(1);
l.testSafeCallSequenceLongNames(1);
l.testSafeCallSequence("1");
l.testSafeCallSequenceLongNames("1");
}
}

View File

@@ -131,6 +131,9 @@ private fun KtExpression.breakIntoConditions(offset: Int, singleElement: Boolean
}
private fun KtExpression.isReversedCondition(): Boolean {
// This works only for non-primitive classes
// For primitive values the condition should not be inverted
if (this is KtBinaryExpression && operationToken == KtTokens.ELVIS) return true
val insideDoWhile = parentOfType<KtDoWhileExpression>()?.condition?.let { it in parents(true) } == true
return insideDoWhile || isLeftInOrExpression()
}