forked from pinterest/ktlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisitorProviderTest.kt
511 lines (461 loc) · 21.4 KB
/
VisitorProviderTest.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
package com.pinterest.ktlint.core
import com.pinterest.ktlint.core.api.editorconfig.DISABLED_RULES_PROPERTY
import com.pinterest.ktlint.core.api.editorconfig.KTLINT_DISABLED_RULES_PROPERTY
import com.pinterest.ktlint.core.api.editorconfig.RULE_EXECUTION_PROPERTY_TYPE
import com.pinterest.ktlint.core.api.editorconfig.RuleExecution
import com.pinterest.ktlint.core.internal.RuleRunner
import com.pinterest.ktlint.core.internal.VisitorProvider
import org.assertj.core.api.Assertions.assertThat
import org.ec4j.core.model.Property
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
class VisitorProviderTest {
@Nested
inner class `Given a visitor provider and some rules disabled via the 'disabled_rules' property` {
@Test
fun `A run-as-late-as-possible-rule runs later than normal rules`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule(RULE_A) },
RuleProvider { RunAsLateAsPossibleRule(RULE_B) },
RuleProvider { NormalRule(RULE_C) },
)
assertThat(actual).containsExactly(
Visit(RULE_A),
Visit(RULE_C),
Visit(RULE_B),
)
}
@Test
fun `Disabled rules in any type of rule set are not executed`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule(RULE_A) },
RuleProvider { NormalRule(SOME_DISABLED_RULE_IN_STANDARD_RULE_SET) },
RuleProvider { NormalRule("$EXPERIMENTAL:$RULE_B") },
RuleProvider { NormalRule(SOME_DISABLED_RULE_IN_EXPERIMENTAL_RULE_SET) },
RuleProvider { NormalRule("$CUSTOM_RULE_SET_A:$RULE_C") },
RuleProvider { NormalRule(SOME_DISABLED_RULE_IN_CUSTOM_RULE_SET_A) },
)
assertThat(actual).containsExactly(
Visit(RULE_A),
Visit(EXPERIMENTAL, RULE_B),
Visit(CUSTOM_RULE_SET_A, RULE_C),
)
}
@Test
fun `When no enabled rules are found for the root node, the visit function on the root node is not executed`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule(SOME_DISABLED_RULE_IN_STANDARD_RULE_SET) },
)
assertThat(actual).isEmpty()
}
@Test
fun `When no runnable rules are found for the root node, the visit function on the root node is not executed`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule(SOME_DISABLED_RULE_IN_STANDARD_RULE_SET) },
RuleProvider {
object : R(
id = RULE_A,
visitorModifier = VisitorModifier.RunAfterRule(
ruleId = SOME_DISABLED_RULE_IN_STANDARD_RULE_SET,
runOnlyWhenOtherRuleIsEnabled = true,
),
) {}
},
)
assertThat(actual).isEmpty()
}
/**
* Create a visitor provider. It returns a list of visits that the provider made after it was invoked. The tests
* of the visitor provider should only focus on whether the visit provider has invoked the correct rules in the
* correct order. Note that the testProvider does not invoke the real visit method of the rule.
*/
private fun testVisitorProvider(vararg ruleProviders: RuleProvider): List<Visit> {
return VisitorProvider(
// Creates a new VisitorProviderFactory for each unit test to prevent that tests for the exact same set of
// ruleIds are influencing each other.
ruleProviders
.map { RuleRunner(it) }
.distinctBy { it.ruleId }
.toSet(),
recreateRuleSorter = true,
).run {
val visits = mutableListOf<Visit>()
visitor(
ktlintDisabledRulesEditorConfigProperties(
SOME_DISABLED_RULE_IN_STANDARD_RULE_SET,
SOME_DISABLED_RULE_IN_EXPERIMENTAL_RULE_SET,
SOME_DISABLED_RULE_IN_CUSTOM_RULE_SET_A,
),
).invoke { _, fqRuleId ->
visits.add(Visit(fqRuleId))
}
visits
}
}
private fun ktlintDisabledRulesEditorConfigProperties(vararg ruleIds: String) =
with(DISABLED_RULES_PROPERTY) {
mapOf(
name to
Property.builder()
.name(name)
.type(type)
.value(ruleIds.joinToString(separator = ","))
.build(),
)
}
}
@Nested
inner class `Given a visitor provider and some rules disabled via the 'ktlint_disabled_rules' property` {
@Test
fun `A run-as-late-as-possible-rule runs later than normal rules`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule(RULE_A) },
RuleProvider { RunAsLateAsPossibleRule(RULE_B) },
RuleProvider { NormalRule(RULE_C) },
)
assertThat(actual).containsExactly(
Visit(RULE_A),
Visit(RULE_C),
Visit(RULE_B),
)
}
@Test
fun `Disabled rules in any type of rule set are not executed`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule(RULE_A) },
RuleProvider { NormalRule(SOME_DISABLED_RULE_IN_STANDARD_RULE_SET) },
RuleProvider { NormalRule("$EXPERIMENTAL:$RULE_B") },
RuleProvider { NormalRule(SOME_DISABLED_RULE_IN_EXPERIMENTAL_RULE_SET) },
RuleProvider { NormalRule("$CUSTOM_RULE_SET_A:$RULE_C") },
RuleProvider { NormalRule(SOME_DISABLED_RULE_IN_CUSTOM_RULE_SET_A) },
)
assertThat(actual).containsExactly(
Visit(RULE_A),
Visit(EXPERIMENTAL, RULE_B),
Visit(CUSTOM_RULE_SET_A, RULE_C),
)
}
@Test
fun `When no enabled rules are found for the root node, the visit function on the root node is not executed`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule(SOME_DISABLED_RULE_IN_STANDARD_RULE_SET) },
)
assertThat(actual).isEmpty()
}
@Test
fun `When no runnable rules are found for the root node, the visit function on the root node is not executed`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule(SOME_DISABLED_RULE_IN_STANDARD_RULE_SET) },
RuleProvider {
object : R(
id = RULE_A,
visitorModifier = VisitorModifier.RunAfterRule(
ruleId = SOME_DISABLED_RULE_IN_STANDARD_RULE_SET,
runOnlyWhenOtherRuleIsEnabled = true,
),
) {}
},
)
assertThat(actual).isEmpty()
}
/**
* Create a visitor provider. It returns a list of visits that the provider made after it was invoked. The tests
* of the visitor provider should only focus on whether the visit provider has invoked the correct rules in the
* correct order. Note that the testProvider does not invoke the real visit method of the rule.
*/
private fun testVisitorProvider(vararg ruleProviders: RuleProvider): List<Visit> {
return VisitorProvider(
// Creates a new VisitorProviderFactory for each unit test to prevent that tests for the exact same set of
// ruleIds are influencing each other.
ruleProviders
.map { RuleRunner(it) }
.distinctBy { it.ruleId }
.toSet(),
recreateRuleSorter = true,
).run {
val visits = mutableListOf<Visit>()
visitor(
ktlintDisabledRulesEditorConfigProperties(
SOME_DISABLED_RULE_IN_STANDARD_RULE_SET,
SOME_DISABLED_RULE_IN_EXPERIMENTAL_RULE_SET,
SOME_DISABLED_RULE_IN_CUSTOM_RULE_SET_A,
),
).invoke { _, fqRuleId ->
visits.add(Visit(fqRuleId))
}
visits
}
}
private fun ktlintDisabledRulesEditorConfigProperties(vararg ruleIds: String) =
with(KTLINT_DISABLED_RULES_PROPERTY) {
mapOf(
name to
Property.builder()
.name(name)
.type(type)
.value(ruleIds.joinToString(separator = ","))
.build(),
)
}
}
@Nested
inner class `Given a visitor provider and some rules disabled via the 'ktlint_rule-id' properties` {
@Test
fun `A run-as-late-as-possible-rule runs later than normal rules`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule(RULE_A) },
RuleProvider { RunAsLateAsPossibleRule(RULE_B) },
RuleProvider { NormalRule(RULE_C) },
)
assertThat(actual).containsExactly(
Visit(RULE_A),
Visit(RULE_C),
Visit(RULE_B),
)
}
@Test
fun `Given that standard rule set is enabled explicitly then run all standard rules except those that are disabled explicitly`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule(RULE_A) },
RuleProvider { NormalRule("$STANDARD:$RULE_B") },
RuleProvider { NormalRule("$STANDARD:$RULE_C") },
editorConfigProperties = mapOf(
ktLintRuleExecutionEditorConfigProperty("ktlint_$STANDARD", RuleExecution.enabled),
ktLintRuleExecutionEditorConfigProperty("ktlint_$STANDARD:$RULE_C", RuleExecution.disabled),
),
)
assertThat(actual).containsExactly(
Visit(RULE_A),
Visit(RULE_B),
)
}
@Test
fun `Given that standard rule set is not enabled explicitly then run all standard rules except those that are disabled explicitly`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule(RULE_A) },
RuleProvider { NormalRule("$STANDARD:$RULE_B") },
RuleProvider { NormalRule("$STANDARD:$RULE_C") },
editorConfigProperties = mapOf(
ktLintRuleExecutionEditorConfigProperty("ktlint_$STANDARD:$RULE_C", RuleExecution.disabled),
),
)
assertThat(actual).containsExactly(
Visit(RULE_A),
Visit(RULE_B),
)
}
@Test
fun `Given that standard rule set is disabled explicitly then only run standard rules that are enabled explicitly`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule(RULE_A) },
RuleProvider { NormalRule("$STANDARD:$RULE_B") },
RuleProvider { NormalRule("$STANDARD:$RULE_C") },
editorConfigProperties = mapOf(
ktLintRuleExecutionEditorConfigProperty("ktlint_$STANDARD", RuleExecution.disabled),
ktLintRuleExecutionEditorConfigProperty("ktlint_$STANDARD:$RULE_A", RuleExecution.enabled),
ktLintRuleExecutionEditorConfigProperty("ktlint_$STANDARD:$RULE_B", RuleExecution.enabled),
),
)
assertThat(actual).containsExactly(
Visit(RULE_A),
Visit(RULE_B),
)
}
@Test
fun `Given that the experimental rule set is not enabled explicitly then only run experimental rules that are enabled explicitly`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule("$EXPERIMENTAL:$RULE_B") },
RuleProvider { NormalRule("$EXPERIMENTAL:$RULE_C") },
editorConfigProperties = mapOf(
ktLintRuleExecutionEditorConfigProperty("ktlint_$EXPERIMENTAL:$RULE_B", RuleExecution.enabled),
),
)
assertThat(actual).containsExactly(
Visit(EXPERIMENTAL, RULE_B),
)
}
@Test
fun `Given that the custom rule set is not enabled explicitly then run all rules that are not disabled explicitly`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule("$CUSTOM_RULE_SET_A:$RULE_B") },
RuleProvider { NormalRule("$CUSTOM_RULE_SET_A:$RULE_C") },
editorConfigProperties = mapOf(
ktLintRuleExecutionEditorConfigProperty("ktlint_$CUSTOM_RULE_SET_A:$RULE_C", RuleExecution.disabled),
),
)
assertThat(actual).containsExactly(
Visit(CUSTOM_RULE_SET_A, RULE_B),
)
}
@Test
fun `Given that a non-standard rule set is disabled explicitly then only run rules that are enabled explicitly`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule("$EXPERIMENTAL:$RULE_B") },
RuleProvider { NormalRule("$EXPERIMENTAL:$RULE_C") },
RuleProvider { NormalRule("$CUSTOM_RULE_SET_A:$RULE_B") },
RuleProvider { NormalRule("$CUSTOM_RULE_SET_A:$RULE_C") },
editorConfigProperties = mapOf(
ktLintRuleExecutionEditorConfigProperty("ktlint_$EXPERIMENTAL", RuleExecution.disabled),
ktLintRuleExecutionEditorConfigProperty("ktlint_$EXPERIMENTAL:$RULE_B", RuleExecution.enabled),
ktLintRuleExecutionEditorConfigProperty("ktlint_$CUSTOM_RULE_SET_A", RuleExecution.disabled),
ktLintRuleExecutionEditorConfigProperty("ktlint_$CUSTOM_RULE_SET_A:$RULE_B", RuleExecution.enabled),
),
)
assertThat(actual).containsExactly(
Visit(EXPERIMENTAL, RULE_B),
Visit(CUSTOM_RULE_SET_A, RULE_B),
)
}
@Test
fun `Given that a non-standard rule set is enabled explicitly then only run rules that are not disabled explicitly`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule("$EXPERIMENTAL:$RULE_B") },
RuleProvider { NormalRule("$EXPERIMENTAL:$RULE_C") },
RuleProvider { NormalRule("$CUSTOM_RULE_SET_A:$RULE_B") },
RuleProvider { NormalRule("$CUSTOM_RULE_SET_A:$RULE_C") },
editorConfigProperties = mapOf(
ktLintRuleExecutionEditorConfigProperty("ktlint_$EXPERIMENTAL", RuleExecution.enabled),
ktLintRuleExecutionEditorConfigProperty("ktlint_$EXPERIMENTAL:$RULE_C", RuleExecution.disabled),
ktLintRuleExecutionEditorConfigProperty("ktlint_$CUSTOM_RULE_SET_A", RuleExecution.enabled),
ktLintRuleExecutionEditorConfigProperty("ktlint_$CUSTOM_RULE_SET_A:$RULE_C", RuleExecution.disabled),
),
)
assertThat(actual).containsExactly(
Visit(EXPERIMENTAL, RULE_B),
Visit(CUSTOM_RULE_SET_A, RULE_B),
)
}
@Test
fun `When no enabled rules are found for the root node, the visit function on the root node is not executed`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule(RULE_A) },
RuleProvider { NormalRule("$STANDARD:$RULE_B") },
editorConfigProperties = mapOf(
ktLintRuleExecutionEditorConfigProperty("ktlint_${STANDARD}_$RULE_A", RuleExecution.disabled),
ktLintRuleExecutionEditorConfigProperty("ktlint_${STANDARD}_$RULE_B", RuleExecution.disabled),
),
)
assertThat(actual).isEmpty()
}
@Test
fun `When no runnable rules are found for the root node, the visit function on the root node is not executed`() {
val actual = testVisitorProvider(
RuleProvider { NormalRule(RULE_C) },
RuleProvider {
object : R(
id = RULE_A,
visitorModifier = VisitorModifier.RunAfterRule(
ruleId = RULE_C,
runOnlyWhenOtherRuleIsEnabled = true,
),
) {}
},
RuleProvider {
object : R(
id = RULE_B,
visitorModifier = VisitorModifier.RunAfterRule(
ruleId = RULE_C,
runOnlyWhenOtherRuleIsEnabled = true,
),
) {}
},
editorConfigProperties = mapOf(
ktLintRuleExecutionEditorConfigProperty("ktlint_$STANDARD:$RULE_C", RuleExecution.disabled),
),
)
assertThat(actual).isEmpty()
}
/**
* Create a visitor provider. It returns a list of visits that the provider made after it was invoked. The tests
* of the visitor provider should only focus on whether the visit provider has invoked the correct rules in the
* correct order. Note that the testProvider does not invoke the real visit method of the rule.
*/
private fun testVisitorProvider(
vararg ruleProviders: RuleProvider,
editorConfigProperties: Map<String, Property> = emptyMap(),
): List<Visit> = VisitorProvider(
// Creates a new VisitorProviderFactory for each unit test to prevent that tests for the exact same set of
// ruleIds are influencing each other.
ruleProviders
.map { RuleRunner(it) }
.distinctBy { it.ruleId }
.toSet(),
recreateRuleSorter = true,
).run {
val visits = mutableListOf<Visit>()
visitor(
editorConfigProperties,
).invoke { _, fqRuleId ->
visits.add(Visit(fqRuleId))
}
visits
}
private fun ktLintRuleExecutionEditorConfigProperty(
ktlintRuleExecutionPropertyName: String,
ruleExecution: RuleExecution,
): Pair<String, Property> =
ktlintRuleExecutionPropertyName
.replace(":", "_")
.let { propertyName ->
propertyName to Property.builder()
.name(propertyName)
.type(RULE_EXECUTION_PROPERTY_TYPE)
.value(ruleExecution.name)
.build()
}
}
private companion object {
const val STANDARD = "standard"
const val EXPERIMENTAL = "experimental"
const val CUSTOM_RULE_SET_A = "custom-rule-set-a"
const val RULE_A = "rule-a"
const val RULE_B = "rule-b"
const val RULE_C = "rule-c"
const val SOME_DISABLED_RULE_IN_STANDARD_RULE_SET = "some-disabled-rule-in-standard-rule-set"
const val SOME_DISABLED_RULE_IN_EXPERIMENTAL_RULE_SET = "$EXPERIMENTAL:some-disabled-rule-in-experimental-rule-set"
const val SOME_DISABLED_RULE_IN_CUSTOM_RULE_SET_A = "$CUSTOM_RULE_SET_A:some-disabled-rule-in-custom-rule-set"
}
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.",
)
}
}
private data class Visit(val shortenedQualifiedRuleId: String) {
constructor(
ruleSetId: String,
ruleId: String,
) : this(
shortenedQualifiedRuleId = "$ruleSetId:$ruleId",
) {
require(!ruleSetId.contains(':')) {
"rule set id may not contain the ':' character"
}
require(!ruleId.contains(':')) {
"rule id may not contain the ':' character"
}
}
init {
require(!shortenedQualifiedRuleId.startsWith(STANDARD)) {
"the shortened qualified rule id may not start with '$STANDARD'"
}
}
}
}