test_client_param_api.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # Software License Agreement (BSD License)
3 #
4 # Copyright (c) 2008, Willow Garage, Inc.
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 # * Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following
15 # disclaimer in the documentation and/or other materials provided
16 # with the distribution.
17 # * Neither the name of Willow Garage, Inc. nor the names of its
18 # contributors may be used to endorse or promote products derived
19 # from this software without specific prior written permission.
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 # POSSIBILITY OF SUCH DAMAGE.
33 
34 ## Integration test for empty services to test serializers
35 ## and transport
36 
37 PKG = 'test_rospy'
38 NAME = 'test_param_api'
39 
40 import sys
41 import time
42 import unittest
43 
44 import rospy
45 import rostest
46 
47 class TestClientParamApi(unittest.TestCase):
48 
49  def test_param_api(self):
50  # test get_param_names
51  param_names = rospy.get_param_names()
52  for n in ['/param1', 'param1', '~param1', 'param_int', 'param_float']:
53  self.assert_(rospy.resolve_name(n) in param_names)
54 
55  # test has_param
56  self.assert_(rospy.has_param('/run_id'))
57  self.assert_(rospy.has_param('/param1'))
58  self.assert_(rospy.has_param('param1'))
59  self.assert_(rospy.has_param('~param1'))
60 
61  # test search_param
62  self.assertEquals(None, rospy.search_param('not_param1'))
63  self.assertEquals(rospy.resolve_name('~param1'), rospy.search_param('param1'))
64  self.assertEquals(rospy.resolve_name('parent_param'), rospy.search_param('parent_param'))
65  self.assertEquals("/global_param", rospy.search_param('global_param'))
66 
67  # test get_param on params set in rostest file
68  self.assertEquals('global_param1', rospy.get_param('/param1'))
69  self.assertEquals('parent_param1', rospy.get_param('param1'))
70  self.assertEquals('private_param1', rospy.get_param('~param1'))
71  # - type tests
72  self.assertEquals(1, rospy.get_param('param_int'))
73  self.assertAlmostEquals(2., rospy.get_param('param_float'))
74  self.assertEquals(True, rospy.get_param('param_bool'))
75  self.assertEquals("hello world", rospy.get_param('param_str'))
76 
77  # test default behavior get_param
78  try:
79  rospy.get_param('not_param1')
80  self.fail("should have raised KeyError")
81  except KeyError: pass
82  self.assertEquals('parent_param1', rospy.get_param('param1', 'foo'))
83  self.assertEquals('private_param1', rospy.get_param('~param1', 'foo'))
84  self.assertEquals('myval', rospy.get_param('not_param1', 'myval'))
85  self.assertEquals('myval', rospy.get_param('~not_param1', 'myval'))
86  self.assertEquals(None, rospy.get_param('not_param1', None))
87  self.assertEquals(None, rospy.get_param('~not_param1', None))
88 
89  # test set/get roundtrips
90  vals = [ '1', 1, 1., [1, 2, 3, 4], {'a': 1, 'b': 2}]
91  for v in vals:
92  self.failIf(rospy.has_param("cp_param"))
93  try:
94  rospy.get_param('cp_param1')
95  self.fail("should have thrown KeyError")
96  except KeyError: pass
97  self.assertEquals(None, rospy.get_param('cp_param', None))
98  self.assertEquals("default", rospy.get_param('cp_param', "default"))
99  rospy.set_param("cp_param", v)
100  self.assert_(rospy.has_param("cp_param"))
101  self.assertEquals(v, rospy.get_param("cp_param"))
102  self.assertEquals(rospy.resolve_name('cp_param'), rospy.search_param('cp_param'))
103  # erase the param and recheck state
104  rospy.delete_param('cp_param')
105  self.failIf(rospy.has_param("cp_param"))
106  self.assertEquals(None, rospy.get_param('cp_param', None))
107  self.assertEquals("default", rospy.get_param('cp_param', "default"))
108  self.assertEquals(None, rospy.search_param('cp_param'))
109 
110 if __name__ == '__main__':
111  rospy.init_node(NAME)
112  rostest.run(PKG, NAME, TestClientParamApi, sys.argv)


test_rospy
Author(s): Ken Conley
autogenerated on Sun Feb 3 2019 03:30:22