-
Notifications
You must be signed in to change notification settings - Fork 89
/
ComposeMultipleContentEmittersCheckTest.kt
147 lines (139 loc) · 4.43 KB
/
ComposeMultipleContentEmittersCheckTest.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
// Copyright 2022 Twitter, Inc.
// SPDX-License-Identifier: Apache-2.0
package com.twitter.compose.rules.detekt
import com.twitter.compose.rules.ComposeMultipleContentEmitters
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.lint
import org.intellij.lang.annotations.Language
import org.junit.jupiter.api.Test
class ComposeMultipleContentEmittersCheckTest {
private val testConfig = TestConfig(
"contentEmitters" to listOf("Potato", "Banana")
)
private val rule = ComposeMultipleContentEmittersCheck(testConfig)
@Test
fun `passes when only one item emits up at the top level`() {
@Language("kotlin")
val code =
"""
@Composable
fun Something() {
val something = rememberWhatever()
Column {
Text("Hi")
Text("Hola")
}
LaunchedEffect(Unit) {
}
}
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors).isEmpty()
}
@Test
fun `passes when the composable is an extension function`() {
@Language("kotlin")
val code =
"""
@Composable
fun ColumnScope.Something() {
Text("Hi")
Text("Hola")
}
@Composable
fun RowScope.Something() {
Spacer16()
Text("Hola")
}
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors).isEmpty()
}
@Test
fun `errors when a Composable function has more than one UI emitter at the top level`() {
@Language("kotlin")
val code =
"""
@Composable
fun Something() {
Text("Hi")
Text("Hola")
}
@Composable
fun Something() {
Spacer16()
Text("Hola")
}
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors).hasSize(2)
.hasStartSourceLocations(
SourceLocation(2, 5),
SourceLocation(7, 5)
)
for (error in errors) {
assertThat(error).hasMessage(ComposeMultipleContentEmitters.MultipleContentEmittersDetected)
}
}
@Test
fun `errors when a Composable function has more than one indirect UI emitter at the top level`() {
@Language("kotlin")
val code =
"""
@Composable
fun Something1() {
Something2()
}
@Composable
fun Something2() {
Text("Hola")
Something3()
}
@Composable
fun Something3() {
Potato()
}
@Composable
fun Something4() {
Banana()
}
@Composable
fun Something5() {
Something3()
Something4()
}
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors).hasSize(2)
.hasStartSourceLocations(
SourceLocation(6, 5),
SourceLocation(19, 5)
)
for (error in errors) {
assertThat(error).hasMessage(ComposeMultipleContentEmitters.MultipleContentEmittersDetected)
}
}
@Test
fun `make sure to not report twice the same composable`() {
@Language("kotlin")
val code =
"""
@Composable
fun Something() {
Text("Hi")
Text("Hola")
Something2()
}
@Composable
fun Something2() {
Text("Alo")
}
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors).hasSize(1)
.hasStartSourceLocation(2, 5)
assertThat(errors.first()).hasMessage(ComposeMultipleContentEmitters.MultipleContentEmittersDetected)
}
}