Tiem Song | 8467810 | 2022-02-17 10:03:42 -0800 | [diff] [blame] | 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) |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 26 | class BanInlineOptInTest : |
| 27 | AbstractLintDetectorTest( |
| 28 | useDetector = BanInlineOptIn(), |
| 29 | useIssues = listOf(BanInlineOptIn.ISSUE), |
| 30 | ) { |
Tiem Song | 8467810 | 2022-02-17 10:03:42 -0800 | [diff] [blame] | 31 | |
| 32 | @Test |
| 33 | fun `Detect inline function with an OptIn annotation`() { |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 34 | val input = |
| 35 | kotlin( |
| 36 | """ |
Tiem Song | 8467810 | 2022-02-17 10:03:42 -0800 | [diff] [blame] | 37 | @RequiresOptIn |
| 38 | annotation class ExperimentalSampleAnnotation |
| 39 | |
| 40 | @OptIn(ExperimentalSampleAnnotation::class) |
| 41 | inline fun String.myInlineFun() = this.length |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 42 | """ |
| 43 | .trimIndent() |
| 44 | ) |
Tiem Song | 8467810 | 2022-02-17 10:03:42 -0800 | [diff] [blame] | 45 | |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 46 | val expected = |
| 47 | """ |
Tiem Song | 8467810 | 2022-02-17 10:03:42 -0800 | [diff] [blame] | 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 |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 52 | """ |
| 53 | .trimIndent() |
Tiem Song | 8467810 | 2022-02-17 10:03:42 -0800 | [diff] [blame] | 54 | |
| 55 | check(input).expect(expected) |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | fun `Detect inline function without an OptIn annotation`() { |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 60 | val input = |
| 61 | kotlin( |
| 62 | """ |
Tiem Song | 8467810 | 2022-02-17 10:03:42 -0800 | [diff] [blame] | 63 | inline fun String.myInlineFun() = this.length |
Omar Ismail | edf734f | 2024-05-28 12:34:57 +0100 | [diff] [blame] | 64 | """ |
| 65 | .trimIndent() |
| 66 | ) |
Tiem Song | 8467810 | 2022-02-17 10:03:42 -0800 | [diff] [blame] | 67 | |
| 68 | check(input).expectClean() |
| 69 | } |
| 70 | } |