-
Notifications
You must be signed in to change notification settings - Fork 89
/
ComposeViewModelInjectionCheckTest.kt
106 lines (99 loc) · 3.75 KB
/
ComposeViewModelInjectionCheckTest.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
// Copyright 2022 Twitter, Inc.
// SPDX-License-Identifier: Apache-2.0
package com.twitter.compose.rules.detekt
import com.twitter.compose.rules.ComposeViewModelInjection
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.lint
import org.intellij.lang.annotations.Language
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
class ComposeViewModelInjectionCheckTest {
private val rule = ComposeViewModelInjectionCheck(Config.empty)
@ParameterizedTest
@ValueSource(strings = ["viewModel", "weaverViewModel", "hiltViewModel", "injectedViewModel", "mavericksViewModel"])
fun `passes when a weaverViewModel is used as a default param`(viewModel: String) {
@Language("kotlin")
val code =
"""
@Composable
fun MyComposable(
modifier: Modifier,
viewModel: MyVM = $viewModel(),
viewModel2: MyVM = $viewModel(),
) { }
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors).isEmpty()
}
@ParameterizedTest
@ValueSource(strings = ["viewModel", "weaverViewModel", "hiltViewModel", "injectedViewModel", "mavericksViewModel"])
fun `overridden functions are ignored`(viewModel: String) {
@Language("kotlin")
val code =
"""
@Composable
override fun Content() {
val viewModel = $viewModel<MyVM>()
}
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors).isEmpty()
}
@ParameterizedTest
@ValueSource(strings = ["viewModel", "weaverViewModel", "hiltViewModel", "injectedViewModel", "mavericksViewModel"])
fun `errors when a weaverViewModel is used at the beginning of a Composable`(viewModel: String) {
@Language("kotlin")
val code =
"""
@Composable
fun MyComposable(modifier: Modifier) {
val viewModel = $viewModel<MyVM>()
}
@Composable
fun MyComposableNoParams() {
val viewModel: MyVM = $viewModel()
}
@Composable
fun MyComposableTrailingLambda(block: () -> Unit) {
val viewModel: MyVM = $viewModel()
}
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors).hasSize(3)
.hasStartSourceLocations(
SourceLocation(3, 9),
SourceLocation(7, 9),
SourceLocation(11, 9)
)
for (error in errors) {
assertThat(error).hasMessage(ComposeViewModelInjection.errorMessage(viewModel))
}
}
@ParameterizedTest
@ValueSource(strings = ["viewModel", "weaverViewModel", "hiltViewModel", "injectedViewModel", "mavericksViewModel"])
fun `errors when a weaverViewModel is used in different branches`(viewModel: String) {
@Language("kotlin")
val code =
"""
@Composable
fun MyComposable(modifier: Modifier) {
if (blah) {
val viewModel = $viewModel<MyVM>()
} else {
val viewModel: MyOtherVM = $viewModel()
}
}
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors).hasSize(2)
.hasStartSourceLocations(
SourceLocation(4, 13),
SourceLocation(6, 13)
)
for (error in errors) {
assertThat(error).hasMessage(ComposeViewModelInjection.errorMessage(viewModel))
}
}
}