-
-
Notifications
You must be signed in to change notification settings - Fork 755
/
NamingConventionViolationSpec.kt
147 lines (128 loc) · 3.7 KB
/
NamingConventionViolationSpec.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
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
package io.gitlab.arturbosch.detekt.rules
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.rules.style.NamingConventionViolation
import io.gitlab.arturbosch.detekt.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.spek.api.dsl.it
import org.jetbrains.spek.subject.SubjectSpek
import org.junit.jupiter.api.Test
/**
* @author Artur Bosch
*/
class NamingConventionViolationSpec : SubjectSpek<NamingConventionViolation>({
subject { NamingConventionViolation() }
it("should find all wrong namings") {
subject.lint(Case.NamingConventions.path())
assertThat(subject.findings).hasSize(9)
}
})
class NamingConventionTest {
@Test
fun lint() {
assertThat(NamingConventionViolation().lint(
"""
const val MY_NAME = "Artur"
const val MYNAME = "Artur"
const val MyNAME = "Artur"
const val serialVersionUID = 42L
"""
)).hasSize(1)
}
@Test
fun uppercaseAllowedForVariablesInsideObjectDeclaration() {
assertThat(NamingConventionViolation().lint(
"""
object Bla {
val MY_NAME = "Artur"
}
"""
)).hasSize(0)
}
@Test
fun upperCasePackageDirectiveName() {
assertThat(NamingConventionViolation().lint("package FOO.BAR")).hasSize(1)
}
@Test
fun upperCamelCasePackageDirectiveName() {
assertThat(NamingConventionViolation().lint("package Foo.Bar")).hasSize(1)
}
@Test
fun camelCasePackageDirectiveName() {
assertThat(NamingConventionViolation().lint("package fOO.bAR")).hasSize(1)
}
@Test
fun correctPackageDirectiveName() {
assertThat(NamingConventionViolation().lint("package foo.bar")).hasSize(0)
}
@Test
fun uppercaseAndUnderscoreAreAllowedLowercaseNotForEnumEntries() {
val lint = NamingConventionViolation().lint(
"""
enum class WorkFlow {
ACTIVE, NOT_ACTIVE, Unknown
}
"""
)
lint.forEach { println(it.compact()) }
assertThat(lint).hasSize(1)
}
}
class NamingConventionCustomPatter {
private val configCustomRules =
object : Config {
override fun subConfig(key: String): Config = Config.empty
@Suppress("UNCHECKED_CAST")
override fun <T : Any> valueOrDefault(key: String, default: T): T =
when (key) {
NamingConventionViolation.METHOD_PATTERN -> "^`.+`$" as T
NamingConventionViolation.CLASS_PATTERN -> "^aBbD$" as T
NamingConventionViolation.VARIABLE_PATTERN -> "^123var$" as T
NamingConventionViolation.CONSTANT_PATTERN -> "^lowerCaseConst$" as T
NamingConventionViolation.ENUM_PATTERN -> "^(enum1)|(enum2)$" as T
else -> default
}
}
private val config = object : Config {
override fun subConfig(key: String): Config =
if (key == NamingConventionViolation.RULE_SUB_CONFIG) {
configCustomRules
} else {
Config.empty
}
override fun <T : Any> valueOrDefault(key: String, default: T): T = default
}
private val rule = NamingConventionViolation(config)
@Test
fun shouldUseCustomNameForMethodAndClass() {
assertThat(rule.lint("""
class aBbD{
fun `name with back ticks`(){
val 123var = ""
}
companion object {
const val lowerCaseConst = ""
}
}
""")).hasSize(0)
}
@Test
fun shouldUseCustomNameForConstant() {
assertThat(rule.lint("""
class aBbD{
companion object {
const val lowerCaseConst = ""
}
}
""")).hasSize(0)
}
@Test
fun shouldUseCustomNameForEnum() {
assertThat(rule.lint("""
class aBbD{
enum class aBbD {
enum1, enum2
}
}
""")).hasSize(0)
}
}