test_client_param_api.py
Go to the documentation of this file.
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 ## Integration test for empty services to test serializers
00035 ## and transport
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         # test get_param_names
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         # test has_param
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         # test search_param
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         # test get_param on params set in rostest file
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         # - type tests
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         # test default behavior get_param 
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         # test set/get roundtrips
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             # erase the param and recheck state
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)


test_rospy
Author(s): Ken Conley
autogenerated on Thu Jun 6 2019 21:10:57