/
ConfigPropertySpec.kt
531 lines (459 loc) · 20 KB
/
ConfigPropertySpec.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
package io.gitlab.arturbosch.detekt.api
import io.gitlab.arturbosch.detekt.test.TestConfig
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.assertj.core.api.Assertions.tuple
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import java.util.concurrent.atomic.AtomicInteger
class
ConfigPropertySpec {
@Nested
inner class `simple property` {
@Nested
inner class `String property` {
private val configValue = "value"
private val defaultValue = "default"
private val subject = object : TestConfigAware("present" to configValue) {
val present: String by config(defaultValue)
val notPresent: String by config(defaultValue)
}
@Test
fun `uses the value provided in config if present`() {
assertThat(subject.present).isEqualTo(configValue)
}
@Test
fun `uses the default value if not present`() {
assertThat(subject.notPresent).isEqualTo(defaultValue)
}
}
@Nested
inner class `Int property` {
private val configValue = 99
private val defaultValue = -1
@Nested
inner class `defined as number` {
private val subject = object : TestConfigAware("present" to configValue) {
val present: Int by config(defaultValue)
val notPresent: Int by config(defaultValue)
}
@Test
fun `uses the value provided in config if present`() {
assertThat(subject.present).isEqualTo(configValue)
}
@Test
fun `uses the default value if not present`() {
assertThat(subject.notPresent).isEqualTo(defaultValue)
}
}
@Nested
inner class `defined as string` {
private val subject = object : TestConfigAware("present" to "$configValue") {
val present: Int by config(defaultValue)
}
@Test
fun `uses the value provided in config if present`() {
assertThat(subject.present).isEqualTo(configValue)
}
}
@Nested
inner class `ValuesWithReason property` {
private val defaultValue = valuesWithReason("aValue" to "aReason")
@Nested
inner class `value defined as list` {
private val subject = object : TestConfigAware("present" to listOf("a", "b", "c")) {
val present: ValuesWithReason by config(defaultValue)
val notPresent: ValuesWithReason by config(defaultValue)
}
@Test
fun `uses the value provided in config if present`() {
assertThat(subject.present)
.extracting(ValueWithReason::value, ValueWithReason::reason)
.containsExactly(tuple("a", null), tuple("b", null), tuple("c", null))
}
@Test
fun `uses the default value if not present`() {
assertThat(subject.notPresent).isEqualTo(defaultValue)
}
}
@Nested
inner class `value defined as list of maps` {
private val subject = object : TestConfigAware(
"present" to listOf(
mapOf("value" to "a", "reason" to "reasonA"),
mapOf("value" to "b", "reason" to null),
mapOf("value" to "c"),
)
) {
val present: ValuesWithReason by config(defaultValue)
val notPresent: ValuesWithReason by config(defaultValue)
}
@Test
fun `uses the value provided in config if present`() {
assertThat(subject.present)
.extracting(ValueWithReason::value, ValueWithReason::reason)
.containsExactly(
tuple("a", "reasonA"),
tuple("b", null),
tuple("c", null)
)
}
@Test
fun `uses the default value if not present`() {
assertThat(subject.notPresent).isEqualTo(defaultValue)
}
}
@Nested
inner class `value defined as list of maps with invalid data` {
@Test
fun `value missing`() {
assertThatThrownBy {
object : TestConfigAware(
"present" to listOf(
mapOf("reason" to "reason")
)
) {
val present: ValuesWithReason by config(defaultValue)
}.present
}.isInstanceOf(Config.InvalidConfigurationError::class.java)
}
@Test
fun `value with an invalid type`() {
assertThatThrownBy {
object : TestConfigAware(
"present" to listOf(
mapOf("value" to 42, "reason" to "reason")
)
) {
val present: ValuesWithReason by config(defaultValue)
}.present
}.isInstanceOf(Config.InvalidConfigurationError::class.java)
}
@Test
fun `reason with an invalid type`() {
assertThatThrownBy {
object : TestConfigAware(
"present" to listOf(
mapOf("value" to "a", "reason" to 42)
)
) {
val present: ValuesWithReason by config(defaultValue)
}.present
}.isInstanceOf(Config.InvalidConfigurationError::class.java)
}
}
}
}
@Nested
inner class `Boolean property` {
private val configValue = false
private val defaultValue = true
@Nested
inner class `defined as Boolean` {
private val subject = object : TestConfigAware("present" to configValue) {
val present: Boolean by config(defaultValue)
val notPresent: Boolean by config(defaultValue)
}
@Test
fun `uses the value provided in config if present`() {
assertThat(subject.present).isEqualTo(configValue)
}
@Test
fun `uses the default value if not present`() {
assertThat(subject.notPresent).isEqualTo(defaultValue)
}
}
@Nested
inner class `defined as string` {
private val subject = object : TestConfigAware("present" to "$configValue") {
val present: Boolean by config(defaultValue)
}
@Test
fun `uses the value provided in config if present`() {
assertThat(subject.present).isEqualTo(configValue)
}
}
}
@Nested
inner class `List property` {
private val defaultValue = listOf("x")
@Nested
inner class `defined as list` {
private val subject = object : TestConfigAware("present" to "a,b,c") {
val present: List<String> by config(defaultValue)
val notPresent: List<String> by config(defaultValue)
}
@Test
fun `uses the value provided in config if present`() {
assertThat(subject.present).isEqualTo(listOf("a", "b", "c"))
}
@Test
fun `uses the default value if not present`() {
assertThat(subject.notPresent).isEqualTo(defaultValue)
}
}
@Nested
inner class `defined as comma separated string` {
private val subject = object : TestConfigAware("present" to "a,b,c") {
val present: List<String> by config(defaultValue)
val notPresent: List<String> by config(defaultValue)
}
@Test
fun `uses the value provided in config if present`() {
assertThat(subject.present).isEqualTo(listOf("a", "b", "c"))
}
@Test
fun `uses the default value if not present`() {
assertThat(subject.notPresent).isEqualTo(defaultValue)
}
}
}
}
@Nested
inner class `invalid type` {
@Nested
inner class `Long property` {
private val defaultValue = 1L
private val subject = object : TestConfigAware() {
val prop: Long by config(defaultValue)
}
@Test
fun `throws`() {
assertThatThrownBy { subject.prop }
.isInstanceOf(IllegalStateException::class.java)
.hasMessageContaining("is not supported")
}
}
@Nested
inner class `Regex property` {
private val defaultValue = Regex("a")
private val subject = object : TestConfigAware() {
val prop: Regex by config(defaultValue)
}
@Test
fun `throws`() {
assertThatThrownBy { subject.prop }
.isInstanceOf(IllegalStateException::class.java)
.hasMessageContaining("is not supported")
}
}
@Nested
inner class `Set property` {
private val defaultValue = setOf("a")
private val subject = object : TestConfigAware() {
val prop: Set<String> by config(defaultValue)
}
@Test
fun `throws`() {
assertThatThrownBy { subject.prop }
.isInstanceOf(IllegalStateException::class.java)
.hasMessageContaining("is not supported")
}
}
@Nested
inner class `List of Int` {
private val defaultValue = listOf(1)
private val subject = object : TestConfigAware() {
val prop: List<Int> by config(defaultValue)
}
@Test
fun `throws`() {
assertThatThrownBy { subject.prop }
.isInstanceOf(IllegalStateException::class.java)
.hasMessageContaining("lists of strings are supported")
}
}
}
@Nested
inner class `transform` {
@Nested
inner class `primitive` {
@Nested
inner class `String property is transformed to regex` {
private val defaultValue = ".*"
private val configValue = "[a-z]+"
private val subject = object : TestConfigAware("present" to configValue) {
val present: Regex by config(defaultValue) { it.toRegex() }
val notPresent: Regex by config(defaultValue) { it.toRegex() }
}
@Test
fun `applies the mapping function to the configured value`() {
assertThat(subject.present.matches("abc")).isTrue
assertThat(subject.present.matches("123")).isFalse()
}
@Test
fun `applies the mapping function to the default`() {
assertThat(subject.notPresent.matches("abc")).isTrue
assertThat(subject.notPresent.matches("123")).isTrue
}
}
@Nested
inner class `Int property is transformed to String` {
private val configValue = 99
private val defaultValue = -1
private val subject = object : TestConfigAware("present" to configValue) {
val present: String by config(defaultValue) { it.toString() }
val notPresent: String by config(defaultValue) { it.toString() }
}
@Test
fun `applies the mapping function to the configured value`() {
assertThat(subject.present).isEqualTo("$configValue")
}
@Test
fun `applies the mapping function to the default`() {
assertThat(subject.notPresent).isEqualTo("$defaultValue")
}
}
@Nested
inner class `Boolean property is transformed to String` {
private val configValue = true
private val defaultValue = false
private val subject = object : TestConfigAware("present" to configValue) {
val present: String by config(defaultValue) { it.toString() }
val notPresent: String by config(defaultValue) { it.toString() }
}
@Test
fun `applies the mapping function to the configured value`() {
assertThat(subject.present).isEqualTo("$configValue")
}
@Test
fun `applies the mapping function to the default`() {
assertThat(subject.notPresent).isEqualTo("$defaultValue")
}
}
@Nested
inner class `Boolean property is transformed to String with function reference` {
private val defaultValue = false
private val subject = object : TestConfigAware() {
val prop1: String by config(defaultValue, Boolean::toString)
val prop2: String by config(transformer = Boolean::toString, defaultValue = defaultValue)
}
@Test
fun `transforms properties`() {
assertThat(subject.prop1).isEqualTo("$defaultValue")
assertThat(subject.prop2).isEqualTo("$defaultValue")
}
}
}
@Nested
inner class `list of strings` {
private val defaultValue = listOf("99")
private val subject = object : TestConfigAware("present" to "1,2,3") {
val present: Int by config(defaultValue) { it.sumOf(String::toInt) }
val notPresent: Int by config(defaultValue) { it.sumOf(String::toInt) }
}
@Test
fun `applies transformer to list configured`() {
assertThat(subject.present).isEqualTo(6)
}
@Test
fun `applies transformer to default list`() {
assertThat(subject.notPresent).isEqualTo(99)
}
}
@Nested
inner class `empty list of strings` {
private val subject = object : TestConfigAware() {
val defaultValue: List<String> = emptyList()
val prop1: List<Int> by config(defaultValue) { it.map(String::toInt) }
val prop2: List<Int> by config(emptyList<String>()) { it.map(String::toInt) }
}
@Test
fun `can be defined as variable`() {
assertThat(subject.prop1).isEmpty()
}
@Test
fun `can be defined using listOf`() {
assertThat(subject.prop2).isEmpty()
}
}
@Nested
inner class `memoization` {
private val subject = object : TestConfigAware() {
val counter = AtomicInteger(0)
val prop: String by config(1) {
counter.getAndIncrement()
it.toString()
}
fun useProperty(): String {
return "something with $prop"
}
}
@Test
fun `transformer is called only once`() {
repeat(5) {
assertThat(subject.useProperty()).isEqualTo("something with 1")
}
assertThat(subject.counter.get()).isEqualTo(1)
}
}
}
@OptIn(UnstableApi::class)
@Nested
inner class `configWithFallback` {
@Nested
inner class `primitive` {
private val configValue = 99
private val defaultValue = 0
private val fallbackValue = -1
private val subject =
object : TestConfigAware("present" to "$configValue", "fallback" to fallbackValue) {
private val fallback: Int by config(42)
private val missing: Int by config(42)
val present: Int by configWithFallback(::fallback, defaultValue)
val notPresentWithFallback: Int by configWithFallback(::fallback, defaultValue)
val notPresentFallbackMissing: Int by configWithFallback(::missing, defaultValue)
}
@Test
fun `uses the value provided in config if present`() {
assertThat(subject.present).isEqualTo(configValue)
}
@Test
fun `uses the value from fallback property if value is missing and fallback exists`() {
assertThat(subject.notPresentWithFallback).isEqualTo(fallbackValue)
}
@Test
fun `uses the default value if not present`() {
assertThat(subject.notPresentFallbackMissing).isEqualTo(defaultValue)
}
}
@Nested
inner class `with transformation` {
private val configValue = 99
private val defaultValue = 0
private val fallbackValue = -1
private val fallbackOffset = 10
private val subject = object : TestConfigAware("present" to configValue, "fallback" to fallbackValue) {
private val fallback: String by config(42) { (it + fallbackOffset).toString() }
private val missing: String by config(42) { (it + fallbackOffset).toString() }
val present: String by configWithFallback(::fallback, defaultValue) { v ->
v.toString()
}
val notPresentWithFallback: String by configWithFallback(::fallback, defaultValue) { v ->
v.toString()
}
val notPresentFallbackMissing: String by configWithFallback(::missing, defaultValue) { v ->
v.toString()
}
}
@Test
fun `uses the value provided in config if present`() {
assertThat(subject.present).isEqualTo("$configValue")
}
@Test
fun `transforms the value from fallback property if value is missing and fallback exists`() {
assertThat(subject.notPresentWithFallback).isEqualTo("${fallbackValue + fallbackOffset}")
}
@Test
fun `uses the default value if not present`() {
assertThat(subject.notPresentFallbackMissing).isEqualTo("$defaultValue")
}
}
}
}
private open class TestConfigAware(private vararg val data: Pair<String, Any>) : ConfigAware {
override val ruleId: RuleId
get() = "test"
override val ruleSetConfig: Config
get() = TestConfig(data.toMap())
}