-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_states.py
108 lines (84 loc) · 2.91 KB
/
test_states.py
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
'''Test FSM states execution with args'''
from smlib import fsmBase, loader
import pytest
import time
class FSM(fsmBase):
def __init__(self, name, **args):
super().__init__(name, **args)
self.counter = []
self.gotoState("idle")
def idle_entry(self):
self.counter.append(0)
def idle_eval(self):
self.counter.append(1)
def only_args_entry(self, num):
self.counter.append(num-1)
def only_args_eval(self, num):
self.counter.append(num)
self.gotoState("idle")
def only_args_exit(self, num):
self.counter.append(num+1)
def third_entry(self, num):
self.counter.append(num-1)
def only_kwargs_entry(self, num=100):
self.counter.append(num-1)
def only_kwargs_eval(self, num=100):
self.counter.append(num)
self.gotoState("idle")
def only_kwargs_exit(self, num=100):
self.counter.append(num+1)
def args_and_kwargs_entry(self, num, num2=-100):
self.counter.append(num-1)
self.counter.append(num2-1)
def args_and_kwargs_eval(self, num, num2=-100):
self.counter.append(num)
self.counter.append(num2)
self.gotoState("idle")
def args_and_kwargs_exit(self, num, num2=-100):
self.counter.append(num+1)
self.counter.append(num2+1)
# Will be called to produce the argument to test_io function
# and again after its execution to kill all the FSMs
@pytest.fixture(scope='module')
def loaded_fsm():
l = loader()
fsm = l.load(FSM, "test_io")
l.start(blocking=False)
yield fsm
# After tests have completed or failed
l.killAll(None, None)
def test_args(loaded_fsm):
'''Test gotoState with args'''
assert loaded_fsm.counter == [0, 1]
loaded_fsm.gotoState("only_args", 10)
loaded_fsm.trigger()
time.sleep(0.1)
assert loaded_fsm.counter == [0, 1, 9, 10, 11, 0, 1]
def test_kwargs(loaded_fsm):
'''Test gotoState with kwargs'''
loaded_fsm.counter = []
loaded_fsm.gotoState("only_kwargs")
loaded_fsm.trigger()
time.sleep(0.1)
assert loaded_fsm.counter == [99, 100, 101, 0, 1]
def test_kwargs_filled(loaded_fsm):
'''Test gotoState with kwargs with value'''
loaded_fsm.counter = []
loaded_fsm.gotoState("only_kwargs", num=50)
loaded_fsm.trigger()
time.sleep(0.1)
assert loaded_fsm.counter == [49, 50, 51, 0, 1]
def test_args_kwargs(loaded_fsm):
'''Test gotoState with args and kwargs unfilled'''
loaded_fsm.counter = []
loaded_fsm.gotoState("args_and_kwargs", 10)
loaded_fsm.trigger()
time.sleep(0.1)
assert loaded_fsm.counter == [9, -101, 10, -100, 11, -99, 0, 1]
def test_args_kwargs_filled(loaded_fsm):
'''Test gotoState with args and kwargs filled'''
loaded_fsm.counter = []
loaded_fsm.gotoState("args_and_kwargs", 10, num2=50)
loaded_fsm.trigger()
time.sleep(0.1)
assert loaded_fsm.counter == [9, 49, 10, 50, 11, 51, 0, 1]