-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_stop_machine.py
More file actions
183 lines (121 loc) · 3.95 KB
/
test_stop_machine.py
File metadata and controls
183 lines (121 loc) · 3.95 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
"""Tests for stop_machine.py. Covers every transition."""
import pytest
from stop_machine import (
InvalidTransitionError,
State,
StopMachine,
TerminalStateError,
)
# --- Initial state ---
def test_default_initial_state():
m = StopMachine()
assert m.state == State.GREEN
def test_custom_initial_state():
for s in State:
m = StopMachine(initial=s)
assert m.state == s
def test_is_terminal_false_for_green():
assert not StopMachine(State.GREEN).is_terminal
def test_is_terminal_false_for_amber():
assert not StopMachine(State.AMBER).is_terminal
def test_is_terminal_true_for_red():
assert StopMachine(State.RED).is_terminal
# --- advance() transitions ---
def test_advance_green_to_amber():
m = StopMachine(State.GREEN)
result = m.advance()
assert result == State.AMBER
assert m.state == State.AMBER
def test_advance_amber_to_red():
m = StopMachine(State.AMBER)
result = m.advance()
assert result == State.RED
assert m.state == State.RED
def test_advance_from_red_raises():
m = StopMachine(State.RED)
with pytest.raises(TerminalStateError):
m.advance()
def test_advance_full_sequence():
m = StopMachine()
assert m.state == State.GREEN
m.advance()
assert m.state == State.AMBER
m.advance()
assert m.state == State.RED
with pytest.raises(TerminalStateError):
m.advance()
# --- transition_to() valid ---
def test_transition_to_green_to_amber():
m = StopMachine(State.GREEN)
result = m.transition_to(State.AMBER)
assert result == State.AMBER
assert m.state == State.AMBER
def test_transition_to_amber_to_red():
m = StopMachine(State.AMBER)
result = m.transition_to(State.RED)
assert result == State.RED
assert m.state == State.RED
# --- transition_to() invalid ---
def test_transition_to_from_red_raises():
m = StopMachine(State.RED)
with pytest.raises(TerminalStateError):
m.transition_to(State.GREEN)
def test_transition_to_green_to_red_raises():
m = StopMachine(State.GREEN)
with pytest.raises(InvalidTransitionError):
m.transition_to(State.RED)
def test_transition_to_green_to_green_raises():
m = StopMachine(State.GREEN)
with pytest.raises(InvalidTransitionError):
m.transition_to(State.GREEN)
def test_transition_to_amber_to_green_raises():
m = StopMachine(State.AMBER)
with pytest.raises(InvalidTransitionError):
m.transition_to(State.GREEN)
def test_transition_to_amber_to_amber_raises():
m = StopMachine(State.AMBER)
with pytest.raises(InvalidTransitionError):
m.transition_to(State.AMBER)
# --- reset() ---
def test_reset_from_green():
m = StopMachine(State.GREEN)
result = m.reset()
assert result == State.GREEN
assert m.state == State.GREEN
def test_reset_from_amber():
m = StopMachine(State.AMBER)
result = m.reset()
assert result == State.GREEN
assert m.state == State.GREEN
def test_reset_from_red():
m = StopMachine(State.RED)
with pytest.raises(TerminalStateError):
m.reset()
def test_reset_then_advance():
m = StopMachine()
m.advance()
m.advance()
assert m.is_terminal
with pytest.raises(TerminalStateError):
m.reset()
# --- repr ---
def test_repr_green():
assert repr(StopMachine(State.GREEN)) == "StopMachine(state=GREEN)"
def test_repr_amber():
assert repr(StopMachine(State.AMBER)) == "StopMachine(state=AMBER)"
def test_repr_red():
assert repr(StopMachine(State.RED)) == "StopMachine(state=RED)"
# --- State enum ---
def test_state_values():
assert State.GREEN.value == "GREEN"
assert State.AMBER.value == "AMBER"
assert State.RED.value == "RED"
def test_exactly_three_states():
assert len(State) == 3
# --- No instance leakage ---
def test_two_machines_are_independent():
m1 = StopMachine()
m2 = StopMachine()
m1.advance()
assert m1.state == State.AMBER
assert m2.state == State.GREEN