Skip to content

Commit

Permalink
Support word spec should in test filter #2319
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Jul 12, 2021
1 parent a5ab2cd commit 2853bf6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,26 @@ class TestPathTestCaseFilter(
spec: KClass<out Spec>,
) : TestFilter {

private val target = testPath.split(TestPathSeparator)
private val target1 = testPath.split(TestPathSeparator)
.fold(spec.toDescription() as Description) { desc, name -> desc.appendTest(name) }

// this is a hack where we append "should" to the first name, until 5.0 where we will
// store names with affixes separately (right now word spec is adding them to the names at source)
var should = true
private val target2 = testPath.split(TestPathSeparator)
.fold(spec.toDescription() as Description) { desc, name ->
if (should) {
should = false
desc.appendTest("$name should")
} else desc.appendTest(name)
}

override fun filter(description: Description): TestFilterResult {
return when {
target.isOnPath(description) || description.isOnPath(target) -> TestFilterResult.Include
target1.isOnPath(description) ||
target2.isOnPath(description) ||
description.isOnPath(target1) ||
description.isOnPath(target2) -> TestFilterResult.Include
else -> TestFilterResult.Exclude
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.sksamuel.kotest.engine.launcher
import io.kotest.core.filter.TestFilterResult
import io.kotest.core.spec.style.FunSpec
import io.kotest.core.spec.style.StringSpec
import io.kotest.core.spec.style.WordSpec
import io.kotest.core.spec.toDescription
import io.kotest.engine.launcher.TestPathTestCaseFilter
import io.kotest.matchers.shouldBe
Expand All @@ -22,13 +23,29 @@ class TestPathTestCaseFilterTest : FunSpec() {
TestPathTestCaseFilter("foo", Spec1::class).filter(Spec1::class.toDescription().appendTest("foo").appendTest("bar")) shouldBe TestFilterResult.Include
}
test("filter should include parent tests of the target") {
TestPathTestCaseFilter("foo -- bar", Spec1::class).filter(Spec1::class.toDescription().appendTest("foo")) shouldBe TestFilterResult.Include
TestPathTestCaseFilter("foo -- bar", Spec1::class).filter(
Spec1::class.toDescription().appendTest("foo")
) shouldBe TestFilterResult.Include
}
test("filter should include the target spec") {
TestPathTestCaseFilter("foo -- bar", Spec1::class).filter(Spec1::class.toDescription()) shouldBe TestFilterResult.Include
TestPathTestCaseFilter(
"foo -- bar",
Spec1::class
).filter(Spec1::class.toDescription()) shouldBe TestFilterResult.Include
}
test("filter should exclude another spec") {
TestPathTestCaseFilter("foo -- bar", Spec1::class).filter(Spec2::class.toDescription()) shouldBe TestFilterResult.Exclude
TestPathTestCaseFilter(
"foo -- bar",
Spec1::class
).filter(Spec2::class.toDescription()) shouldBe TestFilterResult.Exclude
}
test("filter should work for word spec") {
TestPathTestCaseFilter("a container -- pass a test", WordSpec1::class).filter(
WordSpec1::class.toDescription().appendTest("a container should").appendTest("pass a test")
) shouldBe TestFilterResult.Include
TestPathTestCaseFilter("a container -- pass a test", WordSpec1::class).filter(
WordSpec1::class.toDescription().appendTest("a container should").appendTest("skip a test")
) shouldBe TestFilterResult.Exclude
}
}
}
Expand All @@ -46,3 +63,12 @@ private class Spec2 : StringSpec() {
"boo"{}
}
}

private class WordSpec1 : WordSpec() {
init {
"a container" should {
"skip a test".config(enabled = false) {}
"pass a test" { 1 shouldBe 1 }
}
}
}

0 comments on commit 2853bf6

Please sign in to comment.