Skip to content

Commit

Permalink
Rename unresolved test config and allow passing into test methods (#3917
Browse files Browse the repository at this point in the history
)

Fixes #3801
  • Loading branch information
sksamuel committed Mar 10, 2024
1 parent 6941c18 commit 157db55
Show file tree
Hide file tree
Showing 21 changed files with 207 additions and 112 deletions.
76 changes: 46 additions & 30 deletions kotest-framework/kotest-framework-api/api/kotest-framework-api.api

Large diffs are not rendered by default.

Expand Up @@ -12,7 +12,6 @@ import io.kotest.core.spec.Spec
import io.kotest.core.spec.SpecExecutionOrder
import io.kotest.core.test.AssertionMode
import io.kotest.core.test.TestCaseOrder
import io.kotest.core.test.config.ResolvedTestConfig
import io.kotest.core.test.config.TestCaseConfig
import kotlin.reflect.KClass
import kotlin.time.Duration
Expand Down Expand Up @@ -200,9 +199,10 @@ abstract class AbstractProjectConfig {
open val assertionMode: AssertionMode? = null

/**
* Any [ResolvedTestConfig] set here is used as the default for tests, unless overridden in a spec,
* Any [TestCaseConfig] set here is used as the default for tests, unless overridden in a spec,
* or in a test itself. In other words the order is test -> spec -> project config default -> kotest default
*/
@Deprecated("use the individual settings instead of the test case config class. Deprecated since 5.8.1")
open val defaultTestCaseConfig: TestCaseConfig? = null

/**
Expand Down
Expand Up @@ -11,6 +11,7 @@ import io.kotest.core.test.config.TestCaseConfig

object Defaults {

const val threads = 1
const val disableTestNestedJarScanning: Boolean = true

val assertionMode: AssertionMode = AssertionMode.None
Expand All @@ -36,7 +37,7 @@ object Defaults {
const val parallelism: Int = 1

const val defaultTimeoutMillis = 10 * 60 * 1000L
const val defaultInvocationTimeoutMillis = 10 * 60 * 1000L
const val defaultInvocationTimeoutMillis = 10 * 60 * 1000L

const val failOnIgnoredTests: Boolean = false
const val failfast: Boolean = false
Expand Down
Expand Up @@ -352,6 +352,10 @@ class ProjectConfiguration {

var allowOutOfOrderCallbacks: Boolean = Defaults.allowOutOfOrderCallbacks

var threads: Int = defaultTestConfig.threads

var invocations: Int = defaultTestConfig.invocations

/**
* Returns all globally registered [Listener]s.
*/
Expand Down
Expand Up @@ -26,7 +26,7 @@ import io.kotest.core.test.TestScope
import io.kotest.core.test.TestType
import io.kotest.core.test.config.ResolvedTestConfig
import io.kotest.core.test.config.TestCaseConfig
import io.kotest.core.test.config.UnresolvedTestConfig
import io.kotest.core.test.config.TestConfig
import kotlin.js.JsName

/**
Expand Down Expand Up @@ -438,6 +438,6 @@ data class RootTest(
val type: TestType,
val source: SourceRef,
val disabled: Boolean?, // if the test is explicitly disabled, say through an annotation or method name
val config: UnresolvedTestConfig?, // if specified by the test, may be null
val config: TestConfig?, // if specified by the test, may be null
val factoryId: FactoryId?, // if this root test was added from a factory
)
Expand Up @@ -20,7 +20,7 @@ import io.kotest.core.test.TestCase
import io.kotest.core.test.TestResult
import io.kotest.core.test.TestScope
import io.kotest.core.test.TestType
import io.kotest.core.test.config.UnresolvedTestConfig
import io.kotest.core.test.config.TestConfig
import kotlin.coroutines.CoroutineContext

@Deprecated("Renamed to ContainerScope in 5.0")
Expand All @@ -41,11 +41,11 @@ interface ContainerScope : TestScope {
fun hasChildren(): Boolean

suspend fun registerTest(
name: TestName,
disabled: Boolean,
config: UnresolvedTestConfig?,
type: TestType,
test: suspend TestScope.() -> Unit,
name: TestName,
disabled: Boolean,
config: TestConfig?,
type: TestType,
test: suspend TestScope.() -> Unit,
) {
registerTestCase(
NestedTest(
Expand All @@ -60,19 +60,19 @@ interface ContainerScope : TestScope {
}

suspend fun registerContainer(
name: TestName,
disabled: Boolean,
config: UnresolvedTestConfig?,
test: suspend TestScope.() -> Unit,
name: TestName,
disabled: Boolean,
config: TestConfig?,
test: suspend TestScope.() -> Unit,
) {
registerTest(name, disabled, config, TestType.Container, test)
}

suspend fun registerTest(
name: TestName,
disabled: Boolean,
config: UnresolvedTestConfig?,
test: suspend TestScope.() -> Unit,
name: TestName,
disabled: Boolean,
config: TestConfig?,
test: suspend TestScope.() -> Unit,
) {
registerTest(name, disabled, config, TestType.Test, test)
}
Expand Down
Expand Up @@ -6,7 +6,7 @@ import io.kotest.core.names.TestName
import io.kotest.core.test.EnabledIf
import io.kotest.core.test.EnabledOrReasonIf
import io.kotest.core.test.TestScope
import io.kotest.core.test.config.UnresolvedTestConfig
import io.kotest.core.test.config.TestConfig
import kotlin.time.Duration

@ExperimentalKotest
Expand All @@ -27,7 +27,7 @@ class ContainerWithConfigBuilder<T>(
blockingTest: Boolean? = null,
test: suspend T.() -> Unit
) {
val config = UnresolvedTestConfig(
val config = TestConfig(
enabled = enabled,
enabledIf = enabledIf,
enabledOrReasonIf = enabledOrReasonIf,
Expand Down
Expand Up @@ -7,7 +7,7 @@ import io.kotest.core.spec.KotestTestScope
import io.kotest.core.test.EnabledIf
import io.kotest.core.test.TestCaseSeverityLevel
import io.kotest.core.test.TestScope
import io.kotest.core.test.config.UnresolvedTestConfig
import io.kotest.core.test.config.TestConfig
import kotlin.time.Duration

@Deprecated("Renamed to FreeSpecContainerScope. Deprecated since 4.5")
Expand Down Expand Up @@ -75,6 +75,20 @@ class FreeSpecContainerScope(val testScope: TestScope) : AbstractContainerScope(
)
}

suspend fun String.config(
config: TestConfig,
test: suspend TestScope.() -> Unit,
) {
TestWithConfigBuilder(
name = TestName(this),
context = this@FreeSpecContainerScope,
xdisabled = false,
).config(
config = config,
test = test,
)
}


/**
* Adds the contained config and test to this scope as a container test.
Expand Down Expand Up @@ -108,7 +122,7 @@ class FreeSpecContainerScope(val testScope: TestScope) : AbstractContainerScope(
severity: TestCaseSeverityLevel? = null,
failfast: Boolean? = null,
): FreeSpecContextConfigBuilder {
val config = UnresolvedTestConfig(
val config = TestConfig(
enabled = enabled,
tags = tags,
extensions = extensions,
Expand Down
Expand Up @@ -6,13 +6,13 @@ import io.kotest.core.names.TestName
import io.kotest.core.test.EnabledIf
import io.kotest.core.test.TestCaseSeverityLevel
import io.kotest.core.test.TestScope
import io.kotest.core.test.config.UnresolvedTestConfig
import io.kotest.core.test.config.TestConfig
import kotlin.time.Duration

@Deprecated("Renamed to FreeSpecRootContext. Deprecated since 5.0")
typealias FreeSpecRootContext = FreeSpecRootScope

data class FreeSpecContextConfigBuilder(val name: String, val config: UnresolvedTestConfig)
data class FreeSpecContextConfigBuilder(val name: String, val config: TestConfig)

interface FreeSpecRootScope : RootScope {

Expand Down Expand Up @@ -49,7 +49,7 @@ interface FreeSpecRootScope : RootScope {
blockingTest: Boolean? = null,
coroutineTestScope: Boolean? = null,
): FreeSpecContextConfigBuilder {
val config = UnresolvedTestConfig(
val config = TestConfig(
enabled = enabled,
tags = tags,
extensions = extensions,
Expand All @@ -63,6 +63,21 @@ interface FreeSpecRootScope : RootScope {
blockingTest = blockingTest,
coroutineTestScope = coroutineTestScope,
)
return config(config)
}

/**
* Starts a config builder, which can be added to the scope by invoking [minus] on the returned value.
*
* E.g.
*
* ```
* "this test".config(...) - { }
* ```
*/
fun String.config(
config: TestConfig,
): FreeSpecContextConfigBuilder {
return FreeSpecContextConfigBuilder(this, config)
}

Expand Down Expand Up @@ -101,7 +116,7 @@ interface FreeSpecRootScope : RootScope {
coroutineTestScope: Boolean? = null,
test: suspend TestScope.() -> Unit,
) {
val config = UnresolvedTestConfig(
val config = TestConfig(
enabled = enabled,
tags = tags,
extensions = extensions,
Expand All @@ -117,4 +132,8 @@ interface FreeSpecRootScope : RootScope {
)
addTest(TestName(this), false, config, test)
}

fun String.config(config: TestConfig, test: suspend TestScope.() -> Unit): FreeSpecContextConfigBuilder {
return FreeSpecContextConfigBuilder(this, config)
}
}
Expand Up @@ -6,7 +6,7 @@ import io.kotest.core.names.TestName
import io.kotest.core.test.EnabledIf
import io.kotest.core.test.EnabledOrReasonIf
import io.kotest.core.test.TestScope
import io.kotest.core.test.config.UnresolvedTestConfig
import io.kotest.core.test.config.TestConfig
import kotlin.time.Duration

@ExperimentalKotest
Expand All @@ -17,6 +17,14 @@ class RootContainerWithConfigBuilder<T>(
val contextFn: (TestScope) -> T
) {

@ExperimentalKotest
fun config(
config: TestConfig,
test: suspend T.() -> Unit,
) {
context.addContainer(name, xdisabled, config) { contextFn(this).test() }
}

@ExperimentalKotest
fun config(
enabled: Boolean? = null,
Expand All @@ -29,7 +37,7 @@ class RootContainerWithConfigBuilder<T>(
coroutineTestScope: Boolean? = null,
test: suspend T.() -> Unit
) {
val config = UnresolvedTestConfig(
val config = TestConfig(
enabled = enabled,
enabledIf = enabledIf,
enabledOrReasonIf = enabledOrReasonIf,
Expand All @@ -39,6 +47,6 @@ class RootContainerWithConfigBuilder<T>(
blockingTest = blockingTest,
coroutineTestScope = coroutineTestScope,
)
context.addContainer(name, xdisabled, config) { contextFn(this).test() }
config(config, test)
}
}
Expand Up @@ -5,7 +5,7 @@ import io.kotest.core.source.sourceRef
import io.kotest.core.spec.RootTest
import io.kotest.core.test.TestScope
import io.kotest.core.test.TestType
import io.kotest.core.test.config.UnresolvedTestConfig
import io.kotest.core.test.config.TestConfig

@Deprecated("Renamed to RootContext. Deprecated since 5.0")
typealias RootContext = RootScope
Expand All @@ -24,11 +24,11 @@ interface RootScope {
* Convenience method to add a test of type [type] to this [RootScope].
*/
fun RootScope.addTest(
testName: TestName,
disabled: Boolean,
config: UnresolvedTestConfig?,
type: TestType,
test: suspend ContainerScope.() -> Unit
testName: TestName,
disabled: Boolean,
config: TestConfig?,
type: TestType,
test: suspend ContainerScope.() -> Unit
) {
add(
RootTest(
Expand All @@ -47,10 +47,10 @@ fun RootScope.addTest(
* Convenience method to add a [TestType.Test] test to this [RootScope].
*/
fun RootScope.addTest(
testName: TestName,
disabled: Boolean,
config: UnresolvedTestConfig?,
test: suspend TestScope.() -> Unit
testName: TestName,
disabled: Boolean,
config: TestConfig?,
test: suspend TestScope.() -> Unit
) {
addTest(testName, disabled, config, TestType.Test, test)
}
Expand All @@ -59,10 +59,10 @@ fun RootScope.addTest(
* Convenience method to add a [TestType.Container] test to this [RootScope].
*/
fun RootScope.addContainer(
testName: TestName,
disabled: Boolean,
config: UnresolvedTestConfig?,
test: suspend ContainerScope.() -> Unit
testName: TestName,
disabled: Boolean,
config: TestConfig?,
test: suspend ContainerScope.() -> Unit
) {
addTest(testName, disabled, config, TestType.Container, test)
}
Expand Up @@ -7,7 +7,7 @@ import io.kotest.core.test.EnabledIf
import io.kotest.core.test.EnabledOrReasonIf
import io.kotest.core.test.TestCaseSeverityLevel
import io.kotest.core.test.TestScope
import io.kotest.core.test.config.UnresolvedTestConfig
import io.kotest.core.test.config.TestConfig
import kotlin.time.Duration

class RootTestWithConfigBuilder(
Expand All @@ -33,7 +33,7 @@ class RootTestWithConfigBuilder(
coroutineTestScope: Boolean? = null,
test: suspend TestScope.() -> Unit,
) {
val config = UnresolvedTestConfig(
val config = TestConfig(
enabled = enabled,
tags = tags,
extensions = extensions,
Expand Down
Expand Up @@ -8,7 +8,7 @@ import io.kotest.core.test.EnabledIf
import io.kotest.core.test.EnabledOrReasonIf
import io.kotest.core.test.TestCaseSeverityLevel
import io.kotest.core.test.TestScope
import io.kotest.core.test.config.UnresolvedTestConfig
import io.kotest.core.test.config.TestConfig
import kotlin.time.Duration

class TestWithConfigBuilder(
Expand All @@ -17,6 +17,11 @@ class TestWithConfigBuilder(
private val xdisabled: Boolean,
) {

suspend fun config(config: TestConfig, test: suspend TestScope.() -> Unit) {
TestDslState.clear(context.testCase.descriptor.append(name))
context.registerTest(name, xdisabled, config, test)
}

suspend fun config(
enabled: Boolean? = null,
invocations: Int? = null,
Expand All @@ -32,10 +37,7 @@ class TestWithConfigBuilder(
coroutineTestScope: Boolean? = null,
test: suspend TestScope.() -> Unit
) {

TestDslState.clear(context.testCase.descriptor.append(name))

val config = UnresolvedTestConfig(
val config = TestConfig(
enabled = enabled,
enabledIf = enabledIf,
enabledOrReasonIf = enabledOrReasonIf,
Expand All @@ -49,7 +51,6 @@ class TestWithConfigBuilder(
blockingTest = blockingTest,
coroutineTestScope = coroutineTestScope,
)

context.registerTest(name, xdisabled, config, test)
config(config, test)
}
}

0 comments on commit 157db55

Please sign in to comment.