/
RequiresTypeResolutionSpec.kt
115 lines (97 loc) · 3.81 KB
/
RequiresTypeResolutionSpec.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package io.gitlab.arturbosch.detekt.authors
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.junit.jupiter.api.Test
@KotlinCoreEnvironmentTest
internal class RequiresTypeResolutionSpec(private val env: KotlinCoreEnvironment) {
private val rule = RequiresTypeResolution()
@Test
fun `should not report classes that doesn't extend from BaseRule`() {
val code = """
class A {
val issue: Int = error("bindingContext")
}
"""
val findings = rule.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@Test
fun `should report Rules that use bindingContext and are not annotated`() {
val code = """
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Rule
class A(config: Config) : Rule(config) {
override val issue = error("I don't care")
private fun asdf() {
bindingContext
}
}
"""
val findings = rule.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
}
@Test
fun `should not report Rules that doesn't use bindingContext and are not annotated`() {
val code = """
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Rule
class A(config: Config) : Rule(config) {
override val issue = error("I don't care")
}
"""
val findings = rule.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@Test
fun `should not report Rules that use bindingContext and are annotated`() {
val code = """
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
@RequiresTypeResolution
class A(config: Config) : Rule(config) {
override val issue = error("I don't care")
private fun asdf() {
bindingContext
}
}
"""
val findings = rule.compileAndLintWithContext(env, code)
assertThat(findings).isEmpty()
}
@Test
fun `should not report Rules that doesn't use bindingContext and are annotated`() {
val code = """
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.internal.RequiresTypeResolution
@RequiresTypeResolution
class A(config: Config) : Rule(config) {
override val issue = error("I don't care")
}
"""
val findings = rule.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
}
@Test
fun `should report Rules that use bindingContext outside class and are not annotated`() {
val code = """
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Rule
class A(config: Config) : Rule(config) {
override val issue = error("I don't care")
private fun asdf() {
extension()
}
}
private inline fun Rule.extension(): Boolean {
bindingContext
return true
}
"""
val findings = rule.compileAndLintWithContext(env, code)
assertThat(findings).hasSize(1)
}
}