|
1 |
| -package cafe.adriel.hal |
2 |
| - |
3 |
| -import cafe.adriel.hal.util.TestStateMachine |
4 |
| -import cafe.adriel.hal.util.TurnstileAction |
5 |
| -import cafe.adriel.hal.util.TurnstileState |
6 |
| -import kotlinx.coroutines.test.TestCoroutineDispatcher |
7 |
| -import kotlinx.coroutines.test.TestCoroutineScope |
8 |
| -import kotlinx.coroutines.test.runBlockingTest |
9 |
| -import org.junit.After |
10 |
| -import org.junit.Test |
11 |
| -import strikt.api.expectThat |
12 |
| -import strikt.api.expectThrows |
13 |
| -import strikt.assertions.isEqualTo |
14 |
| - |
15 |
| -class StateMachineTest { |
16 |
| - |
17 |
| - private val testScope = TestCoroutineScope() |
18 |
| - private val testDispatcher = TestCoroutineDispatcher() |
19 |
| - |
20 |
| - private val observer = CallbackStateObserver<TurnstileState> { } |
21 |
| - private val stateMachine = TestStateMachine(testScope, testDispatcher) |
22 |
| - |
23 |
| - @After |
24 |
| - fun tearDown() { |
25 |
| - testScope.cleanupTestCoroutines() |
26 |
| - } |
27 |
| - |
28 |
| - @Test |
29 |
| - fun `when set the observer with a StateObserver instance then set the initial state`() = runBlockingTest { |
30 |
| - stateMachine.observeState(observer) |
31 |
| - |
32 |
| - expectThat(stateMachine.currentState).isEqualTo(TurnstileState.Locked) |
33 |
| - } |
34 |
| - |
35 |
| - @Test |
36 |
| - fun `when set the observer with a lambda then set the initial state`() = runBlockingTest { |
37 |
| - stateMachine.observeState { } |
38 |
| - |
39 |
| - expectThat(stateMachine.currentState).isEqualTo(TurnstileState.Locked) |
40 |
| - } |
41 |
| - |
42 |
| - @Test |
43 |
| - fun `when emit an action then transition to the next state`() = runBlockingTest { |
44 |
| - stateMachine.observeState(observer) |
45 |
| - stateMachine + TurnstileAction.InsertCoin |
46 |
| - |
47 |
| - expectThat(stateMachine.currentState).isEqualTo(TurnstileState.Unlocked) |
48 |
| - } |
49 |
| - |
50 |
| - @Test |
51 |
| - fun `when emit an action without set the observer then throw exception`() = runBlockingTest { |
52 |
| - expectThrows<UninitializedPropertyAccessException> { |
53 |
| - stateMachine + TurnstileAction.InsertCoin |
54 |
| - } |
55 |
| - } |
56 |
| - |
57 |
| - @Test |
58 |
| - fun `when get the current state without set the observer then throw exception`() = runBlockingTest { |
59 |
| - expectThrows<UninitializedPropertyAccessException> { |
60 |
| - expectThat(stateMachine.currentState).isEqualTo(TurnstileState.Unlocked) |
61 |
| - } |
62 |
| - } |
63 |
| -} |
| 1 | +package cafe.adriel.hal |
| 2 | + |
| 3 | +import cafe.adriel.hal.util.TestStateMachine |
| 4 | +import cafe.adriel.hal.util.TurnstileAction |
| 5 | +import cafe.adriel.hal.util.TurnstileState |
| 6 | +import io.mockk.coVerify |
| 7 | +import io.mockk.spyk |
| 8 | +import kotlinx.coroutines.test.TestCoroutineScope |
| 9 | +import kotlinx.coroutines.test.runBlockingTest |
| 10 | +import org.junit.After |
| 11 | +import org.junit.Test |
| 12 | +import strikt.api.expectThat |
| 13 | +import strikt.api.expectThrows |
| 14 | +import strikt.assertions.isEqualTo |
| 15 | + |
| 16 | +class StateMachineTest { |
| 17 | + |
| 18 | + private val testScope = TestCoroutineScope() |
| 19 | + |
| 20 | + private val observer = CallbackStateObserver<TurnstileState> { } |
| 21 | + private val reducer = spyk(::suspendReducer) |
| 22 | + private val stateMachine = TestStateMachine(reducer) |
| 23 | + |
| 24 | + @After |
| 25 | + fun tearDown() { |
| 26 | + testScope.cleanupTestCoroutines() |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + fun `when set the observer with a StateObserver instance then set the initial state`() = runBlockingTest { |
| 31 | + stateMachine.observeState(observer) |
| 32 | + |
| 33 | + expectThat(stateMachine.currentState).isEqualTo(TurnstileState.Locked) |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + fun `when set the observer with a lambda then set the initial state`() = runBlockingTest { |
| 38 | + stateMachine.observeState { } |
| 39 | + |
| 40 | + expectThat(stateMachine.currentState).isEqualTo(TurnstileState.Locked) |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `when emit an action then transition to the next state`() = runBlockingTest { |
| 45 | + stateMachine.observeState(observer) |
| 46 | + |
| 47 | + stateMachine + TurnstileAction.InsertCoin |
| 48 | + |
| 49 | + coVerify { reducer(TurnstileAction.InsertCoin, any()) } |
| 50 | + |
| 51 | + expectThat(stateMachine.currentState).isEqualTo(TurnstileState.Unlocked) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun `when emit an action without set the observer then throw exception`() = runBlockingTest { |
| 56 | + expectThrows<UninitializedPropertyAccessException> { |
| 57 | + stateMachine + TurnstileAction.InsertCoin |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + fun `when get the current state without set the observer then throw exception`() = runBlockingTest { |
| 63 | + expectThrows<UninitializedPropertyAccessException> { |
| 64 | + expectThat(stateMachine.currentState).isEqualTo(TurnstileState.Unlocked) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private suspend fun suspendReducer(action: TurnstileAction, transitionTo: (TurnstileState) -> Unit) = |
| 69 | + when (action) { |
| 70 | + is TurnstileAction.InsertCoin -> transitionTo(TurnstileState.Unlocked) |
| 71 | + is TurnstileAction.Push -> transitionTo(TurnstileState.Locked) |
| 72 | + } |
| 73 | +} |
0 commit comments