6 PYTHON2 = sys.version_info < (3, 0)
19 Int8, Int16, Int32, Int64,
20 UInt8, UInt16, UInt32, UInt64,
23 Int8MultiArray, Int16MultiArray, Int32MultiArray, Int64MultiArray,
24 UInt8MultiArray, UInt16MultiArray, UInt32MultiArray, UInt64MultiArray,
25 Float32MultiArray, Float64MultiArray,
28 MultiArrayLayout, MultiArrayDimension,
34 msg = String(data=
"foo")
37 self.assertEqual(extracted[
'data'], msg.data)
39 self.assertEqual(type(extracted[
'data']), unicode)
41 self.assertEqual(type(extracted[
'data']), str)
44 for val
in [
True,
False]:
48 self.assertEqual(extracted[
'data'], msg.data,
'val={}'.format(val))
49 self.assertEqual(type(extracted[
'data']), bool,
'val={}'.format(val))
52 for msg_type
in [Int8, Int16, Int32, Int64]:
53 msg = msg_type(data=-5)
56 self.assertEqual(extracted[
'data'], msg.data,
'type={}'.format(msg_type))
57 self.assertEqual(type(extracted[
'data']), int,
'type={}'.format(msg_type))
59 for msg_type
in [UInt8, UInt16, UInt32, UInt64]:
60 msg = msg_type(data=5)
63 self.assertEqual(extracted[
'data'], msg.data,
'type={}'.format(msg_type))
64 self.assertEqual(type(extracted[
'data']), int,
'type={}'.format(msg_type))
66 for msg_type
in [Float32, Float64]:
67 msg = msg_type(data=2.3)
70 self.assertEqual(extracted[
'data'], msg.data,
'type={}'.format(msg_type))
71 self.assertEqual(type(extracted[
'data']), float,
'type={}'.format(msg_type))
74 for msg_type
in [Time, Duration]:
78 self.assertEqual(extracted[
'data'][
'secs'], msg.data.secs,
'type={}'.format(msg_type))
79 self.assertEqual(extracted[
'data'][
'nsecs'], msg.data.nsecs,
'type={}'.format(msg_type))
80 self.assertEqual(type(extracted[
'data'][
'secs']), int,
'type={}'.format(msg_type))
81 self.assertEqual(type(extracted[
'data'][
'nsecs']), int,
'type={}'.format(msg_type))
84 msg = UInt8MultiArray(data=[0, 1, 2])
87 data = extracted[
'data']
88 self.assertEqual(type(data), bytes)
89 for i, val
in enumerate(msg.data):
91 self.assertEqual(ord(data[i]), val)
93 self.assertEqual(data[i], val)
96 for msg_type
in [Int8MultiArray, Int16MultiArray, Int32MultiArray, Int64MultiArray,
97 UInt16MultiArray, UInt32MultiArray, UInt64MultiArray,
98 Float32MultiArray, Float64MultiArray]:
99 msg = msg_type(data=[0, 1, 2])
102 tag = extracted[
'data']
103 self.assertEqual(type(tag), Tag,
'type={}'.format(msg_type))
104 self.assertEqual(type(tag.value), bytes,
'type={}'.format(msg_type))
107 array_type = msg._slot_types[1]
109 expected_tag = TAGGED_ARRAY_FORMATS[array_type][0]
110 self.assertEqual(tag.tag, expected_tag,
'type={}'.format(msg_type))
112 fmt = TAGGED_ARRAY_FORMATS[array_type][1]
113 fmt_to_length = fmt.format(len(msg.data))
114 unpacked = list(struct.unpack(fmt_to_length, tag.value))
116 self.assertEqual(unpacked, msg.data,
'type={}'.format(msg_type))
119 msg = UInt8MultiArray(layout=MultiArrayLayout(
136 ex_layout = extracted[
'layout']
137 self.assertEqual(type(ex_layout), dict)
138 self.assertEqual(ex_layout[
'data_offset'], msg.layout.data_offset)
139 self.assertEqual(len(ex_layout[
'dim']), len(msg.layout.dim))
140 for i, val
in enumerate(msg.layout.dim):
141 self.assertEqual(ex_layout[
'dim'][i][
'label'], val.label)
142 self.assertEqual(ex_layout[
'dim'][i][
'size'], val.size)
143 self.assertEqual(ex_layout[
'dim'][i][
'stride'], val.stride)
146 msg = String(data=
"foo")
149 keys = extracted.keys()
152 self.assertEqual(type(key), unicode)
154 self.assertEqual(type(key), str)
157 PKG =
'rosbridge_library' 158 NAME =
'test_cbor_conversion' 159 if __name__ ==
'__main__':
160 rostest.unitrun(PKG, NAME, TestCBORConversion)
def test_nested_messages(self)
def test_unicode_keys(self)
def test_byte_array(self)
def test_numeric_array(self)
def extract_cbor_values(msg)