-
-
Notifications
You must be signed in to change notification settings - Fork 757
/
ProtectedMemberInFinalClassSpec.kt
224 lines (204 loc) · 7.27 KB
/
ProtectedMemberInFinalClassSpec.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.SourceLocation
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
class ProtectedMemberInFinalClassSpec {
val subject = ProtectedMemberInFinalClass(Config.empty)
@Nested
inner class `check all variants of protected visibility modifier in final class` {
@Test
fun `reports a protected field in a final class`() {
val code = """
class Foo {
protected var i1 = 0
}
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(2, 5)
}
@Test
fun `reports a protected constructor in a final class`() {
val code = """
class Foo {
var i1: Int = 0
protected constructor(i1: Int) : super() {
this.i1 = i1
}
}
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(3, 5)
}
@Test
fun `reports a protected function in a final class`() {
val code = """
class Foo {
protected fun function() {}
}
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(2, 5)
}
@Test
fun `reports an inner class with a protected field in a final class`() {
val code = """
class Foo {
inner class InnerClass2 {
protected val i = 0
}
}
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(3, 9)
}
@Test
fun `reports a protected inner class with a protected field in a final class`() {
val code = """
class Fee {
protected inner class YetAnotherInnerClass {
protected val i = 0
}
}
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(2)
assertThat(findings).hasStartSourceLocations(
SourceLocation(2, 5),
SourceLocation(3, 9)
)
}
@Test
fun `reports a protected companion object in a final class`() {
val code = """
class Foo {
protected companion object {
protected class A {
protected var x = 0
}
}
}
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(3)
assertThat(findings).hasStartSourceLocations(
SourceLocation(2, 5),
SourceLocation(2, 5),
SourceLocation(4, 13)
)
}
@Test
fun `reports a protected companion object in an nested class`() {
val code = """
abstract class Foo {
protected companion object {
protected class A {
protected var x = 0
}
}
}
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(4, 13)
}
@Test
fun `reports a protected field object in a final inner class`() {
val code = """
open class OpenClass {
inner class InnerClass {
protected val i = 0
}
}
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(3, 9)
}
@Test
fun `reports a protected primary constructor in a final class`() {
val code = """
class FinalClassWithProtectedConstructor protected constructor()
"""
val findings = subject.compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSourceLocation(1, 42)
}
}
@Nested
inner class `check valid occurrences of protected that should not be reported` {
@Test
fun `does not report non-protected members in final class`() {
val code = """
abstract class BaseClass
class Foo : BaseClass() {
private val i = 0
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
@Test
fun `does not report overridden fields`() {
val code = """
abstract class BaseClass {
protected abstract val abstractProp : Int
}
class Foo : BaseClass() {
// should not report protected = private visibility
protected override val abstractProp = 0
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
@Test
fun `does not report overridden functions`() {
val code = """
abstract class BaseClass {
protected abstract fun abstractFunction()
}
class Foo : BaseClass() {
// should not report protected = private visibility
protected override fun abstractFunction() {
}
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
@Test
fun `does not report protected definitions in abstract class`() {
val code = """
abstract class BaseClass {
protected abstract val abstractProp: Int
protected abstract fun abstractFunction()
protected object InnerObject
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
@Test
fun `does not report protected definitions in sealed class`() {
val code = """
sealed class SealedClass {
protected fun a() {}
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
@Test
fun `does not report protected definitions in enum class`() {
val code = """
enum class EnumClass {
;
protected fun foo() {}
}
"""
assertThat(subject.compileAndLint(code)).isEmpty()
}
}
}