test_roslib_srvs.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2011, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 import os
34 import sys
35 import unittest
36 
37 import roslib.packages
38 
39 class RoslibSrvTest(unittest.TestCase):
40 
41  def test_SrvSpec(self):
42  from roslib.msgs import MsgSpec
43  from roslib.srvs import SrvSpec
44 
45  types = ['int32']
46  names = ['a']
47  constants = []
48  text = 'int32 a'
49  msg_a = MsgSpec(types, names, constants, text)
50 
51  types = ['int64']
52  names = ['b']
53  constants = []
54  text = 'int64 b'
55  msg_b = MsgSpec(types, names, constants, text)
56 
57  text = msg_a.text + '\n---\n' + msg_b.text
58  spec = SrvSpec(msg_a, msg_b, text)
59  self.assertEquals(msg_a, spec.request)
60  self.assertEquals(msg_b, spec.response)
61  self.assertEquals(text, spec.text)
62  self.assertEquals('', spec.full_name)
63  self.assertEquals('', spec.short_name)
64  self.assertEquals('',spec.package)
65 
66  # tripwire
67  self.assert_(repr(spec))
68  self.assert_(str(spec))
69 
70  # exercise eq
71  self.assertNotEquals(spec, 'spec')
72  self.assert_(spec != 'spec')
73 
74  spec2 = SrvSpec(msg_a, msg_b, text)
75  self.assertEquals(spec, spec2)
76  self.failIf(spec != spec2)
77 
78  # - full_name
79  spec2.full_name = 'something'
80  self.assertNotEquals(spec, spec2)
81  spec2.full_name = ''
82  self.assertEquals(spec, spec2)
83  # - short_name
84  spec2.short_name = 'something'
85  self.assertNotEquals(spec, spec2)
86  spec2.short_name = ''
87  self.assertEquals(spec, spec2)
88  # - package
89  spec2.package = 'something'
90  self.assertNotEquals(spec, spec2)
91  spec2.package = ''
92  self.assertEquals(spec, spec2)
93 
94  def test_srv_file(self):
95  from roslib.srvs import srv_file
96 
97  d = roslib.packages.get_pkg_dir('test_rosmaster')
98  filename = os.path.join(d, 'srv', 'AddTwoInts.srv')
99  with open(filename, 'r') as f:
100  text = f.read()
101 
102  self.assertEquals(filename, srv_file('test_rosmaster', 'AddTwoInts'))
103 
105  from roslib.srvs import load_from_file, set_verbose
106 
107  d = roslib.packages.get_pkg_dir('test_rosmaster')
108  filename = os.path.join(d, 'srv', 'AddTwoInts.srv')
109  with open(filename, 'r') as f:
110  text = f.read()
111 
112  name, spec = load_from_file(filename)
113  self.assertEquals('AddTwoInts', name)
114  self.assertEquals(['int64', 'int64'], spec.request.types)
115  self.assertEquals(['a', 'b'], spec.request.names)
116  self.assertEquals(text, spec.text)
117 
118  name2, spec2 = load_from_file(filename, package_context='foo')
119  self.assertEquals('foo/AddTwoInts', name2)
120  name2, spec2 = load_from_file(filename, package_context='foo/')
121  self.assertEquals('foo/AddTwoInts', name2)
122  name2, spec2 = load_from_file(filename, package_context='foo//')
123  self.assertEquals('foo/AddTwoInts', name2)
124 
125  # test with verbose on
126  set_verbose(True)
127  name3, spec3 = load_from_file(filename)
128  self.assertEquals(name, name3)
129  self.assertEquals(spec, spec3)
130 


test_roslib_comm
Author(s): Jeremy Leibs, Ken Conley, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:23