Skip to content

Commit 946ae4c

Browse files
committed
protofsm: add generic type assertion to state machine tests
This commit introduces a new generic type assertion function `assertState` to the state machine tests. This function asserts that the state machine is currently in the expected state type and returns the state cast to that type. This allows us to directly access the fields of the state without having to perform a type assertion manually.
1 parent c580666 commit 946ae4c

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

protofsm/state_machine_test.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,20 @@ func (d *dummyStateSpent) IsTerminal() bool {
321321
return true
322322
}
323323

324-
func assertState[Event any, Env Environment](t *testing.T,
325-
m *StateMachine[Event, Env], expectedState State[Event, Env]) {
324+
// assertState asserts that the state machine is currently in the expected
325+
// state type and returns the state cast to that type.
326+
func assertState[Event any, Env Environment, S State[Event, Env]](t *testing.T,
327+
m *StateMachine[Event, Env], expectedState S) S {
326328

327329
state, err := m.CurrentState()
328330
require.NoError(t, err)
329331
require.IsType(t, expectedState, state)
332+
333+
// Perform the type assertion to return the concrete type.
334+
concreteState, ok := state.(S)
335+
require.True(t, ok, "state type assertion failed")
336+
337+
return concreteState
330338
}
331339

332340
func assertStateTransitions[Event any, Env Environment](
@@ -639,18 +647,15 @@ func TestStateMachineConfMapper(t *testing.T) {
639647
assertStateTransitions(t, stateSub, expectedStates)
640648

641649
// Final state assertion.
642-
finalState, err := stateMachine.CurrentState()
643-
require.NoError(t, err)
644-
require.IsType(t, &dummyStateConfirmed{}, finalState)
650+
finalState := assertState(t, &stateMachine, &dummyStateConfirmed{})
645651

646652
// Assert that the details from the confirmation event were correctly
647653
// propagated to the final state.
648-
finalStateDetails := finalState.(*dummyStateConfirmed)
649654
require.Equal(t,
650-
*simulatedConf.BlockHash, finalStateDetails.blockHash,
655+
*simulatedConf.BlockHash, finalState.blockHash,
651656
)
652657
require.Equal(t,
653-
simulatedConf.BlockHeight, finalStateDetails.blockHeight,
658+
simulatedConf.BlockHeight, finalState.blockHeight,
654659
)
655660

656661
adapters.AssertExpectations(t)
@@ -719,18 +724,15 @@ func TestStateMachineSpendMapper(t *testing.T) {
719724
assertStateTransitions(t, stateSub, expectedStates)
720725

721726
// Final state assertion.
722-
finalState, err := stateMachine.CurrentState()
723-
require.NoError(t, err)
724-
require.IsType(t, &dummyStateSpent{}, finalState)
727+
finalState := assertState(t, &stateMachine, &dummyStateSpent{})
725728

726729
// Assert that the details from the spend event were correctly
727730
// propagated to the final state.
728-
finalStateDetails := finalState.(*dummyStateSpent)
729731
require.Equal(t,
730-
*simulatedSpend.SpenderTxHash, finalStateDetails.spenderTxHash,
732+
*simulatedSpend.SpenderTxHash, finalState.spenderTxHash,
731733
)
732734
require.Equal(t,
733-
simulatedSpend.SpendingHeight, finalStateDetails.spendingHeight,
735+
simulatedSpend.SpendingHeight, finalState.spendingHeight,
734736
)
735737

736738
adapters.AssertExpectations(t)

0 commit comments

Comments
 (0)