forked from detekt/detekt
/
InjectDispatcherSpec.kt
126 lines (106 loc) · 4.43 KB
/
InjectDispatcherSpec.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
package io.gitlab.arturbosch.detekt.rules.coroutines
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.rules.KotlinCoreEnvironmentTest
import io.gitlab.arturbosch.detekt.test.TestConfig
import io.gitlab.arturbosch.detekt.test.compileAndLintWithContext
import org.assertj.core.api.Assertions.assertThat
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
@KotlinCoreEnvironmentTest
class InjectDispatcherSpec(val env: KotlinCoreEnvironment) {
@Nested
inner class `InjectDispatcher with default config` {
val subject = InjectDispatcher(Config.empty)
@Test
fun `reports when dispatchers is used inside a function`() {
val code = """
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.Dispatchers
fun useDispatchers() {
runBlocking {
launch(Dispatchers.IO) { }
}
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
@Test
fun `does not report when dispatcher is used as a function parameter`() {
val code = """
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
fun useDispatchers(dispatcher: CoroutineDispatcher = Dispatchers.IO) {
runBlocking {
launch(dispatcher) { }
}
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@Test
fun `does not report when dispatcher is used as a constructor parameter`() {
val code = """
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
class MyRepository(dispatcher: CoroutineDispatcher = Dispatchers.IO)
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@Test
fun `does not report when dispatcher is used as a secondary constructor parameter`() {
val code = """
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
class MyRepository(dispatcher: CoroutineDispatcher) {
constructor() : this(Dispatchers.IO)
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@Test
fun `does not report when dispatcher is used as a property`() {
val code = """
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
class MyRepository(private val dispatcher: CoroutineDispatcher = Dispatchers.IO)
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@Test
fun `does not report when dispatcher main is used`() {
val code = """
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.Dispatchers
fun useDispatchers() {
runBlocking {
launch(Dispatchers.Main) { }
}
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}
@Nested
inner class `InjectDispatcher with config specifying dispatcher names` {
val subject = InjectDispatcher(TestConfig("dispatcherNames" to listOf("Main", "IO", "Default", "Confined")))
@Test
fun `reports when dispatcher main is used`() {
val code = """
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.Dispatchers
fun useDispatchers() {
runBlocking {
launch(Dispatchers.Main) { }
}
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
}
}