测试作为 Map 实现的状态机转换



我有一个状态机,其中包含相对较小的状态和输入集,我想详尽地测试转换。
转换是用Map<State, Map<Input, State>>编码的,代码是这样的:

enum State {
S1,
// ...
}
enum Input {
I1,
// ...
}
class StateMachine {
State current;
Map<State, Map<Input, State>> transitions = {
S1: {
I1: S2,
// ...
},
// ...
};
State changeState(Input x) {
if (transitions[current] == null)
throw Error('Unknows state ${current}');
if (transitions[current][x] == null)
throw Error('Unknown transition from state ${current} with input ${x}');
current = transitions[current][x];
return current;
}
void execute() {
// ...
}
}

为了测试它,我看到了 3 种方法:
1( 编写大量样板代码来检查每个组合
2( 自动化测试创建:这对我来说似乎是一种更好的方法,但这最终会使用与 StateMachine 中使用的 Map 相同的结构。我该怎么办?复制测试文件中的 Map 还是从实现文件导入它?后者会使测试文件依赖于实现,似乎不是一个好主意。
3(测试地图的平等,与以前相同的问题:与自身平等还是与副本平等?这种方法本质上是我对其他 2 个所做的,但似乎不像是规范测试

也许你想看看这个: https://www.itemis.com/en/yakindu/state-machine/documentation/user-guide/sctunit_test-driven_statechart_development_with_sctunit

它展示了如何对状态机进行基于模型和测试驱动的开发,包括生成单元测试代码和测量测试覆盖率的选项。

最新更新