commonMain.com.copperleaf.ballast.test.internal.vm.TestInterceptor.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ballast-test-js Show documentation
Show all versions of ballast-test-js Show documentation
Opinionated Kotlin multiplatform MVI library
package com.copperleaf.ballast.test.internal.vm
import com.copperleaf.ballast.BallastInterceptor
import com.copperleaf.ballast.BallastNotification
import com.copperleaf.ballast.SideEffectScope
import com.copperleaf.ballast.test.TestResults
/**
* An internal class used to keep the test framework
*/
internal class TestInterceptor :
BallastInterceptor, Events, State> {
private val acceptedInputs = mutableListOf()
private val rejectedInputs = mutableListOf()
private val droppedInputs = mutableListOf()
private val successfulInputs = mutableListOf()
private val cancelledInputs = mutableListOf()
private val inputHandlerErrors = mutableListOf>()
private val events = mutableListOf()
private val successfulEvents = mutableListOf()
private val eventHandlerErrors = mutableListOf>()
private val states = mutableListOf()
private val sideEffects = mutableListOf>()
private val completedSideEffects = mutableListOf()
private val sideEffectErrors = mutableListOf>()
private val unhandledErrors = mutableListOf()
private inline fun TestViewModel.Inputs.unwrap(block: (Inputs) -> Unit) {
when (this) {
is TestViewModel.Inputs.AwaitInput -> {
block(this.normalInput)
}
is TestViewModel.Inputs.ProcessInput -> {
block(this.normalInput)
}
is TestViewModel.Inputs.TestCompleted -> {
}
}
}
override suspend fun onNotify(notification: BallastNotification, Events, State>) {
when (notification) {
is BallastNotification.InputAccepted -> {
notification.input.unwrap { acceptedInputs += it }
}
is BallastNotification.InputRejected -> {
notification.input.unwrap { rejectedInputs += it }
}
is BallastNotification.InputDropped -> {
notification.input.unwrap { droppedInputs += it }
}
is BallastNotification.InputHandledSuccessfully -> {
notification.input.unwrap { successfulInputs += it }
}
is BallastNotification.InputCancelled -> {
notification.input.unwrap { cancelledInputs += it }
}
is BallastNotification.InputHandlerError -> {
notification.input.unwrap { inputHandlerErrors += it to notification.throwable }
}
is BallastNotification.EventEmitted -> {
events += notification.event
}
is BallastNotification.EventHandledSuccessfully -> {
successfulEvents += notification.event
}
is BallastNotification.EventHandlerError -> {
eventHandlerErrors += notification.event to notification.throwable
}
is BallastNotification.StateChanged -> {
states += notification.state
}
is BallastNotification.SideEffectStarted -> {
sideEffects += notification.key to notification.restartState
}
is BallastNotification.SideEffectCompleted -> {
completedSideEffects += notification.key
}
is BallastNotification.SideEffectError -> {
sideEffectErrors += notification.key to notification.throwable
}
is BallastNotification.UnhandledError -> {
unhandledErrors += notification.throwable
}
is BallastNotification.ViewModelStarted -> {}
is BallastNotification.ViewModelCleared -> {}
is BallastNotification.EventProcessingStarted -> {}
is BallastNotification.EventProcessingStopped -> {}
}
}
internal suspend fun getResults(): TestResults {
// wait for the final notification to be received
return TestResults(
acceptedInputs = acceptedInputs.toList(),
rejectedInputs = rejectedInputs.toList(),
droppedInputs = droppedInputs.toList(),
successfulInputs = successfulInputs.toList(),
cancelledInputs = cancelledInputs.toList(),
inputHandlerErrors = inputHandlerErrors.toList(),
events = events.toList(),
eventHandlerErrors = eventHandlerErrors.toList(),
states = states.toList(),
sideEffects = sideEffects.toList(),
sideEffectErrors = sideEffectErrors.toList(),
unhandledErrors = unhandledErrors.toList(),
)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy