40 from cStringIO
import StringIO
42 from io
import StringIO
43 from subprocess
import Popen, PIPE, check_call, call
49 return rosgraph.Master(
'/test_rosparam')
51 from contextlib
import contextmanager
54 realstdout = sys.stdout
55 fakestdout = StringIO()
56 sys.stdout = fakestdout
58 sys.stdout = realstdout
63 fakestdin = StringIO(input_str)
69 return [x.strip()
for x
in b.getvalue().split(
'\n')
if x.strip()]
72 return os.path.abspath(os.path.join(os.path.dirname(__file__)))
81 Make sure all elements of expected are present in actual 84 self.assert_(t
in actual)
87 Make sure all elements of not_expected are not present in actual 89 for t
in not_expected:
90 self.failIf(t
in actual)
95 params = [
'/string',
'/int',
'/float',
96 '/g1/string',
'/g1/int',
'/g1/float',
97 '/g2/string',
'/g2/int',
'/g2/float',
99 l = rosparam.list_params(
'')
104 rosparam.yamlmain([cmd,
'list'])
107 rosparam.yamlmain([cmd,
'list',
'/'])
111 g1p = [p
for p
in params
if p.startswith(
'/g1/')]
112 not_g1p = [p
for p
in params
if not p.startswith(
'/g1/')]
114 rosparam.yamlmain([cmd,
'list',
'/g1'])
118 rosparam.yamlmain([cmd,
'list',
'/g1/'])
123 rosparam.yamlmain([cmd,
'list',
'/not/a/namespace/'])
124 self.assertEquals([],
tolist(b))
132 rosparam.yamlmain([cmd,
'load'])
133 self.fail(
"command-line arg should have failed")
134 except SystemExit
as e:
135 self.assertNotEquals(0, e.code)
137 rosparam.yamlmain([cmd,
'load',
'fake-file.yaml'])
138 self.fail(
"command-line arg should have failed")
139 except SystemExit
as e:
140 self.assertNotEquals(0, e.code)
145 rosparam.yamlmain([cmd,
'load', f])
146 self.assertEquals(
'bar', ps.getParam(
'/foo'))
148 self.assertEquals(
'foo-value', ps.getParam(
'/string'))
150 rosparam.yamlmain([cmd,
'load',
'-v', f])
151 self.assertEquals(
'bar', ps.getParam(
'/foo'))
154 with
fakestdin(
'stdin_string: stdin_foo\nstdin_string2: stdin_bar'):
155 rosparam.yamlmain([cmd,
'load',
'-'])
156 self.assertEquals(
'stdin_foo', ps.getParam(
'/stdin_string'))
157 self.assertEquals(
'stdin_bar', ps.getParam(
'/stdin_string2'))
160 rosparam.yamlmain([cmd,
'load', f,
'/rosparam_load/test'])
161 self.assertEquals(
'bar', ps.getParam(
'/rosparam_load/test/foo'))
162 rosparam.yamlmain([cmd,
'load',
'-v', f,
'/rosparam_load/test'])
163 self.assertEquals(
'bar', ps.getParam(
'/rosparam_load/test/foo'))
167 rosparam.yamlmain([cmd,
'load', f_ns])
168 self.assertEquals(
'baz', ps.getParam(
'/a/b/foo'))
169 self.assertEquals(
'bar', ps.getParam(
'/foo'))
170 rosparam.yamlmain([cmd,
'load',
'-v', f_ns])
171 self.assertEquals(
'baz', ps.getParam(
'/a/b/foo'))
174 rosparam.yamlmain([cmd,
'load', f_ns,
'/rosparam_load/test2'])
175 self.assertEquals(
'baz', ps.getParam(
'/rosparam_load/test2/a/b/foo'))
176 rosparam.yamlmain([cmd,
'load',
'-v', f_ns,
'/rosparam_load/test2'])
177 self.assertEquals(
'baz', ps.getParam(
'/rosparam_load/test2/a/b/foo'))
183 rosparam.yamlmain([cmd,
'get'])
184 self.fail(
"command-line arg should have failed")
185 except SystemExit
as e:
186 self.assertNotEquals(0, e.code)
189 rosparam.yamlmain([cmd,
'get',
"string"])
190 self.assertEquals(
'foo-value', b.getvalue().strip())
192 rosparam.yamlmain([cmd,
'get',
'-p',
"string"])
193 self.assertEquals(
'foo-value', b.getvalue().strip())
195 rosparam.yamlmain([cmd,
'get',
"/string"])
196 self.assertEquals(
'foo-value', b.getvalue().strip())
198 rosparam.yamlmain([cmd,
'get',
"/g1/string"])
199 self.assertEquals(
'g1-foo-value', b.getvalue().strip())
201 rosparam.yamlmain([cmd,
'get',
"g1/string"])
202 self.assertEquals(
'g1-foo-value', b.getvalue().strip())
204 rosparam.yamlmain([cmd,
'get',
"int"])
205 self.assertEquals(
'1', b.getvalue().strip())
207 rosparam.yamlmain([cmd,
'get',
"/int"])
208 self.assertEquals(
'1', b.getvalue().strip())
210 rosparam.yamlmain([cmd,
'get',
'-p',
"int"])
211 self.assertEquals(
'1', b.getvalue().strip())
213 rosparam.yamlmain([cmd,
'get',
"/g1/int"])
214 self.assertEquals(
'10', b.getvalue().strip())
216 rosparam.yamlmain([cmd,
'get',
"g1/int"])
217 self.assertEquals(
'10', b.getvalue().strip())
219 rosparam.yamlmain([cmd,
'get',
"float"])
220 self.assertEquals(
'1.0', b.getvalue().strip())
222 rosparam.yamlmain([cmd,
'get',
'-p',
"float"])
223 self.assertEquals(
'1.0', b.getvalue().strip())
225 rosparam.yamlmain([cmd,
'get',
'-p',
"g1/float"])
226 self.assertEquals(
'10.0', b.getvalue().strip())
228 rosparam.yamlmain([cmd,
'get',
"g1"])
230 d = yaml.load(b.getvalue())
231 self.assertEquals(d[
'float'], 10.0)
232 self.assertEquals(d[
'int'], 10.0)
233 self.assertEquals(d[
'string'],
"g1-foo-value")
234 self.assertEquals(set([
'float',
'int',
'string']), set(d.keys()))
236 rosparam.yamlmain([cmd,
'get',
'-p',
"g1"])
238 rosparam.yamlmain([cmd,
'get',
'-pv',
"g1"])
246 rosparam.yamlmain([cmd,
'set',
"/rosparam_set/test1",
"1"])
247 self.assertEquals(1, ps.getParam(
'/rosparam_set/test1'))
250 rosparam.yamlmain([cmd,
'set',
'-v',
"/rosparam_set/test1",
"1"])
251 self.assertEquals(1, ps.getParam(
'/rosparam_set/test1'))
253 rosparam.yamlmain([cmd,
'set',
"rosparam_set/test1",
"2"])
254 self.assertEquals(2, ps.getParam(
'/rosparam_set/test1'))
258 rosparam.yamlmain([cmd,
'set',
"/rosparam_set/test2",
"1.0"])
259 self.assertEquals(1., ps.getParam(
'/rosparam_set/test2'))
262 rosparam.yamlmain([cmd,
'set',
"/rosparam_set/test2",
"2.0"])
263 self.assertEquals(2., ps.getParam(
'/rosparam_set/test2'))
266 rosparam.yamlmain([cmd,
'set',
"/rosparam_set/testbool",
"true"])
267 self.assertEquals(
True, ps.getParam(
'/rosparam_set/testbool'))
270 rosparam.yamlmain([cmd,
'set',
"/rosparam_set/teststr",
"hi"])
271 self.assertEquals(
"hi", ps.getParam(
'/rosparam_set/teststr'))
274 rosparam.yamlmain([cmd,
'set',
"/rosparam_set/testlist",
"[1, 2, 3]"])
275 self.assertEquals([1, 2, 3], ps.getParam(
'/rosparam_set/testlist'))
278 rosparam.yamlmain([cmd,
'set',
"/rosparam_set/testdict",
"{a: b, c: d}"])
279 self.assertEquals(
'b', ps.getParam(
'/rosparam_set/testdict/a'))
280 self.assertEquals(
'd', ps.getParam(
'/rosparam_set/testdict/c'))
283 rosparam.yamlmain([cmd,
'set',
"set/testdict",
"{}"])
284 self.assertEquals(
'b', ps.getParam(
'/rosparam_set/testdict/a'))
285 self.assertEquals(
'd', ps.getParam(
'/rosparam_set/testdict/c'))
288 rosparam.yamlmain([cmd,
'set',
"/rosparam_set/testdict",
"{e: f, g: h}"])
289 self.assertEquals(
'b', ps.getParam(
'/rosparam_set/testdict/a'))
290 self.assertEquals(
'd', ps.getParam(
'/rosparam_set/testdict/c'))
291 self.assertEquals(
'f', ps.getParam(
'/rosparam_set/testdict/e'))
292 self.assertEquals(
'h', ps.getParam(
'/rosparam_set/testdict/g'))
295 rosparam.yamlmain([cmd,
'set',
'-v',
"/rosparam_set/testdictverbose",
"{e: f, g: h}"])
296 self.assertEquals(
'f', ps.getParam(
'/rosparam_set/testdictverbose/e'))
297 self.assertEquals(
'h', ps.getParam(
'/rosparam_set/testdictverbose/g'))
305 rosparam.yamlmain([cmd,
'delete'])
306 except SystemExit
as e:
307 self.assertNotEquals(0, e.code)
309 rosparam.yamlmain([cmd,
'delete',
'one',
'two'])
310 except SystemExit
as e:
311 self.assertNotEquals(0, e.code)
314 ps.setParam(
'/delete/me',
True)
315 self.assert_(ps.hasParam(
'/delete/me'))
316 rosparam.yamlmain([cmd,
'delete',
"/delete/me"])
317 self.failIf(ps.hasParam(
'/delete/me'))
319 ps.setParam(
'/delete/me2',
True)
320 self.assert_(ps.hasParam(
'/delete/me2'))
321 rosparam.yamlmain([cmd,
'delete',
'-v',
"/delete/me2"])
322 self.failIf(ps.hasParam(
'/delete/me2'))
333 rosparam.yamlmain([cmd,
'dump'])
334 except SystemExit
as e:
335 self.assertNotEquals(0, e.code)
337 rosparam.yamlmain([cmd,
'dump', f_out,
'rosparam_dump',
'rosparam_dump2'])
338 except SystemExit
as e:
339 self.assertNotEquals(0, e.code)
341 rosparam.yamlmain([cmd,
'load', f,
'rosparam_dump'])
342 self.assertEquals(
'bar', ps.getParam(
'rosparam_dump/foo'))
344 rosparam.yamlmain([cmd,
'dump', f_out,
'rosparam_dump'])
347 with open(f_out)
as b:
349 self.assertEquals(yaml.load(b.read()), yaml.load(b2.read()))
351 rosparam.yamlmain([cmd,
'dump',
'-v', f_out,
'rosparam_dump'])
352 with open(f_out)
as b:
354 self.assertEquals(yaml.load(b.read()), yaml.load(b2.read()))
358 rosparam.yamlmain([cmd,
'dump'])
360 self.assertEquals(yaml.load(b.getvalue())[
'rosparam_dump'], yaml.load(b2.read()))
365 rosparam._fullusage()
366 except SystemExit:
pass 368 rosparam.yamlmain([
'rosparam'])
369 except SystemExit:
pass 371 rosparam.yamlmain([
'rosparam',
'invalid'])
372 except SystemExit:
pass 374 PKG =
'test_rosparam' 375 NAME =
'test_rosparam_command_line_online' 376 if __name__ ==
'__main__':
377 rostest.unitrun(PKG, NAME, TestRosparam, sys.argv, coverage_packages=[
'rosparam'])
def _check(self, expected, actual)
def test_rosparam_dump(self)
def test_rosparam_set(self)
def _notcheck(self, not_expected, actual)
def test_rosparam_load(self)
def test_rosparam_delete(self)
def test_rosparam_get(self)
def test_rosparam_list(self)