$search
00001 #!/usr/bin/env python 00002 # Software License Agreement (BSD License) 00003 # 00004 # Copyright (c) 2008, Willow Garage, Inc. 00005 # All rights reserved. 00006 # 00007 # Redistribution and use in source and binary forms, with or without 00008 # modification, are permitted provided that the following conditions 00009 # are met: 00010 # 00011 # * Redistributions of source code must retain the above copyright 00012 # notice, this list of conditions and the following disclaimer. 00013 # * Redistributions in binary form must reproduce the above 00014 # copyright notice, this list of conditions and the following 00015 # disclaimer in the documentation and/or other materials provided 00016 # with the distribution. 00017 # * Neither the name of Willow Garage, Inc. nor the names of its 00018 # contributors may be used to endorse or promote products derived 00019 # from this software without specific prior written permission. 00020 # 00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 # POSSIBILITY OF SUCH DAMAGE. 00033 # 00034 # Revision $Id: test_embed_msg.py 3803 2009-02-11 02:04:39Z rob_wheeler $ 00035 00036 ## Integration test for empty services to test serializers 00037 ## and transport 00038 00039 PKG = 'test_rospy' 00040 NAME = 'test_param_api' 00041 import roslib; roslib.load_manifest(PKG) 00042 00043 import sys 00044 import time 00045 import unittest 00046 00047 import rospy 00048 import rostest 00049 00050 class TestClientParamApi(unittest.TestCase): 00051 00052 def test_param_api(self): 00053 # test get_param_names 00054 param_names = rospy.get_param_names() 00055 for n in ['/param1', 'param1', '~param1', 'param_int', 'param_float']: 00056 self.assert_(rospy.resolve_name(n) in param_names) 00057 00058 # test has_param 00059 self.assert_(rospy.has_param('/run_id')) 00060 self.assert_(rospy.has_param('/param1')) 00061 self.assert_(rospy.has_param('param1')) 00062 self.assert_(rospy.has_param('~param1')) 00063 00064 # test search_param 00065 self.assertEquals(None, rospy.search_param('not_param1')) 00066 self.assertEquals(rospy.resolve_name('~param1'), rospy.search_param('param1')) 00067 self.assertEquals(rospy.resolve_name('parent_param'), rospy.search_param('parent_param')) 00068 self.assertEquals("/global_param", rospy.search_param('global_param')) 00069 00070 # test get_param on params set in rostest file 00071 self.assertEquals('global_param1', rospy.get_param('/param1')) 00072 self.assertEquals('parent_param1', rospy.get_param('param1')) 00073 self.assertEquals('private_param1', rospy.get_param('~param1')) 00074 # - type tests 00075 self.assertEquals(1, rospy.get_param('param_int')) 00076 self.assertAlmostEquals(2., rospy.get_param('param_float')) 00077 self.assertEquals(True, rospy.get_param('param_bool')) 00078 self.assertEquals("hello world", rospy.get_param('param_str')) 00079 00080 # test default behavior get_param 00081 try: 00082 rospy.get_param('not_param1') 00083 self.fail("should have raised KeyError") 00084 except KeyError: pass 00085 self.assertEquals('parent_param1', rospy.get_param('param1', 'foo')) 00086 self.assertEquals('private_param1', rospy.get_param('~param1', 'foo')) 00087 self.assertEquals('myval', rospy.get_param('not_param1', 'myval')) 00088 self.assertEquals('myval', rospy.get_param('~not_param1', 'myval')) 00089 self.assertEquals(None, rospy.get_param('not_param1', None)) 00090 self.assertEquals(None, rospy.get_param('~not_param1', None)) 00091 00092 # test set/get roundtrips 00093 vals = [ '1', 1, 1., [1, 2, 3, 4], {'a': 1, 'b': 2}] 00094 for v in vals: 00095 self.failIf(rospy.has_param("cp_param")) 00096 try: 00097 rospy.get_param('cp_param1') 00098 self.fail("should have thrown KeyError") 00099 except KeyError: pass 00100 self.assertEquals(None, rospy.get_param('cp_param', None)) 00101 self.assertEquals("default", rospy.get_param('cp_param', "default")) 00102 rospy.set_param("cp_param", v) 00103 self.assert_(rospy.has_param("cp_param")) 00104 self.assertEquals(v, rospy.get_param("cp_param")) 00105 self.assertEquals(rospy.resolve_name('cp_param'), rospy.search_param('cp_param')) 00106 # erase the param and recheck state 00107 rospy.delete_param('cp_param') 00108 self.failIf(rospy.has_param("cp_param")) 00109 self.assertEquals(None, rospy.get_param('cp_param', None)) 00110 self.assertEquals("default", rospy.get_param('cp_param', "default")) 00111 self.assertEquals(None, rospy.search_param('cp_param')) 00112 00113 if __name__ == '__main__': 00114 rospy.init_node(NAME) 00115 rostest.run(PKG, NAME, TestClientParamApi, sys.argv)