diff --git a/src/main/kotlin/io/kotest/plugin/intellij/styles/BehaviorSpecStyle.kt b/src/main/kotlin/io/kotest/plugin/intellij/styles/BehaviorSpecStyle.kt index 57ec2da..cc7ea62 100644 --- a/src/main/kotlin/io/kotest/plugin/intellij/styles/BehaviorSpecStyle.kt +++ b/src/main/kotlin/io/kotest/plugin/intellij/styles/BehaviorSpecStyle.kt @@ -24,6 +24,9 @@ object BehaviorSpecStyle : SpecStyle { override fun isTestElement(element: PsiElement): Boolean = test(element) != null + private val contexts = listOf("Context", "context", "`Context`", "`context`") + private val xcontexts = contexts.map { "x$it" } + private val givens = listOf("given", "Given", "`given`", "`Given`") private val xgivens = givens.map { "x$it" } @@ -36,22 +39,39 @@ object BehaviorSpecStyle : SpecStyle { private val thens = listOf("then", "Then", "`then`", "`Then`") private val xthens = thens.map { "x$it" } - private val fnNames = (givens + xgivens + ands + xands + whens + xwhens + thens + xthens).toSet() + private val fnNames = (contexts + xcontexts + givens + xgivens + ands + xands + whens + xwhens + thens + xthens).toSet() private fun PsiElement.locateParent(): Test? { return when (val p = parent) { null -> null is KtCallExpression -> p.tryWhen() ?: p.tryXWhen() ?: p.tryAnd() ?: p.tryXAnd() ?: p.tryGiven() - ?: p.tryXGiven() + ?: p.tryXGiven() ?: p.tryContext() ?: p.tryXContext() else -> p.locateParent() } } + private fun KtCallExpression.tryContext(): Test? { + val context = this.extractStringArgForFunctionWithStringAndLambdaArgs(contexts) + return if (context == null) null else { + val name = TestName("Context: ", context.text, context.interpolated) + Test(name, null, TestType.Container, xdisabled = false, psi = this) + } + } + + private fun KtCallExpression.tryXContext(): Test? { + val context = this.extractStringArgForFunctionWithStringAndLambdaArgs(xcontexts) + return if (context == null) null else { + val name = TestName("Context: ", context.text, context.interpolated) + Test(name, null, TestType.Container, xdisabled = true, psi = this) + } + } + private fun KtCallExpression.tryGiven(): Test? { val given = this.extractStringArgForFunctionWithStringAndLambdaArgs(givens) return if (given == null) null else { val name = TestName("Given: ", given.text, given.interpolated) - Test(name, null, TestType.Container, xdisabled = false, psi = this) + val parents = locateParent() + Test(name, parents, TestType.Container, xdisabled = false, psi = this) } } @@ -59,7 +79,8 @@ object BehaviorSpecStyle : SpecStyle { val given = this.extractStringArgForFunctionWithStringAndLambdaArgs(xgivens) return if (given == null) null else { val name = TestName("Given: ", given.text, given.interpolated) - Test(name, null, TestType.Container, xdisabled = true, psi = this) + val parents = locateParent() + Test(name, parents, TestType.Container, xdisabled = true, psi = this) } } @@ -129,7 +150,7 @@ object BehaviorSpecStyle : SpecStyle { override fun test(element: PsiElement): Test? { return when (element) { is KtCallExpression -> - element.tryGiven() ?: element.tryXGiven() ?: element.tryAnd() ?: element.tryXAnd() ?: element.tryWhen() + element.tryContext() ?: element.tryXContext() ?: element.tryGiven() ?: element.tryXGiven() ?: element.tryAnd() ?: element.tryXAnd() ?: element.tryWhen() ?: element.tryXWhen() ?: element.tryThen() ?: element.tryXThen() is KtDotQualifiedExpression -> element.tryThenWithConfig() diff --git a/src/test/kotlin/io/kotest/plugin/intellij/styles/BehaviorSpecRunMarkerTest.kt b/src/test/kotlin/io/kotest/plugin/intellij/styles/BehaviorSpecRunMarkerTest.kt index 507210b..7155002 100644 --- a/src/test/kotlin/io/kotest/plugin/intellij/styles/BehaviorSpecRunMarkerTest.kt +++ b/src/test/kotlin/io/kotest/plugin/intellij/styles/BehaviorSpecRunMarkerTest.kt @@ -25,7 +25,7 @@ class BehaviorSpecRunMarkerTest : LightJavaCodeInsightFixtureTestCase() { val gutters = myFixture.findAllGutters() println(gutters.map { it.tooltipText }.joinToString("\n")) - gutters.size shouldBe 27 + gutters.size shouldBe 34 val expected = listOf( Gutter("Run BehaviorSpecExample", 91, AllIcons.RunConfigurations.TestState.Run_run), @@ -75,6 +75,13 @@ class BehaviorSpecRunMarkerTest : LightJavaCodeInsightFixtureTestCase() { Gutter("Disabled - disabled given disabled and a test", 1337, AllIcons.RunConfigurations.TestIgnored), Gutter("Disabled - disabled given", 1395, AllIcons.RunConfigurations.TestIgnored), Gutter("Disabled - disabled given a nested then", 1429, AllIcons.RunConfigurations.TestIgnored), + Gutter("Run a context", 1472), + Gutter("Run a context a nested given", 1499), + Gutter("Run a context a nested given a when", 1535), + Gutter("Run a context a nested given a when a test", 1564), + Gutter("Disabled - a context disabled given", 1622, AllIcons.RunConfigurations.TestIgnored), + Gutter("Disabled - a context disabled given a disabled when", 1656, AllIcons.RunConfigurations.TestIgnored), + Gutter("Disabled - a context disabled given a disabled when a disabled test", 1694, AllIcons.RunConfigurations.TestIgnored), ) expected.size shouldBe gutters.size diff --git a/src/test/resources/behaviorspec.kt b/src/test/resources/behaviorspec.kt index 0d371c9..761c538 100644 --- a/src/test/resources/behaviorspec.kt +++ b/src/test/resources/behaviorspec.kt @@ -57,4 +57,18 @@ class BehaviorSpecExample : BehaviorSpec() { then("a nested then") { } } + context("a context") { + given("a nested given") { + `when`("a when") { + then("a test") { + } + } + } + xgiven("disabled given") { + When("a disabled when") { + then("a disabled test") { + } + } + } + } }