This commit is contained in:
Ivan
2022-04-05 11:42:28 +03:00
commit 6dc0eb0fcf
5565 changed files with 1200500 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 2.8.3)
project(test_rosparam)
find_package(catkin REQUIRED COMPONENTS rostest)
catkin_package()
if(CATKIN_ENABLE_TESTING)
add_rostest(test/rosparam.test)
catkin_add_nosetests(test)
endif()

View File

@@ -0,0 +1,24 @@
<package>
<name>test_rosparam</name>
<version>1.12.14</version>
<description>
A package containing the unit tests for rosparam.
</description>
<maintainer email="dthomas@osrfoundation.org">Dirk Thomas</maintainer>
<license>BSD</license>
<url>http://ros.org/wiki/rosparam</url>
<author>Ken Conley</author>
<buildtool_depend version_gte="0.5.68">catkin</buildtool_depend>
<build_depend>rostest</build_depend>
<test_depend>python-yaml</test_depend>
<test_depend>rosgraph</test_depend>
<test_depend>rosparam</test_depend>
<export>
<rosdoc config="rosdoc.yaml"/>
</export>
</package>

View File

@@ -0,0 +1,377 @@
#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2009, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Willow Garage, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import os
import sys
import unittest
import rostest
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO
from subprocess import Popen, PIPE, check_call, call
import rosgraph
import rosparam
def get_param_server():
return rosgraph.Master('/test_rosparam')
from contextlib import contextmanager
@contextmanager
def fakestdout():
realstdout = sys.stdout
fakestdout = StringIO()
sys.stdout = fakestdout
yield fakestdout
sys.stdout = realstdout
@contextmanager
def fakestdin(input_str):
realstdin = sys.stdin
fakestdin = StringIO(input_str)
sys.stdin = fakestdin
yield fakestdin
sys.stdin = realstdin
def tolist(b):
return [x.strip() for x in b.getvalue().split('\n') if x.strip()]
def get_test_path():
return os.path.abspath(os.path.join(os.path.dirname(__file__)))
class TestRosparam(unittest.TestCase):
def setUp(self):
pass
def _check(self, expected, actual):
"""
Make sure all elements of expected are present in actual
"""
for t in expected:
self.assert_(t in actual)
def _notcheck(self, not_expected, actual):
"""
Make sure all elements of not_expected are not present in actual
"""
for t in not_expected:
self.failIf(t in actual)
def test_rosparam_list(self):
cmd = 'rosparam'
params = ['/string', '/int', '/float',
'/g1/string', '/g1/int', '/g1/float',
'/g2/string', '/g2/int', '/g2/float',
]
l = rosparam.list_params('')
for t in params:
self.assert_(t in l)
with fakestdout() as b:
rosparam.yamlmain([cmd, 'list'])
self._check(params, tolist(b))
with fakestdout() as b:
rosparam.yamlmain([cmd, 'list', '/'])
self._check(params, tolist(b))
# test with namespace
g1p = [p for p in params if p.startswith('/g1/')]
not_g1p = [p for p in params if not p.startswith('/g1/')]
with fakestdout() as b:
rosparam.yamlmain([cmd, 'list', '/g1'])
self._check(g1p, tolist(b))
self._notcheck(not_g1p, tolist(b))
with fakestdout() as b:
rosparam.yamlmain([cmd, 'list', '/g1/'])
self._check(g1p, tolist(b))
self._notcheck(not_g1p, tolist(b))
# test with no match
with fakestdout() as b:
rosparam.yamlmain([cmd, 'list', '/not/a/namespace/'])
self.assertEquals([], tolist(b))
def test_rosparam_load(self):
f = os.path.join(get_test_path(), 'test.yaml')
f_ns = os.path.join(get_test_path(), 'test_ns.yaml')
cmd = 'rosparam'
try:
rosparam.yamlmain([cmd, 'load'])
self.fail("command-line arg should have failed")
except SystemExit as e:
self.assertNotEquals(0, e.code)
try:
rosparam.yamlmain([cmd, 'load', 'fake-file.yaml'])
self.fail("command-line arg should have failed")
except SystemExit as e:
self.assertNotEquals(0, e.code)
ps = get_param_server()
# load into top-level
rosparam.yamlmain([cmd, 'load', f])
self.assertEquals('bar', ps.getParam('/foo'))
# - make sure it did an overlay, not erase
self.assertEquals('foo-value', ps.getParam('/string'))
rosparam.yamlmain([cmd, 'load', '-v', f])
self.assertEquals('bar', ps.getParam('/foo'))
# load into top-level from stdin
with fakestdin('stdin_string: stdin_foo\nstdin_string2: stdin_bar'):
rosparam.yamlmain([cmd, 'load', '-'])
self.assertEquals('stdin_foo', ps.getParam('/stdin_string'))
self.assertEquals('stdin_bar', ps.getParam('/stdin_string2'))
# load into namespace
rosparam.yamlmain([cmd, 'load', f, '/rosparam_load/test'])
self.assertEquals('bar', ps.getParam('/rosparam_load/test/foo'))
rosparam.yamlmain([cmd, 'load', '-v', f, '/rosparam_load/test'])
self.assertEquals('bar', ps.getParam('/rosparam_load/test/foo'))
# load file with namespace spec in it
# - load into top-level
rosparam.yamlmain([cmd, 'load', f_ns])
self.assertEquals('baz', ps.getParam('/a/b/foo'))
self.assertEquals('bar', ps.getParam('/foo'))
rosparam.yamlmain([cmd, 'load', '-v', f_ns])
self.assertEquals('baz', ps.getParam('/a/b/foo'))
# load into namespace
rosparam.yamlmain([cmd, 'load', f_ns, '/rosparam_load/test2'])
self.assertEquals('baz', ps.getParam('/rosparam_load/test2/a/b/foo'))
rosparam.yamlmain([cmd, 'load', '-v', f_ns, '/rosparam_load/test2'])
self.assertEquals('baz', ps.getParam('/rosparam_load/test2/a/b/foo'))
def test_rosparam_get(self):
import rosparam
cmd = 'rosparam'
try:
rosparam.yamlmain([cmd, 'get'])
self.fail("command-line arg should have failed")
except SystemExit as e:
self.assertNotEquals(0, e.code)
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', "string"])
self.assertEquals('foo-value', b.getvalue().strip())
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', '-p', "string"])
self.assertEquals('foo-value', b.getvalue().strip())
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', "/string"])
self.assertEquals('foo-value', b.getvalue().strip())
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', "/g1/string"])
self.assertEquals('g1-foo-value', b.getvalue().strip())
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', "g1/string"])
self.assertEquals('g1-foo-value', b.getvalue().strip())
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', "int"])
self.assertEquals('1', b.getvalue().strip())
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', "/int"])
self.assertEquals('1', b.getvalue().strip())
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', '-p', "int"])
self.assertEquals('1', b.getvalue().strip())
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', "/g1/int"])
self.assertEquals('10', b.getvalue().strip())
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', "g1/int"])
self.assertEquals('10', b.getvalue().strip())
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', "float"])
self.assertEquals('1.0', b.getvalue().strip())
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', '-p', "float"])
self.assertEquals('1.0', b.getvalue().strip())
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', '-p', "g1/float"])
self.assertEquals('10.0', b.getvalue().strip())
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', "g1"])
import yaml
d = yaml.load(b.getvalue())
self.assertEquals(d['float'], 10.0)
self.assertEquals(d['int'], 10.0)
self.assertEquals(d['string'], "g1-foo-value")
self.assertEquals(set(['float', 'int', 'string']), set(d.keys()))
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', '-p', "g1"])
with fakestdout() as b:
rosparam.yamlmain([cmd, 'get', '-pv', "g1"])
def test_rosparam_set(self):
import rosparam
cmd = 'rosparam'
ps = get_param_server()
with fakestdout() as b:
rosparam.yamlmain([cmd, 'set', "/rosparam_set/test1", "1"])
self.assertEquals(1, ps.getParam('/rosparam_set/test1'))
with fakestdout() as b:
# -- verbose
rosparam.yamlmain([cmd, 'set', '-v', "/rosparam_set/test1", "1"])
self.assertEquals(1, ps.getParam('/rosparam_set/test1'))
with fakestdout() as b:
rosparam.yamlmain([cmd, 'set', "rosparam_set/test1", "2"])
self.assertEquals(2, ps.getParam('/rosparam_set/test1'))
with fakestdout() as b:
# - floats
rosparam.yamlmain([cmd, 'set', "/rosparam_set/test2", "1.0"])
self.assertEquals(1., ps.getParam('/rosparam_set/test2'))
with fakestdout() as b:
# - floats
rosparam.yamlmain([cmd, 'set', "/rosparam_set/test2", "2.0"])
self.assertEquals(2., ps.getParam('/rosparam_set/test2'))
with fakestdout() as b:
# - booleans
rosparam.yamlmain([cmd, 'set', "/rosparam_set/testbool", "true"])
self.assertEquals(True, ps.getParam('/rosparam_set/testbool'))
with fakestdout() as b:
# - strings
rosparam.yamlmain([cmd, 'set', "/rosparam_set/teststr", "hi"])
self.assertEquals("hi", ps.getParam('/rosparam_set/teststr'))
with fakestdout() as b:
# - list
rosparam.yamlmain([cmd, 'set', "/rosparam_set/testlist", "[1, 2, 3]"])
self.assertEquals([1, 2, 3], ps.getParam('/rosparam_set/testlist'))
with fakestdout() as b:
# - dictionary
rosparam.yamlmain([cmd, 'set', "/rosparam_set/testdict", "{a: b, c: d}"])
self.assertEquals('b', ps.getParam('/rosparam_set/testdict/a'))
self.assertEquals('d', ps.getParam('/rosparam_set/testdict/c'))
with fakestdout() as b:
# - empty dictionary should be a noop
rosparam.yamlmain([cmd, 'set', "set/testdict", "{}"])
self.assertEquals('b', ps.getParam('/rosparam_set/testdict/a'))
self.assertEquals('d', ps.getParam('/rosparam_set/testdict/c'))
with fakestdout() as b:
# - this should be an update
rosparam.yamlmain([cmd, 'set', "/rosparam_set/testdict", "{e: f, g: h}"])
self.assertEquals('b', ps.getParam('/rosparam_set/testdict/a'))
self.assertEquals('d', ps.getParam('/rosparam_set/testdict/c'))
self.assertEquals('f', ps.getParam('/rosparam_set/testdict/e'))
self.assertEquals('h', ps.getParam('/rosparam_set/testdict/g'))
with fakestdout() as b:
# -- verbose
rosparam.yamlmain([cmd, 'set', '-v', "/rosparam_set/testdictverbose", "{e: f, g: h}"])
self.assertEquals('f', ps.getParam('/rosparam_set/testdictverbose/e'))
self.assertEquals('h', ps.getParam('/rosparam_set/testdictverbose/g'))
def test_rosparam_delete(self):
import rosparam
cmd = 'rosparam'
ps = get_param_server()
try:
rosparam.yamlmain([cmd, 'delete'])
except SystemExit as e:
self.assertNotEquals(0, e.code)
try:
rosparam.yamlmain([cmd, 'delete', 'one', 'two'])
except SystemExit as e:
self.assertNotEquals(0, e.code)
# delete
ps.setParam('/delete/me', True)
self.assert_(ps.hasParam('/delete/me'))
rosparam.yamlmain([cmd, 'delete', "/delete/me"])
self.failIf(ps.hasParam('/delete/me'))
ps.setParam('/delete/me2', True)
self.assert_(ps.hasParam('/delete/me2'))
rosparam.yamlmain([cmd, 'delete', '-v', "/delete/me2"])
self.failIf(ps.hasParam('/delete/me2'))
def test_rosparam_dump(self):
import rosparam
f = os.path.join(get_test_path(), 'test.yaml')
f_out = os.path.join(get_test_path(), 'test_dump.yaml')
cmd = 'rosparam'
ps = get_param_server()
try:
rosparam.yamlmain([cmd, 'dump'])
except SystemExit as e:
self.assertNotEquals(0, e.code)
try:
rosparam.yamlmain([cmd, 'dump', f_out, 'rosparam_dump', 'rosparam_dump2'])
except SystemExit as e:
self.assertNotEquals(0, e.code)
rosparam.yamlmain([cmd, 'load', f, 'rosparam_dump'])
self.assertEquals('bar', ps.getParam('rosparam_dump/foo'))
rosparam.yamlmain([cmd, 'dump', f_out, 'rosparam_dump'])
# yaml files should be equal
import yaml
with open(f_out) as b:
with open(f) as b2:
self.assertEquals(yaml.load(b.read()), yaml.load(b2.read()))
rosparam.yamlmain([cmd, 'dump', '-v', f_out, 'rosparam_dump'])
with open(f_out) as b:
with open(f) as b2:
self.assertEquals(yaml.load(b.read()), yaml.load(b2.read()))
# yaml file and std_out should be the same
with fakestdout() as b:
rosparam.yamlmain([cmd, 'dump'])
with open(f) as b2:
self.assertEquals(yaml.load(b.getvalue())['rosparam_dump'], yaml.load(b2.read()))
def test_fullusage(self):
import rosparam
try:
rosparam._fullusage()
except SystemExit: pass
try:
rosparam.yamlmain(['rosparam'])
except SystemExit: pass
try:
rosparam.yamlmain(['rosparam', 'invalid'])
except SystemExit: pass
PKG = 'test_rosparam'
NAME = 'test_rosparam_command_line_online'
if __name__ == '__main__':
rostest.unitrun(PKG, NAME, TestRosparam, sys.argv, coverage_packages=['rosparam'])

View File

@@ -0,0 +1,195 @@
#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2009, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Willow Garage, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import os
import sys
import time
import unittest
import rostest
from subprocess import Popen, PIPE, check_call, call
from rosgraph.names import script_resolve_name
def get_param_server():
import rosgraph
return rosgraph.Master('/rosparam')
class TestRosparamOnline(unittest.TestCase):
def setUp(self):
self.vals = set()
self.msgs = {}
def callback(self, msg, val):
self.vals.add(val)
self.msgs[val] = msg
def test_rosparam(self):
ps = get_param_server()
# network is initialized
cmd = 'rosparam'
names = ['/chatter', 'foo/chatter']
# list
params = ['/string', '/int', '/float',
'/g1/string', '/g1/int', '/g1/float',
'/g2/string', '/g2/int', '/g2/float',
]
# - we aren't matching against the core services as those can make the test suites brittle
output = Popen([cmd, 'list'], stdout=PIPE).communicate()[0]
l = set(output.split())
for t in params:
self.assert_(t in l)
# get
# - strings
output = Popen([cmd, 'get', "string"], stdout=PIPE).communicate()[0]
self.assertEquals('foo-value', output.strip())
# -- pretty
output = Popen([cmd, 'get', '-p', "string"], stdout=PIPE).communicate()[0]
self.assertEquals('foo-value', output.strip())
output = Popen([cmd, 'get', "/string"], stdout=PIPE).communicate()[0]
self.assertEquals('foo-value', output.strip())
output = Popen([cmd, 'get', "g1/string"], stdout=PIPE).communicate()[0]
self.assertEquals('g1-foo-value', output.strip())
output = Popen([cmd, 'get', "/g1/string"], stdout=PIPE).communicate()[0]
self.assertEquals('g1-foo-value', output.strip())
output = Popen([cmd, 'get', "/g2/string"], stdout=PIPE).communicate()[0]
self.assertEquals('g2-foo-value', output.strip())
# - ints
output = Popen([cmd, 'get', "int"], stdout=PIPE).communicate()[0]
self.assertEquals('1', output.strip())
# -- pretty
output = Popen([cmd, 'get', '-p', "int"], stdout=PIPE).communicate()[0]
self.assertEquals('1', output.strip())
output = Popen([cmd, 'get', "/int"], stdout=PIPE).communicate()[0]
self.assertEquals('1', output.strip())
output = Popen([cmd, 'get', "g1/int"], stdout=PIPE).communicate()[0]
self.assertEquals('10', output.strip())
output = Popen([cmd, 'get', "/g1/int"], stdout=PIPE).communicate()[0]
self.assertEquals('10', output.strip())
output = Popen([cmd, 'get', "/g2/int"], stdout=PIPE).communicate()[0]
self.assertEquals('20', output.strip())
# - floats
output = Popen([cmd, 'get', "float"], stdout=PIPE).communicate()[0]
self.assertEquals('1.0', output.strip())
# -- pretty
output = Popen([cmd, 'get', '-p', "float"], stdout=PIPE).communicate()[0]
self.assertEquals('1.0', output.strip())
output = Popen([cmd, 'get', "/float"], stdout=PIPE).communicate()[0]
self.assertEquals('1.0', output.strip())
output = Popen([cmd, 'get', "g1/float"], stdout=PIPE).communicate()[0]
self.assertEquals('10.0', output.strip())
output = Popen([cmd, 'get', "/g1/float"], stdout=PIPE).communicate()[0]
self.assertEquals('10.0', output.strip())
output = Popen([cmd, 'get', "/g2/float"], stdout=PIPE).communicate()[0]
self.assertEquals('20.0', output.strip())
# - dictionary
output = Popen([cmd, 'get', "g1"], stdout=PIPE).communicate()[0]
import yaml
d = yaml.load(output)
self.assertEquals(d['float'], 10.0)
self.assertEquals(d['int'], 10.0)
self.assertEquals(d['string'], "g1-foo-value")
self.assertEquals(set(['float', 'int', 'string']), set(d.keys()))
# -- don't bother parsing pretty output of dictionary, but check for no errors
check_call([cmd, 'get', '-p', "g1"])
# --- with verbose
check_call([cmd, 'get', '-pv', "g1"])
# set
# - integers
Popen([cmd, 'set', "/set/test1", "1"], stdout=PIPE).communicate()[0]
self.assertEquals(1, ps.getParam('/set/test1'))
# -- verbose
Popen([cmd, 'set', '-v', "/set/test1", "1"], stdout=PIPE).communicate()[0]
self.assertEquals(1, ps.getParam('/set/test1'))
Popen([cmd, 'set', "set/test1", "2"], stdout=PIPE).communicate()[0]
self.assertEquals(2, ps.getParam('/set/test1'))
# - floats
Popen([cmd, 'set', "/set/test2", "1.0"], stdout=PIPE).communicate()[0]
self.assertEquals(1, ps.getParam('/set/test2'))
Popen([cmd, 'set', "set/test2", "2.0"], stdout=PIPE).communicate()[0]
self.assertEquals(2, ps.getParam('/set/test2'))
# - booleans
Popen([cmd, 'set', "/set/testbool", "true"], stdout=PIPE).communicate()[0]
self.assertEquals(True, ps.getParam('/set/testbool'))
Popen([cmd, 'set', "set/testbool", "false"], stdout=PIPE).communicate()[0]
self.assertEquals(False, ps.getParam('/set/testbool'))
# - strings
# TODO: test more interesting encodings, like multi-line
Popen([cmd, 'set', "/set/teststr", "hi"], stdout=PIPE).communicate()[0]
self.assertEquals("hi", ps.getParam('/set/teststr'))
Popen([cmd, 'set', "set/teststr", "hello world"], stdout=PIPE).communicate()[0]
self.assertEquals("hello world", ps.getParam('/set/teststr'))
Popen([cmd, 'set', "set/teststr", "'true'"], stdout=PIPE).communicate()[0]
self.assertEquals("true", ps.getParam('/set/teststr'))
# - list
Popen([cmd, 'set', "set/testlist", "[]"], stdout=PIPE).communicate()[0]
self.assertEquals([], ps.getParam('/set/testlist'))
Popen([cmd, 'set', "/set/testlist", "[1, 2, 3]"], stdout=PIPE).communicate()[0]
self.assertEquals([1, 2, 3], ps.getParam('/set/testlist'))
# - dictionary
Popen([cmd, 'set', "/set/testdict", "{a: b, c: d}"], stdout=PIPE).communicate()[0]
self.assertEquals('b', ps.getParam('/set/testdict/a'))
self.assertEquals('d', ps.getParam('/set/testdict/c'))
# - empty dictionary should be a noop
Popen([cmd, 'set', "set/testdict", "{}"], stdout=PIPE).communicate()[0]
self.assertEquals('b', ps.getParam('/set/testdict/a'))
self.assertEquals('d', ps.getParam('/set/testdict/c'))
# - this should be an update
Popen([cmd, 'set', "/set/testdict", "{e: f, g: h}"], stdout=PIPE).communicate()[0]
self.assertEquals('b', ps.getParam('/set/testdict/a'))
self.assertEquals('d', ps.getParam('/set/testdict/c'))
self.assertEquals('f', ps.getParam('/set/testdict/e'))
self.assertEquals('h', ps.getParam('/set/testdict/g'))
# -- verbose
check_call([cmd, 'set', '-v', "/set/testdictverbose", "{e: f, g: h}"])
# delete
ps.setParam('/delete/me', True)
self.assert_(ps.hasParam('/delete/me'))
Popen([cmd, 'delete', "/delete/me"], stdout=PIPE).communicate()[0]
self.failIf(ps.hasParam('/delete/me'))
# TODO: dump
# TODO: load
PKG = 'test_rosparam'
NAME = 'test_rosparam_command_line_online'
if __name__ == '__main__':
rostest.run(PKG, NAME, TestRosparamOnline, sys.argv)

View File

@@ -0,0 +1,20 @@
<launch>
<param name="string" value="foo-value" />
<param name="int" value="1" />
<param name="float" value="1.0" />
<group ns="g1">
<param name="string" value="g1-foo-value" />
<param name="int" value="10" />
<param name="float" value="10.0" />
</group>
<group ns="g2">
<param name="string" value="g2-foo-value" />
<param name="int" value="20" />
<param name="float" value="20.0" />
</group>
<test test-name="rosparam_command_line_online" pkg="test_rosparam" type="check_rosparam_command_line_online.py" />
<test test-name="test_rosparam" pkg="test_rosparam" type="check_rosparam.py" />
</launch>

View File

@@ -0,0 +1 @@
foo: bar

View File

@@ -0,0 +1 @@
{foo: bar}

View File

@@ -0,0 +1,2 @@
_ns: a/b
foo: baz

View File

@@ -0,0 +1,107 @@
#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2009, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Willow Garage, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Revision $Id: test_rosparam_command_line_offline.py 5710 2009-08-20 03:11:04Z sfkwc $
import os
import sys
import unittest
import time
from subprocess import Popen, PIPE, check_call, call
import rosparam
def get_test_path():
return os.path.abspath(os.path.join(os.path.dirname(__file__)))
class TestRosparamOffline(unittest.TestCase):
def setUp(self):
pass
## test that the rosmsg command works
def test_cmd_help(self):
cmd = 'rosparam'
sub = ['set', 'get', 'load', 'dump', 'delete', 'list']
output = Popen([cmd], stdout=PIPE).communicate()[0]
self.assert_('Commands' in output.decode(), output)
output = Popen([cmd, '-h'], stdout=PIPE).communicate()[0]
self.assert_('Commands' in output.decode())
for c in sub:
# make sure command is in usage statement
self.assert_("%s %s"%(cmd, c) in output.decode())
for c in sub:
output = Popen([cmd, c, '-h'], stdout=PIPE, stderr=PIPE).communicate()
self.assert_("Usage:" in output[0].decode(), "%s\n%s" % (output, c))
self.assert_("%s %s"%(cmd, c) in output[0].decode(), "%s: %s" % (c, output[0].decode()))
# test no args on commands that require args
for c in ['set', 'get', 'load', 'delete']:
output = Popen([cmd, c], stdout=PIPE, stderr=PIPE).communicate()
self.assert_("Usage:" in output[0].decode() or "Usage:" in output[1].decode(), "%s\n%s"%(output, c))
self.assert_("%s %s"%(cmd, c) in output[1].decode())
def test_offline(self):
cmd = 'rosparam'
# point at a different 'master'
env = os.environ.copy()
env['ROS_MASTER_URI'] = 'http://localhost:11312'
kwds = { 'env': env, 'stdout': PIPE, 'stderr': PIPE}
msg = "ERROR: Unable to communicate with master!\n"
output = Popen([cmd, 'list'], **kwds).communicate()
self.assert_(output[1].decode().endswith(msg))
output = Popen([cmd, 'set', 'foo', '1.0'], **kwds).communicate()
self.assert_(output[1].decode().endswith(msg))
output = Popen([cmd, 'get', 'foo'], **kwds).communicate()
self.assert_(output[1].decode().endswith(msg))
# have to test with actual file to avoid error
path = os.path.join(get_test_path(), 'test.yaml')
output = Popen([cmd, 'load', path], **kwds).communicate()
self.assert_(output[1].decode().endswith(msg))
# test with file that does not exist
output = Popen([cmd, 'load', 'fake.yaml'], **kwds).communicate()
self.assertEquals('ERROR: file [fake.yaml] does not exist\n', output[1].decode())
output = Popen([cmd, 'dump', 'foo.yaml'], **kwds).communicate()
self.assert_(output[1].decode().endswith(msg))
output = Popen([cmd, 'delete', 'foo'], **kwds).communicate()
self.assert_(output[1].decode().endswith(msg))