-
-
Notifications
You must be signed in to change notification settings - Fork 757
/
FunctionNamingSpec.kt
112 lines (100 loc) · 3.4 KB
/
FunctionNamingSpec.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
package io.gitlab.arturbosch.detekt.rules.naming
import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.junit.jupiter.api.Test
class FunctionNamingSpec {
@Test
fun `allows FunctionName as alias for suppressing`() {
val code = """
@Suppress("FunctionName")
fun MY_FUN() {}
"""
assertThat(FunctionNaming().compileAndLint(code)).isEmpty()
}
@Test
fun `allows anonymous functions`() {
val code = """
val f: (Int) -> Int = fun(i: Int): Int {
return i + i
}
"""
assertThat(FunctionNaming().compileAndLint(code)).isEmpty()
}
@Test
fun `ignores functions in classes matching excludeClassPattern`() {
val code = """
class WhateverTest {
fun SHOULD_NOT_BE_FLAGGED() {}
}
"""
val config = TestConfig(mapOf(FunctionNaming.EXCLUDE_CLASS_PATTERN to ".*Test$"))
assertThat(FunctionNaming(config).compileAndLint(code)).isEmpty()
}
@Test
fun `flags functions inside functions`() {
val code = """
class C : I {
override fun shouldNotBeFlagged() {
fun SHOULD_BE_FLAGGED() { }
}
}
interface I { fun shouldNotBeFlagged() }
"""
assertThat(FunctionNaming().compileAndLint(code)).hasSourceLocation(3, 13)
}
@Test
fun `ignores overridden functions by default`() {
val code = """
class C : I {
override fun SHOULD_NOT_BE_FLAGGED() {}
}
interface I { @Suppress("FunctionNaming") fun SHOULD_NOT_BE_FLAGGED() }
"""
assertThat(FunctionNaming().compileAndLint(code)).isEmpty()
}
@Test
fun `does not report when the function name is identical to the type of the result`() {
val code = """
interface Foo
private class FooImpl : Foo
fun Foo(): Foo = FooImpl()
"""
val config = TestConfig(mapOf(FunctionNaming.IGNORE_OVERRIDDEN to "false"))
assertThat(FunctionNaming(config).compileAndLint(code)).isEmpty()
}
@Test
fun `flags functions with bad names inside overridden functions by default`() {
val code = """
class C : I {
override fun SHOULD_BE_FLAGGED() {
fun SHOULD_BE_FLAGGED() {}
}
}
interface I { @Suppress("FunctionNaming") fun SHOULD_BE_FLAGGED() }
"""
assertThat(FunctionNaming().compileAndLint(code)).hasSourceLocation(3, 13)
}
@Test
fun `doesn't ignore overridden functions if ignoreOverridden is false`() {
val code = """
class C : I {
override fun SHOULD_BE_FLAGGED() {}
}
interface I { fun SHOULD_BE_FLAGGED() }
"""
val config = TestConfig(mapOf(FunctionNaming.IGNORE_OVERRIDDEN to "false"))
assertThat(FunctionNaming(config).compileAndLint(code)).hasStartSourceLocations(
SourceLocation(2, 18),
SourceLocation(4, 19)
)
}
@Test
fun `doesn't allow functions with backtick`() {
val code = """
fun `7his is a function name _`() = Unit
"""
assertThat(FunctionNaming().compileAndLint(code)).hasStartSourceLocations(SourceLocation(1, 5))
}
}