-
Notifications
You must be signed in to change notification settings - Fork 89
/
ComposePreviewNamingCheckTest.kt
95 lines (88 loc) · 2.84 KB
/
ComposePreviewNamingCheckTest.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
// Copyright 2022 Twitter, Inc.
// SPDX-License-Identifier: Apache-2.0
package com.twitter.compose.rules.detekt
import com.twitter.compose.rules.ComposePreviewNaming
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.api.Test
class ComposePreviewNamingCheckTest {
private val rule = ComposePreviewNamingCheck(Config.empty)
@Test
fun `passes for non-preview annotations`() {
@Language("kotlin")
val code =
"""
annotation class Banana
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors).isEmpty()
}
@Test
fun `passes for preview annotations with the proper names`() {
@Language("kotlin")
val code =
"""
@Preview
annotation class BananaPreview
@BananaPreview
annotation class DoubleBananaPreview
@Preview
@Preview
annotation class ApplePreviews
@Preview
@ApplePreviews
annotation class CombinedApplePreviews
@BananaPreview
@ApplePreviews
annotation class FruitBasketPreviews
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors).isEmpty()
}
@Test
fun `errors when a multipreview annotation is not correctly named for 1 preview`() {
@Language("kotlin")
val code =
"""
@Preview
annotation class Banana
@Preview
annotation class BananaPreviews
@BananaPreview
annotation class WithBananaPreviews
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors).hasStartSourceLocations(
SourceLocation(2, 18),
SourceLocation(4, 18),
SourceLocation(6, 18)
)
for (error in errors) {
assertThat(error).hasMessage(ComposePreviewNaming.createMessage(1, "Preview"))
}
}
@Test
fun `errors when a multipreview annotation is not correctly named for multi previews`() {
@Language("kotlin")
val code =
"""
@Preview
@Preview
annotation class BananaPreview
@BananaPreview
@BananaPreview
annotation class BananaPreview
""".trimIndent()
val errors = rule.lint(code)
assertThat(errors).hasStartSourceLocations(
SourceLocation(3, 18),
SourceLocation(6, 18)
)
for (error in errors) {
assertThat(error).hasMessage(ComposePreviewNaming.createMessage(2, "Previews"))
}
}
}