forked from detekt/detekt
/
EmptyCodeSpec.kt
163 lines (139 loc) · 4.03 KB
/
EmptyCodeSpec.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package io.gitlab.arturbosch.detekt.rules.empty
import io.github.detekt.test.utils.compileForTest
import io.github.detekt.test.utils.resourceAsPath
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatExceptionOfType
import org.junit.jupiter.api.Test
import java.util.regex.PatternSyntaxException
private const val ALLOWED_EXCEPTION_NAME_REGEX = "allowedExceptionNameRegex"
class EmptyCodeSpec {
val regexTestingCode = """
fun f() {
try {
} catch (foo: Exception) {
}
}
"""
@Test
fun `findsEmptyCatch`() {
test { EmptyCatchBlock(Config.empty) }
}
@Test
fun `findsEmptyNestedCatch`() {
val code = """
fun f() {
try {
} catch (ignore: Exception) {
try {
} catch (e: Exception) {
}
}
}
"""
assertThat(EmptyCatchBlock(Config.empty).compileAndLint(code)).hasSize(1)
}
@Test
fun `doesNotReportIgnoredOrExpectedException`() {
val code = """
fun f() {
try {
} catch (ignore: IllegalArgumentException) {
} catch (expected: Exception) {
}
}
"""
assertThat(EmptyCatchBlock(Config.empty).compileAndLint(code)).isEmpty()
}
@Test
fun `doesNotReportEmptyCatchWithConfig`() {
val code = """
fun f() {
try {
} catch (foo: Exception) {
}
}
"""
val config = TestConfig(mapOf(ALLOWED_EXCEPTION_NAME_REGEX to "foo"))
assertThat(EmptyCatchBlock(config).compileAndLint(code)).isEmpty()
}
@Test
fun `findsEmptyFinally`() {
test { EmptyFinallyBlock(Config.empty) }
}
@Test
fun `findsEmptyIf`() {
test { EmptyIfBlock(Config.empty) }
}
@Test
fun `findsEmptyElse`() {
test { EmptyElseBlock(Config.empty) }
}
@Test
fun `findsEmptyFor`() {
test { EmptyForBlock(Config.empty) }
}
@Test
fun `findsEmptyWhile`() {
test { EmptyWhileBlock(Config.empty) }
}
@Test
fun `findsEmptyDoWhile`() {
test { EmptyDoWhileBlock(Config.empty) }
}
@Test
fun `findsEmptyFun`() {
test { EmptyFunctionBlock(Config.empty) }
}
@Test
fun `findsEmptyClass`() {
test { EmptyClassBlock(Config.empty) }
}
@Test
fun `findsEmptyTry`() {
test { EmptyTryBlock(Config.empty) }
}
@Test
fun `findsEmptyWhen`() {
test { EmptyWhenBlock(Config.empty) }
}
@Test
fun `findsEmptyInit`() {
test { EmptyInitBlock(Config.empty) }
}
@Test
fun `findsOneEmptySecondaryConstructor`() {
test { EmptySecondaryConstructor(Config.empty) }
}
@Test
fun `reports an empty kt file`() {
val rule = EmptyKtFile(Config.empty)
assertThat(rule.compileAndLint("")).hasSize(1)
}
@Test
fun `doesNotFailWithInvalidRegexWhenDisabled`() {
val configValues = mapOf(
"active" to "false",
ALLOWED_EXCEPTION_NAME_REGEX to "*foo"
)
val config = TestConfig(configValues)
assertThat(EmptyCatchBlock(config).compileAndLint(regexTestingCode)).isEmpty()
}
@Test
fun `doesFailWithInvalidRegex`() {
val configValues = mapOf(ALLOWED_EXCEPTION_NAME_REGEX to "*foo")
val config = TestConfig(configValues)
assertThatExceptionOfType(PatternSyntaxException::class.java).isThrownBy {
EmptyCatchBlock(config).compileAndLint(regexTestingCode)
}
}
}
private fun test(block: () -> Rule) {
val rule = block()
rule.lint(compileForTest(resourceAsPath("Empty.kt")))
assertThat(rule.findings).hasSize(1)
}