-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest_nano_library.py
131 lines (102 loc) · 4.13 KB
/
test_nano_library.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
""" Perform unit tests on the nano_library.py file
"""
import unittest
import nano_library
class Fake_dev:
def __init__(self):
self.expect = None
self.write_data = ''
def _handle(self, name):
if self.expect != name:
raise ValueError
def reset(self):
self._handle('reset')
def write(self, addr, data, timeout):
self._handle('write')
self.write_addr = addr
self.write_data += "".join(map(chr, data))
self.write_timeout = timeout
def read(self, addr, buflen, timeout):
# TODO - this often comes after a write, so we cannot expect both
#self._handle('write')
self.read_addr = addr
self.read_buflen = buflen
self.read_timeout = timeout
return [255,206,-1,-1,-1,-1] # 206 == self.OK
class TestK40_CLASS(unittest.TestCase):
def setUp(self):
self.object = nano_library.K40_CLASS()
self.object.dev = Fake_dev()
def tearDown(self):
self.object = None
def test_constants(self):
hello = "".join(map(chr, self.object.hello))
unlock = "".join(map(chr, self.object.unlock))
home = "".join(map(chr, self.object.home))
estop = "".join(map(chr, self.object.estop))
self.assertEqual(hello, '\xa0')
self.assertEqual(unlock, '\xa6\x00IS2PFFFFFFFFFFFFFFFFFFFFFFFFFF\xa6\x0f')
self.assertEqual(home, '\xa6\x00IPPFFFFFFFFFFFFFFFFFFFFFFFFFFF\xa6\xe4')
self.assertEqual(estop, '\xa6\x00IFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\xa6\x82')
def test_say_hello(self):
self.object.dev.expect = 'write'
self.assertEqual(self.object.say_hello(), self.object.OK)
self.assertEqual(
self.object.dev.write_data,
"".join(map(chr, self.object.hello))
)
# Several of these function calls have nested calls to dev functions,
# which my dumb dev mock cannot handle:
# unlock_rail() calls say_hello()
# e_stop() calls say_hello()
# home_position() calls say_hello()
def test_reset_usb(self):
self.object.dev.expect = 'reset'
self.object.reset_usb()
def test_OneWireCRC(self):
line = map(ord, 'AK0FFFFFFFFFFFFFFFFFFFFFFFFFFF')
# Do we get the expected CRC?
self.assertEqual( self.object.OneWireCRC(line), 0xa4 )
# Now, are the magic arrays simply normal packets with a valid CRC?
self.assertEqual(
self.object.OneWireCRC(self.object.unlock[1:-2]),
self.object.unlock[-1]
)
self.assertEqual(
self.object.OneWireCRC(self.object.home[1:-2]),
self.object.home[-1]
)
self.assertEqual(
self.object.OneWireCRC(self.object.estop[1:-2]),
self.object.estop[-1]
)
def test_send_data(self):
data = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
self.object.dev.expect = 'write'
self.object.send_data( map(ord, data), update_gui=None, stop_calc=None)
packet_marker = '\xa6'
packet_end = '\xa0'
packet_data1 = data[0:30]
packet_data2 = data[30:52]
packet_data2 += 'FFFFFFFF' # fill data to a full packet size
expect = packet_end + packet_marker + '\x00' + packet_data1 + packet_marker
expect += chr(self.object.OneWireCRC(map(ord, packet_data1)))
expect += packet_end
expect += packet_end + packet_marker + '\x00' + packet_data2 + packet_marker
expect += chr(self.object.OneWireCRC(map(ord, packet_data2)))
expect += packet_end
self.assertEqual(
expect,
self.object.dev.write_data
)
# FIXME - where is the second packet?
def test_rapid_move(self):
self.object.dev.expect = 'write'
self.object.rapid_move(1000, 1000)
# Deeper probing of this data block is done in test_egv
expect = '\xa0\xa6\x00ILzzz235Bzzz235S1PFFFFFFFFFFFF\xa6\x8a\xa0'
self.assertEqual(self.object.dev.write_data, expect)
def test_hex2dec(self):
input = ["40","e7"]
output = [64, 231]
self.assertEqual(self.object.hex2dec(input), output)