$search
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 PKG = 'test_roslib_comm' 00034 import roslib; roslib.load_manifest(PKG) 00035 00036 import os 00037 import sys 00038 import unittest 00039 00040 import roslib.packages 00041 import rosunit 00042 00043 class RoslibSrvTest(unittest.TestCase): 00044 00045 def test_SrvSpec(self): 00046 from roslib.msgs import MsgSpec 00047 from roslib.srvs import SrvSpec 00048 00049 types = ['int32'] 00050 names = ['a'] 00051 constants = [] 00052 text = 'int32 a' 00053 msg_a = MsgSpec(types, names, constants, text) 00054 00055 types = ['int64'] 00056 names = ['b'] 00057 constants = [] 00058 text = 'int64 b' 00059 msg_b = MsgSpec(types, names, constants, text) 00060 00061 text = msg_a.text + '\n---\n' + msg_b.text 00062 spec = SrvSpec(msg_a, msg_b, text) 00063 self.assertEquals(msg_a, spec.request) 00064 self.assertEquals(msg_b, spec.response) 00065 self.assertEquals(text, spec.text) 00066 self.assertEquals('', spec.full_name) 00067 self.assertEquals('', spec.short_name) 00068 self.assertEquals('',spec.package) 00069 00070 # tripwire 00071 self.assert_(repr(spec)) 00072 self.assert_(str(spec)) 00073 00074 # exercise eq 00075 self.assertNotEquals(spec, 'spec') 00076 self.assert_(spec != 'spec') 00077 00078 spec2 = SrvSpec(msg_a, msg_b, text) 00079 self.assertEquals(spec, spec2) 00080 self.failIf(spec != spec2) 00081 00082 # - full_name 00083 spec2.full_name = 'something' 00084 self.assertNotEquals(spec, spec2) 00085 spec2.full_name = '' 00086 self.assertEquals(spec, spec2) 00087 # - short_name 00088 spec2.short_name = 'something' 00089 self.assertNotEquals(spec, spec2) 00090 spec2.short_name = '' 00091 self.assertEquals(spec, spec2) 00092 # - package 00093 spec2.package = 'something' 00094 self.assertNotEquals(spec, spec2) 00095 spec2.package = '' 00096 self.assertEquals(spec, spec2) 00097 00098 def test_is_verbose(self): 00099 from roslib.srvs import is_verbose, set_verbose 00100 self.failIf(is_verbose()) 00101 set_verbose(True) 00102 self.assert_(is_verbose()) 00103 set_verbose(False) 00104 self.failIf(is_verbose()) 00105 00106 def test_list_srv_types(self): 00107 # tripwire for now 00108 from roslib.srvs import list_srv_types 00109 types = list_srv_types('test_ros', include_depends=False) 00110 self.assertEquals(types, ['AddTwoInts']) 00111 00112 def test_srv_file(self): 00113 from roslib.srvs import srv_file 00114 00115 d = roslib.packages.get_pkg_dir('test_ros') 00116 filename = os.path.join(d, 'srv', 'AddTwoInts.srv') 00117 with open(filename, 'r') as f: 00118 text = f.read() 00119 00120 self.assertEquals(filename, srv_file('test_ros', 'AddTwoInts')) 00121 00122 def test_load_from_file(self): 00123 from roslib.srvs import load_from_file, set_verbose 00124 00125 d = roslib.packages.get_pkg_dir('test_ros') 00126 filename = os.path.join(d, 'srv', 'AddTwoInts.srv') 00127 with open(filename, 'r') as f: 00128 text = f.read() 00129 00130 name, spec = load_from_file(filename) 00131 self.assertEquals('AddTwoInts', name) 00132 self.assertEquals(['int64', 'int64'], spec.request.types) 00133 self.assertEquals(['a', 'b'], spec.request.names) 00134 self.assertEquals(text, spec.text) 00135 00136 name2, spec2 = load_from_file(filename, package_context='foo') 00137 self.assertEquals('foo/AddTwoInts', name2) 00138 name2, spec2 = load_from_file(filename, package_context='foo/') 00139 self.assertEquals('foo/AddTwoInts', name2) 00140 name2, spec2 = load_from_file(filename, package_context='foo//') 00141 self.assertEquals('foo/AddTwoInts', name2) 00142 00143 # test with verbose on 00144 set_verbose(True) 00145 name3, spec3 = load_from_file(filename) 00146 self.assertEquals(name, name3) 00147 self.assertEquals(spec, spec3) 00148 00149 00150 if __name__ == '__main__': 00151 rosunit.unitrun(PKG, 'test_roslib_srvs', RoslibSrvTest, coverage_packages=['roslib.srvs']) 00152