示例#1
0
def test_lifecycles_called(lifecycle_machine):
    machine = StateMachine(lifecycle_machine)
    lifecycles = machine.blueprint["lifecycles"]

    before_any = lifecycles["before"][0]["actions"][0]
    mock_before_any = create_autospec(before_any)
    lifecycles["before"][0]["actions"][0] = mock_before_any

    after_accepted = lifecycles["after"][0]["actions"][0]
    mock_after_accepted = create_autospec(after_accepted)
    lifecycles["after"][0]["actions"][0] = mock_after_accepted

    after_three = lifecycles["after"][0]["actions"][1]
    mock_after_three = create_autospec(after_three)
    lifecycles["after"][0]["actions"][1] = mock_after_three

    for event in range(2, 5):
        machine.transition(event)

        if event == 3:
            mock_after_accepted.assert_called_once()
            mock_after_accepted.assert_called_once()

    assert mock_before_any.call_count == 3
    assert mock_after_accepted.call_count == 1
    assert mock_after_three.call_count == 1
示例#2
0
def test_transition_accepted(simple_machine, event=3):
    machine = StateMachine(simple_machine)

    machine.transition(event)
    assert machine.state == event
    assert not machine.initial
    assert machine.accepted
    assert not machine.final
示例#3
0
def test_transition_function(simple_machine, event=1):
    mock_transition = MagicMock(return_value=event)
    simple_machine["transition"] = mock_transition
    machine = StateMachine(simple_machine)

    machine.transition(event)
    mock_transition.assert_called_once_with(machine.state, machine.context,
                                            event)
示例#4
0
def test_blueprint(simple_machine):
    machine = StateMachine(simple_machine)

    for state in simple_machine["validStates"]:
        assert machine.is_valid(state)

    for state in simple_machine["acceptedStates"]:
        assert machine.is_accepted(state)

    for state in simple_machine["finalStates"]:
        assert machine.is_final(state)

    assert machine.initial
    assert not machine.accepted
    assert not machine.final
示例#5
0
def test_transition_context(simple_machine, event=2):
    def transition(state, context, event):
        context["value"] = event
        return event

    simple_machine["transition"] = transition
    machine = StateMachine(simple_machine)

    assert machine.context == simple_machine.get("context", dict())

    machine.transition(event)

    assert machine.state == event
    assert machine.context["value"] == event
    assert not machine.initial
    assert not machine.accepted
    assert not machine.final
示例#6
0
def test_machine(binary_multiples):
    results = list()

    for _ in range(100):
        machine = StateMachine(binary_multiples)
        result = 0

        while not machine.is_accepted(machine.state) \
                or not result or result in results:
            event = choice(list(machine.blueprint["alphabet"]))

            machine.transition(event)

            result = machine.context["value"]

        print(f"{machine}: {result:b} ({result})")
        assert result % machine.context["divisor"] == 0
        results.append(result)
示例#7
0
def test_transition_final_state(simple_machine, event=4):
    machine = StateMachine(simple_machine)

    machine.transition(event)
    assert machine.state == event
    assert not machine.initial
    assert not machine.accepted

    assert machine.final
    assert machine.is_final(machine.state)

    with raises(StopMachine):
        machine.transition(event)
示例#8
0
def test_reset(simple_machine, event=3):
    def transition(state, context, event):
        context["value"] = event
        return event

    simple_machine["transition"] = transition
    machine = StateMachine(simple_machine)

    machine.transition(event)
    assert machine.context == {"value": event}

    machine.reset()
    assert machine.initial
    assert machine.context == simple_machine.get("context", dict())
示例#9
0
def test_lifecycles_context(lifecycle_machine):
    machine = StateMachine(lifecycle_machine)

    for event in range(2, 5):
        state = machine.state

        machine.transition(event)
        assert machine.context[f"before_{event}"] == state

        if event == 3:
            assert machine.context[f"after_accepted"] == machine.state
            assert machine.context[f"after_{event}"] == machine.state

    machine.reset()
    assert machine.context == {}
示例#10
0
def test_invalid_machine():
    with raises(ValueError):
        StateMachine(dict())
示例#11
0
def test_blueprint_invalid_initial_state(simple_machine):
    simple_machine["initialState"] = -1

    with raises(ValueError):
        StateMachine(simple_machine)
示例#12
0
def test_default_machine(default_machine):
    machine = StateMachine(default_machine)

    assert machine.context == dict()
    assert not machine.is_accepted(machine.state)
    assert not machine.is_final(machine.state)
示例#13
0
def test_transition_invalid_state(simple_machine, event=1):
    simple_machine["transition"] = lambda *_: -1
    machine = StateMachine(simple_machine)

    with raises(ValueError):
        machine.transition(event)
示例#14
0
def test_transition_invalid_event(simple_machine):
    machine = StateMachine(simple_machine)

    with raises(ValueError):
        machine.transition(-1)