Skip to content

Commit a00a279

Browse files
committed
Refactor tests
1 parent 0c88181 commit a00a279

File tree

2 files changed

+81
-75
lines changed

2 files changed

+81
-75
lines changed
Lines changed: 73 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,73 @@
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+
}

hal-core/src/test/kotlin/cafe/adriel/hal/util/TestStateMachine.kt

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@ package cafe.adriel.hal.util
22

33
import cafe.adriel.hal.HAL
44
import io.mockk.spyk
5-
import kotlinx.coroutines.CoroutineDispatcher
6-
import kotlinx.coroutines.CoroutineScope
5+
import kotlinx.coroutines.test.TestCoroutineDispatcher
6+
import kotlinx.coroutines.test.TestCoroutineScope
77

88
class TestStateMachine(
9-
scope: CoroutineScope,
10-
dispatcher: CoroutineDispatcher
9+
reducer: suspend (TurnstileAction, (TurnstileState) -> Unit) -> Unit
1110
) : HAL.StateMachine<TurnstileAction, TurnstileState> {
1211

13-
override val hal by spyk(
14-
HAL(scope, TurnstileState.Locked, reducerDispatcher = dispatcher, reducer = ::reducer)
12+
override val hal by spyk(HAL<TurnstileAction, TurnstileState>(
13+
scope = TestCoroutineScope(),
14+
initialState = TurnstileState.Locked,
15+
reducerDispatcher = TestCoroutineDispatcher(),
16+
reducer = reducer)
1517
)
16-
17-
private suspend fun reducer(action: TurnstileAction, transitionTo: (TurnstileState) -> Unit) =
18-
when (action) {
19-
is TurnstileAction.InsertCoin -> transitionTo(TurnstileState.Unlocked)
20-
is TurnstileAction.Push -> transitionTo(TurnstileState.Locked)
21-
}
2218
}
2319

2420
sealed class TurnstileAction : HAL.Action {

0 commit comments

Comments
 (0)