diff --git a/client/src/main/kotlin/com/r3corda/client/testing/Expect.kt b/client/src/main/kotlin/com/r3corda/client/testing/Expect.kt index 9281af5b85..f3d1113dc8 100644 --- a/client/src/main/kotlin/com/r3corda/client/testing/Expect.kt +++ b/client/src/main/kotlin/com/r3corda/client/testing/Expect.kt @@ -17,7 +17,7 @@ import rx.Observable * * Example usage: * - * val stream: Ovservable = (..) + * val stream: Observable = (..) * stream.expectEvents( * sequence( * expect { event: SomeEvent.A -> require(event.isOk()) }, @@ -118,17 +118,75 @@ internal data class Expect( ) private sealed class ExpectComposeState { - class Finished : ExpectComposeState() - class Single(val single: ExpectCompose.Single) : ExpectComposeState() + + abstract fun nextState(event: E): Pair<() -> Unit, ExpectComposeState>? + abstract fun getExpectedEvents(): List> + + class Finished : ExpectComposeState() { + override fun nextState(event: E) = null + override fun getExpectedEvents(): List> = listOf() + } + class Single(val single: ExpectCompose.Single) : ExpectComposeState() { + override fun nextState(event: E): Pair<() -> Unit, ExpectComposeState>? = + if (single.expect.clazz.isAssignableFrom(event.javaClass)) { + @Suppress("UNCHECKED_CAST") + Pair({ single.expect.expectClosure(event) }, Finished()) + } else { + null + } + override fun getExpectedEvents() = listOf(single.expect.clazz) + } + class Sequential( val sequential: ExpectCompose.Sequential, val index: Int, val state: ExpectComposeState - ) : ExpectComposeState() + ) : ExpectComposeState() { + override fun nextState(event: E): Pair<() -> Unit, ExpectComposeState>? { + val next = state.nextState(event) + return if (next == null) { + null + } else if (next.second is Finished) { + if (index == sequential.sequence.size - 1) { + Pair(next.first, Finished()) + } else { + val nextState = fromExpectCompose(sequential.sequence[index + 1]) + if (nextState is Finished) { + Pair(next.first, Finished()) + } else { + Pair(next.first, Sequential(sequential, index + 1, nextState)) + } + } + } else { + Pair(next.first, Sequential(sequential, index, next.second)) + } + } + + override fun getExpectedEvents() = state.getExpectedEvents() + } + class Parallel( val parallel: ExpectCompose.Parallel, val states: List> - ) : ExpectComposeState() + ) : ExpectComposeState() { + override fun nextState(event: E): Pair<() -> Unit, ExpectComposeState>? { + states.forEachIndexed { stateIndex, state -> + val next = state.nextState(event) + if (next != null) { + val nextStates = states.mapIndexed { i, expectComposeState -> + if (i == stateIndex) next.second else expectComposeState + } + if (nextStates.all { it is Finished }) { + return Pair(next.first, Finished()) + } else { + return Pair(next.first, Parallel(parallel, nextStates)) + } + } + } + return null + } + override fun getExpectedEvents() = states.flatMap { it.getExpectedEvents() } + } companion object { fun fromExpectCompose(expectCompose: ExpectCompose): ExpectComposeState { @@ -151,62 +209,4 @@ private sealed class ExpectComposeState { } } } - - fun getExpectedEvents(): List> { - return when (this) { - is ExpectComposeState.Finished -> listOf() - is ExpectComposeState.Single -> listOf(single.expect.clazz) - is ExpectComposeState.Sequential -> state.getExpectedEvents() - is ExpectComposeState.Parallel -> states.flatMap { it.getExpectedEvents() } - } - } - - fun nextState(event: E): Pair<() -> Unit, ExpectComposeState>? { - return when (this) { - is ExpectComposeState.Finished -> null - is ExpectComposeState.Single -> { - if (single.expect.clazz.isAssignableFrom(event.javaClass)) { - @Suppress("UNCHECKED_CAST") - Pair({ single.expect.expectClosure(event) }, Finished()) - } else { - null - } - } - is ExpectComposeState.Sequential -> { - val next = state.nextState(event) - if (next == null) { - null - } else if (next.second is Finished) { - if (index == sequential.sequence.size - 1) { - Pair(next.first, Finished()) - } else { - val nextState = fromExpectCompose(sequential.sequence[index + 1]) - if (nextState is Finished) { - Pair(next.first, Finished()) - } else { - Pair(next.first, Sequential(sequential, index + 1, nextState)) - } - } - } else { - Pair(next.first, Sequential(sequential, index, next.second)) - } - } - is ExpectComposeState.Parallel -> { - states.forEachIndexed { stateIndex, state -> - val next = state.nextState(event) - if (next != null) { - val nextStates = states.mapIndexed { i, expectComposeState -> - if (i == stateIndex) next.second else expectComposeState - } - if (nextStates.all { it is Finished }) { - return Pair(next.first, Finished()) - } else { - return Pair(next.first, Parallel(parallel, nextStates)) - } - } - } - null - } - } - } }