test_rospy_names.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2008, Willow Garage, Inc.
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of Willow Garage, Inc. nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 
00034 import os
00035 import sys
00036 import struct
00037 import unittest
00038 import time
00039 
00040 # test rospy.names package
00041 class TestRospyNames(unittest.TestCase):
00042 
00043     def test_scoped_name(self):
00044         from rospy.exceptions import ROSException
00045         from rospy.names import scoped_name
00046         tests = [
00047             ['/', '/name', 'name'],
00048             ['/', '/ns/name', 'ns/name'],
00049             ['/ns', '/ns/name', 'ns/name'],            
00050             ['/ns/node', '/ns/name', 'name'],            
00051             ['/ns/', '/ns/name', 'ns/name'],            
00052             ['/ns/ns2/', '/ns/name', 'name'],            
00053             ]
00054         for caller_id, name, v in tests:
00055             val = scoped_name(caller_id, name)
00056             self.assertEquals(v, val, "failed on [%s] [%s]: %s"%(caller_id, name, val))
00057 
00058         fail = [
00059             ['name', '/name'],
00060             ['~name', '/name'],
00061             ]
00062 
00063         for caller_id, name in fail:
00064             try:
00065                 scoped_name(caller_id, name)
00066                 self.fail("should have failed on %s, %s"%(caller_id, name))
00067             except ROSException: pass
00068 
00069     def test_mappings(self):
00070         import roslib.names
00071         import rospy.names
00072         from rospy.names import get_mappings, get_resolved_mappings, initialize_mappings
00073         # get_mappings is initialized statically, so can't test anything other than it is empty
00074         self.assertEquals({}, get_mappings())
00075 
00076         # resolved mappings should be empty with no initialization
00077         self.assertEquals({}, get_resolved_mappings())
00078         
00079         # now, initialize mappings, shouldn't matter as there are no mappings
00080         initialize_mappings('foo')
00081         # should be empty now
00082         self.assertEquals({}, get_resolved_mappings())
00083 
00084         # manipulate mappings to test
00085         rospy.names._mappings = roslib.names.load_mappings(['__name:=newname', '__log:=blah', '_param:=value', 'foo:=bar','/baz:=a/b', '~car:=c/d/e'])
00086         # - param mapping should be removed
00087         self.assertEquals({'__name': 'newname', '__log': 'blah', 
00088                            'foo': 'bar', '/baz': 'a/b', '~car': 'c/d/e'}, get_mappings())
00089         # - should be unaltered
00090         self.assertEquals({}, get_resolved_mappings())        
00091         initialize_mappings('/name')
00092 
00093         # should be unchanged
00094         self.assertEquals({'__name': 'newname', '__log': 'blah', 
00095                            'foo': 'bar', '/baz': 'a/b', '~car': 'c/d/e'}, get_mappings())
00096         # should be remapped
00097         self.assertEquals({'__name': 'newname', '__log': 'blah', 
00098                            '/foo': '/bar', '/baz': '/a/b', '/name/car':'/c/d/e'},
00099                           get_resolved_mappings())
00100         
00101         # try with namespaced node
00102         initialize_mappings('/ns/name')
00103         # should be remapped
00104         self.assertEquals({'__name': 'newname', '__log': 'blah', 
00105                            '/ns/foo': '/ns/bar', '/baz': '/ns/a/b', '/ns/name/car':'/ns/c/d/e'},
00106                           get_resolved_mappings())
00107                                                            
00108         
00109         
00110     def test_canonicalize_name(self):
00111         from rospy.names import canonicalize_name
00112         tests = [
00113             ('', ''),
00114             ('/', '/'),
00115             ('foo', 'foo'),          
00116             ('/foo', '/foo'),          
00117             ('/foo/', '/foo'),
00118             ('/foo/bar', '/foo/bar'),
00119             ('/foo/bar/', '/foo/bar'),
00120             ('/foo/bar//', '/foo/bar'),
00121             ('/foo//bar', '/foo/bar'),
00122             ('//foo/bar', '/foo/bar'),
00123             ('foo/bar', 'foo/bar'),
00124             ('foo//bar', 'foo/bar'),
00125             ('foo/bar/', 'foo/bar'),
00126             ('/foo/bar', '/foo/bar'),
00127             ]
00128         for t, v in tests:
00129             self.assertEquals(v, canonicalize_name(t))
00130             
00131     def test_ANYTYPE(self):
00132         from rospy.names import TOPIC_ANYTYPE, SERVICE_ANYTYPE
00133         self.assertEquals("*", TOPIC_ANYTYPE)
00134         self.assertEquals("*", SERVICE_ANYTYPE)
00135 
00136     def test_resolve_name(self):
00137         from rospy.names import resolve_name
00138         # TODO: test with remappings
00139         tests = [
00140             ('', '/', '/'),
00141             ('', None, '/'), #node_name defaults to /
00142             ('', '/node', '/'),
00143             ('', '/ns1/node', '/ns1/'),
00144 
00145             ('foo', '', '/foo'),
00146             ('foo', None, '/foo'),
00147             ('foo/', '', '/foo'),
00148             ('/foo', '', '/foo'),
00149             ('/foo/', '', '/foo'),
00150             ('/foo', '/', '/foo'),
00151             ('/foo', None, '/foo'),
00152             ('/foo/', '/', '/foo'),
00153             ('/foo', '/bar', '/foo'),
00154             ('/foo/', '/bar', '/foo'),
00155 
00156             ('foo', '/ns1/ns2', '/ns1/foo'),
00157             ('foo', '/ns1/ns2/', '/ns1/foo'),
00158             ('foo', '/ns1/ns2/ns3/', '/ns1/ns2/foo'),
00159             ('foo/', '/ns1/ns2', '/ns1/foo'),
00160             ('/foo', '/ns1/ns2', '/foo'),
00161             ('foo/bar', '/ns1/ns2', '/ns1/foo/bar'),
00162             ('foo//bar', '/ns1/ns2', '/ns1/foo/bar'),
00163             ('foo/bar', '/ns1/ns2/ns3', '/ns1/ns2/foo/bar'),
00164             ('foo//bar//', '/ns1/ns2/ns3', '/ns1/ns2/foo/bar'),
00165             
00166             ('~foo', '/', '/foo'),            
00167             ('~foo', '/node', '/node/foo'),            
00168             ('~foo', '/ns1/ns2', '/ns1/ns2/foo'),            
00169             ('~foo/', '/ns1/ns2', '/ns1/ns2/foo'),            
00170             ('~foo/bar', '/ns1/ns2', '/ns1/ns2/foo/bar'),
00171 
00172             ]
00173         for name, node_name, v in tests:
00174             self.assertEquals(v, resolve_name(name, node_name))
00175 
00176     def test_valid_name(self):
00177         # test with resolution
00178         from rospy.names import valid_name_validator_resolved, valid_name, ParameterInvalid
00179         validator = valid_name('param_name', True)
00180         tests = [
00181             ('name', '/node', '/name'),            
00182             ('/name', '/node', '/name'),            
00183             ('~name', '/node', '/node/name'),
00184             # test unicode
00185             (u'~name', '/node', u'/node/name'),                        
00186             ]
00187         for name, caller_id, v in tests:
00188             self.assertEquals(v, valid_name_validator_resolved('p', name, caller_id), "failed on %s %s"%(name, caller_id))
00189             self.assertEquals(v, validator(name, caller_id))
00190 
00191         # kwc: valid_name is currently very soft in the failures it
00192         # checks as it is targetted at catching parameter
00193         # misalignment. I would like to make it more strict in the
00194         # future.
00195         invalid = [
00196             (1, '/node'),            
00197             (None, '/node'),            
00198             ('localhost:123', '/node'),            
00199             ('Bob Barker', '/node'),
00200             # unicode
00201             (u'Bob Barker', '/node'),            
00202             ]
00203         for name, caller_id in invalid:
00204             try:
00205                 valid_name_validator_resolved('p', name, caller_id)
00206                 self.fail("valid_name_validator_unresolved should have failed on : [%s], [%s]"%(name, caller_id))
00207             except ParameterInvalid: pass
00208             try:
00209                 validator(name, caller_id)
00210                 self.fail("valid_name_validator_unresolved should have failed on : [%s], [%s]"%(name, caller_id))
00211             except ParameterInvalid: pass
00212 
00213         from rospy.names import valid_name_validator_unresolved
00214         validator = valid_name('param_name', False)
00215         tests = [
00216             ('name', '/node', 'name'),            
00217             ('/name', '/node', '/name'),            
00218             ('~name', '/node', '~name'),
00219             # unicode
00220             (u'~name', '/node', u'~name'),            
00221             ]
00222         for name, caller_id, v in tests:
00223             self.assertEquals(v, valid_name_validator_unresolved('p', name, caller_id), "failed on [%s] [%s]"%(name, caller_id))
00224             self.assertEquals(v, validator(name, caller_id))
00225             
00226         for name, caller_id in invalid:
00227             try:
00228                 valid_name_validator_unresolved('p', name, caller_id)
00229                 self.fail("valid_name_validator_unresolved should have failed on : [%s], [%s]"%(name, caller_id))
00230             except ParameterInvalid: pass
00231             try:
00232                 validator(name, caller_id)
00233                 self.fail("valid_name_validator_unresolved should have failed on : [%s], [%s]"%(name, caller_id))
00234             except ParameterInvalid: pass
00235 
00236     def test_global_name(self):
00237         from rospy.names import global_name, ParameterInvalid
00238         validator = global_name('param_name')
00239         tests = [
00240             ('/', '/node', '/'),            
00241             ('/name', '/node', '/name'),
00242             # unicode
00243             (u'/name', '/node', u'/name'),            
00244             ]
00245         for name, caller_id, v in tests:
00246             self.assertEquals(v, validator(name, caller_id))
00247         invalid = [
00248             (1, '/node'),            
00249             (None, '/node'),            
00250             ('name', '/node'),   
00251             ('~name', '/node'),            
00252             ]
00253         for name, caller_id in invalid:
00254             try:
00255                 validator(name, caller_id)
00256                 self.fail("global_name should have failed on : [%s], [%s]"%(name, caller_id))
00257             except ParameterInvalid: pass
00258 
00259     def test_caller_id(self):
00260         from rospy.names import get_caller_id, get_name, _set_caller_id, get_namespace
00261         # test get_name, get_caller_id, and _set_caller_id
00262         try:
00263             self.assertEquals('/unnamed', get_name())
00264             self.assertEquals('/', get_namespace())
00265             _set_caller_id('/foo')
00266             self.assertEquals('/foo', get_name())
00267             self.assertEquals('/', get_namespace())
00268             _set_caller_id('/foo/bar')
00269             self.assertEquals('/foo/bar', get_name())
00270             self.assertEquals('/foo/', get_namespace())
00271         finally:
00272             _set_caller_id('/unnamed')
00273 
00274         # older get_caller_id usage
00275         try:
00276             self.assertEquals('/unnamed', get_caller_id())
00277             self.assertEquals('/', get_namespace())
00278             _set_caller_id('/foo')
00279             self.assertEquals('/foo', get_caller_id())
00280             self.assertEquals('/', get_namespace())
00281             _set_caller_id('/foo/bar')
00282             self.assertEquals('/foo/bar', get_caller_id())
00283             self.assertEquals('/foo/', get_namespace())
00284         finally:
00285             _set_caller_id('/unnamed')


test_rospy
Author(s): Ken Conley
autogenerated on Thu Jun 6 2019 21:10:57