def test_state_machine_init_single(): s = State("INIT") fsm = StateMachine([s]) assert fsm.states == [s] assert fsm.transitions[s] == {} assert fsm.wildcard_transitions == {} assert fsm.init_state == s assert fsm.state == s assert fsm.memory == {}
def test_state_machine_add_wildcard_transition(): s0 = State("INIT") s1 = State("FIRST") def callback(transition, memory): return True def condition(transition, memory): return True fsm = StateMachine([s0, s1]) transition = fsm.add_wildcard_transition(s0, s1, callback, condition, "OUT") assert fsm.states == [s0, s1] assert fsm.transitions[s0] == {} assert fsm.transitions[s1] == {} assert fsm.wildcard_transitions == {s0: transition} assert fsm.init_state == s0 assert fsm.state == s0 assert fsm.memory == {}
def test_state_machine_wildcard_wrong_receive(): s0 = State("INIT") s1 = State("FIRST") def callback(transition, memory): return None def condition(transition, memory): return True fsm = StateMachine([s0, s1]) transition = fsm.add_wildcard_transition(s1, s0, callback, condition, "OUT") next_state, out = fsm.receive("안녕") assert next_state == s0 assert out is None assert fsm.states == [s0, s1] assert fsm.transitions[s0] == {} assert fsm.transitions[s1] == {} assert fsm.wildcard_transitions == {s1: transition} assert fsm.init_state == s0 assert fsm.state == s0 assert fsm.memory == {}
def test_state_machine_receive_with_callback(): s0 = State("INIT") s1 = State("FIRST") def callback(transition, memory): memory["temp"] = "Hello" def condition(transition, memory): return True fsm = StateMachine([s0, s1]) transition = fsm.add_transition(s0, s1, 0, callback, condition, "OUT") next_state, out = fsm.receive(0) assert next_state == s1 assert out == "OUT" assert fsm.states == [s0, s1] assert fsm.transitions[s0] == {0: transition} assert fsm.transitions[s1] == {} assert fsm.wildcard_transitions == {} assert fsm.init_state == s0 assert fsm.state == s1 assert fsm.memory == {"temp": "Hello"}
def test_state_machine_init_multiple(): s0 = State("INIT") s1 = State("FIRST") s2 = State("SECOND") s3 = State("THIRD") fsm = StateMachine([s0, s1, s2, s3]) assert fsm.states == [s0, s1, s2, s3] assert fsm.transitions[s0] == {} assert fsm.transitions[s1] == {} assert fsm.transitions[s2] == {} assert fsm.transitions[s3] == {} assert fsm.wildcard_transitions == {} assert fsm.init_state == s0 assert fsm.state == s0 assert fsm.memory == {}
def test_seasons(): spring = State("SPRING") summer = State("SUMMER") fall = State("FALL") winter = State("WINTER") states = [winter, spring, summer, fall] fsm = StateMachine(states=states) assert fsm.state == winter fsm.add_transition(winter, spring, 3, out="Switched to spring") fsm.add_transition(spring, summer, 6, out="Switched to summer") fsm.add_transition(summer, fall, 9, out="Switched to fall") fsm.add_transition(fall, winter, 12, out="Switched to winter") assert fsm.receive(1) == (winter, None) assert fsm.receive(2) == (winter, None) assert fsm.receive(3) == (spring, "Switched to spring") assert fsm.receive(4) == (spring, None) assert fsm.receive(5) == (spring, None) assert fsm.receive(6) == (summer, "Switched to summer") assert fsm.receive(7) == (summer, None) assert fsm.receive(8) == (summer, None) assert fsm.receive(9) == (fall, "Switched to fall") assert fsm.receive(10) == (fall, None) assert fsm.receive(11) == (fall, None) assert fsm.receive(12) == (winter, "Switched to winter")
def test_state_machine_empty_init(): with pytest.raises(ValueError): fsm = StateMachine([])