|
| 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 | + @Suppress("LintImplTrimIndent") |
| 38 | + kotlin( |
| 39 | + """ |
| 40 | + |import androidx.compose.runtime.Composable |
| 41 | + | |
| 42 | + |@Composable |
| 43 | + |fun App() { |
| 44 | + ${METHOD_NAMES.keys.joinToString("\n") { "| $it()" }} |
| 45 | + |} |
| 46 | + """.trimMargin(), |
| 47 | + ).indented(), |
| 48 | + ) |
| 49 | + .run() |
| 50 | + .expect( |
| 51 | + """ |
| 52 | + src/test.kt:5: Error: Using MaterialTheme instead of NiaTheme [DesignSystem] |
| 53 | + MaterialTheme() |
| 54 | + ~~~~~~~~~~~~~~~ |
| 55 | + src/test.kt:6: Error: Using Button instead of NiaButton [DesignSystem] |
| 56 | + Button() |
| 57 | + ~~~~~~~~ |
| 58 | + src/test.kt:7: Error: Using OutlinedButton instead of NiaOutlinedButton [DesignSystem] |
| 59 | + OutlinedButton() |
| 60 | + ~~~~~~~~~~~~~~~~ |
| 61 | + src/test.kt:8: Error: Using TextButton instead of NiaTextButton [DesignSystem] |
| 62 | + TextButton() |
| 63 | + ~~~~~~~~~~~~ |
| 64 | + src/test.kt:9: Error: Using FilterChip instead of NiaFilterChip [DesignSystem] |
| 65 | + FilterChip() |
| 66 | + ~~~~~~~~~~~~ |
| 67 | + src/test.kt:10: Error: Using ElevatedFilterChip instead of NiaFilterChip [DesignSystem] |
| 68 | + ElevatedFilterChip() |
| 69 | + ~~~~~~~~~~~~~~~~~~~~ |
| 70 | + src/test.kt:11: Error: Using NavigationBar instead of NiaNavigationBar [DesignSystem] |
| 71 | + NavigationBar() |
| 72 | + ~~~~~~~~~~~~~~~ |
| 73 | + src/test.kt:12: Error: Using NavigationBarItem instead of NiaNavigationBarItem [DesignSystem] |
| 74 | + NavigationBarItem() |
| 75 | + ~~~~~~~~~~~~~~~~~~~ |
| 76 | + src/test.kt:13: Error: Using NavigationRail instead of NiaNavigationRail [DesignSystem] |
| 77 | + NavigationRail() |
| 78 | + ~~~~~~~~~~~~~~~~ |
| 79 | + src/test.kt:14: Error: Using NavigationRailItem instead of NiaNavigationRailItem [DesignSystem] |
| 80 | + NavigationRailItem() |
| 81 | + ~~~~~~~~~~~~~~~~~~~~ |
| 82 | + src/test.kt:15: Error: Using TabRow instead of NiaTabRow [DesignSystem] |
| 83 | + TabRow() |
| 84 | + ~~~~~~~~ |
| 85 | + src/test.kt:16: Error: Using Tab instead of NiaTab [DesignSystem] |
| 86 | + Tab() |
| 87 | + ~~~~~ |
| 88 | + src/test.kt:17: Error: Using IconToggleButton instead of NiaIconToggleButton [DesignSystem] |
| 89 | + IconToggleButton() |
| 90 | + ~~~~~~~~~~~~~~~~~~ |
| 91 | + src/test.kt:18: Error: Using FilledIconToggleButton instead of NiaIconToggleButton [DesignSystem] |
| 92 | + FilledIconToggleButton() |
| 93 | + ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 94 | + src/test.kt:19: Error: Using FilledTonalIconToggleButton instead of NiaIconToggleButton [DesignSystem] |
| 95 | + FilledTonalIconToggleButton() |
| 96 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 97 | + src/test.kt:20: Error: Using OutlinedIconToggleButton instead of NiaIconToggleButton [DesignSystem] |
| 98 | + OutlinedIconToggleButton() |
| 99 | + ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 100 | + src/test.kt:21: Error: Using CenterAlignedTopAppBar instead of NiaTopAppBar [DesignSystem] |
| 101 | + CenterAlignedTopAppBar() |
| 102 | + ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 103 | + src/test.kt:22: Error: Using SmallTopAppBar instead of NiaTopAppBar [DesignSystem] |
| 104 | + SmallTopAppBar() |
| 105 | + ~~~~~~~~~~~~~~~~ |
| 106 | + src/test.kt:23: Error: Using MediumTopAppBar instead of NiaTopAppBar [DesignSystem] |
| 107 | + MediumTopAppBar() |
| 108 | + ~~~~~~~~~~~~~~~~~ |
| 109 | + src/test.kt:24: Error: Using LargeTopAppBar instead of NiaTopAppBar [DesignSystem] |
| 110 | + LargeTopAppBar() |
| 111 | + ~~~~~~~~~~~~~~~~ |
| 112 | + 20 errors, 0 warnings |
| 113 | + """.trimIndent(), |
| 114 | + ) |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + fun `detect replacements of Receiver`() { |
| 119 | + lint() |
| 120 | + .issues(ISSUE) |
| 121 | + .allowMissingSdk() |
| 122 | + .files( |
| 123 | + COMPOSABLE_STUB, |
| 124 | + STUBS, |
| 125 | + @Suppress("LintImplTrimIndent") |
| 126 | + kotlin( |
| 127 | + """ |
| 128 | + |fun main() { |
| 129 | + ${RECEIVER_NAMES.keys.joinToString("\n") { "| $it.toString()" }} |
| 130 | + |} |
| 131 | + """.trimMargin(), |
| 132 | + ).indented(), |
| 133 | + ) |
| 134 | + .run() |
| 135 | + .expect( |
| 136 | + """ |
| 137 | + src/test.kt:2: Error: Using Icons instead of NiaIcons [DesignSystem] |
| 138 | + Icons.toString() |
| 139 | + ~~~~~~~~~~~~~~~~ |
| 140 | + 1 errors, 0 warnings |
| 141 | + """.trimIndent(), |
| 142 | + ) |
| 143 | + } |
| 144 | + |
| 145 | + private companion object { |
| 146 | + |
| 147 | + private val COMPOSABLE_STUB: TestFile = kotlin( |
| 148 | + """ |
| 149 | + package androidx.compose.runtime |
| 150 | + annotation class Composable |
| 151 | + """.trimIndent(), |
| 152 | + ).indented() |
| 153 | + |
| 154 | + private val STUBS: TestFile = kotlin( |
| 155 | + """ |
| 156 | + |import androidx.compose.runtime.Composable |
| 157 | + | |
| 158 | + ${METHOD_NAMES.keys.joinToString("\n") { "|@Composable fun $it() = {}" }} |
| 159 | + ${RECEIVER_NAMES.keys.joinToString("\n") { "|object $it" }} |
| 160 | + | |
| 161 | + """.trimMargin(), |
| 162 | + ).indented() |
| 163 | + } |
| 164 | +} |
0 commit comments