Skip to content

Commit 908a35e

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 a317ca6 commit 908a35e

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

protofsm/state_machine_test.go

+16-14
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](
@@ -626,18 +634,15 @@ func TestStateMachineConfMapper(t *testing.T) {
626634
assertStateTransitions(t, stateSub, expectedStates)
627635

628636
// Final state assertion.
629-
finalState, err := stateMachine.CurrentState()
630-
require.NoError(t, err)
631-
require.IsType(t, &dummyStateConfirmed{}, finalState)
637+
finalState := assertState(t, &stateMachine, &dummyStateConfirmed{})
632638

633639
// Assert that the details from the confirmation event were correctly
634640
// propagated to the final state.
635-
finalStateDetails := finalState.(*dummyStateConfirmed)
636641
require.Equal(t,
637-
*simulatedConf.BlockHash, finalStateDetails.blockHash,
642+
*simulatedConf.BlockHash, finalState.blockHash,
638643
)
639644
require.Equal(t,
640-
simulatedConf.BlockHeight, finalStateDetails.blockHeight,
645+
simulatedConf.BlockHeight, finalState.blockHeight,
641646
)
642647

643648
adapters.AssertExpectations(t)
@@ -706,18 +711,15 @@ func TestStateMachineSpendMapper(t *testing.T) {
706711
assertStateTransitions(t, stateSub, expectedStates)
707712

708713
// Final state assertion.
709-
finalState, err := stateMachine.CurrentState()
710-
require.NoError(t, err)
711-
require.IsType(t, &dummyStateSpent{}, finalState)
714+
finalState := assertState(t, &stateMachine, &dummyStateSpent{})
712715

713716
// Assert that the details from the spend event were correctly
714717
// propagated to the final state.
715-
finalStateDetails := finalState.(*dummyStateSpent)
716718
require.Equal(t,
717-
*simulatedSpend.SpenderTxHash, finalStateDetails.spenderTxHash,
719+
*simulatedSpend.SpenderTxHash, finalState.spenderTxHash,
718720
)
719721
require.Equal(t,
720-
simulatedSpend.SpendingHeight, finalStateDetails.spendingHeight,
722+
simulatedSpend.SpendingHeight, finalState.spendingHeight,
721723
)
722724

723725
adapters.AssertExpectations(t)

0 commit comments

Comments
 (0)