aboutsummaryrefslogtreecommitdiff
path: root/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/SpacingAroundCurlyRuleTest.kt
blob: f5d2ad2efde7177f197cc424bf40a6a50ebfe475 (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
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 SpacingAroundCurlyRuleTest {

    @Test
    fun testLint() {
        assertThat(SpacingAroundCurlyRule().lint("fun emit() { }")).isEmpty()
        assertThat(SpacingAroundCurlyRule().lint("fun emit() {}")).isEmpty()
        assertThat(SpacingAroundCurlyRule().lint("fun main() { val v = if (true){return 0} }"))
            .isEqualTo(listOf(
                LintError(1, 31, "curly-spacing", "Missing spacing around \"{\""),
                LintError(1, 40, "curly-spacing", "Missing spacing before \"}\"")
            ))
        assertThat(SpacingAroundCurlyRule().lint("fun main() { val v = if (true) { return 0 } }"))
            .isEmpty()
        assertThat(SpacingAroundCurlyRule().lint("fun main() { fn({a -> a}, 0) }"))
            .isEqualTo(listOf(
                LintError(1, 18, "curly-spacing", "Missing spacing after \"{\""),
                LintError(1, 24, "curly-spacing", "Missing spacing before \"}\"")
            ))
        assertThat(SpacingAroundCurlyRule().lint("fun main() { fn({ a -> a }, 0) }"))
            .isEmpty()
        assertThat(SpacingAroundCurlyRule().lint("fun main() { fn({}, 0) && fn2({ }, 0) }"))
            .isEmpty()
        assertThat(SpacingAroundCurlyRule().lint("fun main() { find { it.default ?: false }?.phone }"))
            .isEmpty()
        assertThat(SpacingAroundCurlyRule().lint("""
            fun main() {
                emptyList<String>().find { true } !!.hashCode()
                emptyList<String>().find { true }!!.hashCode()
            }
            """.trimIndent()))
            .isEqualTo(listOf(
                LintError(2, 37, "curly-spacing", "Unexpected space after \"}\"")
            ))
    }

    @Test
    fun testFormat() {
        assertThat(SpacingAroundCurlyRule().format(
            """
            fun main() {
                val v = if (true){return ""}
                val v = if (true) { return "" }
                fn({a -> a}, 0)
                fn({ a -> a }, 0)
                fn({},{}, {}, 0)
                fn({ }, 0)
                fn({ a -> try{a()}catch (e: Exception){null} }, 0)
                foo.associateBy( { it.length } , { it } )
                try{call()}catch (e: Exception){}
                call({}, {})
                a.let{}.apply({})
                f({ if (true) {r.add(v)}; r})
                emptyList<String>().find { true }!!.hashCode()
                emptyList<String>().find { true } !!.hashCode()
                l.groupBy { it }[key] + l.groupBy { it } [key]
                l.groupBy { it }(key) + l.groupBy { it } (key)
                object : Any() {}::class.java.classLoader
                object : Any() {} ::class.java.classLoader
                class A
                {
                    companion object
                    {
                    }
                }
                interface A
                {
                }
                if (true)
                {
                }
                do
                {
                } while (true)
                call(
                    { echo() },
                    { echo() },
                    //
                    { echo() }
                )
                val f =
                    { true }
            }
            """.trimIndent()
        )).isEqualTo(
            """
            fun main() {
                val v = if (true) { return "" }
                val v = if (true) { return "" }
                fn({ a -> a }, 0)
                fn({ a -> a }, 0)
                fn({}, {}, {}, 0)
                fn({ }, 0)
                fn({ a -> try { a() } catch (e: Exception) { null } }, 0)
                foo.associateBy({ it.length }, { it })
                try { call() } catch (e: Exception) {}
                call({}, {})
                a.let {}.apply({})
                f({ if (true) { r.add(v) }; r })
                emptyList<String>().find { true }!!.hashCode()
                emptyList<String>().find { true }!!.hashCode()
                l.groupBy { it }[key] + l.groupBy { it }[key]
                l.groupBy { it }(key) + l.groupBy { it }(key)
                object : Any() {}::class.java.classLoader
                object : Any() {}::class.java.classLoader
                class A {
                    companion object {
                    }
                }
                interface A {
                }
                if (true) {
                }
                do {
                } while (true)
                call(
                    { echo() },
                    { echo() },
                    //
                    { echo() }
                )
                val f =
                    { true }
            }
            """.trimIndent()
        )
    }
}