aboutsummaryrefslogtreecommitdiff
path: root/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundColonRuleTest.kt
blob: a8a319aa54e61636140429be4a49292f43735e2b (plain)
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
package com.github.shyiko.ktlint.ruleset.standard

import com.github.shyiko.ktlint.core.LintError
import com.github.shyiko.ktlint.test.format
import com.github.shyiko.ktlint.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.Test

class SpacingAroundColonRuleTest {

    @Test
    fun testLint() {
        assertThat(SpacingAroundColonRule().lint(
            """
            class A:B
            class A2 : B2
            fun main() {
                var x:Boolean
                var y: Boolean
                call(object: DefaultConsumer(channel) { })
            }
            interface D
            interface C: D
            interface C2 : D
            """.trimIndent()
        )).isEqualTo(listOf(
            LintError(1, 8, "colon-spacing", "Missing spacing around \":\""),
            LintError(4, 11, "colon-spacing", "Missing spacing after \":\""),
            LintError(6, 16, "colon-spacing", "Missing spacing before \":\""),
            LintError(9, 12, "colon-spacing", "Missing spacing before \":\"")
        ))
        assertThat(SpacingAroundColonRule().lint(
            """
            @file:JvmName("Foo")
            class Example(@field:Ann val foo: String, @get:Ann val bar: String)
            class Example {
                @set:[Inject VisibleForTesting]
                public var collaborator: Collaborator
            }
            fun @receiver:Fancy String.myExtension() { }
            """.trimIndent()
        )).isEmpty()
        assertThat(SpacingAroundColonRule().lint(
            """
            fun main() {
                val x = Foo::class
            }
            """.trimIndent()
        )).isEmpty()
    }

    @Test
    fun testFormat() {
        assertThat(SpacingAroundColonRule().format(
            """
            class A:B
            fun main() {
                var x:Boolean
                var y: Boolean
            }
            interface D
            interface C: D
            """.trimIndent()
        )).isEqualTo(
            """
            class A : B
            fun main() {
                var x: Boolean
                var y: Boolean
            }
            interface D
            interface C : D
            """.trimIndent()
        )
    }

    @Test
    fun testLintMethod() {
        assertThat(SpacingAroundColonRule().lint(
            """
            fun main() : String = "duck"
            fun duck(): String = "main"
            """.trimIndent()
        )).isEqualTo(listOf(
            LintError(1, 12, "colon-spacing", SpacingAroundColonRule.EXTRA_SPACE_MESSAGE)
        ))
    }

    @Test
    fun testFormatMethod() {
        assertThat(SpacingAroundColonRule().format(
            """
            fun main() : String = "duck"
            """.trimIndent()
        )).isEqualTo(
            """
            fun main(): String = "duck"
            """.trimIndent())
    }

    @Test
    fun testLintMethodParams() {
        assertThat(SpacingAroundColonRule().lint(
            """
            fun identity(value: String): String = value
            fun unformattedIdentity(value : String): String = value
            """.trimIndent()
        )).isEqualTo(listOf(
            LintError(2, 31, "colon-spacing", SpacingAroundColonRule.EXTRA_SPACE_MESSAGE)
        ))
    }

    @Test
    fun testFormatMethodParams() {
        assertThat(SpacingAroundColonRule().format(
            """
            fun validIdentity(value: String): String = value
            fun identity(value  : String): String = value
            fun oneSpaceIdentity(value : String): String = value
            """.trimIndent()
        )).isEqualTo(
            """
            fun validIdentity(value: String): String = value
            fun identity(value: String): String = value
            fun oneSpaceIdentity(value: String): String = value
            """.trimIndent())
    }

    @Test
    fun testLintGenericMethodParam() {
        assertThat(SpacingAroundColonRule().lint(
            """
            fun <T: Any> trueIdentity(value: T): T = value
            """.trimIndent()
        )).isEqualTo(listOf(
            LintError(1, 7, "colon-spacing", "Missing spacing before \":\"")
        ))
    }

    @Test
    fun testFormatGenericMethodParam() {
        assertThat(SpacingAroundColonRule().format(
            """
            fun <T: Any> trueIdentity(value: T): T = value
            fun <T : Any> trueIdentity(value: T): T = value
            """.trimIndent()
        )).isEqualTo(
            """
            fun <T : Any> trueIdentity(value: T): T = value
            fun <T : Any> trueIdentity(value: T): T = value
            """.trimIndent())
    }
}