aboutsummaryrefslogtreecommitdiff
path: root/ktlint-ruleset-standard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/FilenameRuleTest.kt
blob: 84e7ad4a78a89192f058edc059a2542596ec87ec (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
package com.github.shyiko.ktlint.ruleset.standard

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

class FilenameRuleTest {

    @Test
    fun testMatchingSingleClassName() {
        for (src in listOf(
            "class A",
            "data class A(val v: Int)",
            "sealed class A",
            "interface A",
            "object A",
            "enum class A {A}",
            "typealias A = Set<Network.Node>",
            // >1 declaration case
            "class B\nfun A.f() {}"
        )) {
            assertThat(FilenameRule().lint(
                """
                /*
                 * license
                 */
                @file:JvmName("Foo")
                package x
                import y.Z
                $src
                //
                """.trimIndent(),
                fileName("/some/path/A.kt")
            )).isEmpty()
        }
    }

    @Test
    fun testNonMatchingSingleClassName() {
        for (src in mapOf(
            "class A" to "class",
            "data class A(val v: Int)" to "class",
            "sealed class A" to "class",
            "interface A" to "interface",
            "object A" to "object",
            "enum class A {A}" to "class",
            "typealias A = Set<Network.Node>" to "typealias"
        )) {
            assertThat(FilenameRule().lint(
                """
                /*
                 * license
                 */
                @file:JvmName("Foo")
                package x
                import y.Z
                ${src.key}
                //
                """.trimIndent(),
                fileName("/some/path/B.kt")
            )).isEqualTo(listOf(
                LintError(1, 1, "filename", "${src.value} A should be declared in a file named A.kt")
            ))
        }
    }

    @Test
    fun testFileWithoutTopLevelDeclarations() {
        assertThat(FilenameRule().lint(
            """
            /*
             * copyright
             */
            """.trimIndent(),
            fileName("A.kt")
        )).isEmpty()
    }

    @Test
    fun testMultipleTopLevelClasses() {
        assertThat(FilenameRule().lint(
            """
            class B
            class C
            """.trimIndent(),
            fileName("A.kt")
        )).isEmpty()
    }

    @Test
    fun testMultipleNonTopLevelClasses() {
        assertThat(FilenameRule().lint(
            """
            class B {
                class C
                class D
            }
            """.trimIndent(),
            fileName("A.kt")
        )).isEqualTo(listOf(
            LintError(1, 1, "filename", "class B should be declared in a file named B.kt")
        ))
    }

    @Test
    fun testCaseSensitiveMatching() {
        assertThat(FilenameRule().lint(
            """
            interface Woohoo
            """.trimIndent(),
            fileName("woohoo.kt")
        )).isEqualTo(listOf(
            LintError(1, 1, "filename", "interface Woohoo should be declared in a file named Woohoo.kt")
        ))
    }

    @Test
    fun testIgnoreKotlinScriptFiles() {
        assertThat(FilenameRule().lint(
            """
            class B
            """.trimIndent(),
            fileName("A.kts")
        )).isEmpty()
    }

    private fun fileName(fileName: String) = mapOf("file_path" to fileName)
}