Skip to content

Commit 0e7f2f7

Browse files
add support for ExpectSpec
1 parent f55ff4b commit 0e7f2f7

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

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

Lines changed: 26 additions & 1 deletion
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.ifCallExpressionLambdaOpenBrace
1212
import io.kotest.plugin.intellij.psi.ifDotExpressionSeparator
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.KtClassOrObject
@@ -25,6 +26,13 @@ object ExpectSpecStyle : SpecStyle {
2526
return "expect(\"$name\") { }"
2627
}
2728

29+
override fun getDataTestMethodNames(): Set<String> =
30+
setOf(
31+
"withData",
32+
"withContexts",
33+
"withExpects"
34+
)
35+
2836
override fun isTestElement(element: PsiElement): Boolean = test(element) != null
2937

3038
private fun locateParent(element: PsiElement): Test? {
@@ -60,7 +68,7 @@ object ExpectSpecStyle : SpecStyle {
6068

6169
override fun test(element: PsiElement): Test? {
6270
return when (element) {
63-
is KtCallExpression -> element.tryExpect() ?: element.tryContext()
71+
is KtCallExpression -> element.tryExpect() ?: element.tryContext() ?: element.tryDataTest()
6472
is KtDotQualifiedExpression -> element.tryExpectWithConfig()
6573
else -> null
6674
}
@@ -70,13 +78,30 @@ object ExpectSpecStyle : SpecStyle {
7078
return setOf("OPEN_QUOTE")
7179
}
7280

81+
/**
82+
* For a ExpectSpec we consider the following scenarios:
83+
*
84+
* expect("test name") { }
85+
* expect("test name").config(...) {}
86+
* context("test name") {}
87+
* context("test name").config(...) {}
88+
* withData(...) { }
89+
* withContexts(...) { }
90+
* withExpects(...) { }
91+
*/
7392
override fun test(element: LeafPsiElement): Test? {
7493
val ktcall = element.ifCallExpressionLambdaOpenBrace()
7594
if (ktcall != null) return test(ktcall)
7695

7796
val ktdot = element.ifDotExpressionSeparator()
7897
if (ktdot != null) return test(ktdot)
7998

99+
// try to find Data Test Method by finding lambda openings
100+
val dataMethodCall = element.isDataTestMethodCall(getDataTestMethodNames())
101+
if (dataMethodCall != null) {
102+
return test(dataMethodCall)
103+
}
104+
80105
return null
81106
}
82107
}

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase
66
import io.kotest.matchers.shouldBe
77
import io.kotest.plugin.intellij.testMode
88
import java.nio.file.Paths
9+
import kotlin.collections.get
910

1011
class ExpectSpecStyleTest : LightJavaCodeInsightFixtureTestCase() {
1112

@@ -23,34 +24,38 @@ class ExpectSpecStyleTest : LightJavaCodeInsightFixtureTestCase() {
2324
)
2425

2526
val gutters = myFixture.findAllGutters()
26-
gutters.size shouldBe 7
27+
gutters.size shouldBe 8
2728

2829
gutters[0].icon shouldBe AllIcons.RunConfigurations.TestState.Run_run
2930
gutters[0].tooltipText shouldBe "Run ExpectSpecExample"
30-
(gutters[0] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 87
31+
(gutters[0] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 122
3132

3233
gutters[1].icon shouldBe AllIcons.RunConfigurations.TestState.Run
3334
gutters[1].tooltipText shouldBe "Run some context"
34-
(gutters[1] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 165
35+
(gutters[1] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 200
3536

3637
gutters[2].icon shouldBe AllIcons.RunConfigurations.TestState.Run
3738
gutters[2].tooltipText shouldBe "Run some context some test"
38-
(gutters[2] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 193
39+
(gutters[2] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 228
3940

4041
gutters[3].icon shouldBe AllIcons.RunConfigurations.TestState.Run
4142
gutters[3].tooltipText shouldBe "Run some context some test with config"
42-
(gutters[3] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 261
43+
(gutters[3] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 296
4344

4445
gutters[4].icon shouldBe AllIcons.RunConfigurations.TestState.Run
4546
gutters[4].tooltipText shouldBe "Run some context another nested context"
46-
(gutters[4] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 357
47+
(gutters[4] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 392
4748

4849
gutters[5].icon shouldBe AllIcons.RunConfigurations.TestState.Run
4950
gutters[5].tooltipText shouldBe "Run some context another nested context some test"
50-
(gutters[5] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 387
51+
(gutters[5] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 422
5152

5253
gutters[6].icon shouldBe AllIcons.RunConfigurations.TestState.Run
5354
gutters[6].tooltipText shouldBe "Run some context another nested context some test with config"
54-
(gutters[6] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 461
55+
(gutters[6] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 496
56+
57+
gutters[7].icon shouldBe AllIcons.RunConfigurations.TestState.Run
58+
gutters[7].tooltipText shouldBe "Run All Spec Tests, including data tests"
59+
(gutters[7] as LineMarkerInfo.LineMarkerGutterIconRenderer<*>).lineMarkerInfo.startOffset shouldBe 599
5560
}
5661
}

src/test/resources/expectspec.kt

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

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

56
class ExpectSpecExample : ExpectSpec() {
67
init {
@@ -20,5 +21,9 @@ class ExpectSpecExample : ExpectSpec() {
2021
}
2122
}
2223
}
24+
25+
withData(1, 2, 3, 4, 5) { value ->
26+
// test here
27+
}
2328
}
2429
}

0 commit comments

Comments
 (0)