/
TrailingCommaSpec.kt
110 lines (97 loc) · 3.69 KB
/
TrailingCommaSpec.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
package io.gitlab.arturbosch.detekt.formatting
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
private const val ALLOW_TRAILING_COMMA = "allowTrailingComma"
private const val ALLOW_TRAILING_COMMA_ON_CALL_SITE = "allowTrailingCommaOnCallSite"
/**
* Some test cases were used directly from KtLint to verify the wrapper rule:
*
* https://github.com/pinterest/ktlint/blob/master/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/TrailingCommaRuleTest.kt
*/
class TrailingCommaSpec {
@Nested
inner class `unnecessary comma` {
@Test
fun `reports unnecessary comma on function call`() {
val code = """
val foo1 = listOf("a", "b",)
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code)
assertThat(findings).hasSize(1)
}
@Test
fun `reports unnecessary comma on constructor call`() {
val code = """
val foo2 = Pair(1, 2,)
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code)
assertThat(findings).hasSize(1)
}
@Test
fun `reports unnecessary comma on generic type definition`() {
val code = """
val foo3: List<String,> = emptyList()
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code)
assertThat(findings).hasSize(1)
}
@Test
fun `reports unnecessary comma on array get`() {
val code = """
val foo4 = Array(2) { 42 }
val bar4 = foo4[1,]
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code)
assertThat(findings).hasSize(1)
}
@Test
fun `reports unnecessary comma on annotation`() {
val code = """
@Foo5([1, 2,])
val foo5: Int = 0
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA to false))).lint(code)
assertThat(findings).hasSize(1)
}
}
@Nested
inner class `missing comma` {
@Test
fun `reports missing comma on field definition`() {
val code = """
data class Foo1(val bar: Int)
data class Foo2(
val bar: Int
)
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA to true))).lint(code)
assertThat(findings).hasSize(1)
}
@Test
fun `reports missing comma on function call`() {
val code = """
val foo1 = listOf("a", "b")
val foo2 = listOf(
"a",
"b"
)
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA_ON_CALL_SITE to true))).lint(code)
assertThat(findings).hasSize(1)
}
@Test
fun `reports missing comma on constructor call`() {
val code = """
val foo2 = Pair(1, 2)
val foo2 = Pair(
1,
2
)
""".trimIndent()
val findings = TrailingComma(TestConfig(mapOf(ALLOW_TRAILING_COMMA_ON_CALL_SITE to true))).lint(code)
assertThat(findings).hasSize(1)
}
}
}