-
-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathtest_microwave.py
More file actions
130 lines (96 loc) · 3.9 KB
/
test_microwave.py
File metadata and controls
130 lines (96 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import pytest
from statemachine.io.scxml.processor import SCXMLProcessor
from statemachine.state import State
from statemachine import StateChart
"""
The <initial> specifies a transition that specifies the default child initial states.
The problem is that the transition must occur, and the state itself is not marked
as `initial` in the model.
"""
MICROWAVE_SCXML = """
<scxml initial="unplugged">
<state id="unplugged">
<transition event="plug-in" target="plugged-in"/>
</state>
<state id="plugged-in">
<initial>
<transition target="idle"/>
</initial>
<state id="idle">
<transition event="start" target="cooking"/>
</state>
<state id="cooking">
<transition event="stop" target="idle"/>
</state>
<transition event="unplug" target="unplugged"/>
</state>
</scxml>
"""
@pytest.mark.scxml()
def test_microwave_scxml():
processor = SCXMLProcessor()
processor.parse_scxml("microwave", MICROWAVE_SCXML)
sm = processor.start()
assert "unplugged" in sm.current_state_value
sm.send("plug-in")
assert "idle" in sm.current_state_value
assert "plugged-in" in sm.current_state_value
sm.send("start")
assert "cooking" in sm.current_state_value
assert "idle" not in sm.current_state_value
assert "plugged-in" in sm.current_state_value
sm.send("unplug")
assert "unplugged" in sm.current_state_value
assert "idle" not in sm.current_state_value
assert "plugged-in" not in sm.current_state_value
assert "cooking" not in sm.current_state_value
class TestMicrowave:
@pytest.fixture()
def microwave_cls(self):
class MicroWave(StateChart):
door_closed: bool = True
class oven(State.Parallel, name="Microwave oven"):
class engine(State.Compound):
off = State(initial=True)
class on(State.Compound):
idle = State(initial=True)
cooking = State()
idle.to(cooking, cond="In('closed')")
cooking.to(idle, cond="In('open')")
time = cooking.to.itself(internal=True, on="increment_timer")
def increment_timer(self):
self.timer += 1
assert isinstance(on, State) # so mypy stop complaining
on.to(off, event="turn-off")
off.to(on, event="turn-on")
on.to(off, cond="timer >= cook_time") # eventless transition
class door(State.Compound):
closed = State(initial=True)
open = State()
closed.to(open, event="door.open")
open.to(closed, event="door.close")
def on_enter_open(self):
self.door_closed = False
def on_enter_closed(self):
self.door_closed = True
def __init__(self):
self.cook_time = 5
self.timer = 0
super().__init__()
return MicroWave
def test_microwave(self, microwave_cls):
sm = microwave_cls()
assert {"door", "closed", "oven", "engine", "off"} == {*sm.current_state_value}
assert sm.door_closed is True
sm.send("turn-on")
assert {"door", "closed", "oven", "engine", "on", "cooking"} == {*sm.current_state_value}
sm.send("door.open")
assert {"door", "open", "oven", "engine", "on", "idle"} == {*sm.current_state_value}
assert sm.door_closed is False
sm.send("door.close")
assert {"door", "closed", "oven", "engine", "on", "cooking"} == {*sm.current_state_value}
assert sm.door_closed is True
for _ in range(5):
sm.send("time")
assert {"door", "closed", "oven", "engine", "off"} == {*sm.current_state_value}
assert sm.door_closed is True