forked from fgmacedo/python-statemachine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_statemachine_inheritance.py
More file actions
110 lines (74 loc) · 2.8 KB
/
test_statemachine_inheritance.py
File metadata and controls
110 lines (74 loc) · 2.8 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
import pytest
from statemachine import exceptions
@pytest.fixture()
def BaseMachine():
from statemachine import State
from statemachine import StateMachine
class BaseMachine(StateMachine, strict_states=False):
state_1 = State(initial=True)
state_2 = State()
trans_1_2 = state_1.to(state_2)
trans_2_2 = state_2.to.itself(internal=True)
return BaseMachine
@pytest.fixture()
def InheritedClass(BaseMachine):
class InheritedClass(BaseMachine, strict_states=False):
pass
return InheritedClass
@pytest.fixture()
def ExtendedClass(BaseMachine):
from statemachine import State
class ExtendedClass(BaseMachine):
state_3 = State()
trans_2_3 = BaseMachine.state_2.to(state_3)
trans_3_3 = state_3.to.itself(internal=True)
return ExtendedClass
@pytest.fixture()
def OverridedClass(BaseMachine):
from statemachine import State
class OverridedClass(BaseMachine):
state_2 = State()
trans_1_2 = BaseMachine.state_1.to(state_2)
trans_2_2 = state_2.to.itself(internal=True)
return OverridedClass
@pytest.fixture()
def OverridedTransitionClass(BaseMachine):
from statemachine import State
class OverridedTransitionClass(BaseMachine):
state_3 = State()
trans_1_2 = BaseMachine.state_1.to(state_3)
trans_3_3 = state_3.to.itself(internal=True)
return OverridedTransitionClass
def test_should_inherit_states_and_transitions(BaseMachine, InheritedClass):
assert InheritedClass.states == [
BaseMachine.state_1,
BaseMachine.state_2,
]
expected = [e.name for e in BaseMachine.events]
actual = [e.name for e in InheritedClass.events]
assert actual == expected
def test_should_extend_states_and_transitions(BaseMachine, ExtendedClass):
assert ExtendedClass.states == [
BaseMachine.state_1,
BaseMachine.state_2,
ExtendedClass.state_3,
]
base_events = [e.name for e in BaseMachine.events]
expected = base_events + [ExtendedClass.trans_2_3.name, ExtendedClass.trans_3_3.name]
actual = [e.name for e in ExtendedClass.events]
assert actual == expected
def test_should_execute_transitions(ExtendedClass):
instance = ExtendedClass()
instance.trans_1_2()
instance.trans_2_3()
assert instance.state_3.is_active
@pytest.mark.xfail(reason="State overriding is not supported")
def test_dont_support_overriden_states(OverridedClass):
# There's no support for overriding states
with pytest.raises(exceptions.InvalidDefinition):
OverridedClass()
@pytest.mark.xfail(reason="Transition overriding is not supported")
def test_support_override_transitions(OverridedTransitionClass):
instance = OverridedTransitionClass()
instance.trans_1_2()
assert instance.state_3.is_active