/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

  1. import unittest
  2. from should_dsl import should
  3. from fluidity import StateMachine, state, transition
  4. class CrazyGuy(StateMachine):
  5. state('looking', exit='no_lookin_anymore')
  6. state('falling', enter='will_fall_right_now')
  7. initial_state = 'looking'
  8. transition(from_='looking', event='jump', to='falling',
  9. action='become_at_risk', guard='always_can_jump')
  10. def __init__(self):
  11. StateMachine.__init__(self)
  12. self.at_risk = False
  13. self.callbacks = []
  14. def become_at_risk(self):
  15. self.at_risk = True
  16. self.callbacks.append('action')
  17. def no_lookin_anymore(self):
  18. self.callbacks.append('old exit')
  19. def will_fall_right_now(self):
  20. self.callbacks.append('new enter')
  21. def always_can_jump(self):
  22. self.callbacks.append('guard')
  23. return True
  24. class CallbackOrder(unittest.TestCase):
  25. def setUp(self):
  26. guy = CrazyGuy()
  27. guy.jump()
  28. self.callbacks = guy.callbacks
  29. def test_it_runs_guard_first(self):
  30. '''(1) guard'''
  31. self.callbacks[0] |should| equal_to('guard')
  32. def test_it_and_then_old_state_exit(self):
  33. '''(2) old state exit action'''
  34. self.callbacks[1] |should| equal_to('old exit')
  35. def test_it_and_then_new_state_exit(self):
  36. '''(3) new state enter action'''
  37. self.callbacks[2] |should| equal_to('new enter')
  38. def test_it_and_then_transaction_action(self):
  39. '''(4) transaction action'''
  40. self.callbacks[3] |should| equal_to('action')
  41. if __name__ == '__main__':
  42. unittest.main()