test_roslib_srvs.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2011, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
00032 
00033 import os
00034 import sys
00035 import unittest
00036 
00037 import roslib.packages
00038 
00039 class RoslibSrvTest(unittest.TestCase):
00040   
00041     def test_SrvSpec(self):
00042         from roslib.msgs import MsgSpec
00043         from roslib.srvs import SrvSpec
00044 
00045         types = ['int32']
00046         names = ['a']
00047         constants = []
00048         text = 'int32 a'
00049         msg_a = MsgSpec(types, names, constants, text)
00050 
00051         types = ['int64']
00052         names = ['b']
00053         constants = []
00054         text = 'int64 b'
00055         msg_b = MsgSpec(types, names, constants, text)
00056 
00057         text = msg_a.text + '\n---\n' + msg_b.text
00058         spec = SrvSpec(msg_a, msg_b, text)
00059         self.assertEquals(msg_a, spec.request)
00060         self.assertEquals(msg_b, spec.response)
00061         self.assertEquals(text, spec.text)
00062         self.assertEquals('', spec.full_name)
00063         self.assertEquals('', spec.short_name)
00064         self.assertEquals('',spec.package)
00065         
00066         # tripwire
00067         self.assert_(repr(spec))
00068         self.assert_(str(spec))
00069 
00070         # exercise eq
00071         self.assertNotEquals(spec, 'spec')
00072         self.assert_(spec != 'spec')
00073         
00074         spec2 = SrvSpec(msg_a, msg_b, text)
00075         self.assertEquals(spec, spec2)
00076         self.failIf(spec != spec2)
00077         
00078         # - full_name
00079         spec2.full_name = 'something'
00080         self.assertNotEquals(spec, spec2)        
00081         spec2.full_name = ''        
00082         self.assertEquals(spec, spec2)
00083         # - short_name
00084         spec2.short_name = 'something'
00085         self.assertNotEquals(spec, spec2)        
00086         spec2.short_name = ''        
00087         self.assertEquals(spec, spec2)
00088         # - package
00089         spec2.package = 'something'
00090         self.assertNotEquals(spec, spec2)        
00091         spec2.package = ''        
00092         self.assertEquals(spec, spec2)
00093         
00094     def test_srv_file(self):
00095         from roslib.srvs import srv_file
00096         
00097         d = roslib.packages.get_pkg_dir('test_rosmaster')
00098         filename = os.path.join(d, 'srv', 'AddTwoInts.srv')
00099         with open(filename, 'r') as f:
00100             text = f.read()
00101 
00102         self.assertEquals(filename, srv_file('test_rosmaster', 'AddTwoInts'))
00103         
00104     def test_load_from_file(self):
00105         from roslib.srvs import load_from_file, set_verbose
00106         
00107         d = roslib.packages.get_pkg_dir('test_rosmaster')
00108         filename = os.path.join(d, 'srv', 'AddTwoInts.srv')
00109         with open(filename, 'r') as f:
00110             text = f.read()
00111         
00112         name, spec = load_from_file(filename)
00113         self.assertEquals('AddTwoInts', name)
00114         self.assertEquals(['int64', 'int64'], spec.request.types)
00115         self.assertEquals(['a', 'b'], spec.request.names)
00116         self.assertEquals(text, spec.text)
00117         
00118         name2, spec2 = load_from_file(filename, package_context='foo')
00119         self.assertEquals('foo/AddTwoInts', name2)
00120         name2, spec2 = load_from_file(filename, package_context='foo/')
00121         self.assertEquals('foo/AddTwoInts', name2)
00122         name2, spec2 = load_from_file(filename, package_context='foo//')
00123         self.assertEquals('foo/AddTwoInts', name2)
00124 
00125         # test with verbose on
00126         set_verbose(True)
00127         name3, spec3 = load_from_file(filename)
00128         self.assertEquals(name, name3)
00129         self.assertEquals(spec, spec3)        
00130 


test_roslib_comm
Author(s): Jeremy Leibs, Ken Conley
autogenerated on Fri Aug 28 2015 12:33:39