From 4732249443397675cc180d6d50613cb5841ec30a Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Fri, 13 Oct 2023 23:05:38 +0200 Subject: [PATCH] Update regex to match expected format --- .../nowinandroid/lint/TestMethodDetector.kt | 6 ++--- .../lint/TestMethodDetectorTest.kt | 23 +++++++++++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt index c7989542e..bcb75ffd5 100644 --- a/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt +++ b/lint/src/main/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetector.kt @@ -83,7 +83,7 @@ class TestMethodDetector : Detector(), SourceCodeScanner { usageInfo: AnnotationUsageInfo, ) { if (!context.isAndroidTest()) return - if ("""^[^\W_]+_[^\W_]+_[^\W_]+$""".toRegex().matches(name)) return + if ("""[^\W_]+(_[^\W_]+){1,2}""".toRegex().matches(name)) return context.report( issue = FORMAT, scope = usageInfo.usage, @@ -153,8 +153,8 @@ class TestMethodDetector : Detector(), SourceCodeScanner { @JvmField val FORMAT: Issue = issue( id = "TestMethodFormat", - briefDescription = "Test method does not follow the `given_when_then` format", - explanation = "Test method should follow the `given_when_then` format.", + briefDescription = "Test method does not follow the `given_when_then` or `when_then` format", + explanation = "Test method should follow the `given_when_then` or `when_then` format.", ) } } diff --git a/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt b/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt index ed24b5ccc..592b4f8fb 100644 --- a/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt +++ b/lint/src/test/kotlin/com/google/samples/apps/nowinandroid/lint/TestMethodDetectorTest.kt @@ -81,10 +81,17 @@ class TestMethodDetectorTest { """ import org.junit.Test class Test { + @Test + fun when_then() = Unit @Test fun given_when_then() = Unit + + @Test + fun foo() = Unit + @Test + fun foo_bar_baz_qux() = Unit @Test - fun given_foo_when_bar_then_baz() = Unit + fun `foo bar baz`() = Unit } """, ).indented(), @@ -92,10 +99,16 @@ class TestMethodDetectorTest { .run() .expect( """ - src/androidTest/com/example/Test.kt:6: Warning: Test method does not follow the given_when_then format [TestMethodFormat] - fun given_foo_when_bar_then_baz() = Unit - ~~~~~~~~~~~~~~~~~~~~~~~~~~~ - 0 errors, 1 warnings + src/androidTest/com/example/Test.kt:9: Warning: Test method does not follow the given_when_then or when_then format [TestMethodFormat] + fun foo() = Unit + ~~~ + src/androidTest/com/example/Test.kt:11: Warning: Test method does not follow the given_when_then or when_then format [TestMethodFormat] + fun foo_bar_baz_qux() = Unit + ~~~~~~~~~~~~~~~ + src/androidTest/com/example/Test.kt:13: Warning: Test method does not follow the given_when_then or when_then format [TestMethodFormat] + fun `foo bar baz`() = Unit + ~~~~~~~~~~~~~ + 0 errors, 3 warnings """.trimIndent(), ) }