Skip to content

Commit dde1822

Browse files
add support for WordSpec
1 parent 621daa9 commit dde1822

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

src/main/kotlin/io/kotest/plugin/intellij/styles/WordSpecStyle.kt

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import io.kotest.plugin.intellij.psi.ifMinusOperator
1414
import io.kotest.plugin.intellij.psi.ifCallExpressionLhsStringOpenQuote
1515
import io.kotest.plugin.intellij.psi.ifDotExpressionSeparator
1616
import io.kotest.plugin.intellij.psi.ifOpenQuoteOfLhsArgOfIndexFunction
17+
import io.kotest.plugin.intellij.psi.isDataTestMethodCall
1718
import org.jetbrains.kotlin.name.FqName
1819
import org.jetbrains.kotlin.psi.KtBinaryExpression
1920
import org.jetbrains.kotlin.psi.KtCallExpression
@@ -32,6 +33,13 @@ object WordSpecStyle : SpecStyle {
3233
return "\"$name\" should { }"
3334
}
3435

36+
override fun getDataTestMethodNames(): Set<String> =
37+
setOf(
38+
"withData",
39+
"withWhens",
40+
"withShoulds"
41+
)
42+
3543
override fun isTestElement(element: PsiElement): Boolean = test(element) != null
3644

3745
private fun PsiElement.locateParentWhen(): Test? {
@@ -123,7 +131,7 @@ object WordSpecStyle : SpecStyle {
123131

124132
override fun test(element: PsiElement): Test? {
125133
return when (element) {
126-
is KtCallExpression -> element.trySubject()
134+
is KtCallExpression -> element.trySubject() ?: element.tryDataTest()
127135
is KtBinaryExpression -> (element.tryShould() ?: element.tryWhen())
128136
is KtDotQualifiedExpression -> element.trySubjectWithConfig()
129137
else -> null
@@ -134,6 +142,21 @@ object WordSpecStyle : SpecStyle {
134142
return setOf("OPEN_QUOTE")
135143
}
136144

145+
/**
146+
* For a FunSpec we consider the following scenarios:
147+
*
148+
* should("test name") { }
149+
* xshould("test name") { }
150+
* should("test name").config(...) {}
151+
* xshould("test name").config(...) {}
152+
* when("test name") {}
153+
* xwhen("test name") {}
154+
* when("test name").config(...) {}
155+
* xwhen("test name").config(...) {}
156+
* withData(...) { }
157+
* withWhens(...) { }
158+
* withShoulds(...) { }
159+
*/
137160
override fun test(element: LeafPsiElement): Test? {
138161
val ktcall = element.ifCallExpressionLhsStringOpenQuote()
139162
if (ktcall != null) return test(ktcall)
@@ -147,6 +170,12 @@ object WordSpecStyle : SpecStyle {
147170
val ktdot = element.ifDotExpressionSeparator()
148171
if (ktdot != null) return test(ktdot)
149172

173+
// try to find Data Test Method by finding lambda openings
174+
val dataMethodCall = element.isDataTestMethodCall(ShouldSpecStyle.getDataTestMethodNames())
175+
if (dataMethodCall != null) {
176+
return ShouldSpecStyle.test(dataMethodCall)
177+
}
178+
150179
return null
151180
}
152181
}

src/test/kotlin/io/kotest/plugin/intellij/styles/WordSpecStyleTest.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,20 @@ class WordSpecStyleTest : LightJavaCodeInsightFixtureTestCase() {
2424
)
2525

2626
val gutters = myFixture.findAllGutters()
27-
gutters.size shouldBe 10
27+
gutters.size shouldBe 11
2828

2929
val expected = listOf(
30-
Gutter("Run WordSpecExample", 87, AllIcons.RunConfigurations.TestState.Run_run),
31-
Gutter("Run some should context", 137),
32-
Gutter("Run some should context test something", 174),
33-
Gutter("Run some should context test something with config", 256),
34-
Gutter("Run with capital When", 302),
35-
Gutter("Run with capital When and capital Should", 335),
36-
Gutter("Run with capital When test something", 373),
37-
Gutter("Run with capital When test something with config", 428),
38-
Gutter("Disabled - with capital When disabled should", 480, AllIcons.RunConfigurations.TestIgnored),
39-
Gutter("Disabled - disabled when", 529, AllIcons.RunConfigurations.TestIgnored),
30+
Gutter("Run WordSpecExample", 122, AllIcons.RunConfigurations.TestState.Run_run),
31+
Gutter("Run some should context", 172),
32+
Gutter("Run some should context test something", 209),
33+
Gutter("Run some should context test something with config", 291),
34+
Gutter("Run with capital When", 337),
35+
Gutter("Run with capital When and capital Should", 370),
36+
Gutter("Run with capital When test something", 408),
37+
Gutter("Run with capital When test something with config", 463),
38+
Gutter("Disabled - with capital When disabled should", 515, AllIcons.RunConfigurations.TestIgnored),
39+
Gutter("Disabled - disabled when", 564, AllIcons.RunConfigurations.TestIgnored),
40+
Gutter("Run All Spec Tests, including data tests", 625),
4041
)
4142

4243
gutters.size shouldBe expected.size

src/test/resources/wordspec.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.sksamuel.kotest.specs.wordspec
22

33
import io.kotest.core.spec.style.WordSpec
4+
import io.kotest.datatest.withData
45

56
class WordSpecExample : WordSpec() {
67
init {
@@ -27,5 +28,8 @@ class WordSpecExample : WordSpec() {
2728

2829
}
2930

31+
withData(1, 2, 3, 4, 5) { value ->
32+
// test here
33+
}
3034
}
3135
}

0 commit comments

Comments
 (0)