00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 import os
00035 import sys
00036 import struct
00037 import unittest
00038 import time
00039
00040
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
00074 self.assertEquals({}, get_mappings())
00075
00076
00077 self.assertEquals({}, get_resolved_mappings())
00078
00079
00080 initialize_mappings('foo')
00081
00082 self.assertEquals({}, get_resolved_mappings())
00083
00084
00085 rospy.names._mappings = roslib.names.load_mappings(['__name:=newname', '__log:=blah', '_param:=value', 'foo:=bar','/baz:=a/b', '~car:=c/d/e'])
00086
00087 self.assertEquals({'__name': 'newname', '__log': 'blah',
00088 'foo': 'bar', '/baz': 'a/b', '~car': 'c/d/e'}, get_mappings())
00089
00090 self.assertEquals({}, get_resolved_mappings())
00091 initialize_mappings('/name')
00092
00093
00094 self.assertEquals({'__name': 'newname', '__log': 'blah',
00095 'foo': 'bar', '/baz': 'a/b', '~car': 'c/d/e'}, get_mappings())
00096
00097 self.assertEquals({'__name': 'newname', '__log': 'blah',
00098 '/foo': '/bar', '/baz': '/a/b', '/name/car':'/c/d/e'},
00099 get_resolved_mappings())
00100
00101
00102 initialize_mappings('/ns/name')
00103
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
00139 tests = [
00140 ('', '/', '/'),
00141 ('', None, '/'),
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
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
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
00192
00193
00194
00195 invalid = [
00196 (1, '/node'),
00197 (None, '/node'),
00198 ('localhost:123', '/node'),
00199 ('Bob Barker', '/node'),
00200
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
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
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
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
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')