blob: 20e6ba9f66a6e6f91a5cdc70c14ff5aa655f5f03 [file] [log] [blame]
Aurimas Liutikas5fdc1342023-08-03 17:16:27 -07001/*
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
17package androidx.build.lint
18
19import org.junit.Test
20import org.junit.runner.RunWith
21import org.junit.runners.JUnit4
22
23@RunWith(JUnit4::class)
Omar Ismailedf734f2024-05-28 12:34:57 +010024class 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 Liutikas5fdc1342023-08-03 17:16:27 -070034
Omar Ismailedf734f2024-05-28 12:34:57 +010035 private val fileWithHideInJavadoc =
36 java(
37 """
Aurimas Liutikas5fdc1342023-08-03 17:16:27 -070038/**
39 * @hide
40 */
41public 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 Ismailedf734f2024-05-28 12:34:57 +010053 """
54 .trimIndent()
55 )
Aurimas Liutikas5fdc1342023-08-03 17:16:27 -070056
57 @Test
58 fun `Detection of Hide tag in Javadoc`() {
59 val input = arrayOf(fileWithHideInJavadoc)
60
Omar Ismailedf734f2024-05-28 12:34:57 +010061 val expected =
62 """
Aurimas Liutikas5fdc1342023-08-03 17:16:27 -070063src/HideClass.java:4: Error: @hide is not allowed in documentation [BanHideTag]
64public class HideClass {
65 ~~~~~~~~~
66src/HideClass.java:9: Error: @hide is not allowed in documentation [BanHideTag]
67 public static final int HIDE = 0;
68 ~~~~
69src/HideClass.java:14: Error: @hide is not allowed in documentation [BanHideTag]
70 public static void hide() {}
71 ~~~~
723 errors, 0 warnings
Omar Ismailedf734f2024-05-28 12:34:57 +010073 """
74 .trimIndent()
Aurimas Liutikas5fdc1342023-08-03 17:16:27 -070075
76 check(*input).expect(expected)
77 }
78
Omar Ismailedf734f2024-05-28 12:34:57 +010079 private val fileWithSuppressInKdoc =
80 kotlin(
81 """
Aurimas Liutikas5fdc1342023-08-03 17:16:27 -070082/**
83 * @suppress
84 */
85public class SuppressClass {
86
87 /**
88 * @suppress
89 */
90 public fun suppress() {}
91 /**
92 * @suppress
93 */
94 public val suppressedProperty = 1
95}
Omar Ismailedf734f2024-05-28 12:34:57 +010096 """
97 .trimIndent()
98 )
Aurimas Liutikas5fdc1342023-08-03 17:16:27 -070099
100 @Test
101 fun `Detection of Suppress tag in Kdoc`() {
102 val input = arrayOf(fileWithSuppressInKdoc)
103
Omar Ismailedf734f2024-05-28 12:34:57 +0100104 val expected =
105 """
Aurimas Liutikas5fdc1342023-08-03 17:16:27 -0700106src/SuppressClass.kt:4: Error: @suppress is not allowed in documentation [BanSuppressTag]
107public class SuppressClass {
108 ~~~~~~~~~~~~~
109src/SuppressClass.kt:9: Error: @suppress is not allowed in documentation [BanSuppressTag]
110 public fun suppress() {}
111 ~~~~~~~~
112src/SuppressClass.kt:13: Error: @suppress is not allowed in documentation [BanSuppressTag]
113 public val suppressedProperty = 1
114 ~~~~~~~~~~~~~~~~~~
1153 errors, 0 warnings
Omar Ismailedf734f2024-05-28 12:34:57 +0100116 """
117 .trimIndent()
Aurimas Liutikas5fdc1342023-08-03 17:16:27 -0700118
119 check(*input).expect(expected)
120 }
Julia McClellan54327952024-02-28 15:46:08 -0500121
122 @Test
123 fun `Detection of removed tag`() {
Omar Ismailedf734f2024-05-28 12:34:57 +0100124 val input =
125 arrayOf(
126 kotlin(
127 """
Julia McClellan54327952024-02-28 15:46:08 -0500128 class Foo {
129 /**
130 * A previously useful function.
131 * @removed
132 **/
133 fun foo() = Unit
134 }
Julia McClellan54327952024-02-28 15:46:08 -0500135 """
Omar Ismailedf734f2024-05-28 12:34:57 +0100136 .trimIndent()
137 ),
138 java(
139 """
Julia McClellan54327952024-02-28 15:46:08 -0500140 /**
141 * Bar class
142 * @removed don't use this
143 */
144 public class Bar {
145 /** @removed */
146 public void bar() {}
147 }
Omar Ismailedf734f2024-05-28 12:34:57 +0100148 """
149 .trimIndent()
150 )
Julia McClellan54327952024-02-28 15:46:08 -0500151 )
Julia McClellan54327952024-02-28 15:46:08 -0500152
Omar Ismailedf734f2024-05-28 12:34:57 +0100153 val expected =
154 """
Julia McClellan54327952024-02-28 15:46:08 -0500155 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 Ismailedf734f2024-05-28 12:34:57 +0100165 """
166 .trimIndent()
Julia McClellan54327952024-02-28 15:46:08 -0500167
168 check(*input).expect(expected)
169 }
Aurimas Liutikas5fdc1342023-08-03 17:16:27 -0700170}