test_roslib_message.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2009, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 from __future__ import print_function
00034 
00035 import os
00036 import sys
00037 import time
00038 import unittest
00039 import traceback
00040 
00041 import yaml
00042 
00043 import roslib.message
00044 import genpy
00045 from genpy import Time, Duration
00046 
00047 # Not much to test, just tripwires
00048 
00049 class MessageTest(unittest.TestCase):
00050     
00051     def test_check_types_Header(self):
00052         # #2128: test that check_types works with a Header
00053         # #message. This is a weird case because Header has an aliased
00054         # #type and is the only ROS type for which that is true
00055         from test_roslib_comm.msg import HeaderTest
00056         x = HeaderTest()
00057         x._check_types()
00058         
00059     def test_strify_message(self):
00060         from genpy.message import Message, strify_message, fill_message_args
00061         def roundtrip(m):
00062             yaml_text = strify_message(m)
00063             print(yaml_text)
00064             loaded = yaml.load(yaml_text) 
00065             print("loaded", loaded)
00066             new_inst = m.__class__()
00067             if loaded is not None:
00068                 fill_message_args(new_inst, [loaded])
00069             else:
00070                 fill_message_args(new_inst, [])                
00071             return new_inst
00072 
00073         # The following tests have not been ported to genpy yet
00074 
00075         # test array of Messages field. We can't use M4 or M5 because fill_message_args has to instantiate the embedded type
00076         from test_roslib_comm.msg import ArrayOfMsgs
00077         from std_msgs.msg import String, Time, MultiArrayLayout, MultiArrayDimension
00078         dims1 = [MultiArrayDimension(*args) for args in [('', 0, 0), ('x', 1, 2), ('y of z', 3, 4)]]
00079         dims2 = [MultiArrayDimension('hello world', 91280, 1983274)]
00080         times = [Time(genpy.Time(*args)) for args in [(0,), (12345, 6789), (1, 1)]]
00081         val = ArrayOfMsgs([String(''), String('foo'), String('bar of soap')],
00082                           times,
00083                           [MultiArrayLayout(dims1, 0), MultiArrayLayout(dims2, 12354)],
00084                           )
00085         self.assertEquals(val, roundtrip(val))
00086         
00087 
00088     def test_get_message_class(self):
00089         from roslib.message import get_message_class
00090       
00091         try:
00092             self.assertEquals(None, get_message_class('String'))
00093             self.fail("should have thrown ValueError")
00094         except ValueError: pass
00095         # non-existent package
00096         self.assertEquals(None, get_message_class('fake/Fake'))
00097         # non-existent message
00098         self.assertEquals(None, get_message_class('roslib/Fake'))
00099         # package with no messages
00100         self.assertEquals(None, get_message_class('genmsg_cpp/Fake'))
00101     
00102         import rosgraph_msgs.msg
00103         import std_msgs.msg
00104         self.assertEquals(std_msgs.msg.Header, get_message_class('Header'))
00105         self.assertEquals(std_msgs.msg.Header, get_message_class('std_msgs/Header'))
00106         self.assertEquals(rosgraph_msgs.msg.Log, get_message_class('rosgraph_msgs/Log'))    
00107 
00108     def test_get_service_class(self):
00109         from roslib.message import get_service_class
00110 
00111         # non-existent package
00112         self.assertEquals(None, get_service_class('fake/Fake'))
00113         # non-existent message
00114         self.assertEquals(None, get_service_class('roslib/Fake'))
00115         # package with no messages
00116         self.assertEquals(None, get_service_class('genmsg_cpp/Fake'))
00117     
00118         import std_srvs.srv 
00119         self.assertEquals(std_srvs.srv.Empty, get_service_class('std_srvs/Empty'))    
00120 


test_roslib_comm
Author(s): Jeremy Leibs, Ken Conley
autogenerated on Thu Jun 6 2019 21:09:59