test_message_converter.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 import unittest
00003 import rospy
00004 import rostest
00005 from pprint import pprint
00006 from rospy_message_converter import message_converter
00007 
00008 class TestMessageConverter(unittest.TestCase):
00009 
00010     def test_ros_message_with_array(self):
00011         from rospy_message_converter.msg import TestArray
00012         expected_dictionary = {
00013             'data': [1.1, 2.2, 3.3]
00014         }
00015         message = TestArray(data = expected_dictionary['data'])
00016         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00017         self.assertEqual(dictionary, expected_dictionary)
00018 
00019     def test_ros_message_with_bool(self):
00020         from std_msgs.msg import Bool
00021         expected_dictionary = { 'data': True }
00022         message = Bool(data=expected_dictionary['data'])
00023         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00024         self.assertEqual(dictionary, expected_dictionary)
00025 
00026     def test_ros_message_with_byte(self):
00027         from std_msgs.msg import Byte
00028         expected_dictionary = { 'data': 5 }
00029         message = Byte(data=expected_dictionary['data'])
00030         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00031         self.assertEqual(dictionary, expected_dictionary)
00032 
00033     def test_ros_message_with_char(self):
00034         from std_msgs.msg import Char
00035         expected_dictionary = { 'data': 'c' }
00036         message = Char(data=expected_dictionary['data'])
00037         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00038         self.assertEqual(dictionary, expected_dictionary)
00039 
00040     def test_ros_message_with_duration(self):
00041         from std_msgs.msg import Duration
00042         duration = rospy.rostime.Duration(33, 25)
00043         expected_dictionary = {
00044             'data': {
00045                 'secs'  : duration.secs,
00046                 'nsecs' : duration.nsecs
00047             }
00048         }
00049         message = Duration(data=duration)
00050         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00051         self.assertEqual(dictionary, expected_dictionary)
00052 
00053     def test_ros_message_with_empty(self):
00054         from std_msgs.msg import Empty
00055         expected_dictionary = {}
00056         message = Empty()
00057         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00058         self.assertEqual(dictionary, expected_dictionary)
00059 
00060     def test_ros_message_with_float32(self):
00061         from std_msgs.msg import Float32
00062         expected_dictionary = { 'data': 0x7F7FFFFD }
00063         message = Float32(data=expected_dictionary['data'])
00064         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00065         self.assertEqual(dictionary, expected_dictionary)
00066 
00067     def test_ros_message_with_float64(self):
00068         from std_msgs.msg import Float64
00069         expected_dictionary = { 'data': 0x7FEFFFFFFFFFFFF }
00070         message = Float64(data=expected_dictionary['data'])
00071         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00072         self.assertEqual(dictionary, expected_dictionary)
00073 
00074     def test_ros_message_with_header(self):
00075         from std_msgs.msg import Header
00076         rospy.init_node('time_node')
00077         now_time = rospy.Time.now()
00078         expected_dictionary = {
00079             'stamp': { 'secs': now_time.secs, 'nsecs': now_time.nsecs },
00080             'frame_id' : 'my_frame',
00081             'seq': 3
00082         }
00083         message = Header(
00084             stamp = now_time,
00085             frame_id = expected_dictionary['frame_id'],
00086             seq = expected_dictionary['seq']
00087         )
00088         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00089         self.assertEqual(dictionary, expected_dictionary)
00090 
00091     def test_ros_message_with_int8(self):
00092         from std_msgs.msg import Int8
00093         expected_dictionary = { 'data': -0x7F }
00094         message = Int8(data=expected_dictionary['data'])
00095         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00096         self.assertEqual(dictionary, expected_dictionary)
00097 
00098     def test_ros_message_with_uint8(self):
00099         from std_msgs.msg import UInt8
00100         expected_dictionary = { 'data': 0xFF }
00101         message = UInt8(data=expected_dictionary['data'])
00102         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00103         self.assertEqual(dictionary, expected_dictionary)
00104 
00105     def test_ros_message_with_uint8_array(self):
00106         from rospy_message_converter.msg import Uint8ArrayTestMessage
00107         from base64 import standard_b64encode
00108         expected_data = "".join([chr(i) for i in [97, 98, 99, 100]])
00109         message = Uint8ArrayTestMessage(data=expected_data)
00110         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00111         self.assertEqual(dictionary["data"], standard_b64encode(expected_data))
00112 
00113     def test_ros_message_with_3uint8_array(self):
00114         from rospy_message_converter.msg import Uint8Array3TestMessage
00115         from base64 import standard_b64encode
00116         expected_data = "".join([chr(i) for i in [97, 98, 99, 100]])
00117         message = Uint8Array3TestMessage(data=expected_data)
00118         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00119         self.assertEqual(dictionary["data"], standard_b64encode(expected_data))
00120 
00121     def test_ros_message_with_int16(self):
00122         from std_msgs.msg import Int16
00123         expected_dictionary = { 'data': -0x7FFF }
00124         message = Int16(data=expected_dictionary['data'])
00125         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00126         self.assertEqual(dictionary, expected_dictionary)
00127 
00128     def test_ros_message_with_uint16(self):
00129         from std_msgs.msg import UInt16
00130         expected_dictionary = { 'data': 0xFFFF }
00131         message = UInt16(data=expected_dictionary['data'])
00132         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00133         self.assertEqual(dictionary, expected_dictionary)
00134 
00135     def test_ros_message_with_int32(self):
00136         from std_msgs.msg import Int32
00137         expected_dictionary = { 'data': -0x7FFFFFFF }
00138         message = Int32(data=expected_dictionary['data'])
00139         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00140         self.assertEqual(dictionary, expected_dictionary)
00141 
00142     def test_ros_message_with_uint32(self):
00143         from std_msgs.msg import UInt32
00144         expected_dictionary = { 'data': 0xFFFFFFFF }
00145         message = UInt32(data=expected_dictionary['data'])
00146         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00147         self.assertEqual(dictionary, expected_dictionary)
00148 
00149     def test_ros_message_with_int64(self):
00150         from std_msgs.msg import Int64
00151         expected_dictionary = { 'data': -0x7FFFFFFFFFFFFFFF }
00152         message = Int64(data=expected_dictionary['data'])
00153         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00154         self.assertEqual(dictionary, expected_dictionary)
00155 
00156     def test_ros_message_with_uint64(self):
00157         from std_msgs.msg import UInt64
00158         expected_dictionary = { 'data': 0xFFFFFFFFFFFFFFFF }
00159         message = UInt64(data=expected_dictionary['data'])
00160         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00161         self.assertEqual(dictionary, expected_dictionary)
00162 
00163     def test_ros_message_with_string(self):
00164         from std_msgs.msg import String
00165         expected_dictionary = { 'data': 'Hello' }
00166         message = String(data=expected_dictionary['data'])
00167         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00168         self.assertEqual(dictionary, expected_dictionary)
00169 
00170     def test_ros_message_with_time(self):
00171         from std_msgs.msg import Time
00172         rospy.init_node('time_node')
00173         now_time = rospy.Time.now()
00174         expected_dictionary = {
00175             'data': { 'secs': now_time.secs, 'nsecs': now_time.nsecs }
00176         }
00177         message = Time(data=now_time)
00178         dictionary = message_converter.convert_ros_message_to_dictionary(message)
00179         self.assertEqual(dictionary, expected_dictionary)
00180 
00181     def test_ros_message_with_child_message(self):
00182         from std_msgs.msg import Float64MultiArray, MultiArrayLayout, MultiArrayDimension
00183         expected_dictionary = {
00184             'layout': {
00185                 'dim': [
00186                     { 'label': 'Dimension1', 'size': 12, 'stride': 7 },
00187                     { 'label': 'Dimension2', 'size': 24, 'stride': 14 }
00188                 ],
00189                 'data_offset': 0
00190             },
00191             'data': [1.1, 2.2, 3.3]
00192         }
00193         dimension1 = MultiArrayDimension(
00194             label  = expected_dictionary['layout']['dim'][0]['label'],
00195             size   = expected_dictionary['layout']['dim'][0]['size'],
00196             stride = expected_dictionary['layout']['dim'][0]['stride']
00197         )
00198         dimension2 = MultiArrayDimension(
00199             label  = expected_dictionary['layout']['dim'][1]['label'],
00200             size   = expected_dictionary['layout']['dim'][1]['size'],
00201             stride = expected_dictionary['layout']['dim'][1]['stride']
00202         )
00203         layout = MultiArrayLayout(
00204             dim = [dimension1, dimension2],
00205             data_offset = expected_dictionary['layout']['data_offset']
00206         )
00207         multiArray = Float64MultiArray(
00208             layout = layout,
00209             data   = expected_dictionary['data']
00210         )
00211         dictionary = message_converter.convert_ros_message_to_dictionary(multiArray)
00212         self.assertEqual(dictionary, expected_dictionary)
00213 
00214     def test_dictionary_with_array(self):
00215         from rospy_message_converter.msg import TestArray
00216         expected_message = TestArray(data = [1.1, 2.2, 3.3, 4.4])
00217         dictionary = { 'data': expected_message.data }
00218         message = message_converter.convert_dictionary_to_ros_message('rospy_message_converter/TestArray', dictionary)
00219         self.assertEqual(message, expected_message)
00220 
00221     def test_dictionary_with_bool(self):
00222         from std_msgs.msg import Bool
00223         expected_message = Bool(data = True)
00224         dictionary = { 'data': expected_message.data }
00225         message = message_converter.convert_dictionary_to_ros_message('std_msgs/Bool', dictionary)
00226         self.assertEqual(message, expected_message)
00227 
00228     def test_dictionary_with_byte(self):
00229         from std_msgs.msg import Byte
00230         expected_message = Byte(data = 3)
00231         dictionary = { 'data': expected_message.data }
00232         message = message_converter.convert_dictionary_to_ros_message('std_msgs/Byte', dictionary)
00233         self.assertEqual(message, expected_message)
00234 
00235     def test_dictionary_with_char(self):
00236         from std_msgs.msg import Char
00237         expected_message = Char(data = 'c')
00238         dictionary = { 'data': expected_message.data }
00239         message = message_converter.convert_dictionary_to_ros_message('std_msgs/Char', dictionary)
00240         self.assertEqual(message, expected_message)
00241 
00242     def test_dictionary_with_duration(self):
00243         from std_msgs.msg import Duration
00244         duration = rospy.rostime.Duration(33, 25)
00245         expected_message = Duration(data = duration)
00246         dictionary = {
00247             'data': {
00248                 'secs'  : duration.secs,
00249                 'nsecs' : duration.nsecs
00250             }
00251         }
00252         message = message_converter.convert_dictionary_to_ros_message('std_msgs/Duration', dictionary)
00253         self.assertEqual(message, expected_message)
00254 
00255     def test_dictionary_with_empty(self):
00256         from std_msgs.msg import Empty
00257         expected_message = Empty()
00258         dictionary = {}
00259         message = message_converter.convert_dictionary_to_ros_message('std_msgs/Empty', dictionary)
00260         self.assertEqual(message, expected_message)
00261 
00262     def test_dictionary_with_float32(self):
00263         from std_msgs.msg import Float32
00264         expected_message = Float32(data = 0x7F7FFFFD)
00265         dictionary = { 'data': expected_message.data }
00266         message = message_converter.convert_dictionary_to_ros_message('std_msgs/Float32', dictionary)
00267         self.assertEqual(message, expected_message)
00268 
00269     def test_dictionary_with_float64(self):
00270         from std_msgs.msg import Float64
00271         expected_message = Float64(data = 0x7FEFFFFFFFFFFFF)
00272         dictionary = { 'data': expected_message.data }
00273         message = message_converter.convert_dictionary_to_ros_message('std_msgs/Float64', dictionary)
00274         self.assertEqual(message, expected_message)
00275 
00276     def test_dictionary_with_header(self):
00277         from std_msgs.msg import Header
00278         rospy.init_node('time_node')
00279         now_time = rospy.Time.now()
00280         expected_message = Header(
00281             stamp = now_time,
00282             frame_id = 'my_frame',
00283             seq = 12
00284         )
00285         dictionary = {
00286             'stamp': { 'secs': now_time.secs, 'nsecs': now_time.nsecs },
00287             'frame_id' : expected_message.frame_id,
00288             'seq': expected_message.seq
00289         }
00290         message = message_converter.convert_dictionary_to_ros_message('std_msgs/Header', dictionary)
00291         self.assertEqual(message, expected_message)
00292 
00293     def test_dictionary_with_header_with_no_prefix(self):
00294         from std_msgs.msg import Header
00295         rospy.init_node('time_node')
00296         now_time = rospy.Time.now()
00297         expected_message = Header(
00298             stamp = now_time,
00299             frame_id = 'my_frame',
00300             seq = 12
00301         )
00302         dictionary = {
00303             'stamp': { 'secs': now_time.secs, 'nsecs': now_time.nsecs },
00304             'frame_id' : expected_message.frame_id,
00305             'seq': expected_message.seq
00306         }
00307         message = message_converter.convert_dictionary_to_ros_message('Header', dictionary)
00308         self.assertEqual(message, expected_message)
00309 
00310     def test_dictionary_with_int8(self):
00311         from std_msgs.msg import Int8
00312         expected_message = Int8(data = -0x7F)
00313         dictionary = { 'data': expected_message.data }
00314         message = message_converter.convert_dictionary_to_ros_message('std_msgs/Int8', dictionary)
00315         self.assertEqual(message, expected_message)
00316 
00317     def test_dictionary_with_uint8(self):
00318         from std_msgs.msg import UInt8
00319         expected_message = UInt8(data = 0xFF)
00320         dictionary = { 'data': expected_message.data }
00321         message = message_converter.convert_dictionary_to_ros_message('std_msgs/UInt8', dictionary)
00322         self.assertEqual(message, expected_message)
00323 
00324     def test_dictionary_with_int16(self):
00325         from std_msgs.msg import Int16
00326         expected_message = Int16(data = -0x7FFF)
00327         dictionary = { 'data': expected_message.data }
00328         message = message_converter.convert_dictionary_to_ros_message('std_msgs/Int16', dictionary)
00329         self.assertEqual(message, expected_message)
00330 
00331     def test_dictionary_with_uint16(self):
00332         from std_msgs.msg import UInt16
00333         expected_message = UInt16(data = 0xFFFF)
00334         dictionary = { 'data': expected_message.data }
00335         message = message_converter.convert_dictionary_to_ros_message('std_msgs/UInt16', dictionary)
00336         self.assertEqual(message, expected_message)
00337 
00338     def test_dictionary_with_int32(self):
00339         from std_msgs.msg import Int32
00340         expected_message = Int32(data = -0x7FFFFFFF)
00341         dictionary = { 'data': expected_message.data }
00342         message = message_converter.convert_dictionary_to_ros_message('std_msgs/Int32', dictionary)
00343         self.assertEqual(message, expected_message)
00344 
00345     def test_dictionary_with_uint32(self):
00346         from std_msgs.msg import UInt32
00347         expected_message = UInt32(data = 0xFFFFFFFF)
00348         dictionary = { 'data': expected_message.data }
00349         message = message_converter.convert_dictionary_to_ros_message('std_msgs/UInt32', dictionary)
00350         self.assertEqual(message, expected_message)
00351 
00352     def test_dictionary_with_int64(self):
00353         from std_msgs.msg import Int64
00354         expected_message = Int64(data = -0x7FFFFFFFFFFFFFFF)
00355         dictionary = { 'data': expected_message.data }
00356         message = message_converter.convert_dictionary_to_ros_message('std_msgs/Int64', dictionary)
00357         self.assertEqual(message, expected_message)
00358 
00359     def test_dictionary_with_uint64(self):
00360         from std_msgs.msg import UInt64
00361         expected_message = UInt64(data = 0xFFFFFFFFFFFFFFFF)
00362         dictionary = { 'data': expected_message.data }
00363         message = message_converter.convert_dictionary_to_ros_message('std_msgs/UInt64', dictionary)
00364         self.assertEqual(message, expected_message)
00365 
00366     def test_dictionary_with_string(self):
00367         from std_msgs.msg import String
00368         expected_message = String(data = 'Hello')
00369         dictionary = { 'data': expected_message.data }
00370         message = message_converter.convert_dictionary_to_ros_message('std_msgs/String', dictionary)
00371         self.assertEqual(message, expected_message)
00372 
00373     def test_dictionary_with_time(self):
00374         from std_msgs.msg import Time
00375         now_time = rospy.Time.now()
00376         expected_message = Time(data=now_time)
00377         dictionary = {
00378             'data': {
00379                 'secs'  : now_time.secs,
00380                 'nsecs' : now_time.nsecs
00381             }
00382         }
00383         message = message_converter.convert_dictionary_to_ros_message('std_msgs/Time', dictionary)
00384         self.assertEqual(message, expected_message)
00385 
00386     def test_dictionary_with_child_message(self):
00387         from std_msgs.msg import Float64MultiArray, MultiArrayLayout, MultiArrayDimension
00388         expected_message = Float64MultiArray(
00389             layout = MultiArrayLayout(
00390                 dim = [
00391                     MultiArrayDimension(label = 'Dimension1', size = 12, stride = 7),
00392                     MultiArrayDimension(label = 'Dimension2', size = 90, stride = 8)
00393                 ],
00394                 data_offset = 1
00395             ),
00396             data = [1.1, 2.2, 3.3]
00397         )
00398         dictionary = {
00399             'layout': {
00400                 'dim': [
00401                     {
00402                         'label'  : expected_message.layout.dim[0].label,
00403                         'size'   : expected_message.layout.dim[0].size,
00404                         'stride' : expected_message.layout.dim[0].stride
00405                     },
00406                     {
00407                         'label'  : expected_message.layout.dim[1].label,
00408                         'size'   : expected_message.layout.dim[1].size,
00409                         'stride' : expected_message.layout.dim[1].stride
00410                     }
00411                 ],
00412                 'data_offset': expected_message.layout.data_offset
00413             },
00414             'data': expected_message.data
00415         }
00416         message = message_converter.convert_dictionary_to_ros_message('std_msgs/Float64MultiArray', dictionary)
00417         self.assertEqual(message, expected_message)
00418 
00419     def test_dictionary_with_invalid_message_fields(self):
00420         self.assertRaises(ValueError,
00421                           message_converter.convert_dictionary_to_ros_message,
00422                           'std_msgs/Empty',
00423                           {'invalid_field': 1})
00424 
00425 
00426 
00427 PKG = 'rospy_message_converter'
00428 NAME = 'test_message_converter'
00429 if __name__ == '__main__':
00430     rostest.unitrun(PKG, NAME, TestMessageConverter)


rospy_message_converter
Author(s): Brandon Alexander
autogenerated on Sat May 6 2017 02:31:54