forked from detekt/detekt
/
TopLevelPropertyNamingSpec.kt
90 lines (79 loc) · 2.69 KB
/
TopLevelPropertyNamingSpec.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
package io.gitlab.arturbosch.detekt.rules.naming
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.compileAndLint
import io.gitlab.arturbosch.detekt.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
class TopLevelPropertyNamingSpec {
val subject = TopLevelPropertyNaming()
@Test
fun `should use custom name top level propeties`() {
assertThat(
TopLevelPropertyNaming(TestConfig(mapOf(TopLevelPropertyNaming.CONSTANT_PATTERN to "^lowerCaseConst$"))).compileAndLint(
"""
class Foo{
companion object {
const val lowerCaseConst = ""
}
}
"""
)
).isEmpty()
}
@Nested
inner class `constants on top level` {
@Test
fun `should not detect any constants not complying to the naming rules`() {
val code = """
const val MY_NAME_8 = "Artur"
const val MYNAME = "Artur"
"""
assertThat(subject.lint(code)).isEmpty()
}
@Test
fun `should detect five constants not complying to the naming rules`() {
val code = """
const val MyNAME = "Artur"
const val name = "Artur"
const val nAme = "Artur"
private const val _nAme = "Artur"
const val serialVersionUID = 42L
"""
assertThat(subject.lint(code)).hasSize(5)
}
}
@Nested
inner class `variables on top level` {
@Test
fun `should not report any`() {
val code = """
val name = "Artur"
val nAme8 = "Artur"
private val _name = "Artur"
val serialVersionUID = 42L
val MY_NAME = "Artur"
val MYNAME = "Artur"
val MyNAME = "Artur"
private val NAME = "Artur"
val s_d_d_1 = listOf("")
private val INTERNAL_VERSION = "1.0.0"
"""
assertThat(subject.lint(code)).isEmpty()
}
@Test
fun `should report non private top level property using underscore`() {
val code = """
val _nAme = "Artur"
"""
assertThat(subject.lint(code)).hasSize(1)
}
@Test
fun `should report private top level property using two underscores`() {
val code = """
private val __NAME = "Artur"
"""
io.gitlab.arturbosch.detekt.test.assertThat(subject.lint(code)).hasSize(1)
}
}
}