예제 #1
0
파일: test_pysm.py 프로젝트: pgularski/pysm
def test_previous_state_event_none():
    m = StateMachine('m')
    s0 = StateMachine('s0')
    s1 = StateMachine('s1')
    s11 = State('s11')
    s12 = State('s12')

    m.add_state(s0, initial=True)
    s0.add_state(s1, initial=True)
    s1.add_state(s11, initial=True)
    s1.add_state(s12)

    m.add_transition(s0, s12, events='a')

    m.initialize()

    assert list(m.leaf_state_stack.deque) == []
    m.dispatch(_e('a'))
    assert list(m.leaf_state_stack.deque) == [s11]

    try:
        m.set_previous_leaf_state(event=None)
    except Exception as exc:
        assert not exc
    assert list(m.leaf_state_stack.deque) == [s11, s12]
예제 #2
0
파일: test_pysm.py 프로젝트: pgularski/pysm
def test_set_previous_state_no_history():
    m = StateMachine('m')
    off = State('Off')
    m.add_state(off, initial=True)
    m.initialize()
    off.handlers = {'test_no_history': lambda s, e: m.set_previous_leaf_state()}

    assert m.leaf_state == off
    try:
        m.dispatch(_e('test_no_history'))
    except Exception as exc:
        assert not exc
    assert m.leaf_state == off
    assert list(m.leaf_state_stack.deque) == []
예제 #3
0
파일: test_pysm.py 프로젝트: pgularski/pysm
def test_previous_state_with_source_event():
    def do(state, event):
        event.cargo['source_event'].cargo['data'].append(state)
        assert state == m.leaf_state

    def do_with_propagate(state, event):
        event.cargo['source_event'].cargo['data'].append(state)
        event.propagate = True
        assert state == m.leaf_state

    m = StateMachine('m')
    s0 = StateMachine('s0')
    s1 = StateMachine('s1')
    s11 = State('s11')
    s12 = State('s12')

    m.add_state(s0, initial=True)
    s0.add_state(s1, initial=True)
    s1.add_state(s11, initial=True)
    s1.add_state(s12)

    for state in s0, s1, s11, s12:
        state.handlers = {'enter': do, 'exit': do}

    m.add_transition(s0, s12, events='a')

    m.initialize()

    data = []
    assert list(m.leaf_state_stack.deque) == []
    m.dispatch(_e('a', data=data))
    assert list(m.leaf_state_stack.deque) == [s11]

    data = []
    m.set_previous_leaf_state(event=_e('a', data=data))
    assert list(m.leaf_state_stack.deque) == [s11, s12]
    assert data == [s12, s11]
예제 #4
0
파일: test_pysm.py 프로젝트: pgularski/pysm
def test_transition_to_history():
    oven = StateMachine('Oven')
    door_closed = StateMachine('DoorClosed')
    door_open = State('DoorOpen')
    heating = StateMachine('Heating')
    toasting = State('Toasting')
    baking = State('Baking')
    off = State('Off')

    oven.add_state(door_closed, initial=True)
    oven.add_state(door_open)
    door_closed.add_state(off, initial=True)
    door_closed.add_state(heating)
    heating.add_state(baking, initial=True)
    heating.add_state(toasting)

    oven.add_transition(door_closed, toasting, events=['toast'])
    oven.add_transition(door_closed, baking, events=['bake'])
    oven.add_transition(door_closed, off, events=['off', 'timeout'])
    oven.add_transition(door_closed, door_open, events=['open'])

    door_open.handlers = {'close': lambda s, e: oven.set_previous_leaf_state()}

    oven.initialize()

    assert oven.leaf_state == off
    oven.dispatch(_e('open'))
    assert oven.leaf_state == door_open
    try:
        oven.dispatch(_e('close'))
    except Exception as exc:
        assert not exc
    assert oven.leaf_state == off
    assert list(oven.leaf_state_stack.deque) == [off, door_open]

    oven.dispatch(_e('bake'))
    assert oven.leaf_state == baking
    oven.dispatch(_e('open'))
    assert oven.leaf_state == door_open
    try:
        oven.dispatch(_e('close'))
    except Exception as exc:
        assert not exc
    assert oven.leaf_state == baking
    expected = [off, door_open, off, baking, door_open]
    assert list(oven.leaf_state_stack.deque) == expected