-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_bay_door_v2.py
58 lines (50 loc) · 2.23 KB
/
test_bay_door_v2.py
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
import unittest
from bay_door_v2 import *
from test_helpers import *
# Define expected behavior
expected_transitions_by_state = namify([
['', CLOSING_STATE, 'ex', 'ex'], # for OPEN_STATE
[OPENING_STATE, '', 'ex', 'ex'], # for CLOSED_STATE
['', CLOSING_STATE, OPEN_STATE, 'ex'], # for OPENING_STATE
[OPENING_STATE, '', 'ex', CLOSED_STATE] # for CLOSING_STATE
], STATE_NAMES)
class BayDoorV2(unittest.TestCase):
def test_pre_hooks(self):
sm_deny = StateMachine(lambda x, y, z: False)
for state in range(len(STATE_NAMES)):
for event in range(len(EVENT_NAMES)):
if len(expected_transitions_by_state[state][event]) > 2:
# A handler that denies the transition should prevent changes.
sm_deny.state = state # artificially alter state
sm_deny.handle(event)
self.assertEquals(state, sm_deny.state)
def test_post_hooks(self):
class Counter:
def __init__(self): self.count = 0
def __call__(self, sm, state, event): self.count += 1
counter = Counter()
sm = StateMachine(post = counter)
for state in range(len(STATE_NAMES)):
for event in range(len(EVENT_NAMES)):
if len(expected_transitions_by_state[state][event]) > 2:
# A handler that denies the transition should prevent changes.
sm.state = state # artificially alter state
sm.handle(event)
self.assertEquals(counter.count, 6)
def test_matrix(self):
sm = StateMachine()
problems = []
# Artificially place state machine in a state, and test every event to see if
# it triggers the expected transition.
for state in range(len(STATE_NAMES)):
for event in range(len(EVENT_NAMES)):
sm.state = state
transition = expected_transitions_by_state[state][event]
outcome = check_transition(sm, event, transition, STATE_NAMES, EVENT_NAMES)
if outcome:
problems.append(outcome)
if problems:
msg = '\n'.join(problems)
self.fail(msg)
if __name__ == '__main__':
unittest.main()