Aurimas Liutikas | 5fdc134 | 2023-08-03 17:16:27 -0700 | [diff] [blame] | 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 | * http://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 androidx.build.lint |
| 18 | |
| 19 | import org.junit.Test |
| 20 | import org.junit.runner.RunWith |
| 21 | import org.junit.runners.JUnit4 |
| 22 | |
| 23 | @RunWith(JUnit4::class) |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 24 | class BanVisibilityDocTagsTest : |
| 25 | AbstractLintDetectorTest( |
| 26 | useDetector = BanVisibilityDocTags(), |
| 27 | useIssues = |
| 28 | listOf( |
| 29 | BanVisibilityDocTags.HIDE_ISSUE, |
| 30 | BanVisibilityDocTags.SUPPRESS_ISSUE, |
| 31 | BanVisibilityDocTags.REMOVED_ISSUE, |
| 32 | ), |
| 33 | ) { |
Aurimas Liutikas | 5fdc134 | 2023-08-03 17:16:27 -0700 | [diff] [blame] | 34 | |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 35 | private val fileWithHideInJavadoc = |
| 36 | java( |
| 37 | """ |
Aurimas Liutikas | 5fdc134 | 2023-08-03 17:16:27 -0700 | [diff] [blame] | 38 | /** |
| 39 | * @hide |
| 40 | */ |
| 41 | public class HideClass { |
| 42 | |
| 43 | /** |
| 44 | * @hide |
| 45 | */ |
| 46 | public static final int HIDE = 0; |
| 47 | |
| 48 | /** |
| 49 | * @hide |
| 50 | */ |
| 51 | public static void hide() {} |
| 52 | } |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 53 | """ |
| 54 | .trimIndent() |
| 55 | ) |
Aurimas Liutikas | 5fdc134 | 2023-08-03 17:16:27 -0700 | [diff] [blame] | 56 | |
| 57 | @Test |
| 58 | fun `Detection of Hide tag in Javadoc`() { |
| 59 | val input = arrayOf(fileWithHideInJavadoc) |
| 60 | |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 61 | val expected = |
| 62 | """ |
Aurimas Liutikas | 5fdc134 | 2023-08-03 17:16:27 -0700 | [diff] [blame] | 63 | src/HideClass.java:4: Error: @hide is not allowed in documentation [BanHideTag] |
| 64 | public class HideClass { |
| 65 | ~~~~~~~~~ |
| 66 | src/HideClass.java:9: Error: @hide is not allowed in documentation [BanHideTag] |
| 67 | public static final int HIDE = 0; |
| 68 | ~~~~ |
| 69 | src/HideClass.java:14: Error: @hide is not allowed in documentation [BanHideTag] |
| 70 | public static void hide() {} |
| 71 | ~~~~ |
| 72 | 3 errors, 0 warnings |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 73 | """ |
| 74 | .trimIndent() |
Aurimas Liutikas | 5fdc134 | 2023-08-03 17:16:27 -0700 | [diff] [blame] | 75 | |
| 76 | check(*input).expect(expected) |
| 77 | } |
| 78 | |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 79 | private val fileWithSuppressInKdoc = |
| 80 | kotlin( |
| 81 | """ |
Aurimas Liutikas | 5fdc134 | 2023-08-03 17:16:27 -0700 | [diff] [blame] | 82 | /** |
| 83 | * @suppress |
| 84 | */ |
| 85 | public class SuppressClass { |
| 86 | |
| 87 | /** |
| 88 | * @suppress |
| 89 | */ |
| 90 | public fun suppress() {} |
| 91 | /** |
| 92 | * @suppress |
| 93 | */ |
| 94 | public val suppressedProperty = 1 |
| 95 | } |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 96 | """ |
| 97 | .trimIndent() |
| 98 | ) |
Aurimas Liutikas | 5fdc134 | 2023-08-03 17:16:27 -0700 | [diff] [blame] | 99 | |
| 100 | @Test |
| 101 | fun `Detection of Suppress tag in Kdoc`() { |
| 102 | val input = arrayOf(fileWithSuppressInKdoc) |
| 103 | |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 104 | val expected = |
| 105 | """ |
Aurimas Liutikas | 5fdc134 | 2023-08-03 17:16:27 -0700 | [diff] [blame] | 106 | src/SuppressClass.kt:4: Error: @suppress is not allowed in documentation [BanSuppressTag] |
| 107 | public class SuppressClass { |
| 108 | ~~~~~~~~~~~~~ |
| 109 | src/SuppressClass.kt:9: Error: @suppress is not allowed in documentation [BanSuppressTag] |
| 110 | public fun suppress() {} |
| 111 | ~~~~~~~~ |
| 112 | src/SuppressClass.kt:13: Error: @suppress is not allowed in documentation [BanSuppressTag] |
| 113 | public val suppressedProperty = 1 |
| 114 | ~~~~~~~~~~~~~~~~~~ |
| 115 | 3 errors, 0 warnings |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 116 | """ |
| 117 | .trimIndent() |
Aurimas Liutikas | 5fdc134 | 2023-08-03 17:16:27 -0700 | [diff] [blame] | 118 | |
| 119 | check(*input).expect(expected) |
| 120 | } |
Julia McClellan | 5432795 | 2024-02-28 15:46:08 -0500 | [diff] [blame] | 121 | |
| 122 | @Test |
| 123 | fun `Detection of removed tag`() { |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 124 | val input = |
| 125 | arrayOf( |
| 126 | kotlin( |
| 127 | """ |
Julia McClellan | 5432795 | 2024-02-28 15:46:08 -0500 | [diff] [blame] | 128 | class Foo { |
| 129 | /** |
| 130 | * A previously useful function. |
| 131 | * @removed |
| 132 | **/ |
| 133 | fun foo() = Unit |
| 134 | } |
Julia McClellan | 5432795 | 2024-02-28 15:46:08 -0500 | [diff] [blame] | 135 | """ |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 136 | .trimIndent() |
| 137 | ), |
| 138 | java( |
| 139 | """ |
Julia McClellan | 5432795 | 2024-02-28 15:46:08 -0500 | [diff] [blame] | 140 | /** |
| 141 | * Bar class |
| 142 | * @removed don't use this |
| 143 | */ |
| 144 | public class Bar { |
| 145 | /** @removed */ |
| 146 | public void bar() {} |
| 147 | } |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 148 | """ |
| 149 | .trimIndent() |
| 150 | ) |
Julia McClellan | 5432795 | 2024-02-28 15:46:08 -0500 | [diff] [blame] | 151 | ) |
Julia McClellan | 5432795 | 2024-02-28 15:46:08 -0500 | [diff] [blame] | 152 | |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 153 | val expected = |
| 154 | """ |
Julia McClellan | 5432795 | 2024-02-28 15:46:08 -0500 | [diff] [blame] | 155 | src/Bar.java:5: Error: @removed is not allowed in documentation [BanRemovedTag] |
| 156 | public class Bar { |
| 157 | ~~~ |
| 158 | src/Bar.java:7: Error: @removed is not allowed in documentation [BanRemovedTag] |
| 159 | public void bar() {} |
| 160 | ~~~ |
| 161 | src/Foo.kt:6: Error: @removed is not allowed in documentation [BanRemovedTag] |
| 162 | fun foo() = Unit |
| 163 | ~~~ |
| 164 | 3 errors, 0 warnings |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 165 | """ |
| 166 | .trimIndent() |
Julia McClellan | 5432795 | 2024-02-28 15:46:08 -0500 | [diff] [blame] | 167 | |
| 168 | check(*input).expect(expected) |
| 169 | } |
Aurimas Liutikas | 5fdc134 | 2023-08-03 17:16:27 -0700 | [diff] [blame] | 170 | } |