com.oneeyedmen.konsent.acceptance-testing.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of konsent Show documentation
Show all versions of konsent Show documentation
An acceptance testing library
package com.oneeyedmen.konsent
import com.natpryce.hamkrest.MatchResult
import com.natpryce.hamkrest.Matcher
fun Given(actor: Actor, function: Steps.() -> Unit) = function.invoke(givenSteps(actor))
fun Given(actor: Actor) = givenSteps(actor)
fun When(actor: Actor, function: Steps.() -> Unit) = function.invoke(whenSteps(actor))
fun When(actor: Actor) = whenSteps(actor)
fun Then(actor: Actor, function: Steps.() -> Unit) = function.invoke(thenSteps(actor))
fun Then(actor: Actor) = thenSteps(actor)
fun Steps.shouldSee(selector: Selector, matcher: Matcher) {
val matchResult = matcher.invoke(selector.select(actor))
val resultString = when(matchResult) {
is MatchResult.Mismatch -> "[expected ${matcher.description}, ${matchResult.description}]"
else -> matcher.description
}
record("sees", selector.description, resultString)
}
class Steps(val actor: Actor, val driver: DriverT, val recorder: FeatureRecorder, private val term: String?) {
fun describedBy(stepName: String, block: Steps.() -> Unit) {
record(stepName)
block()
}
fun record(vararg args: Any) = recorder.term(term, actor.name, *args)
}
fun anonymousSteps(actor: Actor) = Steps(actor, actor.driver, actor.recorder, null)
fun givenSteps(actor: Actor) = Steps(actor, actor.driver, actor.recorder, "Given")
fun whenSteps(actor: Actor) = Steps(actor, actor.driver, actor.recorder, "When")
fun thenSteps(actor: Actor) = Steps(actor, actor.driver, actor.recorder, "Then")