forked from dropbox/securitybot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_machine_test.py
More file actions
223 lines (201 loc) · 6.47 KB
/
Copy pathstate_machine_test.py
File metadata and controls
223 lines (201 loc) · 6.47 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
from unittest2 import TestCase
from securitybot.state_machine import StateMachine, StateMachineException
# Helper junk
class Helper(object):
def __init__(self):
self.x = 0
def increment(self):
self.x += 1
def x_is_five(self):
return self.x == 5
class Helper2(object):
def __init__(self):
self.x = 0
self.y = 0
self.z = 0
def increment_x(self):
self.x += 1
def x_at_least_two(self):
return self.x >= 2
def increment_y(self):
self.y += 5
def increment_z(self):
self.z += 10
class FSMTest(TestCase):
# Functionality tests
def test_simple_chain(self):
'''Test most basic transition chain.'''
states = ['one', 'two', 'three']
transitions = [
{'source': 'one', 'dest': 'two'},
{'source': 'two', 'dest': 'three'},
{'source': 'three', 'dest': 'one'},
]
sm = StateMachine(states, transitions, 'one')
assert(str(sm.state) == 'one')
sm.step()
assert(str(sm.state) == 'two')
sm.step()
assert(str(sm.state) == 'three')
sm.step()
assert(str(sm.state) == 'one')
def test_simple_during(self):
'''Tests a simple action being performed while in a state.'''
helper = Helper()
states = ['one', 'two']
transitions = [
{'source': 'one', 'dest': 'two'},
{'source': 'two', 'dest': 'one'},
]
during = {
'one': helper.increment
}
sm = StateMachine(states, transitions, 'one', during=during)
assert(helper.x == 0)
sm.step()
assert(helper.x == 1)
sm.step()
assert(helper.x == 1)
def test_simple_on_enter(self):
'''Tests a simple action being performed entering into a state.'''
helper = Helper()
states = ['one', 'two']
transitions = [
{'source': 'one', 'dest': 'two'},
{'source': 'two', 'dest': 'one'},
]
on_enter = {
'two': helper.increment
}
sm = StateMachine(states, transitions, 'one', on_enter=on_enter)
assert(helper.x == 0)
sm.step()
assert(helper.x == 1)
sm.step()
assert(helper.x == 1)
def test_simple_on_exit(self):
'''Tests a simple action being performed exiting from a state.'''
helper = Helper()
states = ['one', 'two']
transitions = [
{'source': 'one', 'dest': 'two'},
{'source': 'two', 'dest': 'one'},
]
on_exit = {
'one': helper.increment
}
sm = StateMachine(states, transitions, 'one', on_exit=on_exit)
assert(helper.x == 0)
sm.step()
assert(helper.x == 1)
sm.step()
assert(helper.x == 1)
def test_simple_condition(self):
'''Tests a simple condition check before transitioning.'''
helper = Helper()
states = ['one', 'two']
transitions = [
{'source': 'one', 'dest': 'two', 'condition': helper.x_is_five},
{'source': 'two', 'dest': 'one'},
]
during = {
'one': helper.increment
}
sm = StateMachine(states, transitions, 'one', during=during)
for x in range(5):
assert(helper.x == x)
assert(str(sm.state) == 'one')
sm.step()
assert(helper.x == 5)
assert(str(sm.state) == 'two')
sm.step()
assert(helper.x == 5)
assert(str(sm.state) == 'one')
sm.step()
assert(helper.x == 6)
assert(str(sm.state) == 'one')
sm.step()
assert(helper.x == 7)
assert(str(sm.state) == 'one')
def test_simple_action(self):
'''Tests a simple action being performed upon transitioning.'''
helper = Helper()
states = ['one', 'two']
transitions = [
{'source': 'one', 'dest': 'two', 'action': helper.increment},
{'source': 'two', 'dest': 'one'}
]
sm = StateMachine(states, transitions, 'one')
assert(helper.x == 0)
assert(str(sm.state) == 'one')
sm.step()
assert(helper.x == 1)
assert(str(sm.state) == 'two')
def test_correct_state_actions(self):
'''
Tests that durings, on_enters, and on_exits are called correctly and
don't interfere with one another.
'''
helper = Helper2()
states = ['one', 'two']
transitions = [
{'source': 'one', 'dest': 'two', 'condition': helper.x_at_least_two},
{'source': 'two', 'dest': 'one'}
]
during = {
'one': helper.increment_x
}
on_enter = {
'one': helper.increment_y
}
on_exit = {
'one': helper.increment_z
}
sm = StateMachine(states, transitions, 'one', during=during, on_enter=on_enter,
on_exit=on_exit)
sm.step()
assert(helper.x == 1)
assert(helper.y == 0)
assert(helper.z == 0)
sm.step()
assert(helper.x == 2)
assert(helper.y == 0)
assert(helper.z == 10)
sm.step()
assert(helper.x == 2)
assert(helper.y == 5)
assert(helper.z == 10)
# Invalid input error notification tests
def test_duplicate_states(self):
states = ['one', 'one']
try:
StateMachine(states, {}, 'one')
except StateMachineException:
return
assert False, "No exception thrown on duplicate state names."
def test_invalid_initial_state(self):
try:
StateMachine([], {}, 'foo')
except StateMachineException:
return
assert False, "No exception thrown on invalid initial state name."
def test_invalid_transition_source_name(self):
states = ['one']
transitions = [
{'source': 'foo', 'dest': 'one'}
]
try:
StateMachine(states, transitions, 'one')
except StateMachineException:
return
assert False, "No exception thrown on invalid transition source name."
def test_invalid_transition_dest_name(self):
states = ['one']
transitions = [
{'source': 'one', 'dest': 'foo'}
]
try:
StateMachine(states, transitions, 'one')
except StateMachineException:
return
assert False, "No exception thrown on invalid transition source name."