forked from detekt/detekt
/
CouldBeSequenceSpec.kt
69 lines (63 loc) · 2.01 KB
/
CouldBeSequenceSpec.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
package io.gitlab.arturbosch.detekt.rules.performance
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.Test
@KotlinCoreEnvironmentTest
class CouldBeSequenceSpec(val env: KotlinCoreEnvironment) {
val subject = CouldBeSequence(TestConfig(mapOf("threshold" to 3)))
@Test
fun `long collection chain should be sequence`() {
val code = """
val myCollection = listOf(1, 2, 3, 4, 5, 6, 7, 8)
val processed = myCollection.filter {
it % 2 == 0
}.map {
it*2
}.filter {
it > 5
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).hasSize(1)
}
@Test
fun `one collection operation should not trigger rule`() {
val code = """
val myCollection = listOf(1, 2, 3, 4, 5, 6, 7, 8)
val processed = myCollection.filter {
it % 2 == 0
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@Test
fun `sequence operations should not trigger rule`() {
val code = """
val myCollection = listOf(1, 2, 3, 4, 5, 6, 7, 8)
val processed = myCollection.asSequence().filter {
it % 2 == 0
}.map {
it*2
}.filter {
it > 5
}
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
@Test
fun `sequence should not trigger rule`() {
val code = """
val mySequence = sequenceOf(1,10,4,6,8,39)
val processed = mySequence.filter {
it % 2 == 0
}.map {
it*2
}.filter {
it > 5
}.toList()
"""
assertThat(subject.compileAndLintWithContext(env, code)).isEmpty()
}
}