38 PKG=
'test_roslib_comm' 43 self.failIf(roslib.msgs.is_verbose())
44 roslib.msgs.set_verbose(
True)
45 self.assert_(roslib.msgs.is_verbose())
46 roslib.msgs.set_verbose(
False)
47 self.failIf(roslib.msgs.is_verbose())
51 tests = [(
None,
None), (
'String',
'String'), (
'std_msgs/String',
'std_msgs/String'),
52 (
'String[10]',
'String'), (
'string[10]',
'string'), (
'std_msgs/String[10]',
'std_msgs/String'),
54 for val, res
in tests:
55 self.assertEquals(res, roslib.msgs.base_msg_type(val))
58 from roslib.msgs
import resolve_type
59 for t
in [
'string',
'string[]',
'string[14]',
'int32',
'int32[]']:
60 bt = roslib.msgs.base_msg_type(t)
61 self.assertEquals(t, resolve_type(t, PKG))
63 self.assertEquals(
'foo/string', resolve_type(
'foo/string', PKG))
64 self.assertEquals(
'std_msgs/Header', resolve_type(
'Header',
'roslib'))
65 self.assertEquals(
'std_msgs/Header', resolve_type(
'std_msgs/Header',
'roslib'))
66 self.assertEquals(
'std_msgs/Header', resolve_type(
'Header',
'stereo_msgs'))
67 self.assertEquals(
'std_msgs/String', resolve_type(
'String',
'std_msgs'))
68 self.assertEquals(
'std_msgs/String', resolve_type(
'std_msgs/String',
'std_msgs'))
69 self.assertEquals(
'std_msgs/String', resolve_type(
'std_msgs/String', PKG))
70 self.assertEquals(
'std_msgs/String[]', resolve_type(
'std_msgs/String[]', PKG))
74 (
'a', (
'a',
False,
None)),
75 (
'int8', (
'int8',
False,
None)),
76 (
'std_msgs/String', (
'std_msgs/String',
False,
None)),
77 (
'a[]', (
'a',
True,
None)),
78 (
'int8[]', (
'int8',
True,
None)),
79 (
'std_msgs/String[]', (
'std_msgs/String',
True,
None)),
80 (
'a[1]', (
'a',
True, 1)),
81 (
'int8[1]', (
'int8',
True, 1)),
82 (
'std_msgs/String[1]', (
'std_msgs/String',
True, 1)),
83 (
'a[11]', (
'a',
True, 11)),
84 (
'int8[11]', (
'int8',
True, 11)),
85 (
'std_msgs/String[11]', (
'std_msgs/String',
True, 11)),
87 for val, res
in tests:
88 self.assertEquals(res, roslib.msgs.parse_type(val))
90 fail = [
'a[1][2]',
'a[][]',
'',
None,
'a[',
'a[[1]',
'a[1]]']
93 roslib.msgs.parse_type(f)
94 self.fail(
"should have failed on " +f)
95 except roslib.msgs.MsgSpecException:
pass 100 vals = [random.randint(0, 1000)
for i
in range(0, 3)]
101 type_, name, val = [str(x)
for x
in vals]
102 x = roslib.msgs.Constant(type_, name, val, str(val))
103 self.assertEquals(type_, x.type)
104 self.assertEquals(name, x.name)
105 self.assertEquals(val, x.val)
106 self.assertEquals(roslib.msgs.Constant(type_, name, val, str(val)), x)
108 self.assertNotEquals(1, x)
109 self.assertNotEquals(roslib.msgs.Constant(
'baz', name, val, str(val)), x)
110 self.assertNotEquals(roslib.msgs.Constant(type_,
'foo', val, str(val)), x)
111 self.assertNotEquals(roslib.msgs.Constant(type_, name,
'foo',
'foo'), x)
114 self.assert_(repr(x))
118 roslib.msgs.Constant(
None, name, val, str(val))
121 roslib.msgs.Constant(type_,
None, val, str(val))
124 roslib.msgs.Constant(type_, name,
None,
'None')
127 roslib.msgs.Constant(type_, name, val,
None)
132 self.fail(
'Constant should not allow arbitrary attr assignment')
136 def sub_test_MsgSpec(types, names, constants, text, has_header):
137 m = MsgSpec(types, names, constants, text)
138 self.assertEquals(m.types, types)
139 self.assertEquals(m.names, names)
140 self.assertEquals(m.text, text)
141 self.assertEquals(has_header, m.has_header())
142 self.assertEquals(m.constants, constants)
143 self.assertEquals(list(zip(types, names)), m.fields())
144 self.assertEquals(m, MsgSpec(types, names, constants, text))
147 from roslib.msgs
import MsgSpec
149 empty = sub_test_MsgSpec([], [], [],
'',
False)
150 self.assertEquals([], empty.fields())
153 one_field = sub_test_MsgSpec([
'int32'], [
'x'], [],
'int32 x',
False)
155 self.assertEquals(one_field, MsgSpec([
'int32'], [
'x'], [],
'int32 x'))
156 self.assertNotEquals(one_field, MsgSpec([
'uint32'], [
'x'], [],
'int32 x'))
157 self.assertNotEquals(one_field, MsgSpec([
'int32'], [
'y'], [],
'int32 x'))
158 self.assertNotEquals(one_field, MsgSpec([
'int32'], [
'x'], [],
'uint32 x'))
160 self.assert_(one_field != MsgSpec([
'int32'], [
'x'], [],
'uint32 x'))
162 self.assertEquals(
"int32 x", str(one_field).strip())
165 two_fields = sub_test_MsgSpec([
'int32',
'string'], [
'x',
'str'], [],
'int32 x\nstring str',
False)
166 one_header = sub_test_MsgSpec([
'Header'], [
'header'], [],
'Header header',
True)
167 header_and_fields = sub_test_MsgSpec([
'Header',
'int32',
'string'], [
'header',
'x',
'str'], [],
'Header header\nint32 x\nstring str',
True)
168 embed_types = sub_test_MsgSpec([
'Header',
'std_msgs/Int32',
'string'], [
'header',
'x',
'str'], [],
'Header header\nstd_msgs/Int32 x\nstring str',
True)
170 self.assertEquals(
"int32 x\nstring str", str(two_fields).strip())
174 MsgSpec([
'int32',
'int32'], [
'intval'], [],
'int32 intval\int32 y')
175 self.fail(
"types and names must align")
179 self.failIf(one_field == 1)
180 self.assert_(one_field != 1)
184 [repr(x)
for x
in [empty, one_field, one_header, two_fields, embed_types]]
188 roslib.msgs._initialized =
False 190 self.assert_(roslib.msgs._initialized)
195 from roslib.msgs
import _convert_val, MsgSpecException
196 self.assertEquals(0., _convert_val(
'float32',
'0.0'))
197 self.assertEquals(0., _convert_val(
'float64',
'0.0'))
199 self.assertEquals(
'fo o', _convert_val(
'string',
' fo o '))
201 self.assertEquals(1, _convert_val(
'byte',
'1'))
202 self.assertEquals(1, _convert_val(
'char',
'1'))
203 self.assertEquals(1, _convert_val(
'int8',
'1'))
204 self.assertEquals(12, _convert_val(
'int16',
'12'))
205 self.assertEquals(-13, _convert_val(
'int32',
'-13'))
206 self.assertEquals(14, _convert_val(
'int64',
'14'))
207 self.assertEquals(0, _convert_val(
'uint8',
'0'))
208 self.assertEquals(18, _convert_val(
'uint16',
'18'))
209 self.assertEquals(19, _convert_val(
'uint32',
'19'))
210 self.assertEquals(20, _convert_val(
'uint64',
'20'))
212 width_fail = [(
'int8',
'129'), (
'uint8',
'256'),
213 (
'int16',
'35536'), (
'uint16',
'-1'),(
'uint16',
'65536'),
214 (
'int32',
'3000000000'),(
'int32',
'-2700000000'),
215 (
'uint32',
'-1'),(
'uint32',
'41000000000'),
217 for t, v
in width_fail:
220 self.fail(
"should have failed width check: %s, %s"%(t, v))
221 except MsgSpecException:
pass 222 type_fail = [(
'int32',
'f'), (
'float32',
'baz')]
223 for t, v
in type_fail:
226 self.fail(
"should have failed type check: %s, %s"%(t, v))
227 except ValueError:
pass 229 _convert_val(
'foo',
'1')
230 self.fail(
"should have failed invalid type")
231 except MsgSpecException:
pass 235 f = roslib.msgs.msg_file(
'rosgraph_msgs',
'Log')
236 self.assert_(os.path.isfile(f))
237 self.assert_(f.endswith(
'rosgraph_msgs/msg/Log.msg'))
240 f = roslib.msgs.msg_file(
'roslib',
'Fake')
241 self.failIf(os.path.isfile(f))
242 self.assert_(f.endswith(
'roslib/msg/Fake.msg'))
248 'F',
'f',
'Foo',
'Foo1',
251 'Foo[]',
'Foo[1]',
'Foo[10]',
254 self.assert_(roslib.msgs.is_valid_msg_type(v),
"roslib.msgs.is_valid_msg_type should have returned True for '%s'"%v)
257 vals = [
None,
'',
'#',
'%',
'Foo%',
'Woo Woo',
259 'Foo[f]',
'Foo[1d]',
'Foo[-1]',
'Foo[1:10]',
'Foo[',
'Foo]',
'Foo[]Bar']
261 self.failIf(roslib.msgs.is_valid_msg_type(v),
"roslib.msgs.is_valid_msg_type should have returned False for '%s'"%v)
265 valid = [
'int8',
'uint8',
'int16',
'uint16',
'int32',
'uint32',
'int64', \
266 'uint64',
'float32',
'float64',
'char',
'byte',
'string']
268 'std_msgs/String',
'/',
'String',
269 'time',
'duration',
'header',
272 self.assert_(roslib.msgs.is_valid_constant_type(v),
"roslib.msgs.is_valid_constant_type should have returned True for '%s'"%v)
274 self.failIf(roslib.msgs.is_valid_constant_type(v),
"roslib.msgs.is_valid_constant_type should have returned False for '%s'"%v)
def test___convert_val(self)
def test_resolve_type(self)
def test_is_valid_constant_type(self)
def test_is_valid_msg_type(self)
def test_base_msg_type(self)
def test_parse_type(self)