This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fabiocarballo.rules | |
import com.android.tools.lint.checks.infrastructure.LintDetectorTest | |
import com.android.tools.lint.detector.api.Detector | |
import com.android.tools.lint.detector.api.Issue | |
import com.fabiocarballo.rules.Stubs.ANDROID_LOG_IMPL_JAVA | |
import org.junit.jupiter.api.Test | |
class AndroidLogDetectorTest : LintDetectorTest() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lintResult | |
.expectErrorCount(1) | |
.expect(""" | |
src/com/fabiocarballo/lint/Dog.kt:8: Error: android.util.Log usage is forbidden. [AndroidLogDetector] | |
Log.d(TAG, "woof! woof!") | |
~~~~~~~~~~~~~~~~~~~~~~~~~ | |
1 errors, 0 warnings | |
""".trimIndent()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AndroidLogDetector : Detector(), SourceCodeScanner { | |
override fun getApplicableMethodNames(): List<String> = | |
listOf("tag", "format", "v", "d", "i", "w", "e", "wtf") | |
override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) { | |
super.visitMethodCall(context, node, method) | |
val evaluator = context.evaluator | |
if (evaluator.isMemberInClass(method, "android.util.Log")) { | |
reportUsage(context, node) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AndroidLogDetector : Detector(), SourceCodeScanner { | |
(...) | |
companion object { | |
private val IMPLEMENTATION = Implementation( | |
AndroidLogDetector::class.java, | |
Scope.JAVA_FILE_SCOPE | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.android.tools.lint.client.api.IssueRegistry | |
import com.android.tools.lint.detector.api.CURRENT_API | |
import com.android.tools.lint.detector.api.Issue | |
class IssueRegistry : IssueRegistry() { | |
override val api: Int = CURRENT_API | |
override val issues: List<Issue> | |
get() = listOf(AndroidLogDetector.ISSUE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.util.Log | |
class Dog { | |
fun bark() { | |
Log.d(TAG, "woof! woof!") | |
} | |
companion object { | |
private const val TAG = "Sample" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `an underage user can't drink`() { | |
val ricardo = User( | |
firstName = "Ricardo", | |
lastName = "Trindade", | |
age = 17) | |
val ricardoDrinks = { ricardo.drink() } | |
ricardoDrinks `should throw` UnderageDrinkingException::class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
fun `an underage user can't drink`() { | |
val ricardo = User( | |
firstName = "Ricardo", | |
lastName = "Trindade", | |
age = 17) | |
val ricardoDrinks = { ricardo.drink() } | |
ricardoDrinks `should throw` UnderageDrinkingException::class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test(expected = UnderageDrinkingException::class) | |
fun `an underage user can't drink`() { | |
val ricardo = User(firstName = "Ricardo", lastName = "Trindade", age = 17) | |
ricardo.drink() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun drink() { | |
if (canDrink) { | |
System.out.println("Drinking .. ") | |
} else { | |
throw UnderageDrinkingException() | |
} | |
} |
NewerOlder