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,
) {
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.",
)
}
}

@ -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(),
)
}

Loading…
Cancel
Save