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 import os
00034 import sys
00035 import time
00036 import unittest
00037 import traceback
00038 
00039 import yaml
00040 
00041 import roslib.message
00042 import genpy
00043 from genpy import Time, Duration
00044 
00045 # Not much to test, just tripwires
00046 
00047 class MessageTest(unittest.TestCase):
00048     
00049     def test_check_types_Header(self):
00050         # #2128: test that check_types works with a Header
00051         # #message. This is a weird case because Header has an aliased
00052         # #type and is the only ROS type for which that is true
00053         from test_roslib_comm.msg import HeaderTest
00054         x = HeaderTest()
00055         x._check_types()
00056         
00057     def test_strify_message(self):
00058         from genpy.message import Message, strify_message, fill_message_args
00059         def roundtrip(m):
00060             yaml_text = strify_message(m)
00061             print yaml_text
00062             loaded = yaml.load(yaml_text) 
00063             print "loaded", loaded
00064             new_inst = m.__class__()
00065             if loaded is not None:
00066                 fill_message_args(new_inst, [loaded])
00067             else:
00068                 fill_message_args(new_inst, [])                
00069             return new_inst
00070 
00071         # The following tests have not been ported to genpy yet
00072 
00073         # test array of Messages field. We can't use M4 or M5 because fill_message_args has to instantiate the embedded type
00074         from test_roslib_comm.msg import ArrayOfMsgs
00075         from std_msgs.msg import String, Time, MultiArrayLayout, MultiArrayDimension
00076         dims1 = [MultiArrayDimension(*args) for args in [('', 0, 0), ('x', 1, 2), ('y of z', 3, 4)]]
00077         dims2 = [MultiArrayDimension('hello world', 91280, 1983274)]
00078         times = [Time(genpy.Time(*args)) for args in [(0,), (12345, 6789), (1, 1)]]
00079         val = ArrayOfMsgs([String(''), String('foo'), String('bar of soap')],
00080                           times,
00081                           [MultiArrayLayout(dims1, 0), MultiArrayLayout(dims2, 12354)],
00082                           )
00083         self.assertEquals(val, roundtrip(val))
00084         
00085 
00086     def test_get_message_class(self):
00087         from roslib.message import get_message_class
00088       
00089         try:
00090             self.assertEquals(None, get_message_class('String'))
00091             self.fail("should have thrown ValueError")
00092         except ValueError: pass
00093         # non-existent package
00094         self.assertEquals(None, get_message_class('fake/Fake'))
00095         # non-existent message
00096         self.assertEquals(None, get_message_class('roslib/Fake'))
00097         # package with no messages
00098         self.assertEquals(None, get_message_class('genmsg_cpp/Fake'))
00099     
00100         import rosgraph_msgs.msg
00101         import std_msgs.msg
00102         self.assertEquals(std_msgs.msg.Header, get_message_class('Header'))
00103         self.assertEquals(std_msgs.msg.Header, get_message_class('std_msgs/Header'))
00104         self.assertEquals(rosgraph_msgs.msg.Log, get_message_class('rosgraph_msgs/Log'))    
00105 
00106     def test_get_service_class(self):
00107         from roslib.message import get_service_class
00108 
00109         # non-existent package
00110         self.assertEquals(None, get_service_class('fake/Fake'))
00111         # non-existent message
00112         self.assertEquals(None, get_service_class('roslib/Fake'))
00113         # package with no messages
00114         self.assertEquals(None, get_service_class('genmsg_cpp/Fake'))
00115     
00116         import std_srvs.srv 
00117         self.assertEquals(std_srvs.srv.Empty, get_service_class('std_srvs/Empty'))    
00118 


test_roslib_comm
Author(s): Jeremy Leibs, Ken Conley
autogenerated on Mon Oct 6 2014 11:47:04