Skip to content

Commit

Permalink
Hukumister#52: Add tests for lint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhail57 committed Feb 14, 2021
1 parent da3682b commit f0bc68a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import com.android.tools.lint.checks.infrastructure.LintDetectorTest

@Suppress("UnstableApiUsage")
class WrongGenimiUsageDetectorTest : LintDetectorTest() {
private val GEMINI_STUB1 = kotlin(BINDER_STUB)
private val GEMINI_STUB2 = kotlin(BINDER_RULES)
companion object {
private val GEMINI_STUB1 = kotlin(BINDER_STUB)
private val GEMINI_STUB2 = kotlin(BINDER_RULES)
private val OTHER_STUB = kotlin(OTHER_BINDER_STUB)
}

fun testRightUsageWithActivity() {
lint()
Expand Down Expand Up @@ -93,6 +96,63 @@ class WrongGenimiUsageDetectorTest : LintDetectorTest() {
""".trimIndent())
}

fun testWrongUsageWithActivity_onResume() {
lint()
.files(
GEMINI_STUB1,
GEMINI_STUB2,
kotlin("""
package ru.test.app
import android.app.Activity
import com.haroncode.gemini.binder.*
import com.haroncode.gemini.binder.rule.*
class Activity1 : Activity() {
override fun onResume() {
super.onViewCreated(view, savedInstanceState)
StoreViewBinding.with(generateSampleRulesFactory<Any>())
.bind(this)
}
}
""".trimIndent())
)
.requireCompileSdk()
.run()
.expect("""
src/ru/test/app/Activity1.kt:11: Error: Calling bind from onResume instead of onCreate [ShouldBeCalledInOnCreate]
StoreViewBinding.with(generateSampleRulesFactory<Any>())
^
1 errors, 0 warnings
""".trimIndent())
}

fun testDoesNotTriggeredByOtherClasses() {
lint()
.files(
OTHER_STUB,
kotlin("""
package ru.test.app
import android.app.Fragment
import ru.test.lib.*
class Fragment1 : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
StoreViewBinding.with().bind(this)
}
}
""".trimIndent())
)
.requireCompileSdk()
.run()
.expectClean()

}

override fun getDetector() = WrongGenimiUsageDetector()

override fun getIssues() = WrongGenimiUsageDetector.issues
Expand Down
10 changes: 2 additions & 8 deletions gemini-lint/src/test/java/com/haroncode/gemini/lint/stubs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,10 @@ fun <T : Any> generateSampleRulesFactory() = object : BindingRulesFactory<T> {
""".trimIndent()

val OTHER_BINDER_STUB = """
package ru.test.app
package ru.test.lib
object StoreViewBinding {
fun <T : LifecycleOwner> with(
factory: BindingRulesFactory<T>,
lifecycleStrategy: LifecycleStrategy = StartStopStrategy
): Binder<T> = BinderImpl(
factory = factory,
lifecycleStrategy = lifecycleStrategy
)
fun <T : LifecycleOwner> with(): Binder<T> = BinderImpl()
}
interface Binder<View> {
Expand Down

0 comments on commit f0bc68a

Please sign in to comment.