Skip to content
Permalink
Browse files

Fixed nested test creation method

  • Loading branch information
sksamuel committed Jan 14, 2020
1 parent 03bb19b commit fe1b0fd38acfef02a03ad0eaaabc74fdcd638b1e
@@ -40,10 +40,12 @@ interface WordSpecDsl : SpecDsl {
infix fun String.`when`(init: suspend WhenContext.() -> Unit) = addWhenContext(this, init)

private fun addWhenContext(name: String, init: suspend WhenContext.() -> Unit) {
addTest("$name when", { WhenContext(
this,
this@WordSpecDsl
).init() }, defaultTestCaseConfig, TestType.Container)
addTest(
"$name when",
{ WhenContext(this, this@WordSpecDsl).init() },
defaultTestCaseConfig,
TestType.Container
)
}

@KotestDsl
@@ -104,6 +106,7 @@ interface WordSpecDsl : SpecDsl {
}

@KotestDsl
// this context is used so we can add the deprecated should method to stop nesting a should inside a should
class FinalTestContext(val context: TestContext) : TestContext() {

override suspend fun registerTestCase(test: NestedTest) = context.registerTestCase(test)
@@ -65,9 +65,10 @@ data class NestedTest(
val sourceRef: SourceRef
)

fun NestedTest.toTestCase(spec: SpecConfiguration, parent: Description): TestCase =
TestCase(
parent.append(name),
fun NestedTest.toTestCase(spec: SpecConfiguration, parent: Description): TestCase {
require(!parent.names().contains(this.name)) { "Parent should not contain name of this test" }
return TestCase(
parent.append(this.name),
spec,
test,
sourceRef,
@@ -76,3 +77,4 @@ fun NestedTest.toTestCase(spec: SpecConfiguration, parent: Description): TestCas
null,
null
)
}
@@ -26,7 +26,8 @@ fun TestCase.isActive(): Boolean {
val focused = isFocused() && isTopLevel()
val hasFocused = spec.focusTests().isNotEmpty()
val enabledInConfig = config.enabled
val disabledViaBang = isBang() && sysprop("kotest.bang.disable").isEmpty()
val bangEnabled = sysprop("kotest.bang.disable").isEmpty()
val disabledViaBang = isBang() && bangEnabled
val activeViaTags = Project.tags().isActive(config.tags + spec.tags() + spec.tags)
val filtered = Project.testCaseFilters()
.map { it.filter(description) }.any { it == TestFilterResult.Exclude }
@@ -30,12 +30,11 @@ class SingleInstanceSpecRunner(listener: TestEngineListener) : SpecRunner(listen
val description: Description,
override val coroutineContext: CoroutineContext
) : TestContext() {
// in the single instance runner we execute each nested test as soon as the are registered
override suspend fun registerTestCase(test: NestedTest) {
val description = description.append(test.name)
if (seen.contains(description))
throw IllegalStateException("Cannot add duplicate test name ${test.name}")
// in the single instance runner we execute each nested test as soon as the are registered
val testCase = test.toTestCase(spec, description)
if (seen.contains(testCase.description))
throw IllegalStateException("Cannot add duplicate test name ${test.name}")
executor.execute(testCase, Context(spec, testCase.description, coroutineContext)) { result ->
results[testCase] = result
}

0 comments on commit fe1b0fd

Please sign in to comment.
You can’t perform that action at this time.