00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
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
00067 self.assert_(repr(spec))
00068 self.assert_(str(spec))
00069
00070
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
00079 spec2.full_name = 'something'
00080 self.assertNotEquals(spec, spec2)
00081 spec2.full_name = ''
00082 self.assertEquals(spec, spec2)
00083
00084 spec2.short_name = 'something'
00085 self.assertNotEquals(spec, spec2)
00086 spec2.short_name = ''
00087 self.assertEquals(spec, spec2)
00088
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
00126 set_verbose(True)
00127 name3, spec3 = load_from_file(filename)
00128 self.assertEquals(name, name3)
00129 self.assertEquals(spec, spec3)
00130