我们使用的是boost statechart库,但在为代码编写单元测试时遇到了问题。
在我们的正常执行中,状态机在ClosedState中启动
struct BoostStateMachine : sc::state_machine<BoostStateMachine, ClosedState >我们希望测试特定的状态转换,而不必遍历状态机直到该状态,例如,我们希望在AnotherState中开始测试。问题是sc::state_machine是在其初始状态上模板化的。向状态机提供导致测试状态的所有事件,通常需要大量工作,并使测试复杂化。
原始的解决方案是编写特殊的仅调试事件,并将其添加到ClosedState。此事件将触发立即转换到AnotherState。
你知道完成这项任务的其他方法吗?
发布于 2010-11-17 21:21:29
我承认这不是很好,但是
#ifdef DEBUG
typedef AnotherState StartingState;
#else
typedef ClosedState StartingState;
#endif
struct BoostStateMachine : sc::state_machine<BoostStateMachine, StartingState > {...编辑地址注释
#ifndef INITIAL_STATE
#define INITIAL_STATE ClosedState
#endif
struct BoostStateMachine : sc::state_machine<BoostStateMachine, INITIAL_STATE > {...当然,这意味着您需要重新编译以执行每个test =[
我们可以尝试以下操作:
typedef<class InitialState>
struct StateMachine : sc::state_machine< typename /*?*/ StateMachine<InitialState>, InitialState > {...}
typedef StateMachine<ClosedState> BoostStateMachine; //default case
#ifdef DO_TESTS
...
StateMachine<AnotherState> astate1;
...
StateMachine<AnotherState2> astate2;
...
StateMachine<AnotherState3> astate3;
...
#endif当它是一个需要在不同状态下启动的子状态时,这当然没有帮助。但同样的事情也可能适用:
typedef <typename InitialChild>
struct ClosedState : sc::simple_state< ClosedState<InitialChild>, BoostStateMachine, InitialChild > {...};或者类似的东西。我以前做过模板化的状态(这样我就可以有共同的子状态序列),这是一个需要调试的皇家皮塔(更多的是为了状态图的其余部分)。
https://stackoverflow.com/questions/4203034
复制相似问题