Skip to content

Commit 4714e57

Browse files
add support for BehaviourSpec
1 parent dde1822 commit 4714e57

File tree

4 files changed

+92
-38
lines changed

4 files changed

+92
-38
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import io.kotest.plugin.intellij.psi.extractLhsStringArgForDotExpressionWithRhsF
1010
import io.kotest.plugin.intellij.psi.extractStringArgForFunctionWithStringAndLambdaArgs
1111
import io.kotest.plugin.intellij.psi.ifDotExpressionSeparator
1212
import io.kotest.plugin.intellij.psi.ifOpenQuoteOfFunctionName
13+
import io.kotest.plugin.intellij.psi.isDataTestMethodCall
1314
import org.jetbrains.kotlin.name.FqName
1415
import org.jetbrains.kotlin.psi.KtCallExpression
1516
import org.jetbrains.kotlin.psi.KtDotQualifiedExpression
@@ -23,6 +24,16 @@ object BehaviorSpecStyle : SpecStyle {
2324
override fun fqn() = FqName("io.kotest.core.spec.style.BehaviorSpec")
2425
override fun specStyleName(): String = "Behavior Spec"
2526

27+
override fun getDataTestMethodNames(): Set<String> =
28+
setOf(
29+
"withData",
30+
"withContexts",
31+
"withGivens",
32+
"withWhens",
33+
"withThens",
34+
"withAnds"
35+
)
36+
2637
override fun isTestElement(element: PsiElement): Boolean = test(element) != null
2738

2839
private val contexts = listOf("Context", "context", "`Context`", "`context`")
@@ -173,6 +184,7 @@ object BehaviorSpecStyle : SpecStyle {
173184
?: element.tryXWhen()
174185
?: element.tryThen()
175186
?: element.tryXThen()
187+
?: element.tryDataTest()
176188
is KtDotQualifiedExpression -> element.tryThenWithConfig()
177189
else -> null
178190
}
@@ -191,13 +203,49 @@ object BehaviorSpecStyle : SpecStyle {
191203
return setOf("OPEN_QUOTE", "DOT")
192204
}
193205

206+
/**
207+
* For a Behavior we consider the following scenarios:
208+
*
209+
* context("test name") {}
210+
* xcontext("test name") {}
211+
* context("test name").config(...) {}
212+
* xcontext("test name").config(...) {}
213+
* given("test name") {}
214+
* xgiven("test name") {}
215+
* given("test name").config(...) {}
216+
* xgiven("test name").config(...) {}
217+
* when("test name") {}
218+
* xwhen("test name") {}
219+
* when("test name").config(...) {}
220+
* xwhen("test name").config(...) {}
221+
* then("test name") {}
222+
* xthen("test name") {}
223+
* then("test name").config(...) {}
224+
* xthen("test name").config(...) {}
225+
* and("test name") {}
226+
* xand("test name") {}
227+
* and("test name").config(...) {}
228+
* xand("test name").config(...) {}
229+
* withData(...) { }
230+
* withContexts(...) { }
231+
* withGivens(...) { }
232+
* withWhens(...) { }
233+
* withThens(...) { }
234+
* withAnds(...) { }
235+
*/
194236
override fun test(element: LeafPsiElement): Test? {
195237
val ktcall1 = element.ifOpenQuoteOfFunctionName(fnNames)
196238
if (ktcall1 != null) return test(ktcall1)
197239

198240
val ktdot = element.ifDotExpressionSeparator()
199241
if (ktdot != null) return test(ktdot)
200242

243+
// try to find Data Test Method by finding lambda openings
244+
val dataMethodCall = element.isDataTestMethodCall(getDataTestMethodNames())
245+
if (dataMethodCall != null) {
246+
return test(dataMethodCall)
247+
}
248+
201249
return null
202250
}
203251
}

src/test/kotlin/io/kotest/plugin/intellij/intentions/BangIntentionTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ class BangIntentionTest : LightJavaCodeInsightFixtureTestCase() {
2020
"/behaviorspec.kt",
2121
"/io/kotest/core/spec/style/specs.kt"
2222
)
23-
editor.moveCaret(265)
23+
editor.moveCaret(300)
2424

2525
val intention = myFixture.findSingleIntention("Add/Remove bang to test name")
2626
intention.familyName shouldBe "Add/Remove bang to test name"
2727
WriteCommandAction.runWriteCommandAction(project) {
2828
intention.invoke(project, editor, file)
2929
}
30-
file.findElementAt(265)?.text shouldBe "!another test"
30+
file.findElementAt(300)?.text shouldBe "!another test"
3131

3232
WriteCommandAction.runWriteCommandAction(project) {
3333
intention.invoke(project, editor, file)
3434
}
35-
file.findElementAt(265)?.text shouldBe "another test"
35+
file.findElementAt(300)?.text shouldBe "another test"
3636
}
3737

3838
fun testIntentionShouldOnlyBeAvailableOnStrings() {

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

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -25,65 +25,67 @@ class BehaviorSpecRunMarkerTest : LightJavaCodeInsightFixtureTestCase() {
2525

2626
val gutters = myFixture.findAllGutters()
2727
println(gutters.map { it.tooltipText }.joinToString("\n"))
28-
gutters.size shouldBe 34
28+
gutters.size shouldBe 35
2929

3030
val expected = listOf(
31-
Gutter("Run BehaviorSpecExample", 91, AllIcons.RunConfigurations.TestState.Run_run),
32-
Gutter("Run a given", 159),
33-
Gutter("Run a given a when", 188),
34-
Gutter("Run a given a when a test", 217),
35-
Gutter("Run a given a when another test", 260),
36-
Gutter("Disabled - a given a when a disabled then", 310, AllIcons.RunConfigurations.TestIgnored),
37-
Gutter("Disabled - a given disabled when", 371, AllIcons.RunConfigurations.TestIgnored),
31+
Gutter("Run BehaviorSpecExample", 126, AllIcons.RunConfigurations.TestState.Run_run),
32+
Gutter("Run a given", 194),
33+
Gutter("Run a given a when", 223),
34+
Gutter("Run a given a when a test", 252),
35+
Gutter("Run a given a when another test", 295),
36+
Gutter("Disabled - a given a when a disabled then", 345, AllIcons.RunConfigurations.TestIgnored),
37+
Gutter("Disabled - a given disabled when", 406, AllIcons.RunConfigurations.TestIgnored),
3838
Gutter(
3939
"Disabled - a given disabled when this then should be disabled from its parent",
40-
407,
40+
442,
4141
AllIcons.RunConfigurations.TestIgnored
4242
),
4343
Gutter(
4444
"Disabled - a given disabled when this then should be disabled with config",
45-
531,
45+
566,
4646
AllIcons.RunConfigurations.TestIgnored
4747
),
48-
Gutter("Run a given an and", 596),
49-
Gutter("Run a given an and a when", 627),
50-
Gutter("Run a given an and a when a test", 659),
51-
Gutter("Run a given an and an and in an and", 718),
52-
Gutter("Run a given an and an and in an and a test", 760),
53-
Gutter("Disabled - a given an and disabled when", 821, AllIcons.RunConfigurations.TestIgnored),
48+
Gutter("Run a given an and", 631),
49+
Gutter("Run a given an and a when", 662),
50+
Gutter("Run a given an and a when a test", 694),
51+
Gutter("Run a given an and an and in an and", 753),
52+
Gutter("Run a given an and an and in an and a test", 795),
53+
Gutter("Disabled - a given an and disabled when", 856, AllIcons.RunConfigurations.TestIgnored),
5454
Gutter(
5555
"Disabled - a given an and disabled when this then should be disabled by nesting",
56-
860,
56+
895,
5757
AllIcons.RunConfigurations.TestIgnored
5858
),
5959
Gutter(
6060
"Disabled - a given an and disabled when an xdisabled then",
61-
943,
61+
978,
6262
AllIcons.RunConfigurations.TestIgnored
6363
),
64-
Gutter("Disabled - a given disabled and", 1022, AllIcons.RunConfigurations.TestIgnored),
65-
Gutter("Disabled - a given disabled and a nested when", 1059, AllIcons.RunConfigurations.TestIgnored),
66-
Gutter("Disabled - a given disabled and a nested when a test", 1098, AllIcons.RunConfigurations.TestIgnored),
67-
Gutter("Disabled - disabled given", 1173, AllIcons.RunConfigurations.TestIgnored),
68-
Gutter("Disabled - disabled given disabled when", 1207, AllIcons.RunConfigurations.TestIgnored),
64+
Gutter("Disabled - a given disabled and", 1057, AllIcons.RunConfigurations.TestIgnored),
65+
Gutter("Disabled - a given disabled and a nested when", 1094, AllIcons.RunConfigurations.TestIgnored),
66+
Gutter("Disabled - a given disabled and a nested when a test", 1133, AllIcons.RunConfigurations.TestIgnored),
67+
Gutter("Disabled - disabled given", 1208, AllIcons.RunConfigurations.TestIgnored),
68+
Gutter("Disabled - disabled given disabled when", 1242, AllIcons.RunConfigurations.TestIgnored),
6969
Gutter(
7070
"Disabled - disabled given disabled when a disabled then",
71-
1243,
71+
1278,
7272
AllIcons.RunConfigurations.TestIgnored
7373
),
74-
Gutter("Disabled - disabled given disabled and", 1302, AllIcons.RunConfigurations.TestIgnored),
75-
Gutter("Disabled - disabled given disabled and a test", 1337, AllIcons.RunConfigurations.TestIgnored),
76-
Gutter("Disabled - disabled given", 1395, AllIcons.RunConfigurations.TestIgnored),
77-
Gutter("Disabled - disabled given a nested then", 1429, AllIcons.RunConfigurations.TestIgnored),
78-
Gutter("Run a context", 1472),
79-
Gutter("Run a context a nested given", 1499),
80-
Gutter("Run a context a nested given a when", 1535),
81-
Gutter("Run a context a nested given a when a test", 1564),
82-
Gutter("Disabled - a context disabled given", 1622, AllIcons.RunConfigurations.TestIgnored),
83-
Gutter("Disabled - a context disabled given a disabled when", 1656, AllIcons.RunConfigurations.TestIgnored),
84-
Gutter("Disabled - a context disabled given a disabled when a disabled test", 1694, AllIcons.RunConfigurations.TestIgnored),
74+
Gutter("Disabled - disabled given disabled and", 1337, AllIcons.RunConfigurations.TestIgnored),
75+
Gutter("Disabled - disabled given disabled and a test", 1372, AllIcons.RunConfigurations.TestIgnored),
76+
Gutter("Disabled - disabled given", 1430, AllIcons.RunConfigurations.TestIgnored),
77+
Gutter("Disabled - disabled given a nested then", 1464, AllIcons.RunConfigurations.TestIgnored),
78+
Gutter("Run a context", 1507),
79+
Gutter("Run a context a nested given", 1534),
80+
Gutter("Run a context a nested given a when", 1570),
81+
Gutter("Run a context a nested given a when a test", 1599),
82+
Gutter("Disabled - a context disabled given", 1657, AllIcons.RunConfigurations.TestIgnored),
83+
Gutter("Disabled - a context disabled given a disabled when", 1691, AllIcons.RunConfigurations.TestIgnored),
84+
Gutter("Disabled - a context disabled given a disabled when a disabled test", 1729, AllIcons.RunConfigurations.TestIgnored),
85+
Gutter("Run All Spec Tests, including data tests", 1815),
8586
)
8687

88+
8789
expected.size shouldBe gutters.size
8890

8991
assertSoftly {

src/test/resources/behaviorspec.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.behavior
22

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

56
class BehaviorSpecExample : BehaviorSpec() {
67

@@ -71,4 +72,7 @@ class BehaviorSpecExample : BehaviorSpec() {
7172
}
7273
}
7374
}
75+
withData(1, 2, 3, 4, 5) { value ->
76+
// test here
77+
}
7478
}

0 commit comments

Comments
 (0)