test_rospy_names.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2008, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 import os
35 import sys
36 import struct
37 import unittest
38 import time
39 
40 # test rospy.names package
41 class TestRospyNames(unittest.TestCase):
42 
43  def test_scoped_name(self):
44  from rospy.exceptions import ROSException
45  from rospy.names import scoped_name
46  tests = [
47  ['/', '/name', 'name'],
48  ['/', '/ns/name', 'ns/name'],
49  ['/ns', '/ns/name', 'ns/name'],
50  ['/ns/node', '/ns/name', 'name'],
51  ['/ns/', '/ns/name', 'ns/name'],
52  ['/ns/ns2/', '/ns/name', 'name'],
53  ]
54  for caller_id, name, v in tests:
55  val = scoped_name(caller_id, name)
56  self.assertEquals(v, val, "failed on [%s] [%s]: %s"%(caller_id, name, val))
57 
58  fail = [
59  ['name', '/name'],
60  ['~name', '/name'],
61  ]
62 
63  for caller_id, name in fail:
64  try:
65  scoped_name(caller_id, name)
66  self.fail("should have failed on %s, %s"%(caller_id, name))
67  except ROSException: pass
68 
69  def test_mappings(self):
70  import roslib.names
71  import rospy.names
72  from rospy.names import get_mappings, get_resolved_mappings, initialize_mappings
73  # get_mappings is initialized statically, so can't test anything other than it is empty
74  self.assertEquals({}, get_mappings())
75 
76  # resolved mappings should be empty with no initialization
77  self.assertEquals({}, get_resolved_mappings())
78 
79  # now, initialize mappings, shouldn't matter as there are no mappings
80  initialize_mappings('foo')
81  # should be empty now
82  self.assertEquals({}, get_resolved_mappings())
83 
84  # manipulate mappings to test
85  rospy.names._mappings = roslib.names.load_mappings(['__name:=newname', '__log:=blah', '_param:=value', 'foo:=bar','/baz:=a/b', '~car:=c/d/e'])
86  # - param mapping should be removed
87  self.assertEquals({'__name': 'newname', '__log': 'blah',
88  'foo': 'bar', '/baz': 'a/b', '~car': 'c/d/e'}, get_mappings())
89  # - should be unaltered
90  self.assertEquals({}, get_resolved_mappings())
91  initialize_mappings('/name')
92 
93  # should be unchanged
94  self.assertEquals({'__name': 'newname', '__log': 'blah',
95  'foo': 'bar', '/baz': 'a/b', '~car': 'c/d/e'}, get_mappings())
96  # should be remapped
97  self.assertEquals({'__name': 'newname', '__log': 'blah',
98  '/foo': '/bar', '/baz': '/a/b', '/name/car':'/c/d/e'},
99  get_resolved_mappings())
100 
101  # try with namespaced node
102  initialize_mappings('/ns/name')
103  # should be remapped
104  self.assertEquals({'__name': 'newname', '__log': 'blah',
105  '/ns/foo': '/ns/bar', '/baz': '/ns/a/b', '/ns/name/car':'/ns/c/d/e'},
106  get_resolved_mappings())
107 
108 
109 
111  from rospy.names import canonicalize_name
112  tests = [
113  ('', ''),
114  ('/', '/'),
115  ('foo', 'foo'),
116  ('/foo', '/foo'),
117  ('/foo/', '/foo'),
118  ('/foo/bar', '/foo/bar'),
119  ('/foo/bar/', '/foo/bar'),
120  ('/foo/bar//', '/foo/bar'),
121  ('/foo//bar', '/foo/bar'),
122  ('//foo/bar', '/foo/bar'),
123  ('foo/bar', 'foo/bar'),
124  ('foo//bar', 'foo/bar'),
125  ('foo/bar/', 'foo/bar'),
126  ('/foo/bar', '/foo/bar'),
127  ]
128  for t, v in tests:
129  self.assertEquals(v, canonicalize_name(t))
130 
131  def test_ANYTYPE(self):
132  from rospy.names import TOPIC_ANYTYPE, SERVICE_ANYTYPE
133  self.assertEquals("*", TOPIC_ANYTYPE)
134  self.assertEquals("*", SERVICE_ANYTYPE)
135 
136  def test_resolve_name(self):
137  from rospy.names import resolve_name
138  # TODO: test with remappings
139  tests = [
140  ('', '/', '/'),
141  ('', None, '/'), #node_name defaults to /
142  ('', '/node', '/'),
143  ('', '/ns1/node', '/ns1/'),
144 
145  ('foo', '', '/foo'),
146  ('foo', None, '/foo'),
147  ('foo/', '', '/foo'),
148  ('/foo', '', '/foo'),
149  ('/foo/', '', '/foo'),
150  ('/foo', '/', '/foo'),
151  ('/foo', None, '/foo'),
152  ('/foo/', '/', '/foo'),
153  ('/foo', '/bar', '/foo'),
154  ('/foo/', '/bar', '/foo'),
155 
156  ('foo', '/ns1/ns2', '/ns1/foo'),
157  ('foo', '/ns1/ns2/', '/ns1/foo'),
158  ('foo', '/ns1/ns2/ns3/', '/ns1/ns2/foo'),
159  ('foo/', '/ns1/ns2', '/ns1/foo'),
160  ('/foo', '/ns1/ns2', '/foo'),
161  ('foo/bar', '/ns1/ns2', '/ns1/foo/bar'),
162  ('foo//bar', '/ns1/ns2', '/ns1/foo/bar'),
163  ('foo/bar', '/ns1/ns2/ns3', '/ns1/ns2/foo/bar'),
164  ('foo//bar//', '/ns1/ns2/ns3', '/ns1/ns2/foo/bar'),
165 
166  ('~foo', '/', '/foo'),
167  ('~foo', '/node', '/node/foo'),
168  ('~foo', '/ns1/ns2', '/ns1/ns2/foo'),
169  ('~foo/', '/ns1/ns2', '/ns1/ns2/foo'),
170  ('~foo/bar', '/ns1/ns2', '/ns1/ns2/foo/bar'),
171 
172  ]
173  for name, node_name, v in tests:
174  self.assertEquals(v, resolve_name(name, node_name))
175 
176  def test_valid_name(self):
177  # test with resolution
178  from rospy.names import valid_name_validator_resolved, valid_name, ParameterInvalid
179  validator = valid_name('param_name', True)
180  tests = [
181  ('name', '/node', '/name'),
182  ('/name', '/node', '/name'),
183  ('~name', '/node', '/node/name'),
184  # test unicode
185  (u'~name', '/node', u'/node/name'),
186  ]
187  for name, caller_id, v in tests:
188  self.assertEquals(v, valid_name_validator_resolved('p', name, caller_id), "failed on %s %s"%(name, caller_id))
189  self.assertEquals(v, validator(name, caller_id))
190 
191  # kwc: valid_name is currently very soft in the failures it
192  # checks as it is targetted at catching parameter
193  # misalignment. I would like to make it more strict in the
194  # future.
195  invalid = [
196  (1, '/node'),
197  (None, '/node'),
198  ('localhost:123', '/node'),
199  ('Bob Barker', '/node'),
200  # unicode
201  (u'Bob Barker', '/node'),
202  ]
203  for name, caller_id in invalid:
204  try:
205  valid_name_validator_resolved('p', name, caller_id)
206  self.fail("valid_name_validator_unresolved should have failed on : [%s], [%s]"%(name, caller_id))
207  except ParameterInvalid: pass
208  try:
209  validator(name, caller_id)
210  self.fail("valid_name_validator_unresolved should have failed on : [%s], [%s]"%(name, caller_id))
211  except ParameterInvalid: pass
212 
213  from rospy.names import valid_name_validator_unresolved
214  validator = valid_name('param_name', False)
215  tests = [
216  ('name', '/node', 'name'),
217  ('/name', '/node', '/name'),
218  ('~name', '/node', '~name'),
219  # unicode
220  (u'~name', '/node', u'~name'),
221  ]
222  for name, caller_id, v in tests:
223  self.assertEquals(v, valid_name_validator_unresolved('p', name, caller_id), "failed on [%s] [%s]"%(name, caller_id))
224  self.assertEquals(v, validator(name, caller_id))
225 
226  for name, caller_id in invalid:
227  try:
228  valid_name_validator_unresolved('p', name, caller_id)
229  self.fail("valid_name_validator_unresolved should have failed on : [%s], [%s]"%(name, caller_id))
230  except ParameterInvalid: pass
231  try:
232  validator(name, caller_id)
233  self.fail("valid_name_validator_unresolved should have failed on : [%s], [%s]"%(name, caller_id))
234  except ParameterInvalid: pass
235 
236  def test_global_name(self):
237  from rospy.names import global_name, ParameterInvalid
238  validator = global_name('param_name')
239  tests = [
240  ('/', '/node', '/'),
241  ('/name', '/node', '/name'),
242  # unicode
243  (u'/name', '/node', u'/name'),
244  ]
245  for name, caller_id, v in tests:
246  self.assertEquals(v, validator(name, caller_id))
247  invalid = [
248  (1, '/node'),
249  (None, '/node'),
250  ('name', '/node'),
251  ('~name', '/node'),
252  ]
253  for name, caller_id in invalid:
254  try:
255  validator(name, caller_id)
256  self.fail("global_name should have failed on : [%s], [%s]"%(name, caller_id))
257  except ParameterInvalid: pass
258 
259  def test_caller_id(self):
260  from rospy.names import get_caller_id, get_name, _set_caller_id, get_namespace
261  # test get_name, get_caller_id, and _set_caller_id
262  try:
263  self.assertEquals('/unnamed', get_name())
264  self.assertEquals('/', get_namespace())
265  _set_caller_id('/foo')
266  self.assertEquals('/foo', get_name())
267  self.assertEquals('/', get_namespace())
268  _set_caller_id('/foo/bar')
269  self.assertEquals('/foo/bar', get_name())
270  self.assertEquals('/foo/', get_namespace())
271  finally:
272  _set_caller_id('/unnamed')
273 
274  # older get_caller_id usage
275  try:
276  self.assertEquals('/unnamed', get_caller_id())
277  self.assertEquals('/', get_namespace())
278  _set_caller_id('/foo')
279  self.assertEquals('/foo', get_caller_id())
280  self.assertEquals('/', get_namespace())
281  _set_caller_id('/foo/bar')
282  self.assertEquals('/foo/bar', get_caller_id())
283  self.assertEquals('/foo/', get_namespace())
284  finally:
285  _set_caller_id('/unnamed')


test_rospy
Author(s): Ken Conley
autogenerated on Sun Feb 3 2019 03:30:22