Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix false negative for UseRequire when thrown in conditional block (#…
…5147) * Fix false negative for UseRequire when thrown in conditional block * do not change api * use assertj instead of junit * Update detekt-psi-utils/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/ThrowExtensionsSpec.kt Co-authored-by: Brais Gabín <braisgabin@gmail.com> Co-authored-by: Markus Schwarz <post@markus-schwarz.net> Co-authored-by: Brais Gabín <braisgabin@gmail.com>
- Loading branch information
1 parent
e4bb31b
commit 01063dc
Showing
5 changed files
with
181 additions
and
12 deletions.
There are no files selected for viewing
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
130 changes: 130 additions & 0 deletions
130
detekt-psi-utils/src/test/kotlin/io/gitlab/arturbosch/detekt/rules/ThrowExtensionsSpec.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,130 @@ | ||
package io.gitlab.arturbosch.detekt.rules | ||
|
||
import io.github.detekt.test.utils.compileContentForTest | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.intellij.lang.annotations.Language | ||
import org.jetbrains.kotlin.psi.KtThrowExpression | ||
import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
|
||
internal class ThrowExtensionsSpec { | ||
|
||
@Nested | ||
inner class `is enclosed by conditional statement` { | ||
@Test | ||
fun `is true for if statement on same line`() { | ||
val code = """ | ||
fun test() { | ||
if (i == 1) throw IllegalArgumentException() | ||
} | ||
""" | ||
|
||
verifyThrowExpression(code) { | ||
assertThat(isEnclosedByConditionalStatement()).isTrue() | ||
} | ||
} | ||
|
||
@Test | ||
fun `is true for if statement on separate line`() { | ||
val code = """ | ||
fun test() { | ||
if (i == 1) | ||
throw IllegalArgumentException() | ||
} | ||
""" | ||
|
||
verifyThrowExpression(code) { | ||
assertThat(isEnclosedByConditionalStatement()).isTrue() | ||
} | ||
} | ||
|
||
@Test | ||
fun `is true for if statement on in block`() { | ||
val code = """ | ||
fun test() { | ||
if (i == 1) { | ||
println("message") | ||
throw IllegalArgumentException() | ||
} | ||
} | ||
""" | ||
|
||
verifyThrowExpression(code) { | ||
assertThat(isEnclosedByConditionalStatement()).isTrue() | ||
} | ||
} | ||
|
||
@Test | ||
fun `is false if thrown unconditionally`() { | ||
val code = """ | ||
fun test() { | ||
throw IllegalArgumentException() | ||
} | ||
""" | ||
|
||
verifyThrowExpression(code) { | ||
assertThat(isEnclosedByConditionalStatement()).isFalse() | ||
} | ||
} | ||
|
||
@Test | ||
fun `is false for when statement`() { | ||
val code = """ | ||
fun test(a: Int) { | ||
when (a) { | ||
1 -> throw IllegalArgumentException() | ||
2 -> println("2") | ||
else -> println("other") | ||
} | ||
} | ||
""" | ||
|
||
verifyThrowExpression(code) { | ||
assertThat(isEnclosedByConditionalStatement()).isFalse() | ||
} | ||
} | ||
|
||
@Test | ||
fun `is true in else clause`() { | ||
val code = """ | ||
fun test(a: Int) { | ||
if (a == 2) println("2") else throw IllegalArgumentException() | ||
} | ||
""" | ||
|
||
verifyThrowExpression(code) { | ||
assertThat(isEnclosedByConditionalStatement()).isTrue() | ||
} | ||
} | ||
|
||
@Test | ||
fun `is true in else clause with curly braces`() { | ||
val code = """ | ||
fun test(a: Int) { | ||
if (a == 2) { | ||
println("2") | ||
} else { | ||
throw IllegalArgumentException() | ||
} | ||
} | ||
""" | ||
|
||
verifyThrowExpression(code) { | ||
assertThat(isEnclosedByConditionalStatement()).isTrue() | ||
} | ||
} | ||
} | ||
|
||
private fun verifyThrowExpression( | ||
@Language("kotlin") code: String, | ||
throwingAssertions: KtThrowExpression.() -> Unit | ||
) { | ||
val ktFile = compileContentForTest(code) | ||
val ktThrowExpression = ktFile.findDescendantOfType<KtThrowExpression>() | ||
assertThat(ktThrowExpression) | ||
.withFailMessage("no throw expression found") | ||
.isNotNull | ||
throwingAssertions(ktThrowExpression!!) | ||
} | ||
} |
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