Skip to content

Commit 11ba193

Browse files
committed
Simplify Snippet Usgae Verification Regex (#908)
* Simplify regex * Fix spotless
1 parent c034ff5 commit 11ba193

File tree

1 file changed

+18
-6
lines changed
  • lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/snippet

1 file changed

+18
-6
lines changed

lib/src/integrationTest/kotlin/com/lemonappdev/konsist/core/snippet/SnippetTest.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,29 @@ class SnippetTest {
2929
val snippetMap = mutableMapOf<String, String>()
3030
snippetNames.forEachIndexed { index, s -> snippetMap[s] = snippetPaths[index] }
3131

32-
val r1 = Regex("""getSnippetFile\(\s*"(.+)"""")
33-
val r2 = Regex("""arguments\(\s*"([^"]+)"""")
34-
val r3 = Regex("""arguments\(\s*"([^"]+)"""")
35-
val withGetSnippetMethod = snippetNamesFromFiles(r1, "getSnippetFile(")
36-
val withArgument = snippetNamesFromFiles(r2, "arguments(") + snippetNamesFromFiles(r3, "arguments(\n")
32+
/*
33+
Matches calls to the `getSnippetFile` function, capturing the argument passed to it.
34+
It accounts for any spaces between the function name and the opening parenthesis,
35+
and captures the entire argument as a string enclosed in quotes e.g. `getSnippetFile("snippetName")`
36+
*/
37+
val getSnippetFileRegex = Regex("""getSnippetFile\(\s*"(.+)"""")
38+
39+
/*
40+
This regex matches calls to the `arguments` function, capturing the argument passed to it.
41+
It allows for spaces between the function name and the opening parenthesis,
42+
and captures the argument as a string within quotes e.g. `arguments("snippetName")`
43+
*/
44+
val argumentsRegex = Regex("""arguments\(\s*"([^"]+)"""")
45+
46+
val withGetSnippetMethod = snippetNamesFromFiles(getSnippetFileRegex, "getSnippetFile(")
47+
val withArgument = snippetNamesFromFiles(argumentsRegex, "arguments(")
3748

3849
val snippetNamesUsedInTests = (withGetSnippetMethod + withArgument).toSet()
3950

40-
// then
51+
// when
4152
val sut = snippetMap.keys.toSet() - snippetNamesUsedInTests
4253

54+
// then
4355
assertSoftly {
4456
sut shouldBeEqualTo emptySet()
4557
require(sut.isEmpty()) { "Unused snippets: ${sut.map { snippetMap[it] }}" }

0 commit comments

Comments
 (0)