params_basic.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 PKG = 'test_roslaunch'
35 
36 import os, sys, unittest
37 
38 try:
39  from xmlrpc.client import Binary
40 except ImportError:
41  from xmlrpclib import Binary
42 import rostest
43 import rospkg
44 
45 import rosgraph
46 master = rosgraph.Master('params_basic')
47 def get_param(*args):
48  return master.getParam(*args)
49 
50 ## Test Roslaunch 'param' tags
51 class TestParamsBasic(unittest.TestCase):
52 
53  ## test primitive values
54  def test_values(self):
55  ## Test roslaunch string params
56  self.assertEquals(get_param('stringempty'), '')
57  print(get_param('stringbar'))
58  self.assertEquals(get_param('stringbar'), 'bar')
59  self.assertEquals(get_param('str10'), '10')
60  self.assertEquals(get_param('string10'), '10')
61  self.assertEquals(get_param('stringentity'), '<stringentity/>')
62  ## Test roslaunch integer params
63  self.assertEquals(get_param("integerneg1"), -1)
64  self.assertEquals(get_param("integer0"), 0)
65  self.assertEquals(get_param("integer1"), 1)
66  self.assertEquals(get_param("integernoop1"), 1)
67  self.assertEquals(get_param("integer12345"), 12345)
68  ## Test roslaunch float params
69  self.assertEquals(get_param("floatpi"),3.14159)
70  self.assertEquals(get_param("floatnooppi"),3.14159)
71  self.assertEquals(get_param("float3"),3.0)
72  self.assertEquals(get_param("floatneg1"),-1.0)
73  ## Test roslaunch boolean params
74  for p in ['true', 'TRUE', 'True']:
75  self.assertTrue(get_param(p), "[%s] is not false: %s"%(p, get_param(p)))
76  for p in ['false', "FALSE", 'False']:
77  self.assertFalse(get_param(p), "[%s] is not false: %s"%(p, get_param(p)))
78 
79  ## Test roslaunch ns attribute (namespace) on params
80  def test_ns(self):
81  self.assertEquals(get_param("/wg/childparam"),"wg")
82  self.assertEquals(get_param("/wg2/childparam"),"wg2")
83  self.assertEquals(get_param("/wg3/childparam"),"wg3")
84  self.assertEquals(get_param("/wg/wg4/childparam"),"wg4")
85  self.assertEquals(get_param("/wg/wg4/wg5/childparam"),"wg5")
86  ## Test roslaunch <group> tag with and without ns attribute
87  self.assertEquals(get_param("/wga/wg/childparam"),"wg")
88  self.assertEquals(get_param("/wga/wg2/childparam"),"wg2")
89  self.assertEquals(get_param("/wga/wg3/childparam"),"wg3")
90  self.assertEquals(get_param("/wga/wg/wg4/childparam"),"wg4")
91  self.assertEquals(get_param("/wga/wg/wg4/wg5/childparam"),"wg5")
92  # test second-level group
93  self.assertEquals(get_param("/wga/wgb/wg/childparam"),"bwg")
94  self.assertEquals(get_param("/wga/wgb/wg2/childparam"),"bwg2")
95  self.assertEquals(get_param("/wga/wgb/wg3/childparam"),"bwg3")
96  self.assertEquals(get_param("/wga/wgb/wg/wg4/childparam"),"bwg4")
97  self.assertEquals(get_param("/wga/wgb/wg/wg4/wg5/childparam"),"bwg5")
98  # test unscoped group
99  self.assertEquals(get_param("/wgc/childparam"),"wg")
100  self.assertEquals(get_param("/wgc2/childparam"),"wg2")
101  self.assertEquals(get_param("/wgc3/childparam"),"wg3")
102  self.assertEquals(get_param("/wgc/wg4/childparam"),"wg4")
103  self.assertEquals(get_param("/wgc/wg4/wg5/childparam"),"wg5")
104 
105  ## test 'command' attribute
107  dir = rospkg.RosPack().get_path('roslaunch')
108  with open(os.path.join(dir, 'resources', 'example.launch'), 'r') as f:
109  text_data = f.read()
110  with open(os.path.join(dir, 'resources', 'example.launch'), 'rb') as f:
111  binary_data = f.read()
112  self.assertEquals(get_param("commandoutput"), binary_data)
113  self.assertEquals(get_param("textfile"), text_data)
114  ## test 'binfile' attribute
115  bindata = get_param("binaryfile")
116  self.assertTrue(isinstance(bindata, Binary))
117  self.assertEquals(bindata.data, binary_data)
118 
119 if __name__ == '__main__':
120  rostest.rosrun(PKG, sys.argv[0], TestParamsBasic, sys.argv)
121 
Test Roslaunch &#39;param&#39; tags.
Definition: params_basic.py:51
def get_param(args)
Definition: params_basic.py:47
def test_commandandfile(self)
test &#39;command&#39; attribute
def test_ns(self)
Test roslaunch ns attribute (namespace) on params.
Definition: params_basic.py:80
def test_values(self)
test primitive values
Definition: params_basic.py:54


test_roslaunch
Author(s): Ken Conley, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:53