1 /*
2  * Copyright 2021 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 @file:Suppress("UnstableApiUsage")
18 
19 package androidx.build.lint
20 
21 import org.junit.Test
22 import org.junit.runner.RunWith
23 import org.junit.runners.JUnit4
24 
25 @RunWith(JUnit4::class)
26 class BanInlineOptInTest :
27     AbstractLintDetectorTest(
28         useDetector = BanInlineOptIn(),
29         useIssues = listOf(BanInlineOptIn.ISSUE),
30     ) {
31 
32     @Test
Detect inline function with an OptIn annotationnull33     fun `Detect inline function with an OptIn annotation`() {
34         val input =
35             kotlin(
36                 """
37 @RequiresOptIn
38 annotation class ExperimentalSampleAnnotation
39 
40 @OptIn(ExperimentalSampleAnnotation::class)
41 inline fun String.myInlineFun() = this.length
42             """
43                     .trimIndent()
44             )
45 
46         val expected =
47             """
48 src/ExperimentalSampleAnnotation.kt:5: Error: Inline functions cannot opt into experimental APIs. [BanInlineOptIn]
49 inline fun String.myInlineFun() = this.length
50                   ~~~~~~~~~~~
51 1 errors, 0 warnings
52           """
53                 .trimIndent()
54 
55         check(input).expect(expected)
56     }
57 
58     @Test
Detect inline function without an OptIn annotationnull59     fun `Detect inline function without an OptIn annotation`() {
60         val input =
61             kotlin(
62                 """
63 inline fun String.myInlineFun() = this.length
64             """
65                     .trimIndent()
66             )
67 
68         check(input).expectClean()
69     }
70 }
71