Fix #1984 where TestMethodNameDetector does not scan some tests

pull/1985/head
David Rawson 1 month ago
parent 705fd9068b
commit cc020c0485

@ -18,6 +18,7 @@ package com.google.samples.apps.nowinandroid.lint
import com.android.tools.lint.detector.api.AnnotationInfo import com.android.tools.lint.detector.api.AnnotationInfo
import com.android.tools.lint.detector.api.AnnotationUsageInfo import com.android.tools.lint.detector.api.AnnotationUsageInfo
import com.android.tools.lint.detector.api.AnnotationUsageType
import com.android.tools.lint.detector.api.Category.Companion.TESTING import com.android.tools.lint.detector.api.Category.Companion.TESTING
import com.android.tools.lint.detector.api.Detector import com.android.tools.lint.detector.api.Detector
import com.android.tools.lint.detector.api.Implementation import com.android.tools.lint.detector.api.Implementation
@ -31,6 +32,7 @@ import com.android.tools.lint.detector.api.SourceCodeScanner
import com.android.tools.lint.detector.api.TextFormat.RAW import com.android.tools.lint.detector.api.TextFormat.RAW
import com.intellij.psi.PsiMethod import com.intellij.psi.PsiMethod
import org.jetbrains.uast.UElement import org.jetbrains.uast.UElement
import org.jetbrains.uast.UMethod
import java.util.EnumSet import java.util.EnumSet
import kotlin.io.path.Path import kotlin.io.path.Path
@ -43,13 +45,18 @@ class TestMethodNameDetector : Detector(), SourceCodeScanner {
override fun applicableAnnotations() = listOf("org.junit.Test") override fun applicableAnnotations() = listOf("org.junit.Test")
// Restrict this detector to `AnnotationUsageType.DEFINITION` following
// similar examples in AOSP like `IgnoreWithoutReasonDetector`.
override fun isApplicableAnnotationUsage(type: AnnotationUsageType): Boolean =
type == AnnotationUsageType.DEFINITION
override fun visitAnnotationUsage( override fun visitAnnotationUsage(
context: JavaContext, context: JavaContext,
element: UElement, element: UElement,
annotationInfo: AnnotationInfo, annotationInfo: AnnotationInfo,
usageInfo: AnnotationUsageInfo, usageInfo: AnnotationUsageInfo,
) { ) {
val method = usageInfo.referenced as? PsiMethod ?: return val method = annotationInfo.annotation.uastParent as? UMethod ?: return
method.detectPrefix(context, usageInfo) method.detectPrefix(context, usageInfo)
method.detectFormat(context, usageInfo) method.detectFormat(context, usageInfo)

@ -40,6 +40,11 @@ class TestMethodNameDetectorTest {
fun test_foo() = Unit fun test_foo() = Unit
@Test @Test
fun `test foo`() = Unit fun `test foo`() = Unit
@Test
fun test_foo2() {
// Blank body.
}
} }
""", """,
).indented(), ).indented(),
@ -53,19 +58,26 @@ class TestMethodNameDetectorTest {
src/Test.kt:8: Warning: Test method starts with test [TestMethodPrefix] src/Test.kt:8: Warning: Test method starts with test [TestMethodPrefix]
fun `test foo`() = Unit fun `test foo`() = Unit
~~~~~~~~~~ ~~~~~~~~~~
0 errors, 2 warnings src/Test.kt:11: Warning: Test method starts with test [TestMethodPrefix]
fun test_foo2() {
~~~~~~~~~
0 errors, 3 warnings
""".trimIndent(), """.trimIndent(),
) )
.expectFixDiffs( .expectFixDiffs(
""" """
Autofix for src/Test.kt line 6: Remove prefix: Autofix for src/Test.kt line 6: Remove prefix:
@@ -6 +6 @@ -6 +6 @@
- fun test_foo() = Unit - fun test_foo() = Unit
+ fun foo() = Unit + fun foo() = Unit
Autofix for src/Test.kt line 8: Remove prefix: Autofix for src/Test.kt line 8: Remove prefix:
@@ -8 +8 @@ -8 +8 @@
- fun `test foo`() = Unit - fun `test foo`() = Unit
+ fun `foo`() = Unit + fun `foo`() = Unit
Autofix for src/Test.kt line 11: Remove prefix:
@@ -11 +11 @@
- fun test_foo2() {
+ fun foo2() {
""".trimIndent(), """.trimIndent(),
) )
} }

Loading…
Cancel
Save