forked from drweaver/py_garage_server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgarage_test.py
executable file
·84 lines (53 loc) · 2.28 KB
/
garage_test.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
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
import unittest
from garage import Garage
class TestGarageDoorStateMachine(unittest.TestCase):
def door_switch_count_increment(self, e):
self.door_switch_count+=1
return self.door_switch_successful
def setUp(self):
self.door_switch_count = 0
self.door_switch_successful = True
self.fsm = Garage(door_switch_callback=self.door_switch_count_increment)
def test_initial_state(self):
self.assertEqual(Garage.closed, self.fsm.current)
def test_open(self):
self.fsm.open()
self.assertEqual(Garage.opening, self.fsm.current)
self.assertEqual(1, self.door_switch_count)
def test_cannot_trigger(self):
self.assertEqual(Garage.closed, self.fsm.current)
self.assertTrue(self.fsm.cannot(Garage.stop))
self.assertTrue(self.fsm.cannot(Garage.close))
self.fsm = Garage(initial=Garage.opened,door_switch_callback=self.door_switch_count_increment)
self.assertEqual(Garage.opened, self.fsm.current)
self.assertTrue(self.fsm.cannot(Garage.stop))
self.assertTrue(self.fsm.cannot(Garage.open))
self.assertEqual(0, self.door_switch_count)
def test_full_cycle_from_closed(self):
self.fsm.open()
self.assertEqual(Garage.opening, self.fsm.current)
self.fsm.stop()
self.assertEqual(Garage.stopped_on_opening, self.fsm.current)
self.fsm.close()
self.assertEqual(Garage.closing, self.fsm.current)
self.assertEqual(3, self.door_switch_count)
def test_full_cycle_from_open(self):
self.fsm = Garage(initial=Garage.opened,door_switch_callback=self.door_switch_count_increment)
self.assertEqual(Garage.opened, self.fsm.current)
self.fsm.close()
self.assertEqual(Garage.closing, self.fsm.current)
self.fsm.stop()
self.assertEqual(Garage.stopped_on_closing, self.fsm.current)
self.fsm.open()
self.assertEqual(Garage.opening, self.fsm.current)
self.assertEqual(3, self.door_switch_count)
def test_door_switch_fails(self):
self.door_switch_successful = False
self.fsm.open()
self.assertEqual(Garage.closed, self.fsm.current)
def test_initil(self):
self.assertEqual(Garage.closed, self.fsm.current)
self.fsm = Garage(initial=Garage.opened)
self.assertEqual(Garage.opened, self.fsm.current)
if __name__ == '__main__':
unittest.main()