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
00035
00036
00037 PKG = 'test_rospy'
00038 NAME = 'test_param_api'
00039
00040 import sys
00041 import time
00042 import unittest
00043
00044 import rospy
00045 import rostest
00046
00047 class TestClientParamApi(unittest.TestCase):
00048
00049 def test_param_api(self):
00050
00051 param_names = rospy.get_param_names()
00052 for n in ['/param1', 'param1', '~param1', 'param_int', 'param_float']:
00053 self.assert_(rospy.resolve_name(n) in param_names)
00054
00055
00056 self.assert_(rospy.has_param('/run_id'))
00057 self.assert_(rospy.has_param('/param1'))
00058 self.assert_(rospy.has_param('param1'))
00059 self.assert_(rospy.has_param('~param1'))
00060
00061
00062 self.assertEquals(None, rospy.search_param('not_param1'))
00063 self.assertEquals(rospy.resolve_name('~param1'), rospy.search_param('param1'))
00064 self.assertEquals(rospy.resolve_name('parent_param'), rospy.search_param('parent_param'))
00065 self.assertEquals("/global_param", rospy.search_param('global_param'))
00066
00067
00068 self.assertEquals('global_param1', rospy.get_param('/param1'))
00069 self.assertEquals('parent_param1', rospy.get_param('param1'))
00070 self.assertEquals('private_param1', rospy.get_param('~param1'))
00071
00072 self.assertEquals(1, rospy.get_param('param_int'))
00073 self.assertAlmostEquals(2., rospy.get_param('param_float'))
00074 self.assertEquals(True, rospy.get_param('param_bool'))
00075 self.assertEquals("hello world", rospy.get_param('param_str'))
00076
00077
00078 try:
00079 rospy.get_param('not_param1')
00080 self.fail("should have raised KeyError")
00081 except KeyError: pass
00082 self.assertEquals('parent_param1', rospy.get_param('param1', 'foo'))
00083 self.assertEquals('private_param1', rospy.get_param('~param1', 'foo'))
00084 self.assertEquals('myval', rospy.get_param('not_param1', 'myval'))
00085 self.assertEquals('myval', rospy.get_param('~not_param1', 'myval'))
00086 self.assertEquals(None, rospy.get_param('not_param1', None))
00087 self.assertEquals(None, rospy.get_param('~not_param1', None))
00088
00089
00090 vals = [ '1', 1, 1., [1, 2, 3, 4], {'a': 1, 'b': 2}]
00091 for v in vals:
00092 self.failIf(rospy.has_param("cp_param"))
00093 try:
00094 rospy.get_param('cp_param1')
00095 self.fail("should have thrown KeyError")
00096 except KeyError: pass
00097 self.assertEquals(None, rospy.get_param('cp_param', None))
00098 self.assertEquals("default", rospy.get_param('cp_param', "default"))
00099 rospy.set_param("cp_param", v)
00100 self.assert_(rospy.has_param("cp_param"))
00101 self.assertEquals(v, rospy.get_param("cp_param"))
00102 self.assertEquals(rospy.resolve_name('cp_param'), rospy.search_param('cp_param'))
00103
00104 rospy.delete_param('cp_param')
00105 self.failIf(rospy.has_param("cp_param"))
00106 self.assertEquals(None, rospy.get_param('cp_param', None))
00107 self.assertEquals("default", rospy.get_param('cp_param', "default"))
00108 self.assertEquals(None, rospy.search_param('cp_param'))
00109
00110 if __name__ == '__main__':
00111 rospy.init_node(NAME)
00112 rostest.run(PKG, NAME, TestClientParamApi, sys.argv)