Skip to content

Commit

Permalink
Add LazyBundleDetector test
Browse files Browse the repository at this point in the history
  • Loading branch information
Pluu committed Sep 4, 2023
1 parent 312d8d2 commit 41d697f
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions lint/src/test/java/com/pluu/lint/LazyBundleDetectorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.pluu.lint

import com.android.tools.lint.checks.infrastructure.LintDetectorTest
import com.android.tools.lint.checks.infrastructure.LintDetectorTest.kotlin
import org.junit.Test

class LazyBundleDetectorTest : LintDetectorTest() {

override fun getDetector() = LazyBundleDetector()

override fun getIssues() = listOf(
LazyBundleDetector.ISSUE
)

@Test
fun testSuccess() {
val activityFile = kotlin(
"""
package com.pluu.lintstudy.lazybundle
import android.app.Activity
class LazySampleActivity : Activity() {
private val string = "a"
private val int = 1
private val dataClass = Sample("A")
private val index by inject<String>()
private val lazyIndex by lazy { index.toInt() }
private val d = Sample("A")
}
data class Sample(val a: String)
""".trimIndent(),
).indented()

lint().files(
extension_Stub,
activityFile
).run()
.expectClean()
}

@Test
fun testFail() {
val activityFile = kotlin(
"""
package com.pluu.lintstudy.lazybundle
import android.app.Activity
class LazySampleActivity : Activity() {
private val index by inject<String>()
private val i = index.toInt()
private val i2 = index
}
""".trimIndent(),
).indented()

lint().files(
extension_Stub,
activityFile
).run()
.expect(
"""
src/com/pluu/lintstudy/lazybundle/LazySampleActivity.kt:7: Error: Lazy한 값을 클래스 인스턴스화 시점에 사용하면 안됨 [LazyDetector]
private val i = index.toInt()
~~~~~~~~~~~~~
src/com/pluu/lintstudy/lazybundle/LazySampleActivity.kt:8: Error: Lazy한 값을 클래스 인스턴스화 시점에 사용하면 안됨 [LazyDetector]
private val i2 = index
~~~~~
2 errors, 0 warnings
""".trimIndent()
)
}
}

private val extension_Stub = kotlin(
"""
package com.pluu.lintstudy.lazybundle
import android.app.Activity
import kotlin.reflect.KProperty
inline fun <reified T> Activity.inject(): LazyProvider<Activity, T> {
return lazy { null }
}
interface LazyProvider<A, T> {
operator fun provideDelegate(thisRef: A, prop: KProperty<*>): Lazy<T>
}
""".trimIndent()
).indented().within("src")

0 comments on commit 41d697f

Please sign in to comment.