-
-
Notifications
You must be signed in to change notification settings - Fork 757
/
UnreachableCatchBlockSpec.kt
77 lines (71 loc) · 2.48 KB
/
UnreachableCatchBlockSpec.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package io.gitlab.arturbosch.detekt.rules.bugs
import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.junit.jupiter.api.Test
@KotlinCoreEnvironmentTest
class UnreachableCatchBlockSpec(private val env: KotlinCoreEnvironment) {
private val subject = UnreachableCatchBlock()
@Test
fun `reports a unreachable catch block that is after the super class catch block`() {
val code = """
fun test() {
try {
} catch (t: Throwable) {
} catch (e: Exception) {
}
}
"""
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(4, 7)
}
@Test
fun `reports a unreachable catch block that is after the same class catch block`() {
val code = """
fun test() {
try {
} catch (e: Exception) {
} catch (e: Exception) {
}
}
"""
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(4, 7)
}
@Test
fun `reports two unreachable catch blocks that is after the super class catch block`() {
val code = """
fun test() {
try {
} catch (e: RuntimeException) {
} catch (e: IllegalArgumentException) {
} catch (e: IllegalStateException) {
}
}
"""
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(2)
assertThat(findings).hasStartSourceLocations(
SourceLocation(4, 7),
SourceLocation(5, 7)
)
}
@Test
fun `does not report unreachable catch block`() {
val code = """
fun test() {
try {
} catch (e: IllegalArgumentException) {
} catch (e: IllegalStateException) {
} catch (e: RuntimeException) {
}
}
"""
val findings = subject.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
}