/
NestedScopeFunctionsSpec.kt
113 lines (100 loc) · 2.96 KB
/
NestedScopeFunctionsSpec.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
package io.gitlab.arturbosch.detekt.rules.complexity
import io.gitlab.arturbosch.detekt.api.Finding
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
import io.gitlab.arturbosch.detekt.test.TestConfig
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 NestedScopeFunctionsSpec(private val env: KotlinCoreEnvironment) {
private val defaultConfig = TestConfig(
mapOf(
"threshold" to 1,
"functions" to listOf("kotlin.run", "kotlin.with")
)
)
private val subject = NestedScopeFunctions(defaultConfig)
private lateinit var givenCode: String
private lateinit var actual: List<Finding>
@Test
fun `should report nesting`() {
givenCode = """
fun f() {
1.run {
1.run { }
}
}
"""
whenLintRuns()
expectSourceLocation(3 to 11)
expectFunctionInMsg("run")
}
@Test
fun `should report mixed nesting`() {
givenCode = """
fun f() {
1.run {
with(1) { }
}
}
"""
whenLintRuns()
expectSourceLocation(3 to 9)
expectFunctionInMsg("with")
}
@Test
fun `should report when valid scope in between`() {
givenCode = """
fun f() {
1.run {
"valid".apply {
with(1) { }
}
}
}
"""
whenLintRuns()
expectSourceLocation(4 to 13)
}
@Test
fun `should not report in nested function`() {
givenCode = """
fun f() {
1.run { }
fun f2() {
with(1) { }
}
}
"""
whenLintRuns()
expectNoFindings()
}
@Test
fun `should not report in neighboring scope functions`() {
givenCode = """
fun f() {
1.run { }
1.run { }
with(1) {}
with(1) {}
}
"""
whenLintRuns()
expectNoFindings()
}
private fun whenLintRuns() {
actual = subject.compileAndLintWithContext(env, givenCode)
}
private fun expectSourceLocation(location: Pair<Int, Int>) {
assertThat(actual).hasStartSourceLocation(location.first, location.second)
}
private fun expectFunctionInMsg(scopeFunction: String) {
val expected = "The scope function '$scopeFunction' is nested too deeply ('2'). " +
"Complexity threshold is set to '1'."
assertThat(actual[0]).hasMessage(expected)
}
private fun expectNoFindings() {
assertThat(actual).describedAs("findings size").isEmpty()
}
}