test_roslib_msgs.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 unittest
00036 
00037 import roslib
00038 PKG='test_roslib_comm'
00039 class MsgSpecTest(unittest.TestCase):
00040 
00041   def test_verbose(self):
00042     import roslib.msgs
00043     self.failIf(roslib.msgs.is_verbose())
00044     roslib.msgs.set_verbose(True)
00045     self.assert_(roslib.msgs.is_verbose())
00046     roslib.msgs.set_verbose(False)    
00047     self.failIf(roslib.msgs.is_verbose())
00048     
00049   def test_base_msg_type(self):
00050     import roslib.msgs    
00051     tests = [(None, None), ('String', 'String'), ('std_msgs/String', 'std_msgs/String'),
00052              ('String[10]', 'String'), ('string[10]', 'string'), ('std_msgs/String[10]', 'std_msgs/String'),
00053              ]
00054     for val, res in tests:
00055       self.assertEquals(res, roslib.msgs.base_msg_type(val))
00056 
00057   def test_resolve_type(self):
00058     from roslib.msgs import resolve_type
00059     for t in ['string', 'string[]', 'string[14]', 'int32', 'int32[]']:
00060       bt = roslib.msgs.base_msg_type(t)
00061       self.assertEquals(t, resolve_type(t, PKG))
00062       
00063     self.assertEquals('foo/string', resolve_type('foo/string', PKG))
00064     self.assertEquals('std_msgs/Header', resolve_type('Header', 'roslib'))
00065     self.assertEquals('std_msgs/Header', resolve_type('std_msgs/Header', 'roslib'))
00066     self.assertEquals('std_msgs/Header', resolve_type('Header', 'stereo_msgs'))
00067     self.assertEquals('std_msgs/String', resolve_type('String', 'std_msgs'))    
00068     self.assertEquals('std_msgs/String', resolve_type('std_msgs/String', 'std_msgs'))
00069     self.assertEquals('std_msgs/String', resolve_type('std_msgs/String', PKG))    
00070     self.assertEquals('std_msgs/String[]', resolve_type('std_msgs/String[]', PKG))
00071     
00072   def test_parse_type(self):
00073     tests = [
00074       ('a', ('a', False, None)),
00075       ('int8', ('int8', False, None)),      
00076       ('std_msgs/String', ('std_msgs/String', False, None)),
00077       ('a[]', ('a', True, None)),
00078       ('int8[]', ('int8', True, None)),      
00079       ('std_msgs/String[]', ('std_msgs/String', True, None)),
00080       ('a[1]', ('a', True, 1)),
00081       ('int8[1]', ('int8', True, 1)),      
00082       ('std_msgs/String[1]', ('std_msgs/String', True, 1)),
00083       ('a[11]', ('a', True, 11)),
00084       ('int8[11]', ('int8', True, 11)),      
00085       ('std_msgs/String[11]', ('std_msgs/String', True, 11)),
00086       ]
00087     for val, res in tests:
00088       self.assertEquals(res, roslib.msgs.parse_type(val))
00089     
00090     fail = ['a[1][2]', 'a[][]', '', None, 'a[', 'a[[1]', 'a[1]]']
00091     for f in fail:
00092       try:
00093         roslib.msgs.parse_type(f)
00094         self.fail("should have failed on " +f)
00095       except roslib.msgs.MsgSpecException, e: pass
00096 
00097   def test_Constant(self):
00098     import random
00099     import roslib.msgs    
00100     vals = [random.randint(0, 1000) for i in xrange(0, 3)]
00101     type_, name, val = [str(x) for x in vals]
00102     x = roslib.msgs.Constant(type_, name, val, str(val))
00103     self.assertEquals(type_, x.type)
00104     self.assertEquals(name, x.name)
00105     self.assertEquals(val, x.val)
00106     self.assertEquals(roslib.msgs.Constant(type_, name, val, str(val)), x)
00107 
00108     self.assertNotEquals(1, x)
00109     self.assertNotEquals(roslib.msgs.Constant('baz', name, val, str(val)), x)
00110     self.assertNotEquals(roslib.msgs.Constant(type_, 'foo', val, str(val)), x)
00111     self.assertNotEquals(roslib.msgs.Constant(type_, name, 'foo', 'foo'), x)
00112 
00113     # tripwire
00114     self.assert_(repr(x))
00115     self.assert_(str(x))    
00116     
00117     try:
00118       roslib.msgs.Constant(None, name, val, str(val))
00119     except: pass
00120     try:
00121       roslib.msgs.Constant(type_, None, val, str(val))
00122     except: pass
00123     try:
00124       roslib.msgs.Constant(type_, name, None, 'None')      
00125     except: pass
00126     try:
00127       roslib.msgs.Constant(type_, name, val, None)      
00128     except: pass
00129     
00130     try:
00131       x.foo = 'bar'
00132       self.fail('Constant should not allow arbitrary attr assignment')
00133     except: pass
00134     
00135   def test_MsgSpec(self):
00136     def sub_test_MsgSpec(types, names, constants, text, has_header):
00137       m = MsgSpec(types, names, constants, text)
00138       self.assertEquals(m.types, types)
00139       self.assertEquals(m.names, names)
00140       self.assertEquals(m.text, text)
00141       self.assertEquals(has_header, m.has_header())
00142       self.assertEquals(m.constants, constants)
00143       self.assertEquals(zip(types, names), m.fields())
00144       self.assertEquals(m, MsgSpec(types, names, constants, text))
00145       return m
00146     
00147     from roslib.msgs import MsgSpec
00148     # allow empty msg
00149     empty = sub_test_MsgSpec([], [], [], '', False)
00150     self.assertEquals([], empty.fields())    
00151 
00152     # one-field
00153     one_field = sub_test_MsgSpec(['int32'], ['x'], [], 'int32 x', False)
00154     # make sure that equals tests every declared field
00155     self.assertEquals(one_field, MsgSpec(['int32'], ['x'], [], 'int32 x'))
00156     self.assertNotEquals(one_field, MsgSpec(['uint32'], ['x'], [], 'int32 x'))
00157     self.assertNotEquals(one_field, MsgSpec(['int32'], ['y'], [], 'int32 x'))
00158     self.assertNotEquals(one_field, MsgSpec(['int32'], ['x'], [], 'uint32 x'))
00159     # test against __ne__ as well
00160     self.assert_(one_field != MsgSpec(['int32'], ['x'], [], 'uint32 x'))
00161     #test strify
00162     self.assertEquals("int32 x", str(one_field).strip())
00163     
00164     # test variations of multiple fields and headers
00165     two_fields = sub_test_MsgSpec(['int32', 'string'], ['x', 'str'], [], 'int32 x\nstring str', False)
00166     one_header = sub_test_MsgSpec(['Header'], ['header'], [], 'Header header', True)
00167     header_and_fields = sub_test_MsgSpec(['Header', 'int32', 'string'], ['header', 'x', 'str'], [], 'Header header\nint32 x\nstring str', True)
00168     embed_types = sub_test_MsgSpec(['Header', 'std_msgs/Int32', 'string'], ['header', 'x', 'str'], [], 'Header header\nstd_msgs/Int32 x\nstring str', True)
00169     #test strify
00170     self.assertEquals("int32 x\nstring str", str(two_fields).strip())
00171 
00172     # types and names mismatch
00173     try:
00174       MsgSpec(['int32', 'int32'], ['intval'], [], 'int32 intval\int32 y')
00175       self.fail("types and names must align")
00176     except: pass
00177 
00178     # test (not) equals against non msgspec
00179     self.failIf(one_field == 1)
00180     self.assert_(one_field != 1)    
00181     #TODO: test flatten
00182 
00183     # test that repr doesn't throw an error
00184     [repr(x) for x in [empty, one_field, one_header, two_fields, embed_types]]
00185 
00186   def test_init(self):
00187     import roslib.msgs    
00188     roslib.msgs._initialized = False
00189     roslib.msgs._init()
00190     self.assert_(roslib.msgs._initialized)
00191     # test repeated initialization
00192     roslib.msgs._init()    
00193 
00194   def test___convert_val(self):
00195     from roslib.msgs import _convert_val, MsgSpecException
00196     self.assertEquals(0., _convert_val('float32', '0.0'))
00197     self.assertEquals(0., _convert_val('float64', '0.0'))
00198     
00199     self.assertEquals('fo o', _convert_val('string', '   fo o '))
00200 
00201     self.assertEquals(1, _convert_val('byte', '1'))
00202     self.assertEquals(1, _convert_val('char', '1'))
00203     self.assertEquals(1, _convert_val('int8', '1'))
00204     self.assertEquals(12, _convert_val('int16', '12'))
00205     self.assertEquals(-13, _convert_val('int32', '-13'))
00206     self.assertEquals(14, _convert_val('int64', '14'))
00207     self.assertEquals(0, _convert_val('uint8', '0'))
00208     self.assertEquals(18, _convert_val('uint16', '18'))
00209     self.assertEquals(19, _convert_val('uint32', '19'))    
00210     self.assertEquals(20, _convert_val('uint64', '20'))
00211 
00212     width_fail = [('int8', '129'), ('uint8', '256'),
00213                   ('int16', '35536'), ('uint16', '-1'),('uint16', '65536'),
00214                   ('int32', '3000000000'),('int32', '-2700000000'),
00215                   ('uint32', '-1'),('uint32', '41000000000'),
00216                   ('uint64', '-1')]
00217     for t, v in width_fail:
00218       try:
00219         _convert_val(t, v)
00220         self.fail("should have failed width check: %s, %s"%(t, v))
00221       except MsgSpecException: pass
00222     type_fail = [('int32', 'f'), ('float32', 'baz')]
00223     for t, v in type_fail:
00224       try:
00225         _convert_val(t, v)
00226         self.fail("should have failed type check: %s, %s"%(t, v))
00227       except ValueError: pass
00228     try:
00229       _convert_val('foo', '1')
00230       self.fail("should have failed invalid type")
00231     except MsgSpecException: pass
00232     
00233   def test_msg_file(self):
00234     import roslib.msgs    
00235     f = roslib.msgs.msg_file('rosgraph_msgs', 'Log')
00236     self.assert_(os.path.isfile(f))
00237     self.assert_(f.endswith('rosgraph_msgs/msg/Log.msg'))
00238 
00239     # msg_file should return paths even for non-existent resources
00240     f = roslib.msgs.msg_file('roslib', 'Fake')
00241     self.failIf(os.path.isfile(f))
00242     self.assert_(f.endswith('roslib/msg/Fake.msg'))
00243 
00244   def test_is_valid_msg_type(self):
00245     import roslib.msgs
00246     vals = [
00247       #basic
00248       'F', 'f', 'Foo', 'Foo1',
00249       'std_msgs/String',
00250       # arrays
00251       'Foo[]', 'Foo[1]', 'Foo[10]',
00252       ]
00253     for v in vals:
00254       self.assert_(roslib.msgs.is_valid_msg_type(v), "roslib.msgs.is_valid_msg_type should have returned True for '%s'"%v)
00255  
00256     # bad cases
00257     vals = [None, '', '#', '%', 'Foo%', 'Woo Woo',
00258             '/', '/String', 
00259             'Foo[f]', 'Foo[1d]', 'Foo[-1]', 'Foo[1:10]', 'Foo[', 'Foo]', 'Foo[]Bar']
00260     for v in vals:
00261       self.failIf(roslib.msgs.is_valid_msg_type(v), "roslib.msgs.is_valid_msg_type should have returned False for '%s'"%v)
00262       
00263   def test_is_valid_constant_type(self):
00264     import roslib.msgs
00265     valid = ['int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', \
00266              'uint64', 'float32', 'float64', 'char', 'byte', 'string']
00267     invalid = [
00268       'std_msgs/String', '/', 'String',
00269       'time', 'duration','header',
00270     ]
00271     for v in valid:
00272       self.assert_(roslib.msgs.is_valid_constant_type(v), "roslib.msgs.is_valid_constant_type should have returned True for '%s'"%v)
00273     for v in invalid:
00274       self.failIf(roslib.msgs.is_valid_constant_type(v), "roslib.msgs.is_valid_constant_type should have returned False for '%s'"%v)
00275     


test_roslib_comm
Author(s): Jeremy Leibs/leibs@willowgarage.com, Ken Conley/kwc@willowgarage.com
autogenerated on Sat Dec 28 2013 17:43:03