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 import roslib; roslib.load_manifest('test_roslib')
00033
00034 import os
00035 import sys
00036 import unittest
00037
00038 import roslib.names
00039 import rosunit
00040
00041 class NamesTest(unittest.TestCase):
00042
00043 def test_get_ros_namespace(self):
00044 if 'ROS_NAMESPACE' in os.environ:
00045 rosns = os.environ['ROS_NAMESPACE']
00046 del os.environ['ROS_NAMESPACE']
00047 else:
00048 rosns = None
00049 sysargv = sys.argv
00050
00051 try:
00052 sys.argv = []
00053 self.assertEquals('/', roslib.names.get_ros_namespace())
00054 self.assertEquals('/', roslib.names.get_ros_namespace(argv=[]))
00055 self.assertEquals('/', roslib.names.get_ros_namespace(env={}))
00056 self.assertEquals('/', roslib.names.get_ros_namespace(env={}, argv=[]))
00057
00058 os.environ['ROS_NAMESPACE'] = 'unresolved'
00059 self.assertEquals('/unresolved/', roslib.names.get_ros_namespace())
00060 self.assertEquals('/unresolved/', roslib.names.get_ros_namespace(env={'ROS_NAMESPACE': 'unresolved'}))
00061 sys.argv = ['foo', '__ns:=unresolved_override']
00062 self.assertEquals('/unresolved_override/', roslib.names.get_ros_namespace(env={'ROS_NAMESPACE': 'unresolved'}))
00063 self.assertEquals('/override2/', roslib.names.get_ros_namespace(env={'ROS_NAMESPACE': 'unresolved'}, argv=['foo', '__ns:=override2']))
00064
00065 sys.argv = []
00066 os.environ['ROS_NAMESPACE'] = '/resolved/'
00067 self.assertEquals('/resolved/', roslib.names.get_ros_namespace())
00068 self.assertEquals('/resolved/', roslib.names.get_ros_namespace(env={'ROS_NAMESPACE': '/resolved'}))
00069
00070 del os.environ['ROS_NAMESPACE']
00071
00072 sys.argv = ['foo', '__ns:=unresolved_ns']
00073 self.assertEquals('/unresolved_ns/', roslib.names.get_ros_namespace())
00074 self.assertEquals('/unresolved_ns2/', roslib.names.get_ros_namespace(argv=['foo', '__ns:=unresolved_ns2']))
00075 sys.argv = ['foo', '__ns:=/resolved_ns/']
00076 self.assertEquals('/resolved_ns/', roslib.names.get_ros_namespace())
00077 self.assertEquals('/resolved_ns2/', roslib.names.get_ros_namespace(argv=['foo', '__ns:=resolved_ns2']))
00078 finally:
00079 sys.argv = sysargv
00080
00081
00082 if rosns:
00083 os.environ['ROS_NAMESPACE'] = rosns
00084
00085 def test_make_global_ns(self):
00086 from roslib.names import make_global_ns
00087
00088 for n in ['~foo']:
00089 try:
00090 make_global_ns(n)
00091 self.fail("make_global_ns should fail on %s"%n)
00092 except ValueError: pass
00093
00094 self.assertEquals('/foo/', make_global_ns('foo'))
00095 self.assertEquals('/', make_global_ns(''))
00096 self.assertEquals('/foo/', make_global_ns('/foo'))
00097 self.assertEquals('/foo/', make_global_ns('/foo/'))
00098 self.assertEquals('/foo/bar/', make_global_ns('/foo/bar'))
00099 self.assertEquals('/foo/bar/', make_global_ns('/foo/bar/'))
00100
00101 def test_make_caller_id(self):
00102 from roslib.names import make_caller_id
00103 if 'ROS_NAMESPACE' is os.environ:
00104 rosns = os.environ['ROS_NAMESPACE']
00105 del os.environ['ROS_NAMESPACE']
00106 else:
00107 rosns = None
00108
00109 for n in ['~name']:
00110 try:
00111 make_caller_id('~name')
00112 self.fail("make_caller_id should fail on %s"%n)
00113 except ValueError: pass
00114
00115 self.assertEquals('/node/', make_caller_id('node'))
00116 self.assertEquals('/bar/node/', make_caller_id('bar/node'))
00117 self.assertEquals('/bar/node/', make_caller_id('/bar/node'))
00118
00119 os.environ['ROS_NAMESPACE'] = '/test/'
00120 self.assertEquals('/test/node/', make_caller_id('node'))
00121 self.assertEquals('/test/bar/node/', make_caller_id('bar/node'))
00122 self.assertEquals('/bar/node/', make_caller_id('/bar/node'))
00123
00124
00125 if rosns:
00126 os.environ['ROS_NAMESPACE'] = rosns
00127
00128
00129 def test_is_global(self):
00130 try:
00131 roslib.names.is_global(None)
00132 self.fail("is_global should raise exception on invalid param")
00133 except: pass
00134 tests = ['/', '/global', '/global2']
00135 for t in tests:
00136 self.assert_(roslib.names.is_global(t))
00137 fails = ['', 'not_global', 'not/global']
00138 for t in fails:
00139 self.failIf(roslib.names.is_global(t))
00140
00141 def test_is_private(self):
00142 try:
00143 roslib.names.is_private(None)
00144 self.fail("is_private should raise exception on invalid param")
00145 except: pass
00146 tests = ['~name', '~name/sub']
00147 for t in tests:
00148 self.assert_(roslib.names.is_private(t))
00149 fails = ['', 'not_private', 'not/private', 'not/~private', '/not/~private']
00150 for t in fails:
00151 self.failIf(roslib.names.is_private(t))
00152
00153 def test_namespace(self):
00154 from roslib.names import namespace
00155 try:
00156 namespace(1)
00157 self.fail("1")
00158 except TypeError: pass
00159 try:
00160 namespace(None)
00161 self.fail("None")
00162 except ValueError: pass
00163 self.assertEquals('/', namespace(''))
00164 self.assertEquals('/', namespace('/'))
00165 self.assertEquals('/', namespace('/foo'))
00166 self.assertEquals('/', namespace('/foo/'))
00167 self.assertEquals('/foo/', namespace('/foo/bar'))
00168 self.assertEquals('/foo/', namespace('/foo/bar/'))
00169 self.assertEquals('/foo/bar/', namespace('/foo/bar/baz'))
00170 self.assertEquals('/foo/bar/', namespace('/foo/bar/baz/'))
00171
00172
00173 self.assertEquals(u'/', namespace(u''))
00174 self.assertEquals(u'/', namespace(u'/'))
00175 self.assertEquals(u'/foo/bar/', namespace(u'/foo/bar/baz/'))
00176
00177 def test_nsjoin(self):
00178 from roslib.names import ns_join
00179
00180
00181 self.assertEquals('~name', ns_join('/foo', '~name'))
00182 self.assertEquals('/name', ns_join('/foo', '/name'))
00183 self.assertEquals('~name', ns_join('~', '~name'))
00184 self.assertEquals('/name', ns_join('/', '/name'))
00185
00186
00187 self.assertEquals('~name', ns_join('~', 'name'))
00188 self.assertEquals('/name', ns_join('/', 'name'))
00189
00190 self.assertEquals('/ns/name', ns_join('/ns', 'name'))
00191 self.assertEquals('/ns/name', ns_join('/ns/', 'name'))
00192 self.assertEquals('/ns/ns2/name', ns_join('/ns', 'ns2/name'))
00193 self.assertEquals('/ns/ns2/name', ns_join('/ns/', 'ns2/name'))
00194
00195
00196 self.assertEquals('name', ns_join('', 'name'))
00197
00198
00199 def test_load_mappings(self):
00200 from roslib.names import load_mappings
00201 self.assertEquals({}, load_mappings([]))
00202 self.assertEquals({}, load_mappings(['foo']))
00203 self.assertEquals({}, load_mappings([':=']))
00204 self.assertEquals({}, load_mappings([':=:=']))
00205 self.assertEquals({}, load_mappings(['f:=']))
00206 self.assertEquals({}, load_mappings([':=b']))
00207 self.assertEquals({}, load_mappings(['foo:=bar:=baz']))
00208
00209 self.assertEquals({}, load_mappings(['_foo:=bar']))
00210
00211 self.assertEquals({'foo': 'bar'}, load_mappings(['foo:=bar']))
00212
00213 self.assertEquals({'__foo': 'bar'}, load_mappings(['__foo:=bar']))
00214 self.assertEquals({'foo': 'bar'}, load_mappings(['./f', '-x', '--blah', 'foo:=bar']))
00215 self.assertEquals({'a': '1', 'b': '2', 'c': '3'}, load_mappings(['c:=3', 'c:=', ':=3', 'a:=1', 'b:=2']))
00216
00217 def test_resource_name(self):
00218 from roslib.names import resource_name
00219 self.assertEquals('foo/bar', resource_name('foo', 'bar'))
00220 self.assertEquals('bar', resource_name('foo', 'bar', my_pkg='foo'))
00221 self.assertEquals('foo/bar', resource_name('foo', 'bar', my_pkg='bar'))
00222 self.assertEquals('foo/bar', resource_name('foo', 'bar', my_pkg=''))
00223 self.assertEquals('foo/bar', resource_name('foo', 'bar', my_pkg=None))
00224
00225 def test_resource_name_base(self):
00226 from roslib.names import resource_name_base
00227 self.assertEquals('', resource_name_base(''))
00228 self.assertEquals('bar', resource_name_base('bar'))
00229 self.assertEquals('bar', resource_name_base('foo/bar'))
00230 self.assertEquals('bar', resource_name_base('/bar'))
00231 self.assertEquals('', resource_name_base('foo/'))
00232
00233 def test_resource_name_package(self):
00234 from roslib.names import resource_name_package
00235 self.assertEquals(None, resource_name_package(''))
00236 self.assertEquals(None, resource_name_package('foo'))
00237 self.assertEquals('foo', resource_name_package('foo/'))
00238 self.assertEquals('foo', resource_name_package('foo/bar'))
00239
00240 def test_package_resource_name(self):
00241 from roslib.names import package_resource_name
00242 self.assertEquals(('', ''), package_resource_name(''))
00243 self.assertEquals(('', 'foo'), package_resource_name('foo'))
00244 self.assertEquals(('foo', 'bar'), package_resource_name('foo/bar'))
00245 self.assertEquals(('foo', ''), package_resource_name('foo/'))
00246 try:
00247
00248 package_resource_name("foo/bar/baz")
00249 self.fail("should have raised ValueError")
00250 except ValueError:
00251 pass
00252
00253
00254 def test_is_legal_resource_name(self):
00255 from roslib.names import is_legal_resource_name
00256 failures = [None, '', 'hello\n', '\t', 'foo++', 'foo-bar', '#foo',
00257 ' name', 'name ',
00258 '~name', '/name',
00259 '1name', 'foo\\']
00260 for f in failures:
00261 self.failIf(is_legal_resource_name(f), f)
00262 tests = ['f', 'f1', 'f_', 'foo', 'foo_bar', 'foo/bar', 'roslib/Log']
00263 for t in tests:
00264 self.assert_(is_legal_resource_name(t), t)
00265
00266 def test_is_legal_name(self):
00267 from roslib.names import is_legal_name
00268 failures = [None,
00269 'foo++', 'foo-bar', '#foo',
00270 'hello\n', '\t', ' name', 'name ',
00271 'f//b',
00272 '1name', 'foo\\']
00273 for f in failures:
00274 self.failIf(is_legal_name(f), f)
00275 tests = ['',
00276 'f', 'f1', 'f_', 'f/', 'foo', 'foo_bar', 'foo/bar', 'foo/bar/baz',
00277 '~f', '~a/b/c',
00278 '~/f',
00279 '/a/b/c/d', '/']
00280 for t in tests:
00281 self.assert_(is_legal_name(t), "[%s]"%t)
00282
00283 def test_is_legal_base_name(self):
00284 from roslib.names import is_legal_base_name
00285 failures = [None, '', 'hello\n', '\t', 'foo++', 'foo-bar', '#foo',
00286 'f/', 'foo/bar', '/', '/a',
00287 'f//b',
00288 '~f', '~a/b/c',
00289 ' name', 'name ',
00290 '1name', 'foo\\']
00291 for f in failures:
00292 self.failIf(is_legal_base_name(f), f)
00293 tests = ['f', 'f1', 'f_', 'foo', 'foo_bar']
00294 for t in tests:
00295 self.assert_(is_legal_base_name(t), "[%s]"%t)
00296
00297 def test_is_legal_resource_base_name(self):
00298 from roslib.names import is_legal_resource_base_name
00299 failures = [None, '', 'hello\n', '\t', 'foo++', 'foo-bar', '#foo',
00300 'f/', 'foo/bar', '/', '/a',
00301 'f//b',
00302 '~f', '~a/b/c',
00303 '~/f',
00304 ' name', 'name ',
00305 '1name', 'foo\\']
00306 for f in failures:
00307 self.failIf(is_legal_resource_base_name(f), f)
00308 tests = ['f', 'f1', 'f_', 'foo', 'foo_bar']
00309 for t in tests:
00310 self.assert_(is_legal_resource_base_name(t), "[%s]"%t)
00311
00312 def test_resolve_name(self):
00313 from roslib.names import resolve_name
00314
00315 tests = [
00316 ('', '/', '/'),
00317 ('', '/node', '/'),
00318 ('', '/ns1/node', '/ns1/'),
00319
00320 ('foo', '', '/foo'),
00321 ('foo/', '', '/foo'),
00322 ('/foo', '', '/foo'),
00323 ('/foo/', '', '/foo'),
00324 ('/foo', '/', '/foo'),
00325 ('/foo/', '/', '/foo'),
00326 ('/foo', '/bar', '/foo'),
00327 ('/foo/', '/bar', '/foo'),
00328
00329 ('foo', '/ns1/ns2', '/ns1/foo'),
00330 ('foo', '/ns1/ns2/', '/ns1/foo'),
00331 ('foo', '/ns1/ns2/ns3/', '/ns1/ns2/foo'),
00332 ('foo/', '/ns1/ns2', '/ns1/foo'),
00333 ('/foo', '/ns1/ns2', '/foo'),
00334 ('foo/bar', '/ns1/ns2', '/ns1/foo/bar'),
00335 ('foo//bar', '/ns1/ns2', '/ns1/foo/bar'),
00336 ('foo/bar', '/ns1/ns2/ns3', '/ns1/ns2/foo/bar'),
00337 ('foo//bar//', '/ns1/ns2/ns3', '/ns1/ns2/foo/bar'),
00338
00339 ('~foo', '/', '/foo'),
00340 ('~foo', '/node', '/node/foo'),
00341 ('~foo', '/ns1/ns2', '/ns1/ns2/foo'),
00342 ('~foo/', '/ns1/ns2', '/ns1/ns2/foo'),
00343 ('~foo/bar', '/ns1/ns2', '/ns1/ns2/foo/bar'),
00344
00345
00346 ('~/foo', '/', '/foo'),
00347 ('~/foo', '/node', '/node/foo'),
00348 ('~/foo', '/ns1/ns2', '/ns1/ns2/foo'),
00349 ('~/foo/', '/ns1/ns2', '/ns1/ns2/foo'),
00350 ('~/foo/bar', '/ns1/ns2', '/ns1/ns2/foo/bar'),
00351
00352 ]
00353 for name, node_name, v in tests:
00354 self.assertEquals(v, resolve_name(name, node_name))
00355
00356 if __name__ == '__main__':
00357 rosunit.unitrun('test_roslib', 'test_names', NamesTest, coverage_packages=['roslib.names'])
00358