test_nlj_laser_interfaces.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 # -*- coding: utf-8 -*-
00003 
00004 from __future__ import print_function, division
00005 
00006 import unittest
00007 
00008 import rospy
00009 import roslib.message
00010 
00011 from sensor_msgs.msg import LaserScan
00012 
00013 from nlj_laser.msg import LaserDescriptor
00014 
00015 from lama_interfaces.interface_factory import interface_factory
00016 
00017 from nlj_laser.srv import GetLaserDescriptorRequest
00018 
00019 
00020 class RosTestCase(unittest.TestCase):
00021     def assertMsgEqual(self, msg0, msg1):
00022         """Fail if two ROS messages are not equal"""
00023         self.assertIsInstance(msg0, roslib.message.Message,
00024                               msg='Argument 1 is not a Message')
00025         self.assertIsInstance(msg1, roslib.message.Message,
00026                               msg='Argument 2 is not a Message')
00027         slots0 = msg0.__slots__
00028         slots1 = msg1.__slots__
00029         self.assertEquals(slots0, slots1,
00030                           msg=('Messages do not have the same arguments\n' +
00031                                'Msg1 args: {}\n'.format(slots0) +
00032                                'Msg2 args: {}\n'.format(slots1)))
00033         for slot in slots0:
00034             value0 = getattr(msg0, slot)
00035             value1 = getattr(msg1, slot)
00036             if isinstance(value0, roslib.message.Message):
00037                 self.assertMsgEqual(value0, value1)
00038             elif isinstance(value0, (list, tuple)):
00039                 self.assertEquals(len(value0), len(value1),
00040                                         msg="list size is not same")
00041 #                self.assertAlmostEquals(list(value0), list(value1), places=6,
00042 #                                        msg='Argument {} differ: {} != {}'.
00043 #                                        format(slot, value0, value1))
00044             else:
00045                 self.assertAlmostEqual(value0, value1, places=6,
00046                                        msg='Argument {} differ: {} != {}'.
00047                                        format(slot, value0, value1))
00048 
00049 
00050 class TestDbMessagePassing(RosTestCase):
00051     """test setting and getting laser descriptors"""
00052     def __init__(self, *args, **kwargs):
00053         rospy.init_node('test_lama_interfaces_nlj_laser', anonymous=True)
00054         super(TestDbMessagePassing, self).__init__(*args, **kwargs)
00055 
00056     def test_laser(self):
00057         """Test passing and getting a Laser message"""
00058         interface_name = 'laser_descriptor'
00059         getter_service = 'nlj_laser/GetLaserDescriptor'
00060         setter_service = 'nlj_laser/SetLaserDescriptor'
00061 
00062         # Set up node as well as getter and setter services.
00063         iface = interface_factory(interface_name,
00064                                   getter_service,
00065                                   setter_service)
00066         get_srv = rospy.ServiceProxy(iface.getter_service_name,
00067                                      iface.getter_service_class)
00068         set_srv = rospy.ServiceProxy(iface.setter_service_name,
00069                                      iface.setter_service_class)
00070 
00071         laser0 = LaserDescriptor()
00072         laser0.scans.append(LaserScan())
00073         for  r in xrange(0, 100):
00074             laser0.scans[0].ranges.append(r*0.01)
00075         laser1 = LaserDescriptor()
00076         laser1.scans.append(LaserScan())
00077         for  r in xrange(0, 100):
00078             laser1.scans[0].ranges.append(r*0.01)
00079 
00080         id_from_setter0 = set_srv(laser0)
00081         # id_from_setter cannot be passed to get_srv because of
00082         # type incompatibility, "transform" it to a Get...Request()
00083         id_to_getter0 = GetLaserDescriptorRequest()
00084         id_to_getter0.id = id_from_setter0.id
00085         response0 = get_srv(id_to_getter0)
00086 
00087         id_from_setter1 = set_srv(laser1)
00088         # id_from_setter cannot be passed to get_srv because of
00089         # type incompatibility, "transform" it to a Get...Request()
00090         id_to_getter1 = GetLaserDescriptorRequest()
00091         id_to_getter1.id = id_from_setter1.id
00092         response1 = get_srv(id_to_getter1)
00093 
00094         self.assertIsNot(laser0, response0.descriptor)
00095         self.assertIsNot(laser1, response1.descriptor)
00096         self.assertMsgEqual(laser0, response0.descriptor)
00097         self.assertMsgEqual(laser1, response1.descriptor)
00098 
00099 if __name__ == '__main__':
00100     import rostest
00101     rostest.rosrun('nlj_laser',
00102                    'test_db_message_passing',
00103                    TestDbMessagePassing)


nlj_laser
Author(s): Gaël Ecorchard , Karel Košnar
autogenerated on Thu Jun 6 2019 17:50:56