Skip to content

Commit 1692f0a

Browse files
committed
test(custom-variable): add hasVariable method and corresponding test #51
Add a new method to the CustomVariable class to check if a given content string contains any custom variables. The method uses a regex to match the variable syntax and returns true if any matches are found. Also, create a new test file, CustomVariableTest.kt, to test the hasVariable method. The test ensures that the method correctly identifies when a content string contains a valid custom variable and when it does not.
1 parent f9695b7 commit 1692f0a

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/main/kotlin/cc/unitmesh/devti/custom/variable/CustomVariable.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ enum class CustomVariable(val variable: String, val description: String) {
1515

1616
companion object {
1717
fun all(): List<CustomVariable> = values().toList()
18+
19+
fun hasVariable(content: String): Boolean {
20+
return all().any { content.contains("\${${it.variable}}") }
21+
}
1822
}
1923
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cc.unitmesh.devti.custom.variable
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.junit.Test
5+
6+
7+
class CustomVariableTest {
8+
@Test
9+
fun should_parse_variable_from_content() {
10+
CustomVariable.hasVariable("解释一下代码:\${selection}") shouldBe true
11+
CustomVariable.hasVariable("解释一下代码:\${selection") shouldBe false
12+
}
13+
}

0 commit comments

Comments
 (0)