Update regex to match expected format

pull/899/head
Simon Marquis 1 year ago
parent f9abb3115b
commit 4732249443

@ -83,7 +83,7 @@ class TestMethodDetector : Detector(), SourceCodeScanner {
usageInfo: AnnotationUsageInfo, usageInfo: AnnotationUsageInfo,
) { ) {
if (!context.isAndroidTest()) return if (!context.isAndroidTest()) return
if ("""^[^\W_]+_[^\W_]+_[^\W_]+$""".toRegex().matches(name)) return if ("""[^\W_]+(_[^\W_]+){1,2}""".toRegex().matches(name)) return
context.report( context.report(
issue = FORMAT, issue = FORMAT,
scope = usageInfo.usage, scope = usageInfo.usage,
@ -153,8 +153,8 @@ class TestMethodDetector : Detector(), SourceCodeScanner {
@JvmField @JvmField
val FORMAT: Issue = issue( val FORMAT: Issue = issue(
id = "TestMethodFormat", id = "TestMethodFormat",
briefDescription = "Test method does not 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` format.", explanation = "Test method should follow the `given_when_then` or `when_then` format.",
) )
} }
} }

@ -81,10 +81,17 @@ class TestMethodDetectorTest {
""" """
import org.junit.Test import org.junit.Test
class Test { class Test {
@Test
fun when_then() = Unit
@Test @Test
fun given_when_then() = Unit fun given_when_then() = Unit
@Test
fun foo() = Unit
@Test
fun foo_bar_baz_qux() = Unit
@Test @Test
fun given_foo_when_bar_then_baz() = Unit fun `foo bar baz`() = Unit
} }
""", """,
).indented(), ).indented(),
@ -92,10 +99,16 @@ class TestMethodDetectorTest {
.run() .run()
.expect( .expect(
""" """
src/androidTest/com/example/Test.kt:6: Warning: Test method does not follow the given_when_then format [TestMethodFormat] src/androidTest/com/example/Test.kt:9: Warning: Test method does not follow the given_when_then or when_then format [TestMethodFormat]
fun given_foo_when_bar_then_baz() = Unit fun foo() = Unit
~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~
0 errors, 1 warnings 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(), """.trimIndent(),
) )
} }

Loading…
Cancel
Save