blob: 62a940425e00de01d998dc0bce5bdb4db67095ef [file] [log] [blame]
Tiem Song84678102022-02-17 10:03:42 -08001/*
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
19package androidx.build.lint
20
21import org.junit.Test
22import org.junit.runner.RunWith
23import org.junit.runners.JUnit4
24
25@RunWith(JUnit4::class)
Omar Ismailedf734f2024-05-28 12:34:57 +010026class BanInlineOptInTest :
27 AbstractLintDetectorTest(
28 useDetector = BanInlineOptIn(),
29 useIssues = listOf(BanInlineOptIn.ISSUE),
30 ) {
Tiem Song84678102022-02-17 10:03:42 -080031
32 @Test
33 fun `Detect inline function with an OptIn annotation`() {
Omar Ismailedf734f2024-05-28 12:34:57 +010034 val input =
35 kotlin(
36 """
Tiem Song84678102022-02-17 10:03:42 -080037@RequiresOptIn
38annotation class ExperimentalSampleAnnotation
39
40@OptIn(ExperimentalSampleAnnotation::class)
41inline fun String.myInlineFun() = this.length
Omar Ismailedf734f2024-05-28 12:34:57 +010042 """
43 .trimIndent()
44 )
Tiem Song84678102022-02-17 10:03:42 -080045
Omar Ismailedf734f2024-05-28 12:34:57 +010046 val expected =
47 """
Tiem Song84678102022-02-17 10:03:42 -080048src/ExperimentalSampleAnnotation.kt:5: Error: Inline functions cannot opt into experimental APIs. [BanInlineOptIn]
49inline fun String.myInlineFun() = this.length
50 ~~~~~~~~~~~
511 errors, 0 warnings
Omar Ismailedf734f2024-05-28 12:34:57 +010052 """
53 .trimIndent()
Tiem Song84678102022-02-17 10:03:42 -080054
55 check(input).expect(expected)
56 }
57
58 @Test
59 fun `Detect inline function without an OptIn annotation`() {
Omar Ismailedf734f2024-05-28 12:34:57 +010060 val input =
61 kotlin(
62 """
Tiem Song84678102022-02-17 10:03:42 -080063inline fun String.myInlineFun() = this.length
Omar Ismailedf734f2024-05-28 12:34:57 +010064 """
65 .trimIndent()
66 )
Tiem Song84678102022-02-17 10:03:42 -080067
68 check(input).expectClean()
69 }
70}