Skip to content

Commit 0fd1f83

Browse files
authored
feat: support string template in ava test title (#34)
* feat: support string template in ava test tile * fix(ci): code check fail
1 parent 4776d49 commit 0fd1f83

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/main/kotlin/no/eirikb/avatest/actions/AvaJavaScriptTestRunnerRunConfigurationGenerator.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@ import com.intellij.openapi.fileEditor.FileDocumentManager
2020
import com.intellij.openapi.wm.ToolWindowId
2121
import com.intellij.psi.PsiElement
2222
import com.jetbrains.nodejs.run.NodeJsRunConfiguration
23+
import no.eirikb.avatest.utils.getTestNameByClearUnnecessaryString
2324
import java.nio.file.Paths
2425

2526
fun JSCallExpression.isTest(): Boolean {
26-
var text = this.methodExpression?.text
27+
val text = this.methodExpression?.text
2728

2829
if (text != null) {
2930
return text == "test" || text.startsWith("test.")
3031
}
3132

32-
return false;
33+
return false
3334
}
3435

3536
class AvaJavaScriptTestRunnerRunConfigurationGenerator : AnAction() {
@@ -116,7 +117,7 @@ class AvaJavaScriptTestRunnerRunConfigurationGenerator : AnAction() {
116117
if (arguments.isNotEmpty()) {
117118
if (arguments[0] is JSLiteralExpression) {
118119
val expression: JSLiteralExpression = arguments[0] as JSLiteralExpression
119-
return expression.stringValue
120+
return getTestNameByClearUnnecessaryString(expression)
120121
}
121122
return null
122123
}

src/main/kotlin/no/eirikb/avatest/markers/AvaRunContributor.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.intellij.openapi.actionSystem.AnActionEvent
1111
import com.intellij.psi.PsiElement
1212
import no.eirikb.avatest.actions.AvaJavaScriptTestRunnerRunConfigurationGenerator
1313
import no.eirikb.avatest.actions.isTest
14+
import no.eirikb.avatest.utils.getTestNameByClearUnnecessaryString
1415
import javax.swing.Icon
1516

1617
class AvaRunContributor : RunLineMarkerContributor() {
@@ -25,9 +26,10 @@ class AvaRunContributor : RunLineMarkerContributor() {
2526
if (arguments.isNotEmpty()) {
2627
if (arguments[0] is JSLiteralExpression) {
2728
val expression: JSLiteralExpression = arguments[0] as JSLiteralExpression
29+
2830
return Info(
2931
TestState.Run,
30-
getActions(element.textOffset, "${expression.stringValue}")
32+
getActions(element.textOffset, "${getTestNameByClearUnnecessaryString(expression)}")
3133
) { "Run Test" }
3234
}
3335
return null
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package no.eirikb.avatest.utils
2+
3+
import com.intellij.lang.javascript.psi.JSLiteralExpression
4+
5+
/**
6+
* remove StringTemplate
7+
* e.g:
8+
* `${namespace} - test - ${suffix}` => `* - test - *`
9+
* remove blank string at the beginning and end
10+
*/
11+
fun getTestNameByClearUnnecessaryString(expression: JSLiteralExpression): String? {
12+
val testName = expression.stringValue
13+
14+
if (testName != null) {
15+
return testName
16+
}
17+
18+
val paramSourceCode = expression.text
19+
20+
if (paramSourceCode.isNotEmpty() && paramSourceCode.startsWith("`") && paramSourceCode.endsWith("`")) {
21+
val stringTemplateRegex = Regex("\\$\\{.?\\}")
22+
return paramSourceCode.replace("`", "").replace(stringTemplateRegex, "*").trim()
23+
}
24+
25+
return null
26+
}

0 commit comments

Comments
 (0)