Skip to content

Commit a845017

Browse files
committed
Add tests for DesignSystemDetector lint
1 parent a5c030f commit a845017

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright 2023 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.samples.apps.nowinandroid.lint.designsystem
18+
19+
import com.android.tools.lint.checks.infrastructure.TestFile
20+
import com.android.tools.lint.checks.infrastructure.TestFiles.kotlin
21+
import com.android.tools.lint.checks.infrastructure.TestLintTask.lint
22+
import com.google.samples.apps.nowinandroid.lint.designsystem.DesignSystemDetector.Companion.ISSUE
23+
import com.google.samples.apps.nowinandroid.lint.designsystem.DesignSystemDetector.Companion.METHOD_NAMES
24+
import com.google.samples.apps.nowinandroid.lint.designsystem.DesignSystemDetector.Companion.RECEIVER_NAMES
25+
import org.junit.Test
26+
27+
class DesignSystemDetectorTest {
28+
29+
@Test
30+
fun `detect replacements of Composable`() {
31+
lint()
32+
.issues(ISSUE)
33+
.allowMissingSdk()
34+
.files(
35+
COMPOSABLE_STUB,
36+
STUBS,
37+
kotlin(
38+
"""
39+
|import androidx.compose.runtime.Composable
40+
|
41+
|@Composable
42+
|fun App() {
43+
${METHOD_NAMES.keys.joinToString("\n") { "| $it()" }}
44+
|}
45+
""".trimMargin(),
46+
).indented(),
47+
)
48+
.run()
49+
.expect(
50+
"""
51+
src/test.kt:5: Error: Using MaterialTheme instead of NiaTheme [DesignSystem]
52+
MaterialTheme()
53+
~~~~~~~~~~~~~~~
54+
src/test.kt:6: Error: Using Button instead of NiaButton [DesignSystem]
55+
Button()
56+
~~~~~~~~
57+
src/test.kt:7: Error: Using OutlinedButton instead of NiaOutlinedButton [DesignSystem]
58+
OutlinedButton()
59+
~~~~~~~~~~~~~~~~
60+
src/test.kt:8: Error: Using TextButton instead of NiaTextButton [DesignSystem]
61+
TextButton()
62+
~~~~~~~~~~~~
63+
src/test.kt:9: Error: Using FilterChip instead of NiaFilterChip [DesignSystem]
64+
FilterChip()
65+
~~~~~~~~~~~~
66+
src/test.kt:10: Error: Using ElevatedFilterChip instead of NiaFilterChip [DesignSystem]
67+
ElevatedFilterChip()
68+
~~~~~~~~~~~~~~~~~~~~
69+
src/test.kt:11: Error: Using NavigationBar instead of NiaNavigationBar [DesignSystem]
70+
NavigationBar()
71+
~~~~~~~~~~~~~~~
72+
src/test.kt:12: Error: Using NavigationBarItem instead of NiaNavigationBarItem [DesignSystem]
73+
NavigationBarItem()
74+
~~~~~~~~~~~~~~~~~~~
75+
src/test.kt:13: Error: Using NavigationRail instead of NiaNavigationRail [DesignSystem]
76+
NavigationRail()
77+
~~~~~~~~~~~~~~~~
78+
src/test.kt:14: Error: Using NavigationRailItem instead of NiaNavigationRailItem [DesignSystem]
79+
NavigationRailItem()
80+
~~~~~~~~~~~~~~~~~~~~
81+
src/test.kt:15: Error: Using TabRow instead of NiaTabRow [DesignSystem]
82+
TabRow()
83+
~~~~~~~~
84+
src/test.kt:16: Error: Using Tab instead of NiaTab [DesignSystem]
85+
Tab()
86+
~~~~~
87+
src/test.kt:17: Error: Using IconToggleButton instead of NiaIconToggleButton [DesignSystem]
88+
IconToggleButton()
89+
~~~~~~~~~~~~~~~~~~
90+
src/test.kt:18: Error: Using FilledIconToggleButton instead of NiaIconToggleButton [DesignSystem]
91+
FilledIconToggleButton()
92+
~~~~~~~~~~~~~~~~~~~~~~~~
93+
src/test.kt:19: Error: Using FilledTonalIconToggleButton instead of NiaIconToggleButton [DesignSystem]
94+
FilledTonalIconToggleButton()
95+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96+
src/test.kt:20: Error: Using OutlinedIconToggleButton instead of NiaIconToggleButton [DesignSystem]
97+
OutlinedIconToggleButton()
98+
~~~~~~~~~~~~~~~~~~~~~~~~~~
99+
src/test.kt:21: Error: Using CenterAlignedTopAppBar instead of NiaTopAppBar [DesignSystem]
100+
CenterAlignedTopAppBar()
101+
~~~~~~~~~~~~~~~~~~~~~~~~
102+
src/test.kt:22: Error: Using SmallTopAppBar instead of NiaTopAppBar [DesignSystem]
103+
SmallTopAppBar()
104+
~~~~~~~~~~~~~~~~
105+
src/test.kt:23: Error: Using MediumTopAppBar instead of NiaTopAppBar [DesignSystem]
106+
MediumTopAppBar()
107+
~~~~~~~~~~~~~~~~~
108+
src/test.kt:24: Error: Using LargeTopAppBar instead of NiaTopAppBar [DesignSystem]
109+
LargeTopAppBar()
110+
~~~~~~~~~~~~~~~~
111+
20 errors, 0 warnings
112+
""".trimIndent(),
113+
)
114+
}
115+
116+
@Test
117+
fun `detect replacements of Receiver`() {
118+
lint()
119+
.issues(ISSUE)
120+
.allowMissingSdk()
121+
.files(
122+
COMPOSABLE_STUB,
123+
STUBS,
124+
kotlin(
125+
"""
126+
|fun main() {
127+
${RECEIVER_NAMES.keys.joinToString("\n") { "| $it.toString()" }}
128+
|}
129+
""".trimMargin(),
130+
).indented(),
131+
)
132+
.run()
133+
.expect(
134+
"""
135+
src/test.kt:2: Error: Using Icons instead of NiaIcons [DesignSystem]
136+
Icons.toString()
137+
~~~~~~~~~~~~~~~~
138+
1 errors, 0 warnings
139+
""".trimIndent(),
140+
)
141+
}
142+
143+
private companion object {
144+
145+
private val COMPOSABLE_STUB: TestFile = kotlin(
146+
"""
147+
package androidx.compose.runtime
148+
annotation class Composable
149+
""".trimIndent(),
150+
).indented()
151+
152+
private val STUBS: TestFile = kotlin(
153+
"""
154+
|import androidx.compose.runtime.Composable
155+
|
156+
${METHOD_NAMES.keys.joinToString("\n") { "|@Composable fun $it() = {}" }}
157+
${RECEIVER_NAMES.keys.joinToString("\n") { "|object $it" }}
158+
|
159+
""".trimMargin(),
160+
).indented()
161+
}
162+
}

0 commit comments

Comments
 (0)