-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_serialdispatch.py
More file actions
243 lines (171 loc) · 6.14 KB
/
Copy pathtest_serialdispatch.py
File metadata and controls
243 lines (171 loc) · 6.14 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import pytest
from test_mockserial import MockSerialPort
from serialdispatch.frame import Frame
from serialdispatch.sdispatch import SerialDispatch
process_data = None
@pytest.fixture()
def sending_fixture():
port = MockSerialPort()
sd = SerialDispatch(port)
yield port, sd
def test_publish_str(sending_fixture):
""" test publish """
port, sd = sending_fixture
# this is the data that should be published
sd.publish('foo', 'bar')
# this is the data that should be contained in the serial port outbuffer
data_test = [Frame.SOF,
102, 111, 111, 0, # 'foo\0'
1, 3, 0, 1, # 1-dimensional data, length=0x0003, datatype=string
98, 97, 114, # 'bar'
126, 22, # fletcher checksum
Frame.EOF]
assert port.serial_data_out == data_test
def test_publish_3_u8(sending_fixture):
port, sd = sending_fixture
# this is the data that should be published
sd.publish('foo', [[10, 20, 30]], ['U8'])
# this is the data that should be contained in the serial port outbuffer
data_test = [Frame.SOF,
102, 111, 111, 0,
1, 3, 0, 2,
10, 20, 30,
134, 36,
Frame.EOF]
assert port.serial_data_out == data_test
def test_publish_3_s8(sending_fixture):
port, sd = sending_fixture
# this is the data that should be published
sd.publish('foo', [[-10, -20, -30]], ['S8'])
# this is the data that should be contained in the serial port outbuffer
data_test = [Frame.SOF,
102, 111, 111, 0,
1, 3, 0, 3,
246, 246 ^ 0x20, 236, 226,
15, 96,
Frame.EOF]
assert port.serial_data_out == data_test
def test_publish_3x3_u8u16u32(sending_fixture):
port, sd = sending_fixture
# this is the data that should be published
sd.publish('foo', [[10, 20, 30], [40, 50, 60], [70, 80, 90]], ['U8', 'U16', 'U32'])
# this is the data that should be contained in the serial port outbuffer
data_test = [Frame.SOF,
102, 111, 111, 0,
3, 3, 0, 0x42, 0x06,
10, 20, 30,
40, 0, 50, 0, 60, 0,
70, 0, 0, 0, 80, 0, 0, 0, 90, 0, 0, 0,
84, 186,
Frame.EOF]
assert port.serial_data_out == data_test
def test_publish_3x3_s8s16s32(sending_fixture):
port, sd = sending_fixture
# this is the data that should be published
sd.publish('foo', [[-10, -20, -30], [-40, -50, -60], [-70, -80, -90]], ['S8', 'S16', 'S32'])
# this is the data that should be contained in the serial port outbuffer
data_test = [Frame.SOF,
102, 111, 111, 0,
3, 3, 0, 0x53, 0x07,
246, 246 ^ 0x20, 236, 226, # <= one of these happens to be an esc char
216, 255, 206, 255, 196, 255,
186, 255, 255, 255, 176, 255, 255, 255, 166, 255, 255, 255,
214, 236,
Frame.EOF]
assert port.serial_data_out == data_test
@pytest.fixture()
def subscribing_fixture():
global process_data
port = MockSerialPort()
sd = SerialDispatch(port, threaded=False)
process_data = None
def toggle_flag(data):
global process_data
nonlocal sd
process_data = data
sd.subscribe('foo', toggle_flag)
yield port, sd
def test_subscribing_init(subscribing_fixture):
port, sd = subscribing_fixture
assert process_data is None
def test_subscribing_string(subscribing_fixture):
assert process_data is None
port, sd = subscribing_fixture
port.serial_data_in = [
Frame.SOF,
102, 111, 111, 0, # 'foo\0'
1, 3, 0, 1, # 1-dimensional data, length=0x0003, datatype=string
98, 97, 114, # 'bar'
126, 22, # fletcher checksum
Frame.EOF
]
sd.run()
assert process_data
assert process_data == 'bar'
def test_subscribing_3_u8(subscribing_fixture):
assert process_data is None
port, sd = subscribing_fixture
port.serial_data_in = [
Frame.SOF,
102, 111, 111, 0,
1, 3, 0, 2,
10, 20, 30,
134, 36,
Frame.EOF
]
sd.run()
assert process_data
assert process_data == [10, 20, 30]
def test_subscribing_3_s8(subscribing_fixture):
assert process_data is None
port, sd = subscribing_fixture
# this is the data that should be contained in the serial port input buffer
port.serial_data_in = [
Frame.SOF,
102, 111, 111, 0,
1, 3, 0, 3,
246, 246 ^ 0x20, 236, 226,
15, 96,
Frame.EOF
]
sd.run()
assert process_data
assert process_data == [-10, -20, -30]
def test_subscribing_3x3_u8u16u32(subscribing_fixture):
assert process_data is None
port, sd = subscribing_fixture
# this is the data that should be contained in the serial port input buffer
port.serial_data_in = [
Frame.SOF,
102, 111, 111, 0,
3, 3, 0, 0x42, 0x06,
10, 20, 30,
40, 0, 50, 0, 60, 0,
70, 0, 0, 0, 80, 0, 0, 0, 90, 0, 0, 0,
84, 186,
Frame.EOF
]
sd.run()
assert process_data
assert process_data[0] == [10, 20, 30]
assert process_data[1] == [40, 50, 60]
assert process_data[2] == [70, 80, 90]
def test_subscribe_3x3_s8s16s32(subscribing_fixture):
assert process_data is None
port, sd = subscribing_fixture
# this is the data that should be contained in the serial port input buffer
port.serial_data_in = [
Frame.SOF,
102, 111, 111, 0,
3, 3, 0, 0x53, 0x07,
246, 246 ^ 0x20, 236, 226, # <= one of these happens to be an esc char
216, 255, 206, 255, 196, 255,
186, 255, 255, 255, 176, 255, 255, 255, 166, 255, 255, 255,
214, 236,
Frame.EOF
]
sd.run()
assert process_data
assert process_data[0] == [-10, -20, -30]
assert process_data[1] == [-40, -50, -60]
assert process_data[2] == [-70, -80, -90]