forked from pinterest/ktlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RuleRunnerSorterTest.kt
654 lines (616 loc) · 28.8 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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
package com.pinterest.ktlint.core.internal
import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.RuleProvider
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.Nested
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),
),
)
.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"),
),
).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),
),
)
.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"),
),
).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 `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"),
),
).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",
)
}
@Nested
inner class `Given a rule having a RunAfterRule visitor modifier then this modifier can not refer to the rule itself` {
@ParameterizedTest(name = "Rule id: {0}")
@ValueSource(
strings = [
RULE_A,
"$CUSTOM_RULE_SET_A:$RULE_A",
],
)
fun `Given a rule with a single RunAfterRule modifier`(ruleId: String) {
assertThatIllegalStateException().isThrownBy {
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
object : R(
id = ruleId,
visitorModifier = VisitorModifier.RunAfterRule(ruleId),
) {},
),
)
}.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.",
)
}
@ParameterizedTest(name = "Rule id: {0}")
@ValueSource(
strings = [
RULE_A,
"$CUSTOM_RULE_SET_A:$RULE_A",
],
)
fun `A rule having multiple RunAfterRule visitor modifiers then none of those modifiers may refer to the rule itself`(
ruleId: String,
) {
assertThatIllegalStateException().isThrownBy {
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
object : R(
id = ruleId,
visitorModifiers = setOf(
VisitorModifier.RunAfterRule("some-other-rule-id"),
VisitorModifier.RunAfterRule(ruleId),
),
) {},
),
)
}.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),
) {},
),
).map { it.qualifiedRuleId }
assertThat(actual).containsExactly(
"$STANDARD:$RULE_B",
"$STANDARD:$RULE_C",
"$STANDARD:$RULE_D",
// RULE_D is ordered before RULE_A because rules are evaluated in order of the initial sorting (A, B, C, D). In the first
// iteration of the rules, RULE_A is blocked because rule C is not yet added. RULE_B, RULE_C and RULE_D can be added during the
// first iteration as the rules are not blocked when they are evaluated. In the second iteration, RULE_A can be added as well.
"$STANDARD:$RULE_A",
)
}
@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),
) {},
),
).map { it.qualifiedRuleId }
assertThat(actual).containsExactly(
"$STANDARD:$RULE_B",
"$STANDARD:$RULE_C",
"$STANDARD:$RULE_D",
"$EXPERIMENTAL:$RULE_A",
)
}
@Nested
inner class `Given the IndentationRule, TrailingCommaOnCallSiteRule, WrappingRule and FunctionSignatureRule` {
// Rule definitions below are extracted from the corresponding rules form the Standard and Experimental rule
// sets of ktlint
private val indentationRule =
object : Rule(
id = "indent",
visitorModifiers = setOf(
VisitorModifier.RunAsLateAsPossible,
VisitorModifier.RunAfterRule(
ruleId = "experimental:function-signature",
loadOnlyWhenOtherRuleIsLoaded = false,
runOnlyWhenOtherRuleIsEnabled = false,
),
VisitorModifier.RunAfterRule(
ruleId = "trailing-comma-on-call-site",
loadOnlyWhenOtherRuleIsLoaded = false,
runOnlyWhenOtherRuleIsEnabled = false,
),
VisitorModifier.RunAfterRule(
ruleId = "trailing-comma-on-declaration-site",
loadOnlyWhenOtherRuleIsLoaded = false,
runOnlyWhenOtherRuleIsEnabled = false,
),
),
) {}
private val trailingCommaOnCallSiteRule =
object : Rule(
id = "trailing-comma-on-call-site",
visitorModifiers = setOf(
VisitorModifier.RunAfterRule(
ruleId = "standard:wrapping",
loadOnlyWhenOtherRuleIsLoaded = true,
runOnlyWhenOtherRuleIsEnabled = true,
),
VisitorModifier.RunAsLateAsPossible,
),
) {}
private val wrappingRule = object : Rule("wrapping") {}
private val functionSignatureRule =
object : Rule(
id = "experimental:function-signature",
visitorModifiers = setOf(
// Run after wrapping and spacing rules
VisitorModifier.RunAsLateAsPossible,
),
) {}
@Test
fun `Given that the experimental FunctionSignatureRule is not included in the rules to be sorted`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
indentationRule,
trailingCommaOnCallSiteRule,
wrappingRule,
),
).map { it.qualifiedRuleId }
assertThat(actual).containsExactly(
"standard:wrapping",
"standard:trailing-comma-on-call-site",
"standard:indent",
)
}
@Test
fun `Given that the experimental FunctionSignatureRule is included in the rules to be sorted`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
indentationRule,
trailingCommaOnCallSiteRule,
wrappingRule,
functionSignatureRule,
),
).map { it.qualifiedRuleId }
assertThat(actual).containsExactly(
"standard:wrapping",
"standard:trailing-comma-on-call-site",
"experimental:function-signature",
"standard:indent",
)
}
}
@Nested
inner class `Given a rule with a RunAfterRule visitor modifier for a rule which is required to be loaded then throw an exception` {
@Test
fun `Given that the rule contains a single visitor modifier`() {
assertThatIllegalStateException().isThrownBy {
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
object : R(
id = RULE_A,
visitorModifier = VisitorModifier.RunAfterRule(
ruleId = RULE_B,
loadOnlyWhenOtherRuleIsLoaded = true,
),
) {},
object : R(
id = "$EXPERIMENTAL:$RULE_C",
visitorModifier = VisitorModifier.RunAfterRule(
ruleId = "$EXPERIMENTAL:$RULE_B",
loadOnlyWhenOtherRuleIsLoaded = true,
),
) {},
),
)
}.withMessage(
"""
Skipping rule(s) which are depending on a rule which is not loaded. Please check if you need to additional rule sets before creating an issue.
- Rule with id '$STANDARD:$RULE_A' requires rule with id '$STANDARD:$RULE_B' to be loaded
- Rule with id '$EXPERIMENTAL:$RULE_C' requires rule with id '$EXPERIMENTAL:$RULE_B' to be loaded
""".trimIndent(),
)
}
@Test
fun `Given that the rule contains multiple visitor modifiers`() {
assertThatIllegalStateException().isThrownBy {
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
object : R(
id = RULE_A,
visitorModifiers = setOf(
VisitorModifier.RunAfterRule(RULE_B),
VisitorModifier.RunAfterRule(
ruleId = RULE_C,
loadOnlyWhenOtherRuleIsLoaded = true,
),
),
) {},
object : R(
id = "$EXPERIMENTAL:$RULE_B",
visitorModifiers = setOf(
VisitorModifier.RunAfterRule(
ruleId = "$EXPERIMENTAL:$RULE_C",
loadOnlyWhenOtherRuleIsLoaded = true,
),
),
) {},
),
)
}.withMessage(
"""
Skipping rule(s) which are depending on a rule which is not loaded. Please check if you need to additional rule sets before creating an issue.
- Rule with id '$STANDARD:$RULE_A' requires rule with id '$STANDARD:$RULE_C' to be loaded
- Rule with id '$EXPERIMENTAL:$RULE_B' requires rule with id '$EXPERIMENTAL:$RULE_C' to be loaded
""".trimIndent(),
)
}
}
@Nested
inner class `Given a rule with a RunAfterRule visitor modifier on a rule which is not required to be loaded then run the rule` {
@Test
fun `Given a rule with a single RunAfterRule visitor modifier`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
object : R(
id = RULE_A,
visitorModifier = VisitorModifier.RunAfterRule(
ruleId = "not-loaded-rule",
loadOnlyWhenOtherRuleIsLoaded = false,
),
) {},
),
).map { it.qualifiedRuleId }
assertThat(actual).containsExactly(
"$STANDARD:$RULE_A",
)
}
@Test
fun `Given a rule with a multiple RunAfterRule visitor modifiers`() {
val actual =
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
object : R(RULE_A) {},
object : R(
id = RULE_B,
visitorModifiers = setOf(
VisitorModifier.RunAfterRule(RULE_A),
VisitorModifier.RunAfterRule(
ruleId = "not-loaded-rule",
loadOnlyWhenOtherRuleIsLoaded = false,
),
),
) {},
),
).map { it.qualifiedRuleId }
assertThat(actual).containsExactly(
"$STANDARD:$RULE_A",
"$STANDARD:$RULE_B",
)
}
}
@Nested
inner class `Given some rules that have a cyclic dependency and no custom rules sets involved` {
@Test
fun `Given some rules having only a single RunAfterRule visitor modifier then throw 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),
) {},
),
)
}.withMessage(
"""
Found cyclic dependencies between required rules that should run after another rule:
- Rule with id '$STANDARD:$RULE_A' should run after rule(s) with id '$STANDARD:$RULE_B'
- Rule with id '$STANDARD:$RULE_B' should run after rule(s) with id '$EXPERIMENTAL:$RULE_C'
- Rule with id '$EXPERIMENTAL:$RULE_C' should run after rule(s) with id '$STANDARD:$RULE_A'
""".trimIndent(),
)
}
@Test
fun `Given a rule with multiple RunAfterRule visitor modifier is part of a cyclic dependency then throw an exception`() {
assertThatIllegalStateException().isThrownBy {
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
object : R(
id = RULE_A,
visitorModifiers = setOf(
VisitorModifier.RunAfterRule("$CUSTOM_RULE_SET_A:$RULE_B"),
VisitorModifier.RunAfterRule(RULE_B),
),
) {},
object : R(
id = RULE_B,
visitorModifiers = setOf(
VisitorModifier.RunAfterRule(RULE_C),
VisitorModifier.RunAfterRule("$CUSTOM_RULE_SET_A:$RULE_C"),
),
) {},
object : R(
id = "$CUSTOM_RULE_SET_A:$RULE_C",
visitorModifier = VisitorModifier.RunAfterRule(RULE_A),
) {},
),
)
}.withMessage(
"""
Found cyclic dependencies between required rules that should run after another rule. Please contact the maintainer(s) of the custom rule set(s) [custom-rule-set-a] before creating an issue in the KtLint project. Dependencies:
- Rule with id '$STANDARD:$RULE_A' should run after rule(s) with id '$STANDARD:$RULE_B'
- Rule with id '$STANDARD:$RULE_B' should run after rule(s) with id '$CUSTOM_RULE_SET_A:$RULE_C'
- Rule with id '$CUSTOM_RULE_SET_A:$RULE_C' should run after rule(s) with id '$STANDARD:$RULE_A'
""".trimIndent(),
)
}
}
@Nested
inner class `Given some rules that have a cyclic dependency and custom rules sets involved` {
@Test
fun `Given some rules having only a single RunAfterRule visitor modifier then throw 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"),
) {},
),
)
}.withMessage(
"""
Found cyclic dependencies between required 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(s) with id '$CUSTOM_RULE_SET_B:$RULE_B'
- Rule with id '$CUSTOM_RULE_SET_A:$RULE_A' should run after rule(s) with id '$STANDARD:$RULE_C'
- Rule with id '$CUSTOM_RULE_SET_B:$RULE_B' should run after rule(s) with id '$CUSTOM_RULE_SET_A:$RULE_A'
""".trimIndent(),
)
}
@Test
fun `Given a rule with multiple RunAfterRule visitor modifier is part of a cyclic dependency then throw an exception`() {
assertThatIllegalStateException().isThrownBy {
RuleRunnerSorter()
.getSortedRuleRunners(
ruleRunners = createRuleRunners(
object : R(
id = RULE_C,
visitorModifiers = setOf(
VisitorModifier.RunAfterRule("$CUSTOM_RULE_SET_B:$RULE_B"),
VisitorModifier.RunAfterRule(RULE_B),
),
) {},
object : R(
id = "$CUSTOM_RULE_SET_B:$RULE_B",
visitorModifiers = setOf(
VisitorModifier.RunAfterRule(RULE_A),
VisitorModifier.RunAfterRule("$CUSTOM_RULE_SET_A:$RULE_A"),
),
) {},
object : R(
id = "$CUSTOM_RULE_SET_A:$RULE_A",
visitorModifier = VisitorModifier.RunAfterRule("$STANDARD:$RULE_C"),
) {},
),
)
}.withMessage(
"""
Found cyclic dependencies between required 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(s) with id '$CUSTOM_RULE_SET_B:$RULE_B'
- Rule with id '$CUSTOM_RULE_SET_A:$RULE_A' should run after rule(s) with id '$STANDARD:$RULE_C'
- Rule with id '$CUSTOM_RULE_SET_B:$RULE_B' should run after rule(s) 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 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 RunAsLateAsPossibleRule(id: String) : R(
id = id,
visitorModifiers = setOf(
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.",
)
}
}
}