forked from detekt/detekt
/
MisusedAlsoSpec.kt
92 lines (80 loc) · 2.34 KB
/
MisusedAlsoSpec.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
package io.gitlab.arturbosch.detekt.rules.style
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.test.assertThat
import io.gitlab.arturbosch.detekt.test.compileAndLint
import org.junit.jupiter.api.Test
class MisusedAlsoSpec {
val subject = MisusedAlso(Config.empty)
@Test
fun `does not report when no also is used`() {
val code = """
class Test {
private var a = 5
init {
a.let {
it.plus(5)
it.minus(10)
}
}
}
""".trimIndent()
assertThat(subject.compileAndLint(code)).isEmpty()
}
@Test
fun `reports an also in init of class`() {
val code = """
class Test {
private var a = 5
init {
a.also {
it.plus(5)
}
}
}
""".trimIndent()
assertThat(subject.compileAndLint(code)).hasSize(1)
}
@Test
fun `reports an also on nullable type`() {
val code = """
class Test {
private var a: Int? = 5
init {
a?.also {
it.plus(5)
}
}
}
""".trimIndent()
assertThat(subject.compileAndLint(code)).hasSize(1)
}
@Test
fun `reports an also with lambda passed as Argument in parenthesis`() {
val code = """
class Test {
private var a: Int? = 5
init {
a?.also({
it.plus(5)
})
}
}
""".trimIndent()
assertThat(subject.compileAndLint(code)).hasSize(1)
}
@Test
fun `does not report if it is not used in also`() {
val code = """
class Test {
private var a: Int? = 5
private var b: Int = 0
init {
a?.also({
b.plus(5)
})
}
}
""".trimIndent()
assertThat(subject.compileAndLint(code)).isEmpty()
}
}