Skip to content

Commit

Permalink
EVA-SPECS generics in uow verification (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rattenkrieg committed Oct 1, 2023
1 parent 9c41f33 commit a5f013f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 10 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/CI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import java.lang.Boolean.parseBoolean
object Ci {

private const val SNAPSHOT_BASE = "0.5.0"
private const val RELEASE_VERSION = "0.4.0"
private const val RELEASE_VERSION = "0.3.5"
private val githubSha = System.getenv("GITHUB_SHA") ?: "latest"

val publishRelease = System.getProperty("release", "true").let(::parseBoolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@ package com.razz.eva.test.uow

import com.razz.eva.uow.Clocks.fixedUTC
import com.razz.eva.uow.Clocks.millisUTC
import com.razz.eva.uow.verify.EqualityVerifier
import com.razz.eva.uow.verify.EqualityVerifierAware
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe

abstract class UowBehaviorSpec(
body: UowBehaviorSpec.() -> Unit = {}
) : BehaviorSpec() {
) : BehaviorSpec(), EqualityVerifierAware {

val now = millisUTC().instant()
val clock = fixedUTC(now)

private object KotestEqualityVerifier : EqualityVerifier {
override fun <T> verify(expected: T, actual: T) {
expected shouldBe actual
}
}

override val equalityVerifier: EqualityVerifier = KotestEqualityVerifier

init {
this.body()
}
Expand Down
34 changes: 32 additions & 2 deletions eva-uow/src/main/kotlin/com/razz/eva/uow/verify/UowSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,39 @@ package com.razz.eva.uow.verify

import com.razz.eva.domain.Model
import com.razz.eva.domain.ModelEvent
import com.razz.eva.domain.ModelId
import com.razz.eva.uow.Changes

interface EqualityVerifierAware {
val equalityVerifier: EqualityVerifier
}

interface EqualityVerifier {
fun <T> verify(expected: T, actual: T)
}

infix fun <R> Changes<R>.verifyInOrder(block: UowSpec<R>.() -> Unit) {
val spec = UowSpec(this)
block(spec)
spec.verifyEnd()
}

class UowSpec<R> internal constructor(
changes: Changes<R>
changes: Changes<R>,
) : UowSpecBase<R>(changes) {

fun returnsEq(expected: R) = returns {
fun <RR : R> returnsEq(expected: RR) = returnsAs<RR> {
check(expected == this) { "Result doesn't match" }
}

fun returns(verifyResult: R.() -> Unit) {
verifyResult(verifyResult)
}

fun <RR : R> returnsAs(verifyResult: RR.() -> Unit) {
verifyResultAs(verifyResult)
}

fun <M : Model<*, *>> addsEq(expected: M) {
verifyAdded<M> { actual ->
check(actual == expected) { "Got unexpected add of [$actual]" }
Expand All @@ -32,6 +45,12 @@ class UowSpec<R> internal constructor(
return verifyAdded(verify)
}

fun <M : Model<*, *>> EqualityVerifierAware.adds(id: ModelId<*>, verify: M.() -> Unit): M {
val verified = verifyAdded(verify)
equalityVerifier.verify(verified.id(), id)
return verified
}

fun <M : Model<*, *>> updatesEq(expected: M) {
verifyUpdated<M> { actual ->
check(actual == expected) { "Got unexpected update of [$actual]" }
Expand All @@ -42,6 +61,12 @@ class UowSpec<R> internal constructor(
return verifyUpdated(verify)
}

fun <M : Model<*, *>> EqualityVerifierAware.updates(id: ModelId<*>, verify: M.() -> Unit): M {
val verified = verifyUpdated(verify)
equalityVerifier.verify(verified.id(), id)
return verified
}

fun <E : ModelEvent<*>> emitsEq(expected: E) {
verifyEmitted<E> { actual ->
check(actual == expected) { "Got unexpected emit of [$actual]" }
Expand All @@ -51,4 +76,9 @@ class UowSpec<R> internal constructor(
fun <E : ModelEvent<*>> emits(verify: E.() -> Unit) {
verifyEmitted(verify)
}

fun <E : ModelEvent<*>> EqualityVerifierAware.emits(id: ModelId<*>, verify: E.() -> Unit) {
val verified = verifyEmitted(verify)
equalityVerifier.verify(verified.modelId, id)
}
}
10 changes: 8 additions & 2 deletions eva-uow/src/main/kotlin/com/razz/eva/uow/verify/UowSpecBase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ open class UowSpecBase<R> private constructor(
) {

internal constructor(
changes: Changes<R>
changes: Changes<R>,
) : this(
result = changes.result,
executionHistory = ArrayDeque(changes.toPersist.filter { it !is Noop }),
Expand All @@ -34,6 +34,11 @@ open class UowSpecBase<R> private constructor(
verification(result)
}

protected fun <RR : R> verifyResultAs(verification: (RR) -> Unit) {
@Suppress("UNCHECKED_CAST")
verification(result as RR)
}

protected fun <M : Model<*, *>> verifyAdded(verify: (M) -> Unit): M {
val model = when (val next = checkNotNull(executionHistory.pollFirst()) { "Expecting [Add] got nothing" }) {
is Add<*, *, *> -> {
Expand All @@ -60,9 +65,10 @@ open class UowSpecBase<R> private constructor(
return model
}

protected fun <E : ModelEvent<out ModelId<out Comparable<*>>>> verifyEmitted(verify: (E) -> Unit) {
protected fun <E : ModelEvent<out ModelId<out Comparable<*>>>> verifyEmitted(verify: (E) -> Unit): E {
@Suppress("UNCHECKED_CAST")
val next = checkNotNull(publishedEvents.pollFirst()) { "Expecting [ModelEvent] got nothing" } as E
verify(next)
return next
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ class UnitOfWorkDemoSpec : UowBehaviorSpec({
employeeId shouldBe boomerId
newHeadcount shouldBe 1
}

returns {
this shouldBe Unit
}
returnsEq(Unit)
}
}
}
Expand Down

0 comments on commit a5f013f

Please sign in to comment.