|
1 | 1 | import pytest
|
2 | 2 |
|
3 | 3 | from statemachine import State
|
4 |
| -from statemachine import StateMachine |
5 | 4 |
|
6 | 5 |
|
7 | 6 | @pytest.fixture()
|
8 |
| -def compound_engine_cls(): |
9 |
| - class TestMachine(StateMachine): |
10 |
| - class engine(State.Builder, name="Engine", initial=True): |
11 |
| - off = State("Off", initial=True) |
12 |
| - on = State("On") |
| 7 | +def microwave_cls(): |
| 8 | + from tests.examples.microwave_inheritance_machine import MicroWave |
13 | 9 |
|
14 |
| - turn_on = off.to(on) |
15 |
| - turn_off = on.to(off) |
| 10 | + return MicroWave |
16 | 11 |
|
17 |
| - return TestMachine |
| 12 | + |
| 13 | +def assert_state(s, name, initial=False, final=False, parallel=False, substates=None): |
| 14 | + if substates is None: |
| 15 | + substates = [] |
| 16 | + |
| 17 | + assert isinstance(s, State) |
| 18 | + assert s.name == name |
| 19 | + assert s.initial is initial |
| 20 | + assert s.final is final |
| 21 | + assert s.parallel is parallel |
| 22 | + assert isinstance(s, State) |
| 23 | + assert set(s.substates) == set(substates) |
18 | 24 |
|
19 | 25 |
|
20 | 26 | class TestNestedDeclarations:
|
21 |
| - def test_capture_constructor_arguments(self, compound_engine_cls): |
22 |
| - sm = compound_engine_cls() |
23 |
| - assert isinstance(sm.engine, State) |
24 |
| - assert sm.engine.name == "Engine" |
25 |
| - assert sm.engine.initial is True |
26 |
| - |
27 |
| - def test_list_children_states(self, compound_engine_cls): |
28 |
| - sm = compound_engine_cls() |
29 |
| - assert [s.id for s in sm.engine.substates] == ["off", "on"] |
30 |
| - |
31 |
| - def test_list_events(self, compound_engine_cls): |
32 |
| - sm = compound_engine_cls() |
33 |
| - assert [e.name for e in sm.events] == ["turn_on", "turn_off"] |
| 27 | + def test_capture_constructor_arguments(self, microwave_cls): |
| 28 | + sm = microwave_cls() |
| 29 | + |
| 30 | + assert_state( |
| 31 | + sm.oven, |
| 32 | + "Microwave oven", |
| 33 | + parallel=True, |
| 34 | + substates=[sm.oven.engine, sm.oven.door], |
| 35 | + ) |
| 36 | + assert_state( |
| 37 | + sm.oven.engine, |
| 38 | + "Engine", |
| 39 | + initial=False, |
| 40 | + substates=[sm.oven.engine.on, sm.oven.engine.off], |
| 41 | + ) |
| 42 | + assert_state(sm.oven.engine.off, "Off", initial=True) |
| 43 | + assert_state( |
| 44 | + sm.oven.engine.on, |
| 45 | + "On", |
| 46 | + substates=[sm.oven.engine.on.idle, sm.oven.engine.on.cooking], |
| 47 | + ) |
| 48 | + assert_state( |
| 49 | + sm.oven.door, |
| 50 | + "Door", |
| 51 | + initial=False, |
| 52 | + substates=[sm.oven.door.closed, sm.oven.door.open], |
| 53 | + ) |
| 54 | + assert_state(sm.oven.door.closed, "Closed", initial=True) |
| 55 | + assert_state(sm.oven.door.open, "Open") |
| 56 | + |
| 57 | + def test_list_children_states(self, microwave_cls): |
| 58 | + sm = microwave_cls() |
| 59 | + assert [s.id for s in sm.oven.engine.substates] == ["off", "on"] |
| 60 | + |
| 61 | + def test_list_events(self, microwave_cls): |
| 62 | + sm = microwave_cls() |
| 63 | + assert [e.name for e in sm.events] == [ |
| 64 | + "turn_on", |
| 65 | + "turn_off", |
| 66 | + "door_open", |
| 67 | + "door_close", |
| 68 | + ] |
0 commit comments