-
Notifications
You must be signed in to change notification settings - Fork 501
/
RuleRunnerSorterTest.kt
472 lines (437 loc) · 18.3 KB
/
RuleRunnerSorterTest.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package com.pinterest.ktlint.core.internal
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.RuleProvider
import com.pinterest.ktlint.core.RuleRunner
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatIllegalStateException
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
class RuleRunnerSorterTest {
@Test
fun `Multiple normal rules in the same rule set are run in alphabetical order`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
NormalRule(RULE_B),
NormalRule(RULE_A)
),
debug = true
)
.map { it.ruleId }
assertThat(actual).containsExactly(
RULE_A,
RULE_B
)
}
@Test
fun `Multiple normal rules in different rule sets are run in alphabetical order but grouped in order standard, experimental and custom`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
NormalRule("$EXPERIMENTAL:$RULE_B"),
NormalRule("$EXPERIMENTAL:$RULE_A"),
NormalRule("$CUSTOM_RULE_SET_A:$RULE_B"),
NormalRule("$CUSTOM_RULE_SET_A:$RULE_A"),
NormalRule(RULE_B),
NormalRule(RULE_A),
NormalRule("$CUSTOM_RULE_SET_B:$RULE_B"),
NormalRule("$CUSTOM_RULE_SET_B:$RULE_A")
),
debug = true
).map { it.qualifiedRuleId }
assertThat(actual).containsExactly(
"$STANDARD:$RULE_A",
"$STANDARD:$RULE_B",
"$EXPERIMENTAL:$RULE_A",
"$EXPERIMENTAL:$RULE_B",
// Rules from custom rule sets are all grouped together
"$CUSTOM_RULE_SET_A:$RULE_A",
"$CUSTOM_RULE_SET_A:$RULE_B",
"$CUSTOM_RULE_SET_B:$RULE_A",
"$CUSTOM_RULE_SET_B:$RULE_B"
)
}
@Test
fun `Root only rule is run before non-root-only rule`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
RootNodeOnlyRule(ROOT_NODE_ONLY_RULE),
NormalRule(NORMAL_RULE)
),
debug = true
)
.map { it.ruleId }
assertThat(actual).containsExactly(
ROOT_NODE_ONLY_RULE,
NORMAL_RULE
)
}
@Test
fun `Multiple root only rules in the same rule set are run in alphabetical order`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
RootNodeOnlyRule("$EXPERIMENTAL:$RULE_B"),
RootNodeOnlyRule("$EXPERIMENTAL:$RULE_A"),
RootNodeOnlyRule("$CUSTOM_RULE_SET_A:$RULE_B"),
RootNodeOnlyRule("$CUSTOM_RULE_SET_A:$RULE_A"),
RootNodeOnlyRule(RULE_B),
RootNodeOnlyRule(RULE_A),
RootNodeOnlyRule("$CUSTOM_RULE_SET_B:$RULE_B"),
RootNodeOnlyRule("$CUSTOM_RULE_SET_B:$RULE_A")
),
debug = true
).map { it.qualifiedRuleId }
assertThat(actual).containsExactly(
"$STANDARD:$RULE_A",
"$STANDARD:$RULE_B",
"$EXPERIMENTAL:$RULE_A",
"$EXPERIMENTAL:$RULE_B",
// Rules from custom rule sets are all grouped together
"$CUSTOM_RULE_SET_A:$RULE_A",
"$CUSTOM_RULE_SET_A:$RULE_B",
"$CUSTOM_RULE_SET_B:$RULE_A",
"$CUSTOM_RULE_SET_B:$RULE_B"
)
}
@Test
fun `A run as late as possible rule runs after the rules not marked to run as late as possible`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
NormalRule(RULE_C),
RunAsLateAsPossibleRule(RULE_A),
NormalRule(RULE_B)
),
debug = true
)
.map { it.ruleId }
assertThat(actual).containsExactly(
RULE_B,
RULE_C,
RULE_A
)
}
@Test
fun `Multiple run as late as possible rules in the same rule set are sorted alphabetically`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
RunAsLateAsPossibleRule("$EXPERIMENTAL:$RULE_B"),
RunAsLateAsPossibleRule("$EXPERIMENTAL:$RULE_A"),
RunAsLateAsPossibleRule("$CUSTOM_RULE_SET_A:$RULE_B"),
RunAsLateAsPossibleRule("$CUSTOM_RULE_SET_A:$RULE_A"),
RunAsLateAsPossibleRule(RULE_B),
RunAsLateAsPossibleRule(RULE_A),
RunAsLateAsPossibleRule("$CUSTOM_RULE_SET_B:$RULE_B"),
RunAsLateAsPossibleRule("$CUSTOM_RULE_SET_B:$RULE_A")
),
debug = true
).map { it.qualifiedRuleId }
assertThat(actual).containsExactly(
"$STANDARD:$RULE_A",
"$STANDARD:$RULE_B",
"$EXPERIMENTAL:$RULE_A",
"$EXPERIMENTAL:$RULE_B",
// Rules from custom rule sets are all grouped together
"$CUSTOM_RULE_SET_A:$RULE_A",
"$CUSTOM_RULE_SET_A:$RULE_B",
"$CUSTOM_RULE_SET_B:$RULE_A",
"$CUSTOM_RULE_SET_B:$RULE_B"
)
}
@Test
fun `A run as late as possible rule on root node only runs after the rules not marked to run as late as possible`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
NormalRule(RULE_C),
RunAsLateAsPossibleOnRootNodeOnlyRule(RULE_A),
NormalRule(RULE_B)
),
debug = true
)
.map { it.ruleId }
assertThat(actual).containsExactly(
RULE_B,
RULE_C,
RULE_A
)
}
@Test
fun `Multiple run as late as possible on root node only rules in the same rule set are sorted alphabetically`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
RunAsLateAsPossibleRule("$EXPERIMENTAL:$RULE_B"),
RunAsLateAsPossibleRule("$EXPERIMENTAL:$RULE_A"),
RunAsLateAsPossibleRule("$CUSTOM_RULE_SET_A:$RULE_B"),
RunAsLateAsPossibleRule("$CUSTOM_RULE_SET_A:$RULE_A"),
RunAsLateAsPossibleRule(RULE_B),
RunAsLateAsPossibleRule(RULE_A),
RunAsLateAsPossibleRule("$CUSTOM_RULE_SET_B:$RULE_B"),
RunAsLateAsPossibleRule("$CUSTOM_RULE_SET_B:$RULE_A")
),
debug = true
).map { it.qualifiedRuleId }
assertThat(actual).containsExactly(
"$STANDARD:$RULE_A",
"$STANDARD:$RULE_B",
"$EXPERIMENTAL:$RULE_A",
"$EXPERIMENTAL:$RULE_B",
// Rules from custom rule sets are all grouped together
"$CUSTOM_RULE_SET_A:$RULE_A",
"$CUSTOM_RULE_SET_A:$RULE_B",
"$CUSTOM_RULE_SET_B:$RULE_A",
"$CUSTOM_RULE_SET_B:$RULE_B"
)
}
@ParameterizedTest(name = "Rule id: {0}")
@ValueSource(
strings = [
RULE_A,
"$CUSTOM_RULE_SET_A:$RULE_A"
]
)
fun `A rule annotated with run after rule can not refer to itself`(ruleId: String) {
assertThatIllegalStateException().isThrownBy {
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
object : R(
id = ruleId,
visitorModifier = VisitorModifier.RunAfterRule(ruleId)
) {}
),
debug = true
)
}.withMessage(
"Rule with id '$ruleId' has a visitor modifier of type 'RunAfterRule' but it is not referring to another " +
"rule but to the rule itself. A rule can not run after itself. This should be fixed by the maintainer " +
"of the rule."
)
}
@Test
fun `A rule annotated with run after rule for a rule in the same rule set runs after that rule and override the alphabetical sort order`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
object : R(
id = RULE_A,
visitorModifier = VisitorModifier.RunAfterRule(RULE_C)
) {},
NormalRule(RULE_B),
object : R(
id = RULE_D,
visitorModifier = VisitorModifier.RunAfterRule(RULE_B)
) {},
object : R(
id = RULE_C,
visitorModifier = VisitorModifier.RunAfterRule(RULE_B)
) {}
),
debug = true
).map { it.qualifiedRuleId }
assertThat(actual).containsExactly(
"$STANDARD:$RULE_B",
"$STANDARD:$RULE_C",
"$STANDARD:$RULE_A",
// Although RULE_D like RULE_C depends on RULE_B it still comes after RULE_A because that rules according to
// the default sort order comes before rule D
"$STANDARD:$RULE_D"
)
}
@Test
fun `A rule annotated with run after rule for a rule in the different rule set runs after that rule and override the alphabetical sort order`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
NormalRule(RULE_B),
object : R(
id = RULE_D,
visitorModifier = VisitorModifier.RunAfterRule(RULE_B)
) {},
object : R(
id = RULE_C,
visitorModifier = VisitorModifier.RunAfterRule(RULE_B)
) {},
object : R(
id = "$EXPERIMENTAL:$RULE_A",
visitorModifier = VisitorModifier.RunAfterRule(RULE_C)
) {}
),
debug = true
).map { it.qualifiedRuleId }
assertThat(actual).containsExactly(
"$STANDARD:$RULE_B",
"$STANDARD:$RULE_C",
"$STANDARD:$RULE_D",
"$EXPERIMENTAL:$RULE_A"
)
}
@Test
fun `A rule annotated with run after rule which has to be loaded throws an exception in case that other rule is not loaded`() {
assertThatIllegalStateException().isThrownBy {
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
object : R(
id = RULE_A,
visitorModifier = VisitorModifier.RunAfterRule(
ruleId = "not-loaded-rule",
loadOnlyWhenOtherRuleIsLoaded = true
)
) {}
),
debug = true
)
}.withMessage("No runnable rules found. Please ensure that at least one is enabled.")
}
@Test
fun `A rule annotated with run after rule of a rule which is not required to be loaded will be loaded when that other rule is not loaded`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
object : R(
id = RULE_A,
visitorModifier = VisitorModifier.RunAfterRule(
ruleId = "not-loaded-rule",
loadOnlyWhenOtherRuleIsLoaded = false
)
) {}
),
debug = true
).map { it.qualifiedRuleId }
assertThat(actual).containsExactly(
"$STANDARD:$RULE_A"
)
}
@Test
fun `Rules annotated with run after rule but cyclic depend on each others, no custom rule sets involved, throws an exception`() {
assertThatIllegalStateException().isThrownBy {
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
object : R(
id = RULE_A,
visitorModifier = VisitorModifier.RunAfterRule(RULE_B)
) {},
object : R(
id = RULE_B,
visitorModifier = VisitorModifier.RunAfterRule("$EXPERIMENTAL:$RULE_C")
) {},
object : R(
id = "$EXPERIMENTAL:$RULE_C",
visitorModifier = VisitorModifier.RunAfterRule(RULE_A)
) {}
),
debug = true
)
}.withMessage(
"""
Found cyclic dependencies between rules that should run after another rule:
- Rule with id '$STANDARD:$RULE_A' should run after rule with id '$STANDARD:$RULE_B'
- Rule with id '$STANDARD:$RULE_B' should run after rule with id '$EXPERIMENTAL:$RULE_C'
- Rule with id '$EXPERIMENTAL:$RULE_C' should run after rule with id '$STANDARD:$RULE_A'
""".trimIndent()
)
}
@Test
fun `Rules annotated with run after rule but cyclic depend on each others, custom rule sets involved, throws an exception`() {
assertThatIllegalStateException().isThrownBy {
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
object : R(
id = RULE_C,
visitorModifier = VisitorModifier.RunAfterRule("$CUSTOM_RULE_SET_B:$RULE_B")
) {},
object : R(
id = "$CUSTOM_RULE_SET_B:$RULE_B",
visitorModifier = VisitorModifier.RunAfterRule("$CUSTOM_RULE_SET_A:$RULE_A")
) {},
object : R(
id = "$CUSTOM_RULE_SET_A:$RULE_A",
visitorModifier = VisitorModifier.RunAfterRule("$STANDARD:$RULE_C")
) {}
),
debug = true
)
}.withMessage(
"""
Found cyclic dependencies between rules that should run after another rule. Please contact the maintainer(s) of the custom rule set(s) [$CUSTOM_RULE_SET_A, $CUSTOM_RULE_SET_B] before creating an issue in the KtLint project. Dependencies:
- Rule with id '$STANDARD:$RULE_C' should run after rule with id '$CUSTOM_RULE_SET_B:$RULE_B'
- Rule with id '$CUSTOM_RULE_SET_A:$RULE_A' should run after rule with id '$STANDARD:$RULE_C'
- Rule with id '$CUSTOM_RULE_SET_B:$RULE_B' should run after rule with id '$CUSTOM_RULE_SET_A:$RULE_A'
""".trimIndent()
)
}
private fun createRuleRunners(vararg rules: Rule): Set<RuleRunner> =
rules
.map {
RuleRunner(
RuleProvider { it }
)
}.toSet()
private companion object {
const val STANDARD = "standard"
const val EXPERIMENTAL = "experimental"
const val CUSTOM_RULE_SET_A = "custom-rule-set-a"
const val CUSTOM_RULE_SET_B = "custom-rule-set-b"
const val NORMAL_RULE = "normal-rule"
const val ROOT_NODE_ONLY_RULE = "root-node-only-rule"
const val RULE_A = "rule-a"
const val RULE_B = "rule-b"
const val RULE_C = "rule-c"
const val RULE_D = "rule-d"
}
open class NormalRule(id: String) : R(id)
class RootNodeOnlyRule(id: String) : R(
id = id,
visitorModifiers = setOf(
VisitorModifier.RunOnRootNodeOnly
)
)
class RunAsLateAsPossibleRule(id: String) : R(
id = id,
visitorModifiers = setOf(
VisitorModifier.RunAsLateAsPossible
)
)
class RunAsLateAsPossibleOnRootNodeOnlyRule(id: String) : R(
id = id,
visitorModifiers = setOf(
VisitorModifier.RunOnRootNodeOnly,
VisitorModifier.RunAsLateAsPossible
)
)
open class R(
id: String,
visitorModifiers: Set<VisitorModifier> = emptySet()
) : Rule(id, visitorModifiers) {
constructor(id: String, visitorModifier: VisitorModifier) : this(id, setOf(visitorModifier))
override fun beforeVisitChildNodes(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
throw UnsupportedOperationException(
"Rule should never be really invoked because that is not the aim of this unit test."
)
}
}
}