def test_compute_constructor(): from genpy.generator import compute_constructor msg_context = MsgContext.create_default() msg_context.register('fake_msgs/String', MsgSpec(['string'], ['data'], [], 'string data\n', 'fake_msgs/String')) msg_context.register('fake_msgs/ThreeNums', MsgSpec(['int32', 'int32', 'int32'], ['x', 'y', 'z'], [], 'int32 x\nint32 y\nint32 z\n', 'fake_msgs/ThreeNums')) # builtin specials assert 'genpy.Time()' == compute_constructor(msg_context, 'roslib', 'time') assert 'genpy.Duration()' == compute_constructor(msg_context, 'roslib', 'duration') assert 'std_msgs.msg._Header.Header()' == compute_constructor(msg_context, 'std_msgs', 'Header') assert 'genpy.Time()' == compute_constructor(msg_context, 'std_msgs', 'time') assert 'genpy.Duration()' == compute_constructor(msg_context, 'std_msgs', 'duration') # generic instances # - unregistered type assert compute_constructor(msg_context, 'unknown_msgs', 'unknown_msgs/Foo') is None assert compute_constructor(msg_context, 'unknown_msgs', 'Foo') is None # - wrong context assert compute_constructor(msg_context, 'std_msgs', 'ThreeNums') is None # - registered types assert 'fake_msgs.msg.String()' == compute_constructor(msg_context, 'std_msgs', 'fake_msgs/String') assert 'fake_msgs.msg.String()' == compute_constructor(msg_context, 'fake_msgs', 'fake_msgs/String') assert 'fake_msgs.msg.String()' == compute_constructor(msg_context, 'fake_msgs', 'String') assert 'fake_msgs.msg.ThreeNums()' == compute_constructor(msg_context, 'fake_msgs', 'fake_msgs/ThreeNums') assert 'fake_msgs.msg.ThreeNums()' == compute_constructor(msg_context, 'fake_msgs', 'fake_msgs/ThreeNums') assert 'fake_msgs.msg.ThreeNums()' == compute_constructor(msg_context, 'fake_msgs', 'ThreeNums')
def test_flatten(): from genpy.generator import flatten msg_context = MsgContext.create_default() simple = MsgSpec(['string'], ['data'], [], 'string data\n', 'simple/String') simple2 = MsgSpec(['string', 'int32'], ['data', 'data2'], [], 'string data\nint32 data2\n', 'simpe/Data2') assert simple == flatten(msg_context, simple) assert simple2 == flatten(msg_context, simple2) b1 = MsgSpec(['int8'], ['data'], [], 'X', 'f_msgs/Base') b2 = MsgSpec(['f_msgs/Base'], ['data'], [], 'X', 'f_msgs/Base2') b3 = MsgSpec(['f_msgs/Base2', 'f_msgs/Base2'], ['data3', 'data4'], [], 'X', 'f_msgs/Base3') b4 = MsgSpec(['f_msgs/Base3', 'f_msgs/Base3'], ['dataA', 'dataB'], [], 'X', 'f_msgs/Base4') msg_context.register('f_msgs/Base', b1) msg_context.register('f_msgs/Base2', b2) msg_context.register('f_msgs/Base3', b3) msg_context.register('f_msgs/Base4', b4) assert MsgSpec(['int8'], ['data.data'], [], 'X', 'f_msgs/Base2') == flatten(msg_context, b2) assert MsgSpec(['int8', 'int8'], ['data3.data.data', 'data4.data.data'], [], 'X', 'f_msgs/Base3') == flatten(msg_context, b3) assert MsgSpec(['int8', 'int8', 'int8', 'int8'], [ 'dataA.data3.data.data', 'dataA.data4.data.data', 'dataB.data3.data.data', 'dataB.data4.data.data' ], [], 'X', 'f_msgs/Base4') == flatten(msg_context, b4)
def test_flatten_array_objects(): # make sure array of types don't flatten from genpy.generator import flatten msg_context = MsgContext.create_default() b1 = MsgSpec(['int8'], ['data'], [], 'X', 'f_msgs/Base') b5 = MsgSpec(['f_msgs/Base[]'], ['data'], [], 'X', 'f_msgs/Base5') msg_context.register('f_msgs/Base', b1) msg_context.register('f_msgs/Base5', b5) assert b5 == flatten(msg_context, b5)
def test_get_registered_ex(): import genpy.generator msg_context = MsgContext.create_default() s = MsgSpec(['string'], ['data'], [], 'string data\n', 'tgr_msgs/String') msg_context.register('tgr_msgs/String', s) assert s == genpy.generator.get_registered_ex(msg_context, 'tgr_msgs/String') try: genpy.generator.get_registered_ex(msg_context, 'bad_msgs/String') except genpy.generator.MsgGenerationException: pass
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_compute_import(): import genpy.generator msg_context = MsgContext.create_default() assert [] == genpy.generator.compute_import(msg_context, 'foo', 'bar') assert [] == genpy.generator.compute_import(msg_context, 'foo', 'int32') msg_context.register('ci_msgs/Base', MsgSpec(['int8'], ['data'], [], 'int8 data\n', 'ci_msgs/Base')) msg_context.register('ci2_msgs/Base2', MsgSpec(['ci_msgs/Base'], ['data2'], [], 'ci_msgs/Base data2\n', 'ci2_msgs/Base2')) msg_context.register('ci3_msgs/Base3', MsgSpec(['ci2_msgs/Base2'], ['data3'], [], 'ci2_msgs/Base2 data3\n', 'ci3_msgs/Base3')) msg_context.register('ci4_msgs/Base', MsgSpec(['int8'], ['data'], [], 'int8 data\n', 'ci4_msgs/Base')) msg_context.register('ci4_msgs/Base4', MsgSpec( ['ci2_msgs/Base2', 'ci3_msgs/Base3'], ['data4a', 'data4b'], [], 'ci2_msgs/Base2 data4a\nci3_msgs/Base3 data4b\n', 'ci4_msgs/Base4')) msg_context.register('ci5_msgs/Base', MsgSpec(['time'], ['data'], [], 'time data\n', 'ci5_msgs/Base')) assert ['import ci_msgs.msg'] == genpy.generator.compute_import(msg_context, 'foo', 'ci_msgs/Base') assert ['import ci_msgs.msg'] == genpy.generator.compute_import(msg_context, 'ci_msgs', 'ci_msgs/Base') assert ['import ci2_msgs.msg', 'import ci_msgs.msg'] == genpy.generator.compute_import(msg_context, 'ci2_msgs', 'ci2_msgs/Base2') assert ['import ci2_msgs.msg', 'import ci_msgs.msg'] == genpy.generator.compute_import(msg_context, 'foo', 'ci2_msgs/Base2') assert ['import ci3_msgs.msg', 'import ci2_msgs.msg', 'import ci_msgs.msg'] == genpy.generator.compute_import(msg_context, 'ci3_msgs', 'ci3_msgs/Base3') assert {'import ci4_msgs.msg', 'import ci3_msgs.msg', 'import ci2_msgs.msg', 'import ci_msgs.msg'} == set(genpy.generator.compute_import(msg_context, 'foo', 'ci4_msgs/Base4')) assert {'import ci4_msgs.msg', 'import ci3_msgs.msg', 'import ci2_msgs.msg', 'import ci_msgs.msg'} == set(genpy.generator.compute_import(msg_context, 'ci4_msgs', 'ci4_msgs/Base4')) assert ['import ci4_msgs.msg'] == genpy.generator.compute_import(msg_context, 'foo', 'ci4_msgs/Base') assert ['import ci4_msgs.msg'] == genpy.generator.compute_import(msg_context, 'ci4_msgs', 'ci4_msgs/Base') assert ['import ci4_msgs.msg'] == genpy.generator.compute_import(msg_context, 'ci4_msgs', 'Base') assert ['import ci5_msgs.msg', 'import genpy'] == genpy.generator.compute_import(msg_context, 'foo', 'ci5_msgs/Base')
def test_default_value(): from genpy.generator import default_value msg_context = MsgContext.create_default() msg_context.register( 'fake_msgs/String', MsgSpec(['string'], ['data'], [], 'string data\n', 'fake_msgs/String')) msg_context.register( 'fake_msgs/ThreeNums', MsgSpec(['int32', 'int32', 'int32'], ['x', 'y', 'z'], [], 'int32 x\nint32 y\nint32 z\n', 'fake_msgs/ThreeNums')) # trip-wire: make sure all builtins have a default value for t in genmsg.msgs.BUILTIN_TYPES: assert type(default_value(msg_context, t, 'roslib')) == str # simple types first for t in [ 'uint8', 'int8', 'uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64', 'byte', 'char' ]: assert '0' == default_value(msg_context, t, 'std_msgs') assert '0' == default_value(msg_context, t, 'roslib') for t in ['float32', 'float64']: assert '0.' == default_value(msg_context, t, 'std_msgs') assert '0.' == default_value(msg_context, t, 'roslib') assert "''" == default_value(msg_context, 'string', 'roslib') # builtin specials assert 'genpy.Time()' == default_value(msg_context, 'time', 'roslib') assert 'genpy.Duration()' == default_value(msg_context, 'duration', 'roslib') assert 'std_msgs.msg._Header.Header()' == default_value( msg_context, 'Header', 'roslib') assert 'genpy.Time()' == default_value(msg_context, 'time', 'std_msgs') assert 'genpy.Duration()' == default_value(msg_context, 'duration', 'std_msgs') assert 'std_msgs.msg._Header.Header()' == default_value( msg_context, 'Header', 'std_msgs') # generic instances # - unregistered type assert default_value(msg_context, 'unknown_msgs/Foo', 'unknown_msgs') is None # - wrong context assert default_value(msg_context, 'ThreeNums', 'std_msgs') is None # - registered types assert 'fake_msgs.msg.String()' == default_value(msg_context, 'fake_msgs/String', 'std_msgs') assert 'fake_msgs.msg.String()' == default_value(msg_context, 'fake_msgs/String', 'fake_msgs') assert 'fake_msgs.msg.String()' == default_value(msg_context, 'String', 'fake_msgs') assert 'fake_msgs.msg.ThreeNums()' == default_value( msg_context, 'fake_msgs/ThreeNums', 'roslib') assert 'fake_msgs.msg.ThreeNums()' == default_value( msg_context, 'fake_msgs/ThreeNums', 'fake_msgs') assert 'fake_msgs.msg.ThreeNums()' == default_value( msg_context, 'ThreeNums', 'fake_msgs') # var-length arrays always default to empty arrays... except for byte and uint8 which are strings for t in [ 'int8', 'uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64', 'float32', 'float64' ]: val = default_value(msg_context, t + '[]', 'std_msgs') assert '[]' == val, '[%s]: %s' % (t, val) assert '[]' == default_value(msg_context, t + '[]', 'roslib') assert b'' == eval(default_value(msg_context, 'uint8[]', 'roslib')) # fixed-length arrays should be zero-filled... except for byte and uint8 which are strings for t in ['float32', 'float64']: assert '[0.] * 3' == default_value(msg_context, t + '[3]', 'std_msgs') assert '[0.] * 1' == default_value(msg_context, t + '[1]', 'std_msgs') for t in ['int8', 'uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64']: assert '[0] * 4' == default_value(msg_context, t + '[4]', 'std_msgs') assert '[0] * 1' == default_value(msg_context, t + '[1]', 'roslib') assert b'\0' == eval(default_value(msg_context, 'uint8[1]', 'roslib')) assert b'\0\0\0\0' == eval(default_value(msg_context, 'uint8[4]', 'roslib')) assert '[]' == default_value(msg_context, 'fake_msgs/String[]', 'std_msgs') assert '[fake_msgs.msg.String() for _ in range(2)]' == default_value( msg_context, 'fake_msgs/String[2]', 'std_msgs')