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 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.msgs
00041 import rosunit
00042
00043 class MsgSpecTest(unittest.TestCase):
00044
00045 def test_verbose(self):
00046 self.failIf(roslib.msgs.is_verbose())
00047 roslib.msgs.set_verbose(True)
00048 self.assert_(roslib.msgs.is_verbose())
00049 roslib.msgs.set_verbose(False)
00050 self.failIf(roslib.msgs.is_verbose())
00051
00052 def test_base_msg_type(self):
00053 tests = [(None, None), ('String', 'String'), ('std_msgs/String', 'std_msgs/String'),
00054 ('String[10]', 'String'), ('string[10]', 'string'), ('std_msgs/String[10]', 'std_msgs/String'),
00055 ]
00056 for val, res in tests:
00057 self.assertEquals(res, roslib.msgs.base_msg_type(val))
00058
00059 def test_resolve_type(self):
00060 from roslib.msgs import resolve_type
00061 for t in ['string', 'string[]', 'string[14]', 'int32', 'int32[]']:
00062 bt = roslib.msgs.base_msg_type(t)
00063 self.assertEquals(t, resolve_type(t, PKG))
00064
00065 self.assertEquals('foo/string', resolve_type('foo/string', PKG))
00066 self.assertEquals('std_msgs/Header', resolve_type('Header', 'roslib'))
00067 self.assertEquals('std_msgs/Header', resolve_type('std_msgs/Header', 'roslib'))
00068 self.assertEquals('std_msgs/Header', resolve_type('Header', 'stereo_msgs'))
00069 self.assertEquals('std_msgs/String', resolve_type('String', 'std_msgs'))
00070 self.assertEquals('std_msgs/String', resolve_type('std_msgs/String', 'std_msgs'))
00071 self.assertEquals('std_msgs/String', resolve_type('std_msgs/String', PKG))
00072 self.assertEquals('std_msgs/String[]', resolve_type('std_msgs/String[]', PKG))
00073
00074 def test_parse_type(self):
00075 tests = [
00076 ('a', ('a', False, None)),
00077 ('int8', ('int8', False, None)),
00078 ('std_msgs/String', ('std_msgs/String', False, None)),
00079 ('a[]', ('a', True, None)),
00080 ('int8[]', ('int8', True, None)),
00081 ('std_msgs/String[]', ('std_msgs/String', True, None)),
00082 ('a[1]', ('a', True, 1)),
00083 ('int8[1]', ('int8', True, 1)),
00084 ('std_msgs/String[1]', ('std_msgs/String', True, 1)),
00085 ('a[11]', ('a', True, 11)),
00086 ('int8[11]', ('int8', True, 11)),
00087 ('std_msgs/String[11]', ('std_msgs/String', True, 11)),
00088 ]
00089 for val, res in tests:
00090 self.assertEquals(res, roslib.msgs.parse_type(val))
00091
00092 fail = ['a[1][2]', 'a[][]', '', None, 'a[', 'a[[1]', 'a[1]]']
00093 for f in fail:
00094 try:
00095 roslib.msgs.parse_type(f)
00096 self.fail("should have failed on " +f)
00097 except roslib.msgs.MsgSpecException, e: pass
00098
00099 def test_Constant(self):
00100 import random
00101 vals = [random.randint(0, 1000) for i in xrange(0, 3)]
00102 type_, name, val = [str(x) for x in vals]
00103 x = roslib.msgs.Constant(type_, name, val, str(val))
00104 self.assertEquals(type_, x.type)
00105 self.assertEquals(name, x.name)
00106 self.assertEquals(val, x.val)
00107 self.assertEquals(roslib.msgs.Constant(type_, name, val, str(val)), x)
00108
00109 self.assertNotEquals(1, x)
00110 self.assertNotEquals(roslib.msgs.Constant('baz', name, val, str(val)), x)
00111 self.assertNotEquals(roslib.msgs.Constant(type_, 'foo', val, str(val)), x)
00112 self.assertNotEquals(roslib.msgs.Constant(type_, name, 'foo', 'foo'), x)
00113
00114 try:
00115 roslib.msgs.Constant(None, name, val, str(val))
00116 except: pass
00117 try:
00118 roslib.msgs.Constant(type_, None, val, str(val))
00119 except: pass
00120 try:
00121 roslib.msgs.Constant(type_, name, None, 'None')
00122 except: pass
00123 try:
00124 roslib.msgs.Constant(type_, name, val, None)
00125 except: pass
00126
00127 try:
00128 x.foo = 'bar'
00129 self.fail('Constant should not allow arbitrary attr assignment')
00130 except: pass
00131
00132 def test_MsgSpec(self):
00133 def sub_test_MsgSpec(types, names, constants, text, has_header):
00134 m = MsgSpec(types, names, constants, text)
00135 self.assertEquals(m.types, types)
00136 self.assertEquals(m.names, names)
00137 self.assertEquals(m.text, text)
00138 self.assertEquals(has_header, m.has_header())
00139 self.assertEquals(m.constants, constants)
00140 self.assertEquals(zip(types, names), m.fields())
00141 self.assertEquals(m, MsgSpec(types, names, constants, text))
00142 return m
00143
00144 from roslib.msgs import MsgSpec
00145
00146 empty = sub_test_MsgSpec([], [], [], '', False)
00147 self.assertEquals([], empty.fields())
00148
00149
00150 one_field = sub_test_MsgSpec(['int32'], ['x'], [], 'int32 x', False)
00151
00152 self.assertEquals(one_field, MsgSpec(['int32'], ['x'], [], 'int32 x'))
00153 self.assertNotEquals(one_field, MsgSpec(['uint32'], ['x'], [], 'int32 x'))
00154 self.assertNotEquals(one_field, MsgSpec(['int32'], ['y'], [], 'int32 x'))
00155 self.assertNotEquals(one_field, MsgSpec(['int32'], ['x'], [], 'uint32 x'))
00156
00157 self.assert_(one_field != MsgSpec(['int32'], ['x'], [], 'uint32 x'))
00158
00159
00160 two_fields = sub_test_MsgSpec(['int32', 'string'], ['x', 'str'], [], 'int32 x\nstring str', False)
00161 one_header = sub_test_MsgSpec(['Header'], ['header'], [], 'Header header', True)
00162 header_and_fields = sub_test_MsgSpec(['Header', 'int32', 'string'], ['header', 'x', 'str'], [], 'Header header\nint32 x\nstring str', True)
00163 embed_types = sub_test_MsgSpec(['Header', 'std_msgs/Int32', 'string'], ['header', 'x', 'str'], [], 'Header header\nstd_msgs/Int32 x\nstring str', True)
00164
00165
00166 try:
00167 MsgSpec(['int32', 'int32'], ['intval'], [], 'int32 intval\int32 y')
00168 self.fail("types and names must align")
00169 except: pass
00170
00171
00172 self.failIf(one_field == 1)
00173 self.assert_(one_field != 1)
00174
00175
00176
00177 [repr(x) for x in [empty, one_field, one_header, two_fields, embed_types]]
00178
00179 def test_init(self):
00180 roslib.msgs._initialized = False
00181 roslib.msgs._init()
00182 self.assert_(roslib.msgs._initialized)
00183
00184 roslib.msgs._init()
00185
00186 def test___convert_val(self):
00187 from roslib.msgs import _convert_val, MsgSpecException
00188 self.assertEquals(0., _convert_val('float32', '0.0'))
00189 self.assertEquals(0., _convert_val('float64', '0.0'))
00190
00191 self.assertEquals('fo o', _convert_val('string', ' fo o '))
00192
00193 self.assertEquals(1, _convert_val('byte', '1'))
00194 self.assertEquals(1, _convert_val('char', '1'))
00195 self.assertEquals(1, _convert_val('int8', '1'))
00196 self.assertEquals(12, _convert_val('int16', '12'))
00197 self.assertEquals(-13, _convert_val('int32', '-13'))
00198 self.assertEquals(14, _convert_val('int64', '14'))
00199 self.assertEquals(0, _convert_val('uint8', '0'))
00200 self.assertEquals(18, _convert_val('uint16', '18'))
00201 self.assertEquals(19, _convert_val('uint32', '19'))
00202 self.assertEquals(20, _convert_val('uint64', '20'))
00203
00204 width_fail = [('int8', '129'), ('uint8', '256'),
00205 ('int16', '35536'), ('uint16', '-1'),('uint16', '65536'),
00206 ('int32', '3000000000'),('int32', '-2700000000'),
00207 ('uint32', '-1'),('uint32', '41000000000'),
00208 ('uint64', '-1')]
00209 for t, v in width_fail:
00210 try:
00211 _convert_val(t, v)
00212 self.fail("should have failed width check: %s, %s"%(t, v))
00213 except MsgSpecException: pass
00214 type_fail = [('int32', 'f'), ('float32', 'baz')]
00215 for t, v in type_fail:
00216 try:
00217 _convert_val(t, v)
00218 self.fail("should have failed type check: %s, %s"%(t, v))
00219 except ValueError: pass
00220 try:
00221 _convert_val('foo', '1')
00222 self.fail("should have failed invalid type")
00223 except MsgSpecException: pass
00224
00225 def test_load_package_dependencies(self):
00226
00227 roslib.msgs.reinit()
00228 self.failIf(PKG in roslib.msgs._loaded_packages)
00229 self.failIf('std_msgs' in roslib.msgs._loaded_packages)
00230 roslib.msgs.load_package_dependencies(PKG)
00231
00232 self.assert_('std_msgs' in roslib.msgs._loaded_packages, roslib.msgs._loaded_packages)
00233
00234 self.failIf(PKG in roslib.msgs._loaded_packages)
00235
00236 spec = roslib.msgs.get_registered('std_msgs/String')
00237 self.assert_('data' in spec.names)
00238
00239
00240
00241 roslib.msgs.reinit()
00242 self.failIf(PKG in roslib.msgs._loaded_packages)
00243 self.failIf('std_msgs' in roslib.msgs._loaded_packages)
00244 roslib.msgs.load_package_dependencies(PKG, load_recursive=True)
00245 self.assert_('std_msgs' in roslib.msgs._loaded_packages, roslib.msgs._loaded_packages)
00246
00247 self.failIf(PKG in roslib.msgs._loaded_packages)
00248
00249 spec = roslib.msgs.get_registered('std_msgs/String')
00250 self.assert_('data' in spec.names)
00251
00252 def test_load_package(self):
00253
00254 roslib.msgs.reinit()
00255 self.failIf(PKG in roslib.msgs._loaded_packages)
00256 self.failIf('std_msgs' in roslib.msgs._loaded_packages)
00257 roslib.msgs.load_package(PKG)
00258
00259 self.assert_(PKG in roslib.msgs._loaded_packages)
00260
00261 self.failIf('std_msgs' in roslib.msgs._loaded_packages, roslib.msgs._loaded_packages)
00262
00263 self.failIf(roslib.msgs.is_registered('std_msgs/String'))
00264 spec = roslib.msgs.get_registered('%s/FillSimple'%(PKG))
00265 self.assertEquals(['i32', 'str', 'i32_array', 'b'], spec.names)
00266
00267 def test_list_msg_types(self):
00268 types1 = roslib.msgs.list_msg_types('rosgraph_msgs', False)
00269 types2 = roslib.msgs.list_msg_types('rosgraph_msgs', True)
00270
00271 self.assert_('Clock' in types1, types1)
00272 self.assert_('Log' in types1, types1)
00273
00274 types1 = roslib.msgs.list_msg_types('std_msgs', False)
00275 types2 = roslib.msgs.list_msg_types('std_msgs', True)
00276
00277 self.assert_('Header' in types1, types1)
00278 self.assert_('Header' in types2, types2)
00279 self.assert_('String' in types1, types1)
00280 self.assert_('String' in types2, types2)
00281 self.assert_('Int32' in types1, types1)
00282 self.assert_('Int32' in types2, types2)
00283
00284
00285 types1 = roslib.msgs.list_msg_types(PKG, False)
00286 types2 = roslib.msgs.list_msg_types(PKG, True)
00287
00288 self.assert_('std_msgs/Header' in types2, types2)
00289 self.assert_('std_msgs/Header' not in types1, types1)
00290 self.assert_('std_msgs/Int32' in types2, types2)
00291 self.assert_('std_msgs/Int32' not in types1, types1)
00292
00293
00294 self.assertNotEquals(types1, types2)
00295
00296 def test_msg_file(self):
00297 f = roslib.msgs.msg_file('rosgraph_msgs', 'Log')
00298 self.assert_(os.path.isfile(f))
00299 self.assert_(f.endswith('rosgraph_msgs/msg/Log.msg'))
00300
00301
00302 f = roslib.msgs.msg_file('roslib', 'Fake')
00303 self.failIf(os.path.isfile(f))
00304 self.assert_(f.endswith('roslib/msg/Fake.msg'))
00305
00306 def test_is_valid_msg_type(self):
00307
00308 vals = [
00309
00310 'F', 'f', 'Foo', 'Foo1',
00311 'std_msgs/String',
00312
00313 'Foo[]', 'Foo[1]', 'Foo[10]',
00314 ]
00315 for v in vals:
00316 self.assert_(roslib.msgs.is_valid_msg_type(v), "roslib.msgs.is_valid_msg_type should have returned True for '%s'"%v)
00317
00318
00319 vals = [None, '', '#', '%', 'Foo%', 'Woo Woo',
00320 '/', '/String',
00321 'Foo[f]', 'Foo[1d]', 'Foo[-1]', 'Foo[1:10]', 'Foo[', 'Foo]', 'Foo[]Bar']
00322 for v in vals:
00323 self.failIf(roslib.msgs.is_valid_msg_type(v), "roslib.msgs.is_valid_msg_type should have returned False for '%s'"%v)
00324
00325 def test_is_valid_constant_type(self):
00326 valid = ['int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', \
00327 'uint64', 'float32', 'float64', 'char', 'byte', 'string']
00328 invalid = [
00329 'std_msgs/String', '/', 'String',
00330 'time', 'duration','header',
00331 ]
00332 for v in valid:
00333 self.assert_(roslib.msgs.is_valid_constant_type(v), "roslib.msgs.is_valid_constant_type should have returned True for '%s'"%v)
00334 for v in invalid:
00335 self.failIf(roslib.msgs.is_valid_constant_type(v), "roslib.msgs.is_valid_constant_type should have returned False for '%s'"%v)
00336
00337
00338 if __name__ == '__main__':
00339 rosunit.unitrun(PKG, 'test_msgspec', MsgSpecTest, coverage_packages=['roslib.msgs'])
00340