-
Notifications
You must be signed in to change notification settings - Fork 4
/
AggregateStatesTest.kt
76 lines (64 loc) · 1.99 KB
/
AggregateStatesTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package de.halfbit.comachine.tests
import de.halfbit.comachine.comachine
import de.halfbit.comachine.startInScope
import de.halfbit.comachine.tests.utils.await
import de.halfbit.comachine.tests.utils.executeBlockingTest
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import kotlin.test.Test
class AggregateStatesTest {
sealed interface State {
data class Solid(val progress: String = "") : State
data class Liquid(val progress: String = "") : State
data class Gas(val progress: String = "") : State
}
sealed interface Event {
object OnHeat : Event
object OnCold : Event
}
@Test
fun test() {
val comachine = comachine<State, Event>(
startWith = State.Solid()
) {
whenIn<State.Solid> {
on<Event.OnHeat> {
transitionTo { State.Liquid() }
}
}
whenIn<State.Liquid> {
on<Event.OnHeat> {
transitionTo { State.Gas() }
}
on<Event.OnCold> {
transitionTo { State.Solid() }
}
}
whenIn<State.Gas> {
on<Event.OnCold> {
transitionTo { State.Liquid() }
}
}
}
executeBlockingTest {
launch {
comachine.state.collect {
println("------ $it")
}
}
comachine.startInScope(this)
println("OnHeat")
comachine.send(Event.OnHeat)
comachine.await<State.Liquid>()
println("OnHeat")
comachine.send(Event.OnHeat)
comachine.await<State.Gas>()
println("OnCold")
comachine.send(Event.OnCold)
comachine.await<State.Liquid>()
println("OnCold")
comachine.send(Event.OnCold)
comachine.await<State.Solid>()
}
}
}