com.ubertob.pesticide.core.DomainDrivenTest.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pesticide-core Show documentation
Show all versions of pesticide-core Show documentation
A Library To Write Domain-Driven Tests
package com.ubertob.pesticide.core
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.DynamicContainer
import org.junit.jupiter.api.TestFactory
import org.junit.jupiter.api.fail
import java.time.LocalDate
import java.util.function.Consumer
import java.util.stream.Stream
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
typealias DDT = TestFactory
/**
* DomainDrivenTest is the base class to inherit to create a test.
* A test should look something like:
*
*
* class MyDDT : DomainDrivenTest(actionsForAllProtocols()) {
*
* val adam by NamedActor(::MyAUser)
*
* @DDT
* fun `do something`() = ddtScenario {
* setup {
* preparation()
* }.thenPlay(
* adam.`do this`()
* adam.`do that`()
* }
* }
*
*
*
* See also {@link DdtScenario}
*/
abstract class DomainDrivenTest>(private val actionsSet: Iterable) {
fun play(vararg stepsArray: DdtStep): DdtScenario =
DdtScenario(withoutSetting, stepsArray.toList())
/**
* wip
*
* Mark a test as Work In Progress until a given date. It's possible to specify that some protocols are not in WIP (that is the test should work)
*/
fun DdtScenario.wip(
dueDate: LocalDate,
reason: String = "Work In Progress",
except: Set> = emptySet()
): DdtScenario =
this.copy(wipData = WipData(dueDate, except, reason))
/**
* ddtScenario
*
* Define a use case for a test.
* Example of use:
*
*
* @DDT
* fun `do something`() = ddtScenario { protocol ->
*
* val secret = getCredentials(protocol)
*
* setUp {
* preparation()
* }.thenPlay(
* adam.`can do this`(secret)
* adam.`can do that`()
* }
* }
*
*
*/
fun ddtScenario(
scenarioBuilder: (DdtProtocol) -> DdtScenario
): Stream =
actionsSet.map { actions -> scenarioBuilder(actions.protocol)(actions) }
.ifEmpty { fail("No protocols selected!") }
.stream()
@AfterEach
fun pullDownActions() {
actionsSet.onEach { it.tearDown() }
}
@Deprecated("Use play() directly")
@JvmField
val withoutSetting: DdtSetup =
DdtSetup()
@Deprecated("Use setUp() method instead")
fun setting(block: D.() -> Unit): DdtSetup =
DdtSetup {
block(this)
}
fun setUp(block: D.() -> Unit): DdtSetup =
DdtSetup {
block(this)
}
fun DdtSetup.thenPlay(vararg stepsArray: DdtStep): DdtScenario =
DdtScenario(this, stepsArray.toList())
//useful for Java
fun onSetUp(block: Consumer): DdtSetup = setUp { block.accept(this) }
infix fun DdtSetup.atRise(steps: DdtScenario): DdtScenario =
steps.copy(ddtSetup = this)
class NamedActor, A : DdtActorWithContext>(val userConstructor: (String) -> A) :
ReadOnlyProperty, A> {
override operator fun getValue(thisRef: DomainDrivenTest, property: KProperty<*>): A =
userConstructor(property.name.replaceFirstChar { it.titlecase() })
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy