Skip to content

Commit

Permalink
Add the context container to the BehaviorSpecStyle and update the cor…
Browse files Browse the repository at this point in the history
…responding tests (#276)
  • Loading branch information
jcthenerd authored Nov 30, 2023
1 parent f0e87aa commit 9583a84
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand All @@ -36,30 +39,48 @@ 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)
}
}

private fun KtCallExpression.tryXGiven(): Test? {
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)
}
}

Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/test/resources/behaviorspec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
}
}
}
}
}

0 comments on commit 9583a84

Please sign in to comment.