-
-
Notifications
You must be signed in to change notification settings - Fork 755
/
NestedScopeFunctionsSpec.kt
131 lines (116 loc) · 3.39 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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.compileAndLint
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()
}
@Test
fun `should not report when binding context is empty`() {
givenCode = """
fun f() {
1.run {
1.run { }
}
}
"""
whenLintRunsWithoutContext()
expectNoFindings()
}
private fun whenLintRuns() {
actual = subject.compileAndLintWithContext(env, givenCode)
}
private fun whenLintRunsWithoutContext() {
actual = subject.compileAndLint(givenCode)
}
private fun expectSourceLocation(location: Pair<Int, Int>) {
assertThat(actual).hasSourceLocation(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()
}
}