|
| 1 | +""" |
| 2 | +All actions machine |
| 3 | +------------------- |
| 4 | +
|
| 5 | +A StateMachine that exercices all possible :ref:`Actions` and :ref:`Guards`. |
| 6 | +
|
| 7 | +""" |
| 8 | + |
| 9 | +import mock |
| 10 | + |
| 11 | +from statemachine import StateMachine, State |
| 12 | + |
| 13 | + |
| 14 | +class AllActionsMachine(StateMachine): |
| 15 | + |
| 16 | + initial = State("Initial", initial=True) |
| 17 | + final = State("Final", final=True) |
| 18 | + |
| 19 | + go = initial.to( |
| 20 | + final, |
| 21 | + validators=["validation_1", "validation_2"], |
| 22 | + conditions=["condition_1", "condition_2"], |
| 23 | + unless=["unless_1", "unless_2"], |
| 24 | + on_execute=["on_execute_1", "on_execute_2"], |
| 25 | + before=["before_go_inline_1", "before_go_inline_2"], |
| 26 | + after=["after_go_inline_1", "after_go_inline_2"], |
| 27 | + ) |
| 28 | + |
| 29 | + def __init__(self, *args, **kwargs): |
| 30 | + self.spy = mock.Mock(side_effect=lambda x: x) |
| 31 | + super(AllActionsMachine, self).__init__(*args, **kwargs) |
| 32 | + |
| 33 | + # validations and conditions |
| 34 | + |
| 35 | + def validation_1(self): |
| 36 | + # this method may raise an exception |
| 37 | + return self.spy("validation_1") |
| 38 | + |
| 39 | + def validation_2(self): |
| 40 | + # this method may raise an exception |
| 41 | + return self.spy("validation_2") |
| 42 | + |
| 43 | + def condition_1(self): |
| 44 | + self.spy("condition_1") |
| 45 | + return True |
| 46 | + |
| 47 | + def condition_2(self): |
| 48 | + self.spy("condition_2") |
| 49 | + return True |
| 50 | + |
| 51 | + def unless_1(self): |
| 52 | + self.spy("unless_1") |
| 53 | + return False |
| 54 | + |
| 55 | + def unless_2(self): |
| 56 | + self.spy("unless_2") |
| 57 | + return False |
| 58 | + |
| 59 | + # generics state |
| 60 | + |
| 61 | + def on_enter_state(self): |
| 62 | + return self.spy("on_enter_state") |
| 63 | + |
| 64 | + def on_exit_state(self): |
| 65 | + return self.spy("on_exit_state") |
| 66 | + |
| 67 | + # generics transition |
| 68 | + |
| 69 | + def before_transition(self): |
| 70 | + return self.spy("before_transition") |
| 71 | + |
| 72 | + def after_transition(self): |
| 73 | + return self.spy("after_transition") |
| 74 | + |
| 75 | + # before / after specific |
| 76 | + |
| 77 | + def on_execute_1(self): |
| 78 | + return self.spy("on_execute_1") |
| 79 | + |
| 80 | + def on_execute_2(self): |
| 81 | + return self.spy("on_execute_2") |
| 82 | + |
| 83 | + def before_go_inline_1(self): |
| 84 | + return self.spy("before_go_inline_1") |
| 85 | + |
| 86 | + def before_go_inline_2(self): |
| 87 | + return self.spy("before_go_inline_2") |
| 88 | + |
| 89 | + def before_go(self): |
| 90 | + return self.spy("before_go") |
| 91 | + |
| 92 | + def on_go(self): |
| 93 | + return self.spy("on_go") |
| 94 | + |
| 95 | + def after_go_inline_1(self): |
| 96 | + return self.spy("after_go_inline_1") |
| 97 | + |
| 98 | + def after_go_inline_2(self): |
| 99 | + return self.spy("after_go_inline_2") |
| 100 | + |
| 101 | + def after_go(self): |
| 102 | + return self.spy("after_go") |
| 103 | + |
| 104 | + # enter / exit specific |
| 105 | + |
| 106 | + def on_enter_initial(self): |
| 107 | + return self.spy("on_enter_initial") |
| 108 | + |
| 109 | + def on_exit_initial(self): |
| 110 | + return self.spy("on_exit_initial") |
| 111 | + |
| 112 | + def on_enter_final(self): |
| 113 | + return self.spy("on_enter_final") |
| 114 | + |
| 115 | + def on_exit_final(self): |
| 116 | + "hopefully this will not be called" |
| 117 | + return self.spy("on_exit_final") |
0 commit comments