commonTest.com.zegreatrob.testmints.action.async.GeneralSuspendActionDispatcherTest.kt Maven / Gradle / Ivy
package com.zegreatrob.testmints.action.async
import com.zegreatrob.minassert.assertIsEqualTo
import com.zegreatrob.minspy.Spy
import com.zegreatrob.minspy.SpyData
import com.zegreatrob.minspy.spyFunction
import com.zegreatrob.testmints.async.asyncSetup
import kotlin.test.Test
private typealias DivideActionDispatcher = suspend (GeneralSuspendActionDispatcherTest.DivideAction) -> Int
class GeneralSuspendActionDispatcherTest {
data class DivideAction(val left: Int, val right: Int) : SimpleSuspendAction {
override val performFunc = link(DivideActionDispatcher::invoke)
}
@Test
fun syntaxAllowsInterceptionOfActionExecutionIncludingReplacingResult() =
asyncSetup(object : GeneralSuspendActionDispatcherSyntax {
val expectedReplacedResult = 127
override val generalDispatcher = generalDispatcherSpy().apply { spyWillReturn(expectedReplacedResult) }
val action = DivideAction(6, 7)
val divideDispatcherSpy = SpyData()
val divideDispatcher: DivideActionDispatcher = { divideDispatcherSpy.spyFunction(it) }
}) exercise {
divideDispatcher.execute(action)
} verify { result ->
result.assertIsEqualTo(expectedReplacedResult)
generalDispatcher.spyReceivedValues
.assertIsEqualTo(listOf(action to divideDispatcher))
divideDispatcherSpy.spyReceivedValues
.assertIsEqualTo(emptyList())
}
private fun generalDispatcherSpy() = object :
GeneralSuspendActionDispatcher,
Spy, *>, Any> by SpyData() {
@Suppress("UNCHECKED_CAST")
override suspend fun dispatch(action: SuspendAction, dispatcher: D): R =
spyFunction(action to dispatcher) as R
}
}