1

我们正在使用 boost 状态图库,并且在为代码编写单元测试时遇到了麻烦。

在我们的正常执行中,状态机开始于ClosedState

struct BoostStateMachine : sc::state_machine<BoostStateMachine, ClosedState >

我们想测试一个特定的状态转换,而不必遍历状态机直到那个状态,例如我们想在AnotherState. 问题在于sc::state_machine它的初始状态是模板化的。向状态机提供导致测试状态的所有事件通常需要大量工作并使测试复杂化。

一个原始的解决方案是编写特殊的仅调试事件并将其添加到ClosedState. 此事件将触发立即转换为AnotherState.

你知道完成任务的其他方法吗?

4

1 回答 1

0

我承认这不是很好,但是

#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 > {...

当然,这意味着您需要重新编译才能进行每个测试 =[

我们可以尝试以下方法:

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 > {...};

或类似的东西。我以前做过模板化状态(这样我就可以有共同的子状态序列),它是一个皇家 PITA 来调试(更多的是状态图的其余部分)。

于 2010-11-17T13:21:28.803 回答