I am currently using the Boost MSM library to write a state machine and I want to write unit tests to check transitions between its states. For each unit test, I need to write repetitive lines of code to reach the state from which I want to start. Therefore I would like to know if their is a way to start the state machine in a given state instead of the starting state.
For example if I have a simple state machine like this that usually starts at StartingState, I would like to reach directly IdleState to do my test :
- --> StartingState --> IdleState --> ErrorState
Boost.MSM doesn't directly support the functionality that you want.
But you can control the initial state using
initial_stateinner type and preprocessor macro.Let's say your state machine is defined in
sm1.hpp.sm1.hpp
test.cpp
Demo: https://wandbox.org/permlink/dnLrAZ7fTJhg473q
The key point is the following code:
You can set any state as the initial state. Define initial state before including
sm1.hpplike as follows: