Skip to content
Permalink
Browse files

Added new spec DSL for overriding values and functions (#976)

  • Loading branch information...
sksamuel committed Sep 4, 2019
1 parent b2a1632 commit 7adc1975b9c0fcdca89195d390e54e7a6289af63
@@ -51,7 +51,7 @@ allprojects {
group = "io.kotlintest"

if (isTravis) {
version "4.0.0." + travisBuildNumber + "-SNAPSHOT"
version "4.0." + travisBuildNumber + "-SNAPSHOT"
}

release {
@@ -74,7 +74,7 @@ interface Spec : TestListener {

fun description(): Description = Description.fromSpecClass(this::class)

val assertionMode: AssertionMode? get() = null
fun assertionMode(): AssertionMode? = null
}

/**
@@ -16,15 +16,20 @@ import io.kotlintest.assertions.classname
*/
abstract class Tag {

/**
* Simple name of the singleton/class derived from this class.
*/
open val name: String = this::class.classname()

/**
* Same as [name].
*/
override fun toString() = name
/**
* Simple name of the singleton/class derived from this class.
*/
open val name: String = this::class.classname()

/**
* Same as [name].
*/
override fun toString() = name

companion object {
operator fun invoke(name: String): StringTag = StringTag(name)
}
}


class StringTag(override val name: String) : Tag()
@@ -9,7 +9,7 @@ import kotlin.time.Duration
import kotlin.time.ExperimentalTime

@Suppress("FunctionName")
abstract class AbstractBehaviorSpec(body: AbstractBehaviorSpec.() -> Unit = {}) : AbstractSpec() {
abstract class AbstractBehaviorSpec(body: AbstractBehaviorSpec.() -> Unit = {}) : AbstractSpecDsl() {

init {
body()
@@ -7,7 +7,7 @@ import io.kotlintest.extensions.TestCaseExtension
import kotlin.time.Duration
import kotlin.time.ExperimentalTime

abstract class AbstractDescribeSpec(body: AbstractDescribeSpec.() -> Unit = {}) : AbstractSpec() {
abstract class AbstractDescribeSpec(body: AbstractDescribeSpec.() -> Unit = {}) : AbstractSpecDsl() {

init {
body()
@@ -8,7 +8,7 @@ import io.kotlintest.extensions.TestCaseExtension
import kotlin.time.Duration
import kotlin.time.ExperimentalTime

abstract class AbstractExpectSpec(body: AbstractExpectSpec.() -> Unit = {}) : AbstractSpec() {
abstract class AbstractExpectSpec(body: AbstractExpectSpec.() -> Unit = {}) : AbstractSpecDsl() {

init {
body()
@@ -8,7 +8,7 @@ import io.kotlintest.extensions.TestCaseExtension
import kotlin.time.Duration
import kotlin.time.ExperimentalTime

abstract class AbstractFeatureSpec(body: AbstractFeatureSpec.() -> Unit = {}) : AbstractSpec() {
abstract class AbstractFeatureSpec(body: AbstractFeatureSpec.() -> Unit = {}) : AbstractSpecDsl() {

init {
body()
@@ -8,7 +8,7 @@ import io.kotlintest.extensions.TestCaseExtension
import kotlin.time.Duration
import kotlin.time.ExperimentalTime

abstract class AbstractFreeSpec(body: AbstractFreeSpec.() -> Unit = {}) : AbstractSpec() {
abstract class AbstractFreeSpec(body: AbstractFreeSpec.() -> Unit = {}) : AbstractSpecDsl() {

init {
body()
@@ -8,11 +8,11 @@ import io.kotlintest.extensions.TestCaseExtension
import kotlin.time.Duration
import kotlin.time.ExperimentalTime

abstract class AbstractFunSpec(body: AbstractFunSpec.() -> Unit = {}) : AbstractSpec() {
abstract class AbstractFunSpec(body: AbstractFunSpec.() -> Unit = {}) : AbstractSpecDsl() {

init {
body()
}
init {
body()
}

inner class TestBuilder(val name: String) {
@UseExperimental(ExperimentalTime::class)
@@ -35,52 +35,52 @@ abstract class AbstractFunSpec(body: AbstractFunSpec.() -> Unit = {}) : Abstract
}
}

fun context(name: String, init: suspend ContextScope.() -> Unit) {
addTestCase(name, { ContextScope(this).init() }, defaultTestCaseConfig, TestType.Container)
}
fun context(name: String, init: suspend ContextScope.() -> Unit) {
addTestCase(name, { ContextScope(this).init() }, defaultTestCaseConfig, TestType.Container)
}

@KotlinTestDsl
inner class ContextScope(val context: TestContext) {
@KotlinTestDsl
inner class ContextScope(val context: TestContext) {

suspend fun context(name: String, init: suspend ContextScope.() -> Unit) {
context.registerTestCase(
name,
this@AbstractFunSpec,
{ ContextScope(this).init() },
defaultTestCaseConfig,
TestType.Container
)
}
suspend fun context(name: String, init: suspend ContextScope.() -> Unit) {
context.registerTestCase(
name,
this@AbstractFunSpec,
{ ContextScope(this).init() },
defaultTestCaseConfig,
TestType.Container
)
}

inner class TestBuilder(val name: String) {
@UseExperimental(ExperimentalTime::class)
suspend fun config(
invocations: Int? = null,
enabled: Boolean? = null,
timeout: Duration? = null,
parallelism: Int? = null,
tags: Set<Tag>? = null,
extensions: List<TestCaseExtension>? = null,
test: suspend TestContext.() -> Unit) {
val config = TestCaseConfig(
enabled ?: defaultTestCaseConfig.enabled,
invocations ?: defaultTestCaseConfig.invocations,
timeout ?: defaultTestCaseConfig.timeout,
parallelism ?: defaultTestCaseConfig.threads,
tags ?: defaultTestCaseConfig.tags,
extensions ?: defaultTestCaseConfig.extensions)
context.registerTestCase(name, this@AbstractFunSpec, test, config, TestType.Test)
}
}
inner class TestBuilder(val name: String) {
@UseExperimental(ExperimentalTime::class)
suspend fun config(
invocations: Int? = null,
enabled: Boolean? = null,
timeout: Duration? = null,
parallelism: Int? = null,
tags: Set<Tag>? = null,
extensions: List<TestCaseExtension>? = null,
test: suspend TestContext.() -> Unit) {
val config = TestCaseConfig(
enabled ?: defaultTestCaseConfig.enabled,
invocations ?: defaultTestCaseConfig.invocations,
timeout ?: defaultTestCaseConfig.timeout,
parallelism ?: defaultTestCaseConfig.threads,
tags ?: defaultTestCaseConfig.tags,
extensions ?: defaultTestCaseConfig.extensions)
context.registerTestCase(name, this@AbstractFunSpec, test, config, TestType.Test)
}
}

fun test(name: String) = TestBuilder(name)
fun test(name: String) = TestBuilder(name)

suspend fun test(name: String, test: suspend TestContext.() -> Unit) =
context.registerTestCase(name, this@AbstractFunSpec, test, defaultTestCaseConfig, TestType.Test)
}
suspend fun test(name: String, test: suspend TestContext.() -> Unit) =
context.registerTestCase(name, this@AbstractFunSpec, test, defaultTestCaseConfig, TestType.Test)
}

fun test(name: String) = TestBuilder(name)
fun test(name: String) = TestBuilder(name)

fun test(name: String, test: suspend TestContext.() -> Unit) =
fun test(name: String, test: suspend TestContext.() -> Unit) =
addTestCase(name, test, defaultTestCaseConfig, TestType.Test)
}
@@ -25,7 +25,7 @@ import kotlin.time.ExperimentalTime
* // test here
* }
*/
abstract class AbstractShouldSpec(body: AbstractShouldSpec.() -> Unit = {}) : AbstractSpec() {
abstract class AbstractShouldSpec(body: AbstractShouldSpec.() -> Unit = {}) : AbstractSpecDsl() {

init {
body()
@@ -0,0 +1,106 @@
package io.kotlintest.core.specs

import io.kotlintest.AssertionMode
import io.kotlintest.IsolationMode
import io.kotlintest.Spec
import io.kotlintest.Tag
import io.kotlintest.TestCase
import io.kotlintest.TestCaseOrder
import io.kotlintest.TestResult
import io.kotlintest.extensions.SpecLevelExtension
import io.kotlintest.extensions.TestListener

abstract class AbstractSpecDsl : AbstractSpec() {

private var beforeTestFn: (TestCase) -> Unit = {}
private var afterTestFn: (TestCase, TestResult) -> Unit = { _, _ -> }
private var beforeSpecFn: (Spec) -> Unit = {}
private var afterSpecFn: (Spec) -> Unit = {}
private var afterSpecClassFn: (Spec, Map<TestCase, TestResult>) -> Unit = { _, _ -> }
private var tags: Set<Tag> = emptySet()
private var listeners: List<TestListener> = emptyList()
private var extensions: List<SpecLevelExtension> = emptyList()
private var testCaseOrder: TestCaseOrder? = null
private var isolationMode: IsolationMode? = null
private var assertionMode: AssertionMode? = null

override fun testCaseOrder(): TestCaseOrder? = testCaseOrder
override fun isolationMode(): IsolationMode? = isolationMode
override fun assertionMode(): AssertionMode? = assertionMode

override fun listeners(): List<TestListener> = listeners
override fun extensions(): List<SpecLevelExtension> = extensions

override fun beforeTest(testCase: TestCase) {
super.beforeTest(testCase)
beforeTestFn(testCase)
}

override fun afterTest(testCase: TestCase, result: TestResult) {
super.afterTest(testCase, result)
afterTestFn(testCase, result)
}

override fun beforeSpec(spec: Spec) {
super.beforeSpec(spec)
beforeSpecFn(spec)
}

override fun afterSpec(spec: Spec) {
super.afterSpec(spec)
afterSpecFn(spec)
}

override fun tags(): Set<Tag> {
return super.tags()
}

override fun afterSpecClass(spec: Spec, results: Map<TestCase, TestResult>) {
super.afterSpecClass(spec, results)
afterSpecClassFn(spec, results)
}

fun set(isolationMode: IsolationMode) {
this.isolationMode = isolationMode
}

fun set(assertionMode: AssertionMode) {
this.assertionMode = assertionMode
}

fun set(testCaseOrder: TestCaseOrder) {
this.testCaseOrder = testCaseOrder
}

fun tags(vararg tags: Tag) {
this.tags = tags.toSet()
}

fun listeners(vararg listeners: TestListener) {
this.listeners = listeners.toList()
}

fun extensions(vararg extensions: SpecLevelExtension) {
this.extensions = extensions.toList()
}

fun beforeTest(f: (TestCase) -> Unit) {
beforeTestFn = f
}

fun afterTest(f: (TestCase, TestResult) -> Unit) {
afterTestFn = f
}

fun beforeSpec(f: (Spec) -> Unit) {
beforeSpecFn = f
}

fun afterSpec(f: (Spec) -> Unit) {
afterSpecFn = f
}

fun afterSpecClass(f: (Spec, Map<TestCase, TestResult>) -> Unit) {
afterSpecClassFn = f
}
}
@@ -15,7 +15,7 @@ import kotlin.time.ExperimentalTime
* }
*
*/
abstract class AbstractStringSpec(body: AbstractStringSpec.() -> Unit = {}) : AbstractSpec() {
abstract class AbstractStringSpec(body: AbstractStringSpec.() -> Unit = {}) : AbstractSpecDsl() {

init {
body()
@@ -22,7 +22,7 @@ import kotlin.time.ExperimentalTime
*
*/
@Suppress("FunctionName")
abstract class AbstractWordSpec(body: AbstractWordSpec.() -> Unit = {}) : AbstractSpec() {
abstract class AbstractWordSpec(body: AbstractWordSpec.() -> Unit = {}) : AbstractSpecDsl() {

init {
body()
@@ -1,6 +1,6 @@
package io.kotlintest

actual fun Spec.resolvedAssertionMode(): AssertionMode = when (val v = this.assertionMode) {
actual fun Spec.resolvedAssertionMode(): AssertionMode = when (val v = this.assertionMode()) {
null -> Project.assertionMode()
else -> v
}

0 comments on commit 7adc197

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