params_basic.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 PKG = 'test_roslaunch'
00035 
00036 import os, sys, unittest
00037 
00038 try:
00039     from xmlrpc.client import Binary
00040 except ImportError:
00041     from xmlrpclib import Binary
00042 import rostest
00043 import rospkg
00044 
00045 import rosgraph
00046 master = rosgraph.Master('params_basic')
00047 def get_param(*args):
00048     return master.getParam(*args)
00049     
00050 ## Test Roslaunch 'param' tags
00051 class TestParamsBasic(unittest.TestCase):
00052 
00053     ## test primitive values
00054     def test_values(self):
00055         ## Test roslaunch string params
00056         self.assertEquals(get_param('stringempty'), '')
00057         print(get_param('stringbar'))
00058         self.assertEquals(get_param('stringbar'), 'bar')
00059         self.assertEquals(get_param('str10'), '10')
00060         self.assertEquals(get_param('string10'), '10')        
00061         self.assertEquals(get_param('stringentity'), '<stringentity/>')        
00062         ## Test roslaunch integer params
00063         self.assertEquals(get_param("integerneg1"), -1)
00064         self.assertEquals(get_param("integer0"), 0)
00065         self.assertEquals(get_param("integer1"), 1)
00066         self.assertEquals(get_param("integernoop1"), 1)
00067         self.assertEquals(get_param("integer12345"), 12345)
00068         ## Test roslaunch float params
00069         self.assertEquals(get_param("floatpi"),3.14159)
00070         self.assertEquals(get_param("floatnooppi"),3.14159)
00071         self.assertEquals(get_param("float3"),3.0)
00072         self.assertEquals(get_param("floatneg1"),-1.0)
00073         ## Test roslaunch boolean params
00074         for p in ['true', 'TRUE', 'True']:
00075             self.assertTrue(get_param(p), "[%s] is not false: %s"%(p, get_param(p)))
00076         for p in ['false', "FALSE", 'False']:
00077             self.assertFalse(get_param(p), "[%s] is not false: %s"%(p, get_param(p)))
00078             
00079     ## Test roslaunch ns attribute (namespace) on params
00080     def test_ns(self):
00081         self.assertEquals(get_param("/wg/childparam"),"wg")
00082         self.assertEquals(get_param("/wg2/childparam"),"wg2")
00083         self.assertEquals(get_param("/wg3/childparam"),"wg3")
00084         self.assertEquals(get_param("/wg/wg4/childparam"),"wg4")
00085         self.assertEquals(get_param("/wg/wg4/wg5/childparam"),"wg5")          
00086         ## Test roslaunch <group> tag with and without ns attribute
00087         self.assertEquals(get_param("/wga/wg/childparam"),"wg")
00088         self.assertEquals(get_param("/wga/wg2/childparam"),"wg2")
00089         self.assertEquals(get_param("/wga/wg3/childparam"),"wg3")
00090         self.assertEquals(get_param("/wga/wg/wg4/childparam"),"wg4")
00091         self.assertEquals(get_param("/wga/wg/wg4/wg5/childparam"),"wg5")          
00092         # test second-level group
00093         self.assertEquals(get_param("/wga/wgb/wg/childparam"),"bwg")
00094         self.assertEquals(get_param("/wga/wgb/wg2/childparam"),"bwg2")
00095         self.assertEquals(get_param("/wga/wgb/wg3/childparam"),"bwg3")
00096         self.assertEquals(get_param("/wga/wgb/wg/wg4/childparam"),"bwg4")
00097         self.assertEquals(get_param("/wga/wgb/wg/wg4/wg5/childparam"),"bwg5")
00098         # test unscoped group
00099         self.assertEquals(get_param("/wgc/childparam"),"wg")
00100         self.assertEquals(get_param("/wgc2/childparam"),"wg2")
00101         self.assertEquals(get_param("/wgc3/childparam"),"wg3")
00102         self.assertEquals(get_param("/wgc/wg4/childparam"),"wg4")
00103         self.assertEquals(get_param("/wgc/wg4/wg5/childparam"),"wg5")          
00104         
00105     ## test 'command' attribute
00106     def test_commandandfile(self):
00107         dir = rospkg.RosPack().get_path('roslaunch')
00108         with open(os.path.join(dir, 'resources', 'example.launch'), 'r') as f:
00109             text_data = f.read()
00110         with open(os.path.join(dir, 'resources', 'example.launch'), 'rb') as f:
00111             binary_data = f.read()
00112         self.assertEquals(get_param("commandoutput"), binary_data)
00113         self.assertEquals(get_param("textfile"), text_data)
00114         ## test 'binfile' attribute
00115         bindata = get_param("binaryfile")
00116         self.assertTrue(isinstance(bindata, Binary))
00117         self.assertEquals(bindata.data, binary_data)
00118     
00119 if __name__ == '__main__':
00120     rostest.rosrun(PKG, sys.argv[0], TestParamsBasic, sys.argv)
00121     


test_roslaunch
Author(s): Ken Conley
autogenerated on Tue Mar 7 2017 03:45:37