samedi 1 septembre 2018

Testing state machine transitions implemented as Map

I have a state machine with a relatively small set of states and inputs and I want to test the transitions exhaustively.
Transitions are coded using a Map<State, Map<Input, State>>, the code is something like this:

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() {
        // ...
    }
}

To test it I see 3 approaches:
1) Write lot of boilerplate code to check every single combination
2) Automate the tests creation: this seems a better approach to me, but this would end up using a structure that is identical to the Map used in the StateMachine. What should I do? Copy the Map in the test file or import it from the implementation file? The latter would make the test file depend on the implementation and doesn't seem a good idea.
3) Test Map for equality, same problem as before: equality with itself or with a copy? This approach is essentially what I do with the other 2 but doesn't seem like a canonical test

Aucun commentaire:

Enregistrer un commentaire