/
MaxLineLengthSpec.kt
251 lines (207 loc) · 8.49 KB
/
MaxLineLengthSpec.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package io.gitlab.arturbosch.detekt.rules.style
import io.github.detekt.test.utils.compileContentForTest
import io.github.detekt.test.utils.compileForTest
import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.rules.Case
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.assertj.core.api.Assertions.assertThat as doAssert
private const val MAX_LINE_LENGTH = "maxLineLength"
private const val EXCLUDE_PACKAGE_STATEMENTS = "excludePackageStatements"
private const val EXCLUDE_IMPORT_STATEMENTS = "excludeImportStatements"
private const val EXCLUDE_COMMENT_STATEMENTS = "excludeCommentStatements"
class MaxLineLengthSpec {
@Nested
inner class `a kt file with some long lines` {
private val file = compileForTest(Case.MaxLineLength.path())
private val lines = file.text.splitToSequence("\n")
private val fileContent = KtFileContent(file, lines)
@Test
fun `should report no errors when maxLineLength is set to 200`() {
val rule = MaxLineLength(TestConfig(mapOf(MAX_LINE_LENGTH to "200")))
rule.visit(fileContent)
assertThat(rule.findings).isEmpty()
}
@Test
fun `should report all errors with default maxLineLength`() {
val rule = MaxLineLength()
rule.visit(fileContent)
assertThat(rule.findings).hasSize(3)
}
@Test
fun `should report all errors with default maxLineLength including raw strings`() {
val rule = MaxLineLength(TestConfig("excludeRawStrings" to false))
rule.visit(fileContent)
assertThat(rule.findings).hasSize(7)
}
@Test
fun `should report meaningful signature for all violations`() {
val rule = MaxLineLength()
rule.visit(fileContent)
val locations = rule.findings.map { it.signature.substringAfterLast('$') }
doAssert(locations).allSatisfy { doAssert(it).isNotBlank() }
}
}
@Nested
inner class `a kt file with long but suppressed lines` {
private val file = compileForTest(Case.MaxLineLengthSuppressed.path())
private val lines = file.text.splitToSequence("\n")
private val fileContent = KtFileContent(file, lines)
@Test
fun `should not report as lines are suppressed`() {
val rule = MaxLineLength()
rule.visit(fileContent)
assertThat(rule.findings).isEmpty()
}
}
@Nested
inner class `a kt file with a long package name and long import statements` {
val code = """
package anIncrediblyLongAndComplexPackageNameThatProbablyShouldBeMuchShorterButForTheSakeOfTheTestItsNot
import anIncrediblyLongAndComplexImportNameThatProbablyShouldBeMuchShorterButForTheSakeOfTheTestItsNot
class Test {
}
"""
private val file = compileContentForTest(code)
private val lines = file.text.splitToSequence("\n")
private val fileContent = KtFileContent(file, lines)
@Test
fun `should not report the package statement and import statements by default`() {
val rule = MaxLineLength(
TestConfig(
mapOf(
MAX_LINE_LENGTH to "60"
)
)
)
rule.visit(fileContent)
assertThat(rule.findings).isEmpty()
}
@Test
fun `should report the package statement and import statements if they're enabled`() {
val rule = MaxLineLength(
TestConfig(
mapOf(
MAX_LINE_LENGTH to "60",
EXCLUDE_PACKAGE_STATEMENTS to "false",
EXCLUDE_IMPORT_STATEMENTS to "false"
)
)
)
rule.visit(fileContent)
assertThat(rule.findings).hasSize(2)
}
@Test
fun `should not report anything if both package and import statements are disabled`() {
val rule = MaxLineLength(
TestConfig(
MAX_LINE_LENGTH to "60",
EXCLUDE_PACKAGE_STATEMENTS to "true",
EXCLUDE_IMPORT_STATEMENTS to "true",
)
)
rule.visit(fileContent)
assertThat(rule.findings).isEmpty()
}
}
@Nested
inner class `a kt file with a long package name, long import statements, a long line and long comments` {
private val file = compileForTest(Case.MaxLineLengthWithLongComments.path())
private val lines = file.text.splitToSequence("\n")
private val fileContent = KtFileContent(file, lines)
@Test
fun `should report the package statement, import statements, line and comments by default`() {
val rule = MaxLineLength(
TestConfig(MAX_LINE_LENGTH to "60")
)
rule.visit(fileContent)
assertThat(rule.findings).hasSize(8)
}
@Test
fun `should report the package statement, import statements, line and comments if they're enabled`() {
val rule = MaxLineLength(
TestConfig(
MAX_LINE_LENGTH to "60",
EXCLUDE_PACKAGE_STATEMENTS to "false",
EXCLUDE_IMPORT_STATEMENTS to "false",
EXCLUDE_COMMENT_STATEMENTS to "false",
)
)
rule.visit(fileContent)
assertThat(rule.findings).hasSize(8)
}
@Test
fun `should not report comments if they're disabled`() {
val rule = MaxLineLength(
TestConfig(
mapOf(
MAX_LINE_LENGTH to "60",
EXCLUDE_COMMENT_STATEMENTS to "true"
)
)
)
rule.visit(fileContent)
assertThat(rule.findings).hasSize(5)
}
}
@Nested
inner class `a kt file with a long package name, long import statements and a long line` {
val code = """
package anIncrediblyLongAndComplexPackageNameThatProbablyShouldBeMuchShorterButForTheSakeOfTheTestItsNot
import anIncrediblyLongAndComplexImportNameThatProbablyShouldBeMuchShorterButForTheSakeOfTheTestItsNot
class Test {
fun anIncrediblyLongAndComplexMethodNameThatProbablyShouldBeMuchShorterButForTheSakeOfTheTestItsNot() {}
}
""".trimIndent()
private val file = compileContentForTest(code)
private val lines = file.text.splitToSequence("\n")
private val fileContent = KtFileContent(file, lines)
@Test
fun `should only the function line by default`() {
val rule = MaxLineLength(
TestConfig(MAX_LINE_LENGTH to "60")
)
rule.visit(fileContent)
assertThat(rule.findings).hasSize(1)
}
@Test
fun `should report the package statement, import statements and line if they're not excluded`() {
val rule = MaxLineLength(
TestConfig(
MAX_LINE_LENGTH to "60",
EXCLUDE_PACKAGE_STATEMENTS to "false",
EXCLUDE_IMPORT_STATEMENTS to "false",
)
)
rule.visit(fileContent)
assertThat(rule.findings).hasSize(3)
}
@Test
fun `should report only method if both package and import statements are disabled`() {
val rule = MaxLineLength(
TestConfig(
MAX_LINE_LENGTH to "60",
EXCLUDE_PACKAGE_STATEMENTS to "true",
EXCLUDE_IMPORT_STATEMENTS to "true",
)
)
rule.visit(fileContent)
assertThat(rule.findings).hasSize(1)
}
@Test
fun `should report correct line and column for function with excessive length`() {
val rule = MaxLineLength(
TestConfig(
MAX_LINE_LENGTH to "60",
EXCLUDE_PACKAGE_STATEMENTS to "true",
EXCLUDE_IMPORT_STATEMENTS to "true",
)
)
rule.visit(fileContent)
assertThat(rule.findings).hasSize(1)
assertThat(rule.findings).hasStartSourceLocations(SourceLocation(6, 5))
}
}
}