def test_make_python_safe(): from genpy.generator import make_python_safe from genmsg.msgs import Constant s = MsgSpec(['int32', 'int32', 'int32', 'int32', 'int32', 'int32'], ['ok', 'if', 'self', 'fine', 'self.x', 'self.while'], [Constant('int32', 'if', '1', '1'), Constant('int32', 'okgo', '1', '1')], 'x', 'test_msgs/Foo') s2 = make_python_safe(s) assert s != s2 assert ['ok', 'if_', 'self_', 'fine', 'self.x', 'self.while_'] == s2.names, s2.names assert s2.types == s.types assert [Constant('int32', 'if_', '1', '1') == Constant('int32', 'okgo', '1', '1')], s2.constants assert s2.text == s.text
def test_load_msg_from_string(): # make sure Header -> std_msgs/Header conversion works from genmsg.msgs import Constant from genmsg.msg_loader import load_msg_from_string, MsgContext context = MsgContext.create_default() msgspec = load_msg_from_string(context, "Header header", 'test_pkg/HeaderTest') print(msgspec) assert msgspec.has_header() assert msgspec.types == ['std_msgs/Header'] assert msgspec.names == ['header'] assert msgspec.constants == [] assert msgspec.short_name == 'HeaderTest' assert msgspec.package == 'test_pkg' assert msgspec.full_name == 'test_pkg/HeaderTest' msgspec = load_msg_from_string(context, "int8 c=1\nHeader header\nint64 data", 'test_pkg/HeaderValsTest') assert msgspec.has_header() assert msgspec.types == ['std_msgs/Header', 'int64'] assert msgspec.names == ['header', 'data'] assert msgspec.constants == [Constant('int8', 'c', 1, '1')] assert msgspec.short_name == 'HeaderValsTest' assert msgspec.package == 'test_pkg' assert msgspec.full_name == 'test_pkg/HeaderValsTest' msgspec = load_msg_from_string(context, "string data\nint64 data2", 'test_pkg/ValsTest') assert not msgspec.has_header() assert msgspec.types == ['string', 'int64'] assert msgspec.names == ['data', 'data2'] assert msgspec.constants == [] assert msgspec.short_name == 'ValsTest' assert msgspec.full_name == 'test_pkg/ValsTest'
def test__load_constant_line(): from genmsg.msgs import Constant, InvalidMsgSpec from genmsg.msg_loader import _load_constant_line try: _load_constant_line("int8 field=alpha") assert False, "should have raised" except InvalidMsgSpec: pass try: _load_constant_line("int8 field=") assert False, "should have raised" except InvalidMsgSpec: pass try: _load_constant_line("faketype field=1") assert False, "should have raised" except InvalidMsgSpec: pass c = _load_constant_line("int8 field=1") assert c == Constant('int8', 'field', 1, '1') c = _load_constant_line("string val=hello #world") assert c == Constant('string', 'val', 'hello #world', 'hello #world')
def test_MsgSpec(): def sub_test_MsgSpec(types, names, constants, text, full_name, has_header): m = MsgSpec(types, names, constants, text, full_name) assert m.types == types assert m.names == names assert m.text == text assert has_header == m.has_header() assert m.constants == constants assert list(zip(types, names)) == m.fields() assert m == MsgSpec(types, names, constants, text, full_name) return m from genmsg import MsgSpec, InvalidMsgSpec from genmsg.msgs import Field # don't allow duplicate fields try: MsgSpec(['int32', 'int64'], ['x', 'x'], [], 'int32 x\nint64 x', 'x/DupFields') assert False, "should have raised" except InvalidMsgSpec: pass # don't allow invalid fields try: MsgSpec(['string['], ['x'], [], 'int32 x\nint64 x', 'x/InvalidFields') assert False, "should have raised" except InvalidMsgSpec: pass # allow empty msg empty = sub_test_MsgSpec([], [], [], '', 'x/Nothing', False) assert [] == empty.fields() assert [] == empty.parsed_fields() assert 'x/Nothing' == empty.full_name assert 'x' == empty.package assert 'Nothing' == empty.short_name # one-field one_field = sub_test_MsgSpec(['int32'], ['x'], [], 'int32 x', 'x/OneInt', False) # make sure that equals tests every declared field assert one_field == MsgSpec(['int32'], ['x'], [], 'int32 x', 'x/OneInt') assert one_field != MsgSpec(['uint32'], ['x'], [], 'int32 x', 'x/OneInt') assert one_field != MsgSpec(['int32'], ['y'], [], 'int32 x', 'x/OneInt') assert one_field != MsgSpec(['int32'], ['x'], [], 'uint32 x', 'x/OneInt') assert one_field != MsgSpec(['int32'], ['x'], [], 'int32 x', 'x/OneIntBad') # test against __ne__ as well assert one_field != MsgSpec(['int32'], ['x'], [], 'uint32 x', 'x/OneInt') assert [ Field('x', 'int32') ] == one_field.parsed_fields(), "%s vs %s" % ([Field( 'x', 'int32')], one_field.parsed_fields()) #test str assert "int32 x" == str(one_field).strip() # test variations of multiple fields and headers two_fields = sub_test_MsgSpec(['int32', 'string'], ['x', 'str'], [], 'int32 x\nstring str', 'x/TwoFields', False) assert [Field('x', 'int32'), Field('str', 'string')] == two_fields.parsed_fields() one_header = sub_test_MsgSpec(['std_msgs/Header'], ['header'], [], 'Header header', 'x/OneHeader', True) header_and_fields = sub_test_MsgSpec( ['std_msgs/Header', 'int32', 'string'], ['header', 'x', 'str'], [], 'Header header\nint32 x\nstring str', 'x/HeaderAndFields', True) embed_types = sub_test_MsgSpec( ['std_msgs/Header', 'std_msgs/Int32', 'string'], ['header', 'x', 'str'], [], 'Header header\nstd_msgs/Int32 x\nstring str', 'x/EmbedTypes', True) #test strify assert "int32 x\nstring str" == str(two_fields).strip() # types and names mismatch try: MsgSpec(['int32', 'int32'], ['intval'], [], 'int32 intval\nint32 y', 'x/Mismatch') assert False, "types and names must align" except: pass # test (not) equals against non msgspec assert not (one_field == 1) assert one_field != 1 # test constants from genmsg.msgs import Constant msgspec = MsgSpec(['int32'], ['x'], [Constant('int8', 'c', 1, '1')], 'int8 c=1\nuint32 x', 'x/Constants') assert msgspec.constants == [Constant('int8', 'c', 1, '1')] # tripwire str(msgspec) repr(msgspec) # test that repr doesn't throw an error [repr(x) for x in [empty, one_field, one_header, two_fields, embed_types]]