PageRenderTime 27ms CodeModel.GetById 23ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/spec/callback_order_spec.py

http://github.com/nsi-iff/fluidity
Python | 59 lines | 38 code | 17 blank | 4 comment | 1 complexity | 95ac55bb467216dd71894b5d4ca414d6 MD5 | raw file
Possible License(s): MIT
 1import unittest
 2from should_dsl import should
 3from fluidity import StateMachine, state, transition
 4
 5
 6class CrazyGuy(StateMachine):
 7    state('looking', exit='no_lookin_anymore')
 8    state('falling', enter='will_fall_right_now')
 9    initial_state = 'looking'
10    transition(from_='looking', event='jump', to='falling',
11               action='become_at_risk', guard='always_can_jump')
12
13    def __init__(self):
14        StateMachine.__init__(self)
15        self.at_risk = False
16        self.callbacks = []
17
18    def become_at_risk(self):
19        self.at_risk = True
20        self.callbacks.append('action')
21
22    def no_lookin_anymore(self):
23        self.callbacks.append('old exit')
24
25    def will_fall_right_now(self):
26        self.callbacks.append('new enter')
27
28    def always_can_jump(self):
29        self.callbacks.append('guard')
30        return True
31
32
33class CallbackOrder(unittest.TestCase):
34
35    def setUp(self):
36        guy = CrazyGuy()
37        guy.jump()
38        self.callbacks = guy.callbacks
39
40    def test_it_runs_guard_first(self):
41        '''(1) guard'''
42        self.callbacks[0] |should| equal_to('guard')
43
44    def test_it_and_then_old_state_exit(self):
45        '''(2) old state exit action'''
46        self.callbacks[1] |should| equal_to('old exit')
47
48    def test_it_and_then_new_state_exit(self):
49        '''(3) new state enter action'''
50        self.callbacks[2] |should| equal_to('new enter')
51
52    def test_it_and_then_transaction_action(self):
53        '''(4) transaction action'''
54        self.callbacks[3] |should| equal_to('action')
55
56
57if __name__ == '__main__':
58    unittest.main()
59