Remove underscore check

Change-Id: Iec1d07787a6e1cd350b9d8d082e729ef62492013
pull/899/head
Don Turner 1 year ago
parent 16fd42a766
commit 9747255e33

@ -25,7 +25,6 @@ class NiaIssueRegistry : IssueRegistry() {
override val issues = listOf( override val issues = listOf(
DesignSystemDetector.ISSUE, DesignSystemDetector.ISSUE,
TestMethodNameDetector.UNDERSCORE,
TestMethodNameDetector.FORMAT, TestMethodNameDetector.FORMAT,
TestMethodNameDetector.PREFIX, TestMethodNameDetector.PREFIX,
) )

@ -37,7 +37,6 @@ import kotlin.io.path.Path
/** /**
* A detector that checks for common patterns in naming the test methods: * A detector that checks for common patterns in naming the test methods:
* - [detectPrefix] removes unnecessary "test" prefix in all unit test. * - [detectPrefix] removes unnecessary "test" prefix in all unit test.
* - [detectUnderscore] removes underscores in JVM unit test (and add backticks if necessary).
* - [detectFormat] Checks the `given_when_then` format of Android instrumented tests (backticks are not supported). * - [detectFormat] Checks the `given_when_then` format of Android instrumented tests (backticks are not supported).
*/ */
class TestMethodNameDetector : Detector(), SourceCodeScanner { class TestMethodNameDetector : Detector(), SourceCodeScanner {
@ -54,7 +53,6 @@ class TestMethodNameDetector : Detector(), SourceCodeScanner {
method.detectPrefix(context, usageInfo) method.detectPrefix(context, usageInfo)
method.detectFormat(context, usageInfo) method.detectFormat(context, usageInfo)
method.detectUnderscore(context, usageInfo)
} }
private fun JavaContext.isAndroidTest() = Path("androidTest") in file.toPath() private fun JavaContext.isAndroidTest() = Path("androidTest") in file.toPath()
@ -92,31 +90,6 @@ class TestMethodNameDetector : Detector(), SourceCodeScanner {
) )
} }
private fun PsiMethod.detectUnderscore(
context: JavaContext,
usageInfo: AnnotationUsageInfo,
) {
if (context.isAndroidTest()) return
if ("_" !in name) return
context.report(
issue = UNDERSCORE,
scope = usageInfo.usage,
location = context.getNameLocation(this),
message = UNDERSCORE.getBriefDescription(RAW),
quickfixData = LintFix.create()
.name("Replace underscores with spaces")
.replace()
.range(context.getNameLocation(this))
.with(
name.replace("_", " ")
.removeSurrounding("`")
.let { """`$it`""" },
)
.autoFix()
.build(),
)
}
companion object { companion object {
private fun issue( private fun issue(
@ -136,13 +109,6 @@ class TestMethodNameDetector : Detector(), SourceCodeScanner {
), ),
) )
@JvmField
val UNDERSCORE: Issue = issue(
id = "TestMethodUnderscore",
briefDescription = "Test method contains underscores",
explanation = "Test methods should not contains underscores.",
)
@JvmField @JvmField
val PREFIX: Issue = issue( val PREFIX: Issue = issue(
id = "TestMethodPrefix", id = "TestMethodPrefix",

@ -21,7 +21,6 @@ import com.android.tools.lint.checks.infrastructure.TestFiles.kotlin
import com.android.tools.lint.checks.infrastructure.TestLintTask.lint import com.android.tools.lint.checks.infrastructure.TestLintTask.lint
import com.google.samples.apps.nowinandroid.lint.TestMethodNameDetector.Companion.FORMAT import com.google.samples.apps.nowinandroid.lint.TestMethodNameDetector.Companion.FORMAT
import com.google.samples.apps.nowinandroid.lint.TestMethodNameDetector.Companion.PREFIX import com.google.samples.apps.nowinandroid.lint.TestMethodNameDetector.Companion.PREFIX
import com.google.samples.apps.nowinandroid.lint.TestMethodNameDetector.Companion.UNDERSCORE
import org.junit.Test import org.junit.Test
class TestMethodNameDetectorTest { class TestMethodNameDetectorTest {
@ -113,51 +112,6 @@ class TestMethodNameDetectorTest {
) )
} }
@Test
fun `detect underscores`() {
lint().issues(UNDERSCORE)
.files(
JUNIT_TEST_STUB,
kotlin(
"""
import org.junit.Test
class Test {
@Test
fun foo() = Unit
@Test
fun foo_bar_baz() = Unit
@Test
fun `foo_bar_baz`() = Unit
}
""",
).indented(),
)
.run()
.expect(
"""
src/Test.kt:6: Warning: Test method contains underscores [TestMethodUnderscore]
fun foo_bar_baz() = Unit
~~~~~~~~~~~
src/Test.kt:8: Warning: Test method contains underscores [TestMethodUnderscore]
fun `foo_bar_baz`() = Unit
~~~~~~~~~~~~~
0 errors, 2 warnings
""".trimIndent(),
)
.expectFixDiffs(
"""
Autofix for src/Test.kt line 6: Replace underscores with spaces:
@@ -6 +6
- fun foo_bar_baz() = Unit
+ fun `foo bar baz`() = Unit
Autofix for src/Test.kt line 8: Replace underscores with spaces:
@@ -8 +8
- fun `foo_bar_baz`() = Unit
+ fun `foo bar baz`() = Unit
""".trimIndent(),
)
}
private companion object { private companion object {
private val JUNIT_TEST_STUB: TestFile = kotlin( private val JUNIT_TEST_STUB: TestFile = kotlin(
""" """

Loading…
Cancel
Save