Esempio n. 1
0
def test_load_msg_depends_stamped():
    # TODO: should there just be a 'load_msg, implicit=True?'
    from genmsg.msg_loader import MsgContext, load_msg_by_type, load_msg_depends

    test_d = get_test_dir()
    geometry_d = os.path.join(test_d, "geometry_msgs", "msg")
    search_path = {
        "test_ros": os.path.join(test_d, "test_ros", "msg"),
        "std_msgs": os.path.join(test_d, "std_msgs", "msg"),
        "geometry_msgs": geometry_d,
        "sensor_msgs": os.path.join(test_d, "sensor_msgs", "msg"),
    }

    # Test with Stamped and deeper hierarchies, Header

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, "geometry_msgs/PoseStamped", search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, "geometry_msgs", "msg", "PoseStamped.msg")
    assert file_p == msg_context.get_file("geometry_msgs/PoseStamped")
    val = msg_context.get_depends("geometry_msgs/PoseStamped")
    assert set(["std_msgs/Header", "geometry_msgs/Pose", "geometry_msgs/Point", "geometry_msgs/Quaternion"]) == set(
        val
    ), val
    for s in ["Header"]:
        file_p = os.path.join(test_d, "std_msgs", "msg", "%s.msg" % s)
        assert file_p == msg_context.get_file("std_msgs/%s" % s)
    for s in ["Pose", "Point", "Quaternion"]:
        file_p = os.path.join(geometry_d, "%s.msg" % s)
        assert file_p == msg_context.get_file("geometry_msgs/%s" % s)

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, "geometry_msgs/TwistWithCovarianceStamped", search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, "geometry_msgs", "msg", "TwistWithCovarianceStamped.msg")
    assert file_p == msg_context.get_file("geometry_msgs/TwistWithCovarianceStamped")
    val = msg_context.get_depends("geometry_msgs/TwistWithCovarianceStamped")
    assert set(
        ["std_msgs/Header", "geometry_msgs/TwistWithCovariance", "geometry_msgs/Twist", "geometry_msgs/Vector3"]
    ) == set(val), val
    for s in ["Header"]:
        file_p = os.path.join(test_d, "std_msgs", "msg", "%s.msg" % s)
        assert file_p == msg_context.get_file("std_msgs/%s" % s)
    for s in ["TwistWithCovariance", "Twist", "Vector3"]:
        file_p = os.path.join(geometry_d, "%s.msg" % s)
        assert file_p == msg_context.get_file("geometry_msgs/%s" % s)

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, "sensor_msgs/Imu", search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, "sensor_msgs", "msg", "Imu.msg")
    assert file_p == msg_context.get_file("sensor_msgs/Imu")
    val = msg_context.get_depends("sensor_msgs/Imu")
    assert set(["std_msgs/Header", "geometry_msgs/Quaternion", "geometry_msgs/Vector3"]) == set(val), val
    for s in ["Header"]:
        file_p = os.path.join(test_d, "std_msgs", "msg", "%s.msg" % s)
        assert file_p == msg_context.get_file("std_msgs/%s" % s)
    for s in ["Quaternion", "Vector3"]:
        file_p = os.path.join(geometry_d, "%s.msg" % s)
        assert file_p == msg_context.get_file("geometry_msgs/%s" % s)
Esempio n. 2
0
def test_load_msg_depends():
    # TODO: should there just be a 'load_msg, implicit=True?'
    from genmsg.msg_loader import MsgContext, load_msg_by_type, load_msg_depends, MsgNotFound

    test_d = get_test_dir()
    search_path = {
        "test_ros": os.path.join(test_d, "test_ros", "msg"),
        "std_msgs": os.path.join(test_d, "std_msgs", "msg"),
        "geometry_msgs": os.path.join(test_d, "geometry_msgs", "msg"),
        "sensor_msgs": os.path.join(test_d, "sensor_msgs", "msg"),
        "invalid": os.path.join(test_d, "invalid", "msg"),
    }

    # Test not found
    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, "invalid/BadDepend", search_path)
    try:
        load_msg_depends(msg_context, root_spec, search_path)
        assert False, "should have raised MsgNotFound"
    except MsgNotFound:
        pass
    root_spec = load_msg_by_type(msg_context, "invalid/BadLocalDepend", search_path)
    try:
        load_msg_depends(msg_context, root_spec, search_path)
        assert False, "should have raised MsgNotFound"
    except MsgNotFound:
        pass

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, "std_msgs/Int32", search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, "std_msgs", "msg", "Int32.msg")
    assert file_p == msg_context.get_file("std_msgs/Int32")
    assert [] == msg_context.get_depends("std_msgs/Int32")

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, "std_msgs/Header", search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, "std_msgs", "msg", "Header.msg")
    assert file_p == msg_context.get_file("std_msgs/Header")
    assert [] == msg_context.get_depends("std_msgs/Header")

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, "Header", search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, "std_msgs", "msg", "Header.msg")
    assert file_p == msg_context.get_file("std_msgs/Header")
    assert [] == msg_context.get_depends("std_msgs/Header")

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, "std_msgs/Int32MultiArray", search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, "std_msgs", "msg", "Int32MultiArray.msg")
    assert file_p == msg_context.get_file("std_msgs/Int32MultiArray")
    val = msg_context.get_depends("std_msgs/Int32MultiArray")
    assert 2 == len(val)
    assert set(["std_msgs/MultiArrayLayout", "std_msgs/MultiArrayDimension"]) == set(val), val
    for s in ["MultiArrayLayout", "MultiArrayDimension"]:
        file_p = os.path.join(test_d, "std_msgs", "msg", "%s.msg" % s)
        assert file_p == msg_context.get_file("std_msgs/%s" % s)
def test_load_depends_msg():
    from genmsg.msg_loader import MsgContext, load_msg_by_type, load_depends, MsgNotFound, load_srv_by_type
    test_d = get_test_dir()
    geometry_d = os.path.join(test_d, 'geometry_msgs', 'msg')
    msg_search_path = {
        'test_ros': [ os.path.join(test_d, 'test_ros', 'msg') ],
        'std_msgs': [ os.path.join(test_d, 'std_msgs', 'msg') ],
        'geometry_msgs': [ geometry_d ],
        'sensor_msgs': [ os.path.join(test_d, 'sensor_msgs', 'msg') ],
        'invalid': [ os.path.join(test_d, 'invalid', 'msg') ],
        }

    # Test not found
    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'invalid/BadDepend', msg_search_path)
    try:
        load_depends(msg_context, root_spec, msg_search_path)
        assert False, "should have raised MsgNotFound"
    except MsgNotFound:
        pass
    root_spec = load_msg_by_type(msg_context, 'invalid/BadLocalDepend', msg_search_path)
    try:
        load_depends(msg_context, root_spec, msg_search_path)
        assert False, "should have raised MsgNotFound"
    except MsgNotFound:
        pass
    
    # Test with msgs
    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'geometry_msgs/PoseStamped', msg_search_path)
    load_depends(msg_context, root_spec, msg_search_path)
    file_p = os.path.join(test_d, 'geometry_msgs', 'msg', 'PoseStamped.msg')
    assert file_p == msg_context.get_file('geometry_msgs/PoseStamped')
    val = msg_context.get_all_depends('geometry_msgs/PoseStamped')
    assert set(['std_msgs/Header', 'geometry_msgs/Pose', 'geometry_msgs/Point', 'geometry_msgs/Quaternion']) == set(val), val
    val = msg_context.get_depends('geometry_msgs/PoseStamped')
    assert set(['std_msgs/Header', 'geometry_msgs/Pose']) == set(val), val

    for s in ['Header']:
        file_p = os.path.join(test_d, 'std_msgs', 'msg', '%s.msg'%s)
        assert file_p == msg_context.get_file('std_msgs/%s'%s)
    for s in ['Pose', 'Point', 'Quaternion']:
        file_p = os.path.join(geometry_d, '%s.msg'%s)
        assert file_p == msg_context.get_file('geometry_msgs/%s'%s)

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'sensor_msgs/Imu', msg_search_path)
    load_depends(msg_context, root_spec, msg_search_path)
    file_p = os.path.join(test_d, 'sensor_msgs', 'msg', 'Imu.msg')
    assert file_p == msg_context.get_file('sensor_msgs/Imu')
    val = msg_context.get_depends('sensor_msgs/Imu')
    assert set(['std_msgs/Header', 'geometry_msgs/Quaternion', 'geometry_msgs/Vector3']) == set(val), val
    for s in ['Header']:
        file_p = os.path.join(test_d, 'std_msgs', 'msg', '%s.msg'%s)
        assert file_p == msg_context.get_file('std_msgs/%s'%s)
    for s in ['Quaternion', 'Vector3']:
        file_p = os.path.join(geometry_d, '%s.msg'%s)
        assert file_p == msg_context.get_file('geometry_msgs/%s'%s)
Esempio n. 4
0
def test_load_depends_msg():
    from genmsg.msg_loader import MsgContext, load_msg_by_type, load_depends, MsgNotFound, load_srv_by_type

    test_d = get_test_dir()
    geometry_d = os.path.join(test_d, "geometry_msgs", "msg")
    msg_search_path = {
        "test_ros": os.path.join(test_d, "test_ros", "msg"),
        "std_msgs": os.path.join(test_d, "std_msgs", "msg"),
        "geometry_msgs": geometry_d,
        "sensor_msgs": os.path.join(test_d, "sensor_msgs", "msg"),
        "invalid": os.path.join(test_d, "invalid", "msg"),
    }

    # Test not found
    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, "invalid/BadDepend", msg_search_path)
    try:
        load_depends(msg_context, root_spec, msg_search_path)
        assert False, "should have raised MsgNotFound"
    except MsgNotFound:
        pass
    root_spec = load_msg_by_type(msg_context, "invalid/BadLocalDepend", msg_search_path)
    try:
        load_depends(msg_context, root_spec, msg_search_path)
        assert False, "should have raised MsgNotFound"
    except MsgNotFound:
        pass

    # Test with msgs
    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, "geometry_msgs/PoseStamped", msg_search_path)
    load_depends(msg_context, root_spec, msg_search_path)
    file_p = os.path.join(test_d, "geometry_msgs", "msg", "PoseStamped.msg")
    assert file_p == msg_context.get_file("geometry_msgs/PoseStamped")
    val = msg_context.get_depends("geometry_msgs/PoseStamped")
    assert set(["std_msgs/Header", "geometry_msgs/Pose", "geometry_msgs/Point", "geometry_msgs/Quaternion"]) == set(
        val
    ), val
    for s in ["Header"]:
        file_p = os.path.join(test_d, "std_msgs", "msg", "%s.msg" % s)
        assert file_p == msg_context.get_file("std_msgs/%s" % s)
    for s in ["Pose", "Point", "Quaternion"]:
        file_p = os.path.join(geometry_d, "%s.msg" % s)
        assert file_p == msg_context.get_file("geometry_msgs/%s" % s)

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, "sensor_msgs/Imu", msg_search_path)
    load_depends(msg_context, root_spec, msg_search_path)
    file_p = os.path.join(test_d, "sensor_msgs", "msg", "Imu.msg")
    assert file_p == msg_context.get_file("sensor_msgs/Imu")
    val = msg_context.get_depends("sensor_msgs/Imu")
    assert set(["std_msgs/Header", "geometry_msgs/Quaternion", "geometry_msgs/Vector3"]) == set(val), val
    for s in ["Header"]:
        file_p = os.path.join(test_d, "std_msgs", "msg", "%s.msg" % s)
        assert file_p == msg_context.get_file("std_msgs/%s" % s)
    for s in ["Quaternion", "Vector3"]:
        file_p = os.path.join(geometry_d, "%s.msg" % s)
        assert file_p == msg_context.get_file("geometry_msgs/%s" % s)
Esempio n. 5
0
def test_load_depends_srv():
    from genmsg.msg_loader import MsgContext, load_msg_by_type, load_depends, MsgNotFound, load_srv_by_type

    test_d = get_test_dir()
    geometry_d = os.path.join(test_d, "geometry_msgs", "msg")
    msg_search_path = {
        "test_ros": [os.path.join(test_d, "test_ros", "msg")],
        "std_msgs": [os.path.join(test_d, "std_msgs", "msg")],
        "geometry_msgs": [geometry_d],
        "sensor_msgs": [os.path.join(test_d, "sensor_msgs", "msg")],
        "invalid": [os.path.join(test_d, "invalid", "msg")],
    }

    # Test with srvs
    srv_search_path = {
        "test_ros": [os.path.join(test_d, "test_ros", "srv")],
        "std_srvs": [os.path.join(test_d, "std_srvs", "srv")],
    }

    msg_context = MsgContext.create_default()
    root_spec = load_srv_by_type(msg_context, "test_ros/AddTwoInts", srv_search_path)
    load_depends(msg_context, root_spec, msg_search_path)
    val = msg_context.get_depends("test_ros/AddTwoIntsRequest")
    assert val == [], val
    val = msg_context.get_depends("test_ros/AddTwoIntsResponse")
    assert val == [], val

    # test with srv that has depends
    msg_context = MsgContext.create_default()
    response_deps = [
        "std_msgs/Header",
        "geometry_msgs/Pose",
        "geometry_msgs/PoseStamped",
        "geometry_msgs/Point",
        "geometry_msgs/Quaternion",
    ]
    root_spec = load_srv_by_type(msg_context, "test_ros/GetPoseStamped", srv_search_path)
    load_depends(msg_context, root_spec, msg_search_path)
    for d in response_deps:
        assert msg_context.is_registered(d)
    val = msg_context.get_depends("test_ros/GetPoseStampedRequest")
    assert val == [], val
    val = msg_context.get_depends("test_ros/GetPoseStampedResponse")
    assert val == ["geometry_msgs/PoseStamped"]

    # Test with nonsense
    class Foo(object):
        pass

    try:
        load_depends(msg_context, Foo(), msg_search_path)
        assert False, "should have raised"
    except ValueError:
        pass
Esempio n. 6
0
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 set(['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 set(['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')
Esempio n. 7
0
def test_flatten():
    import genpy.generator
    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_load_msg_from_file():
    from genmsg.msgs import InvalidMsgSpec
    from genmsg.msg_loader import load_msg_from_file, MsgContext
    test_d = get_test_dir()
    test_ros_dir = os.path.join(test_d, 'test_ros', 'msg')
    test_string_path = os.path.join(test_ros_dir, 'TestString.msg')

    msg_context = MsgContext.create_default()
    spec = load_msg_from_file(msg_context, test_string_path, 'test_ros/TestString')
    assert spec.full_name == 'test_ros/TestString'
    assert spec.package == 'test_ros'
    assert spec.short_name == 'TestString'
    _validate_TestString(spec)

    # test repeat
    spec_2 = load_msg_from_file(msg_context, test_string_path, 'test_ros/TestString')
    assert spec == spec_2
    assert spec.package == spec_2.package
    assert spec.short_name == spec_2.short_name
    
    # test w/ bad file
    test_bad_path = os.path.join(test_ros_dir, 'Bad.msg')
    try:
        load_msg_from_file(msg_context, test_bad_path, 'test_ros/Bad')
        assert False, "should have raised"
    except InvalidMsgSpec:
        pass
    
    # supposed to register
    assert msg_context.is_registered('test_ros/TestString'), msg_context
Esempio n. 9
0
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 None == compute_constructor(msg_context, "unknown_msgs", "unknown_msgs/Foo")
    assert None == compute_constructor(msg_context, "unknown_msgs", "Foo")
    # - wrong context
    assert None == compute_constructor(msg_context, 'std_msgs', 'ThreeNums')

    # - 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_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'
Esempio n. 11
0
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"
Esempio n. 12
0
def test_array_serializer_generator_numpy():
    is_numpy = True
    from genpy.generator import array_serializer_generator
    d = os.path.join(get_test_dir(), 'array')
    # generator tests are mainly tripwires/coverage tests

    #array_serializer_generator(msg_context, package, type_, name, serialize, is_numpy):
    msg_context = MsgContext.create_default()

    # permutations: var length, unint8
    serialize = True
    result = array_serializer_generator(msg_context, '', 'uint8[]', 'data', serialize, is_numpy)
    compare_file(d, 'uint8_varlen_ser_np.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[]', 'data', serialize, is_numpy)
    compare_file(d, 'int16_varlen_ser_np.txt', result)
    result = array_serializer_generator(msg_context, '', 'uint8[8]', 'data', serialize, is_numpy)
    compare_file(d, 'uint8_fixed_ser_np.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[10]', 'data', serialize, is_numpy)
    compare_file(d, 'int16_fixed_ser_np.txt', result)
    
    serialize = False
    result = array_serializer_generator(msg_context, '', 'uint8[]', 'data', serialize, is_numpy)
    compare_file(d, 'uint8_varlen_deser_np.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[]', 'data', serialize, is_numpy)
    compare_file(d, 'int16_varlen_deser_np.txt', result)
    result = array_serializer_generator(msg_context, '', 'uint8[8]', 'data', serialize, is_numpy)
    compare_file(d, 'uint8_fixed_deser_np.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[10]', 'data', serialize, is_numpy)
    compare_file(d, 'int16_fixed_deser_np.txt', result)
def test_load_depends_srv():
    from genmsg.msg_loader import MsgContext, load_msg_by_type, load_depends, MsgNotFound, load_srv_by_type
    test_d = get_test_dir()
    geometry_d = os.path.join(test_d, 'geometry_msgs', 'msg')
    msg_search_path = {
        'test_ros': [ os.path.join(test_d, 'test_ros', 'msg') ],
        'std_msgs': [ os.path.join(test_d, 'std_msgs', 'msg') ],
        'geometry_msgs': [ geometry_d ],
        'sensor_msgs': [ os.path.join(test_d, 'sensor_msgs', 'msg') ],
        'invalid': [ os.path.join(test_d, 'invalid', 'msg') ],
        }

    # Test with srvs
    srv_search_path = {
        'test_ros': [ os.path.join(test_d, 'test_ros', 'srv') ],
        'std_srvs': [ os.path.join(test_d, 'std_srvs', 'srv') ],
        }

    msg_context = MsgContext.create_default()
    root_spec = load_srv_by_type(msg_context, 'test_ros/AddTwoInts', srv_search_path)
    load_depends(msg_context, root_spec, msg_search_path)
    val = msg_context.get_depends('test_ros/AddTwoIntsRequest')
    assert val == [], val
    val = msg_context.get_depends('test_ros/AddTwoIntsResponse')    
    assert val == [], val

    # test with srv that has depends
    msg_context = MsgContext.create_default()
    response_deps = ['std_msgs/Header', 'geometry_msgs/Pose', 'geometry_msgs/PoseStamped', 'geometry_msgs/Point', 'geometry_msgs/Quaternion']
    root_spec = load_srv_by_type(msg_context, 'test_ros/GetPoseStamped', srv_search_path)
    load_depends(msg_context, root_spec, msg_search_path)
    for d in response_deps:
        assert msg_context.is_registered(d)
    val = msg_context.get_depends('test_ros/GetPoseStampedRequest')
    assert val == [], val
    val = msg_context.get_depends('test_ros/GetPoseStampedResponse')    
    assert val == ['geometry_msgs/PoseStamped']
    
    # Test with nonsense
    class Foo(object): pass
    try:
        load_depends(msg_context, Foo(), msg_search_path)
        assert False, "should have raised"
    except ValueError:
        pass
Esempio n. 14
0
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
Esempio n. 15
0
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_load_msg_from_string_TestString():
    from genmsg.msg_loader import load_msg_from_string, MsgContext

    test_d = get_test_dir()
    test_ros_dir = os.path.join(test_d, 'test_ros', 'msg')
    test_string_path = os.path.join(test_ros_dir, 'TestString.msg')
    with open(test_string_path) as f:
        text = f.read()

    msg_context = MsgContext.create_default()
    _validate_TestString(load_msg_from_string(msg_context, text, 'test_ros/TestString'))
    # supposed to register
    assert msg_context.is_registered('test_ros/TestString'), msg_context
def test_load_srv_by_type():
    from genmsg.msg_loader import load_srv_by_type, MsgContext, MsgNotFound
    
    test_d = get_test_dir()
    test_ros_dir = os.path.join(test_d, 'test_ros', 'srv')
    std_srvs_dir = os.path.join(test_d, 'std_srvs', 'srv')
    empty_path = os.path.join(std_srvs_dir, 'Empty.srv')
    a2i_path = os.path.join(std_srvs_dir, 'AddTwoInts.srv')
    search_path = {
        'test_ros': [ test_ros_dir ],
        'std_srvs': [ std_srvs_dir ],
        }
    msg_context = MsgContext.create_default()
    spec = load_srv_by_type(msg_context, 'std_srvs/Empty', search_path)
    assert msg_context.is_registered('std_srvs/EmptyRequest')
    assert msg_context.is_registered('std_srvs/EmptyResponse')
    assert msg_context.get_registered('std_srvs/EmptyRequest') == spec.request
    assert msg_context.get_registered('std_srvs/EmptyResponse') == spec.response
    assert msg_context.get_file('std_srvs/EmptyRequest') == empty_path, msg_context.get_file('std_srvs/EmptyRequest')
    assert msg_context.get_file('std_srvs/EmptyResponse') == empty_path,msg_context.get_file('std_srvs/EmptyResponse')
    assert spec.request.full_name == 'std_srvs/EmptyRequest'
    assert spec.response.full_name == 'std_srvs/EmptyResponse'
    assert spec.request.short_name == 'EmptyRequest'
    assert spec.response.short_name == 'EmptyResponse'
    assert spec.request.package == 'std_srvs'
    assert spec.response.package == 'std_srvs'
    for f in [spec.request.names, spec.request.types, spec.response.names, spec.response.types]:
        assert [] == f

    spec = load_srv_by_type(msg_context, 'test_ros/AddTwoInts', search_path)
    assert msg_context.is_registered('test_ros/AddTwoIntsRequest')
    assert msg_context.is_registered('test_ros/AddTwoIntsResponse')
    assert msg_context.get_registered('test_ros/AddTwoIntsRequest') == spec.request
    assert msg_context.get_registered('test_ros/AddTwoIntsResponse') == spec.response
    assert spec.request.types == ['int64', 'int64'], spec.request.types
    assert spec.request.names == ['a', 'b'], spec.request.names
    assert spec.response.types == ['int64'], spec.response.types
    assert spec.response.names == ['sum'], spec.response.names

    # test invalid search path
    try:
        load_srv_by_type(msg_context, 'test_ros/AddTwoInts', [std_srvs_dir])
        assert False, "should have raised"
    except ValueError:
        pass
    # test not found
    try:
        load_srv_by_type(msg_context, 'test_ros/Fake', search_path)
        assert False, "should have raised"
    except MsgNotFound:
        pass
Esempio n. 18
0
def test_load_msg_from_string_TestString():
    from genmsg.msg_loader import load_msg_from_string, MsgContext

    test_d = get_test_dir()
    test_ros_dir = os.path.join(test_d, 'test_ros', 'msg')
    test_string_path = os.path.join(test_ros_dir, 'TestString.msg')
    with open(test_string_path) as f:
        text = f.read()

    msg_context = MsgContext.create_default()
    _validate_TestString(
        load_msg_from_string(msg_context, text, 'test_ros/TestString'))
    # supposed to register
    assert msg_context.is_registered('test_ros/TestString'), msg_context
Esempio n. 19
0
def test__group_fields5():
    from genfrkl import group_fields
    from genmsg.msg_loader import load_msg_from_string, MsgContext

    context = MsgContext.create_default()
    msg_txt = """
      uint32[4] a
      uint16[5] b
    """
    msgspec = load_msg_from_string(context, msg_txt, 'my_pkg/FieldGroupTest')
    groups = group_fields(msgspec)

    assert len(groups) == 2, "Expected 2 groups"
    assert len(groups[0]) == 1, "First group should have only one element"
    assert len(groups[1]) == 1, "Second group should have only one element"
Esempio n. 20
0
def test_load_srv_by_type():
    from genmsg.msg_loader import load_srv_by_type, MsgContext, MsgNotFound

    test_d = get_test_dir()
    test_ros_dir = os.path.join(test_d, "test_ros", "srv")
    std_srvs_dir = os.path.join(test_d, "std_srvs", "srv")
    empty_path = os.path.join(std_srvs_dir, "Empty.srv")
    a2i_path = os.path.join(std_srvs_dir, "AddTwoInts.srv")
    search_path = {"test_ros": [test_ros_dir], "std_srvs": [std_srvs_dir]}
    msg_context = MsgContext.create_default()
    spec = load_srv_by_type(msg_context, "std_srvs/Empty", search_path)
    assert msg_context.is_registered("std_srvs/EmptyRequest")
    assert msg_context.is_registered("std_srvs/EmptyResponse")
    assert msg_context.get_registered("std_srvs/EmptyRequest") == spec.request
    assert msg_context.get_registered("std_srvs/EmptyResponse") == spec.response
    assert msg_context.get_file("std_srvs/EmptyRequest") == empty_path, msg_context.get_file("std_srvs/EmptyRequest")
    assert msg_context.get_file("std_srvs/EmptyResponse") == empty_path, msg_context.get_file("std_srvs/EmptyResponse")
    assert spec.request.full_name == "std_srvs/EmptyRequest"
    assert spec.response.full_name == "std_srvs/EmptyResponse"
    assert spec.request.short_name == "EmptyRequest"
    assert spec.response.short_name == "EmptyResponse"
    assert spec.request.package == "std_srvs"
    assert spec.response.package == "std_srvs"
    for f in [spec.request.names, spec.request.types, spec.response.names, spec.response.types]:
        assert [] == f

    spec = load_srv_by_type(msg_context, "test_ros/AddTwoInts", search_path)
    assert msg_context.is_registered("test_ros/AddTwoIntsRequest")
    assert msg_context.is_registered("test_ros/AddTwoIntsResponse")
    assert msg_context.get_registered("test_ros/AddTwoIntsRequest") == spec.request
    assert msg_context.get_registered("test_ros/AddTwoIntsResponse") == spec.response
    assert spec.request.types == ["int64", "int64"], spec.request.types
    assert spec.request.names == ["a", "b"], spec.request.names
    assert spec.response.types == ["int64"], spec.response.types
    assert spec.response.names == ["sum"], spec.response.names

    # test invalid search path
    try:
        load_srv_by_type(msg_context, "test_ros/AddTwoInts", [std_srvs_dir])
        assert False, "should have raised"
    except ValueError:
        pass
    # test not found
    try:
        load_srv_by_type(msg_context, "test_ros/Fake", search_path)
        assert False, "should have raised"
    except MsgNotFound:
        pass
Esempio n. 21
0
def test_load_msg_by_type():
    from genmsg.msg_loader import load_msg_by_type, MsgContext, MsgNotFound

    test_d = get_test_dir()
    geometry_d = os.path.join(test_d, 'geometry_msgs', 'msg')

    test_ros_dir = os.path.join(test_d, 'test_ros', 'msg')
    test_string_path = os.path.join(test_ros_dir, 'TestString.msg')
    search_path = {
        'test_ros': [test_ros_dir],
        'geometry_msgs': [geometry_d],
    }
    msg_context = MsgContext.create_default()
    msgspec = load_msg_by_type(msg_context, 'test_ros/TestString', search_path)
    _validate_TestString(msgspec)
    # supposed to register
    assert msg_context.is_registered('test_ros/TestString'), msg_context

    # test invalid search path
    try:
        load_msg_by_type(msg_context, 'test_ros/TestString',
                         [test_string_path])
        assert False, "should have raised"
    except ValueError:
        pass
    # test not found
    try:
        load_msg_by_type(msg_context, 'test_ros/Fake', search_path)
        assert False, "should have raised"
    except MsgNotFound:
        pass

    # test all the known geometry msgs
    test_d = get_test_dir()
    for f in os.listdir(geometry_d):
        if f.endswith('.msg'):
            short = f[:-4]
            msg_type = 'geometry_msgs/%s' % short
            spec = load_msg_by_type(msg_context, msg_type, search_path)
            assert spec is not None
            assert spec.package == 'geometry_msgs'
            assert spec.full_name == msg_type
            assert spec.short_name == short
            with open(os.path.join(geometry_d, f)) as file_h:
                assert spec.text == file_h.read()
            # all types with 'Stamped' in name have headers
            if 'Stamped' in f:
                assert spec.has_header(), msg_type
Esempio n. 22
0
def test__group_fields5():
    from genfrkl import group_fields
    from genmsg.msg_loader import load_msg_from_string, MsgContext

    context = MsgContext.create_default()
    msg_txt = """
      uint8 a
      uint16 b
      uint32 c
      uint32[3] d
      Int64 e
    """
    msgspec = load_msg_from_string(context, msg_txt, 'my_pkg/FieldGroupTest')
    groups = group_fields(msgspec)

    assert len(groups) == 3, "Expected 3 groups"
Esempio n. 23
0
def test_complex_serializer_generator():
    from genmsg.msg_loader import load_msg_by_type
    from genpy.generator import complex_serializer_generator, MsgGenerationException, reset_var
    array_d = os.path.join(get_test_dir(), 'array')
    complex_d = os.path.join(get_test_dir(), 'complex')
    # generator tests are mainly tripwires/coverage tests

    # array_serializer_generator(msg_context, package, type_, name, serialize, is_numpy):
    msg_context = MsgContext.create_default()
    # load in some objects
    search_path = {'foo': [array_d]}
    load_msg_by_type(msg_context, 'foo/Object', search_path)
    load_msg_by_type(msg_context, 'foo/ObjectArray', search_path)

    serialize = True
    is_numpy = False
    reset_var()
    result = complex_serializer_generator(msg_context, 'foo', 'foo/Object',
                                          'data', serialize, is_numpy)
    compare_file(complex_d, 'object_ser.txt', result)
    reset_var()
    result = complex_serializer_generator(msg_context, 'foo', 'foo/Object[]',
                                          'data', serialize, is_numpy)
    compare_file(array_d, 'object_varlen_ser.txt', result)
    reset_var()
    result = complex_serializer_generator(msg_context, 'foo', 'foo/Object[3]',
                                          'data', serialize, is_numpy)
    compare_file(array_d, 'object_fixed_ser.txt', result)

    serialize = False

    reset_var()
    result = complex_serializer_generator(msg_context, 'foo', 'foo/Object[]',
                                          'data', serialize, is_numpy)
    compare_file(array_d, 'object_varlen_deser.txt', result)
    reset_var()
    result = complex_serializer_generator(msg_context, 'foo', 'foo/Object[3]',
                                          'data', serialize, is_numpy)
    compare_file(array_d, 'object_fixed_deser.txt', result)

    try:
        result = complex_serializer_generator(msg_context, 'foo', 'bad/Object',
                                              'data', serialize, is_numpy)
        exhaust(result)
        assert False, 'should have raised'
    except MsgGenerationException:
        pass
def test_load_msg_by_type():
    from genmsg.msg_loader import load_msg_by_type, MsgContext, MsgNotFound
    
    test_d = get_test_dir()
    geometry_d = os.path.join(test_d, 'geometry_msgs', 'msg')

    test_ros_dir = os.path.join(test_d, 'test_ros', 'msg')
    test_string_path = os.path.join(test_ros_dir, 'TestString.msg')
    search_path = {
        'test_ros': [ test_ros_dir ],
        'geometry_msgs': [ geometry_d ],
        }
    msg_context = MsgContext.create_default()
    msgspec = load_msg_by_type(msg_context, 'test_ros/TestString', search_path)
    _validate_TestString(msgspec)
    # supposed to register
    assert msg_context.is_registered('test_ros/TestString'), msg_context

    # test invalid search path
    try:
        load_msg_by_type(msg_context, 'test_ros/TestString', [test_string_path])
        assert False, "should have raised"
    except ValueError:
        pass
    # test not found
    try:
        load_msg_by_type(msg_context, 'test_ros/Fake', search_path)
        assert False, "should have raised"
    except MsgNotFound:
        pass

    # test all the known geometry msgs
    test_d = get_test_dir()
    for f in os.listdir(geometry_d):
        if f.endswith('.msg'):
            short = f[:-4]
            msg_type = 'geometry_msgs/%s'%short
            spec = load_msg_by_type(msg_context, msg_type, search_path)
            assert spec is not None
            assert spec.package == 'geometry_msgs'
            assert spec.full_name == msg_type
            assert spec.short_name == short
            with open(os.path.join(geometry_d, f)) as file_h:
                assert spec.text == file_h.read()
            # all types with 'Stamped' in name have headers
            if 'Stamped' in f:
                assert spec.has_header(), msg_type
def test_load_srv_from_file():
    from genmsg.msg_loader import MsgContext, load_srv_from_file
        
    msg_context = MsgContext.create_default()
    
    d = get_test_dir()
    filename = os.path.join(d, 'test_ros', 'srv', 'AddTwoInts.srv')
    with open(filename, 'r') as f:
        text = f.read()
        
    full_name = 'test_ros/AddTwoInts'
    spec = load_srv_from_file(msg_context, filename, full_name)
    assert spec == load_srv_from_file(msg_context, filename, full_name)
    assert ['int64', 'int64'] == spec.request.types, spec.request.types
    assert ['a', 'b'] == spec.request.names
    assert text == spec.text
    assert full_name == spec.full_name
Esempio n. 26
0
def test_load_srv_from_file():
    from genmsg.msg_loader import MsgContext, load_srv_from_file

    msg_context = MsgContext.create_default()

    d = get_test_dir()
    filename = os.path.join(d, 'test_ros', 'srv', 'AddTwoInts.srv')
    with open(filename, 'r') as f:
        text = f.read()

    full_name = 'test_ros/AddTwoInts'
    spec = load_srv_from_file(msg_context, filename, full_name)
    assert spec == load_srv_from_file(msg_context, filename, full_name)
    assert ['int64', 'int64'] == spec.request.types, spec.request.types
    assert ['a', 'b'] == spec.request.names
    assert text == spec.text
    assert full_name == spec.full_name
Esempio n. 27
0
def test_load_srv_from_file():
    from genmsg.msg_loader import MsgContext, load_srv_from_file

    msg_context = MsgContext.create_default()

    d = get_test_dir()
    filename = os.path.join(d, "test_ros", "srv", "AddTwoInts.srv")
    with open(filename, "r") as f:
        text = f.read()

    full_name = "test_ros/AddTwoInts"
    spec = load_srv_from_file(msg_context, filename, full_name)
    assert spec == load_srv_from_file(msg_context, filename, full_name)
    assert ["int64", "int64"] == spec.request.types, spec.request.types
    assert ["a", "b"] == spec.request.names
    assert text == spec.text
    assert full_name == spec.full_name
Esempio n. 28
0
def test_compute_constructor():
    import genpy.generator
    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 None == compute_constructor(msg_context, "unknown_msgs",
                                       "unknown_msgs/Foo")
    assert None == compute_constructor(msg_context, "unknown_msgs", "Foo")
    # - wrong context
    assert None == compute_constructor(msg_context, 'std_msgs', 'ThreeNums')

    # - 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')
Esempio n. 29
0
def test__group_fields3():
    from genfrkl import group_fields
    from genmsg.msg_loader import load_msg_from_string, MsgContext

    context = MsgContext.create_default()
    msg_txt = """
      Float32 x
      Float32[4] xx
      float32 z
      float32 zz
    """
    msgspec = load_msg_from_string(context, msg_txt, 'my_pkg/FieldGroupTest')
    groups = group_fields(msgspec)

    assert len(groups) == 3, "Expected 2 groups"
    assert len(groups[0]) == 1, "First group should have only one element"
    assert len(groups[1]) == 1, "Second group should have only one element"
    assert len(groups[2]) == 2, "Third group should have two elements"
Esempio n. 30
0
def test_complex_serializer_generator():
    from genmsg.msg_loader import load_msg_by_type
    from genpy.generator import complex_serializer_generator, MsgGenerationException, reset_var
    array_d = os.path.join(get_test_dir(), 'array')
    complex_d = os.path.join(get_test_dir(), 'complex')
    # generator tests are mainly tripwires/coverage tests

    #array_serializer_generator(msg_context, package, type_, name, serialize, is_numpy):
    msg_context = MsgContext.create_default()
    # load in some objects
    search_path = {'foo': [array_d]}
    load_msg_by_type(msg_context, 'foo/Object', search_path)
    load_msg_by_type(msg_context, 'foo/ObjectArray', search_path)


    serialize = True
    is_numpy = False
    reset_var()
    result = complex_serializer_generator(msg_context, 'foo', 'foo/Object', 'data', serialize, is_numpy)
    compare_file(complex_d, 'object_ser.txt', result)
    reset_var()
    result = complex_serializer_generator(msg_context, 'foo', 'foo/Object[]', 'data', serialize, is_numpy)
    compare_file(array_d, 'object_varlen_ser.txt', result)
    reset_var()
    result = complex_serializer_generator(msg_context, 'foo', 'foo/Object[3]', 'data', serialize, is_numpy)
    compare_file(array_d, 'object_fixed_ser.txt', result)

    serialize = False
    
    reset_var()
    result = complex_serializer_generator(msg_context, 'foo', 'foo/Object[]', 'data', serialize, is_numpy)
    compare_file(array_d, 'object_varlen_deser.txt', result)
    reset_var()
    result = complex_serializer_generator(msg_context, 'foo', 'foo/Object[3]', 'data', serialize, is_numpy)
    compare_file(array_d, 'object_fixed_deser.txt', result)

    try:
        result = complex_serializer_generator(msg_context, 'foo', 'bad/Object', 'data', serialize, is_numpy)
        exhaust(result)
        assert False, "should have raised"
    except MsgGenerationException:
        pass
Esempio n. 31
0
def test_serialize_fn_generator():
    from genmsg.msg_loader import load_msg_by_type
    from genpy.generator import serialize_fn_generator, reset_var
    array_d = os.path.join(get_test_dir(), 'array')
    complex_d = os.path.join(get_test_dir(), 'complex')
    # generator tests are mainly tripwires/coverage tests

    # array_serializer_generator(msg_context, package, type_, name, serialize, is_numpy):
    msg_context = MsgContext.create_default()
    # load in some objects
    search_path = {'foo': [array_d]}
    object_spec = load_msg_by_type(msg_context, 'foo/Object', search_path)
    object_array_spec = load_msg_by_type(msg_context, 'foo/ObjectArray', search_path)

    is_numpy = False
    reset_var()
    result = serialize_fn_generator(msg_context, object_spec, is_numpy)
    compare_file(complex_d, 'object_ser_full.txt', result)
    reset_var()
    result = serialize_fn_generator(msg_context, object_array_spec, is_numpy)
    compare_file(array_d, 'object_varlen_ser_full.txt', result)
    reset_var()
Esempio n. 32
0
def test_serialize_fn_generator():
    
    from genmsg.msg_loader import load_msg_by_type
    from genpy.generator import serialize_fn_generator, reset_var
    array_d = os.path.join(get_test_dir(), 'array')
    complex_d = os.path.join(get_test_dir(), 'complex')
    # generator tests are mainly tripwires/coverage tests

    #array_serializer_generator(msg_context, package, type_, name, serialize, is_numpy):
    msg_context = MsgContext.create_default()
    # load in some objects
    search_path = {'foo': [array_d]}
    object_spec = load_msg_by_type(msg_context, 'foo/Object', search_path)
    object_array_spec = load_msg_by_type(msg_context, 'foo/ObjectArray', search_path)

    is_numpy = False
    reset_var()
    result = serialize_fn_generator(msg_context, object_spec, is_numpy)
    compare_file(complex_d, 'object_ser_full.txt', result)
    reset_var()
    result = serialize_fn_generator(msg_context, object_array_spec, is_numpy)
    compare_file(array_d, 'object_varlen_ser_full.txt', result)
    reset_var()
Esempio n. 33
0
def test_array_serializer_generator_numpy():
    is_numpy = True
    from genpy.generator import array_serializer_generator
    d = os.path.join(get_test_dir(), 'array')
    # generator tests are mainly tripwires/coverage tests

    # array_serializer_generator(msg_context, package, type_, name, serialize, is_numpy):
    msg_context = MsgContext.create_default()

    # permutations: var length, unint8
    serialize = True
    result = array_serializer_generator(msg_context, '', 'uint8[]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'uint8_varlen_ser_np.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'int16_varlen_ser_np.txt', result)
    result = array_serializer_generator(msg_context, '', 'uint8[8]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'uint8_fixed_ser_np.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[10]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'int16_fixed_ser_np.txt', result)

    serialize = False
    result = array_serializer_generator(msg_context, '', 'uint8[]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'uint8_varlen_deser_np.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'int16_varlen_deser_np.txt', result)
    result = array_serializer_generator(msg_context, '', 'uint8[8]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'uint8_fixed_deser_np.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[10]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'int16_fixed_deser_np.txt', result)
Esempio n. 34
0
def test_load_srv_with_string_constant_from_file():
    from genmsg.msg_loader import MsgContext, load_srv_from_file

    msg_context = MsgContext.create_default()

    d = get_test_dir()
    filename = os.path.join(d, 'test_ros', 'srv',
                            'ServiceWithStringConstant.srv')
    with open(filename, 'r') as f:
        text = f.read()

    full_name = 'test_ros/ServiceWithStringConstant'
    spec = load_srv_from_file(msg_context, filename, full_name)
    assert spec == load_srv_from_file(msg_context, filename, full_name)
    assert ['string'] == spec.request.types, spec.request.types
    assert ['value'] == spec.request.names
    assert ['return_value'] == spec.response.names
    assert len(spec.request.constants) == 2
    index_string = 0 if spec.request.constants[0].name == 'EXAMPLE' else 1
    index_int = 1 - index_string
    constant = spec.request.constants[index_string]
    assert constant.type == 'string'
    assert constant.name == 'EXAMPLE'
    assert constant.val == 'here "#comments" are ignored, and leading and trailing whitespace removed', '<{}>'.format(
        constant.val)
    constant = spec.request.constants[index_int]
    assert constant.type == 'int32'
    assert constant.name == 'INTCONST'
    assert constant.val == 42, '<{}>'.format(constant.val)
    assert len(spec.response.constants) == 1
    constant = spec.response.constants[0]
    assert constant.type == 'string'
    assert constant.name == 'RESULTEXAMPLE'
    assert constant.val == 'here "#comments" are ignored too, and leading and trailing whitespace removed', '<{}>'.format(
        constant.val)
    assert text == spec.text
    assert full_name == spec.full_name
Esempio n. 35
0
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'
Esempio n. 36
0
def test_load_msg_depends_stamped():
    #TODO: should there just be a 'load_msg, implicit=True?'
    from genmsg.msg_loader import MsgContext, load_msg_by_type, load_msg_depends
    test_d = get_test_dir()
    geometry_d = os.path.join(test_d, 'geometry_msgs', 'msg')
    search_path = {
        'test_ros': [os.path.join(test_d, 'test_ros', 'msg')],
        'std_msgs': [os.path.join(test_d, 'std_msgs', 'msg')],
        'geometry_msgs': [geometry_d],
        'sensor_msgs': [os.path.join(test_d, 'sensor_msgs', 'msg')],
    }

    # Test with Stamped and deeper hierarchies, Header

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'geometry_msgs/PoseStamped',
                                 search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, 'geometry_msgs', 'msg', 'PoseStamped.msg')
    assert file_p == msg_context.get_file('geometry_msgs/PoseStamped')
    val = msg_context.get_all_depends('geometry_msgs/PoseStamped')
    assert set([
        'std_msgs/Header', 'geometry_msgs/Pose', 'geometry_msgs/Point',
        'geometry_msgs/Quaternion'
    ]) == set(val), val
    val = msg_context.get_depends('geometry_msgs/PoseStamped')
    assert set(['std_msgs/Header', 'geometry_msgs/Pose']) == set(val), val
    for s in ['Header']:
        file_p = os.path.join(test_d, 'std_msgs', 'msg', '%s.msg' % s)
        assert file_p == msg_context.get_file('std_msgs/%s' % s)
    for s in ['Pose', 'Point', 'Quaternion']:
        file_p = os.path.join(geometry_d, '%s.msg' % s)
        assert file_p == msg_context.get_file('geometry_msgs/%s' % s)

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context,
                                 'geometry_msgs/TwistWithCovarianceStamped',
                                 search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, 'geometry_msgs', 'msg',
                          'TwistWithCovarianceStamped.msg')
    assert file_p == msg_context.get_file(
        'geometry_msgs/TwistWithCovarianceStamped')
    val = msg_context.get_all_depends(
        'geometry_msgs/TwistWithCovarianceStamped')
    assert set([
        'std_msgs/Header', 'geometry_msgs/TwistWithCovariance',
        'geometry_msgs/Twist', 'geometry_msgs/Vector3'
    ]) == set(val), val
    val = msg_context.get_depends('geometry_msgs/TwistWithCovarianceStamped')
    assert set(['std_msgs/Header',
                'geometry_msgs/TwistWithCovariance']) == set(val), val

    for s in ['Header']:
        file_p = os.path.join(test_d, 'std_msgs', 'msg', '%s.msg' % s)
        assert file_p == msg_context.get_file('std_msgs/%s' % s)
    for s in ['TwistWithCovariance', 'Twist', 'Vector3']:
        file_p = os.path.join(geometry_d, '%s.msg' % s)
        assert file_p == msg_context.get_file('geometry_msgs/%s' % s)

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'sensor_msgs/Imu', search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, 'sensor_msgs', 'msg', 'Imu.msg')
    assert file_p == msg_context.get_file('sensor_msgs/Imu')
    val = msg_context.get_all_depends('sensor_msgs/Imu')
    assert set([
        'std_msgs/Header', 'geometry_msgs/Quaternion', 'geometry_msgs/Vector3'
    ]) == set(val), val
    val = msg_context.get_depends('sensor_msgs/Imu')
    assert set([
        'std_msgs/Header', 'geometry_msgs/Quaternion', 'geometry_msgs/Vector3'
    ]) == set(val), val
    for s in ['Header']:
        file_p = os.path.join(test_d, 'std_msgs', 'msg', '%s.msg' % s)
        assert file_p == msg_context.get_file('std_msgs/%s' % s)
    for s in ['Quaternion', 'Vector3']:
        file_p = os.path.join(geometry_d, '%s.msg' % s)
        assert file_p == msg_context.get_file('geometry_msgs/%s' % s)
Esempio n. 37
0
def test_load_msg_depends():
    #TODO: should there just be a 'load_msg, implicit=True?'
    from genmsg.msg_loader import MsgContext, load_msg_by_type, load_msg_depends, MsgNotFound
    test_d = get_test_dir()
    search_path = {
        'test_ros': [os.path.join(test_d, 'test_ros', 'msg')],
        'std_msgs': [os.path.join(test_d, 'std_msgs', 'msg')],
        'geometry_msgs': [os.path.join(test_d, 'geometry_msgs', 'msg')],
        'sensor_msgs': [os.path.join(test_d, 'sensor_msgs', 'msg')],
        'invalid': [os.path.join(test_d, 'invalid', 'msg')],
    }

    # Test not found
    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'invalid/BadDepend', search_path)
    try:
        load_msg_depends(msg_context, root_spec, search_path)
        assert False, "should have raised MsgNotFound"
    except MsgNotFound:
        pass
    root_spec = load_msg_by_type(msg_context, 'invalid/BadLocalDepend',
                                 search_path)
    try:
        load_msg_depends(msg_context, root_spec, search_path)
        assert False, "should have raised MsgNotFound"
    except MsgNotFound:
        pass

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'std_msgs/Int32', search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, 'std_msgs', 'msg', 'Int32.msg')
    assert file_p == msg_context.get_file('std_msgs/Int32')
    assert [] == msg_context.get_depends('std_msgs/Int32')

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'std_msgs/Header', search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, 'std_msgs', 'msg', 'Header.msg')
    assert file_p == msg_context.get_file('std_msgs/Header')
    assert [] == msg_context.get_depends('std_msgs/Header')

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'Header', search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, 'std_msgs', 'msg', 'Header.msg')
    assert file_p == msg_context.get_file('std_msgs/Header')
    assert [] == msg_context.get_depends('std_msgs/Header')

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'std_msgs/Int32MultiArray',
                                 search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, 'std_msgs', 'msg', 'Int32MultiArray.msg')
    assert file_p == msg_context.get_file('std_msgs/Int32MultiArray')
    val = msg_context.get_all_depends('std_msgs/Int32MultiArray')
    assert set(['std_msgs/MultiArrayLayout',
                'std_msgs/MultiArrayDimension']) == set(val), val
    assert 2 == len(val), val

    val = msg_context.get_depends('std_msgs/Int32MultiArray')
    assert set(['std_msgs/MultiArrayLayout']) == set(val), val
    for s in ['MultiArrayLayout', 'MultiArrayDimension']:
        file_p = os.path.join(test_d, 'std_msgs', 'msg', '%s.msg' % s)
        assert file_p == msg_context.get_file('std_msgs/%s' % s)
Esempio n. 38
0
def test_array_serializer_generator():
    from genmsg.msg_loader import load_msg_by_type
    from genpy.generator import array_serializer_generator, MsgGenerationException, reset_var
    d = os.path.join(get_test_dir(), 'array')
    # generator tests are mainly tripwires/coverage tests

    # array_serializer_generator(msg_context, package, type_, name, serialize, is_numpy):
    msg_context = MsgContext.create_default()
    # load in some objects
    search_path = {'foo': [d]}
    load_msg_by_type(msg_context, 'foo/Object', search_path)
    load_msg_by_type(msg_context, 'foo/ObjectArray', search_path)

    # permutations: var length, unint8
    is_numpy = False

    serialize = True
    result = array_serializer_generator(msg_context, '', 'uint8[]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'uint8_varlen_ser.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'int16_varlen_ser.txt', result)
    result = array_serializer_generator(msg_context, '', 'uint8[8]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'uint8_fixed_ser.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[10]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'int16_fixed_ser.txt', result)
    result = array_serializer_generator(msg_context, '', 'bool[]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'bool_varlen_ser.txt', result)
    result = array_serializer_generator(msg_context, '', 'bool[3]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'bool_fixed_ser.txt', result)

    # for 'complex' types have to reset the variable generator
    reset_var()
    result = array_serializer_generator(msg_context, '', 'string[]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'string_varlen_ser.txt', result)
    reset_var()
    result = array_serializer_generator(msg_context, '', 'string[2]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'string_fixed_ser.txt', result)

    reset_var()
    result = array_serializer_generator(msg_context, 'foo', 'foo/Object[]',
                                        'data', serialize, is_numpy)
    compare_file(d, 'object_varlen_ser.txt', result)
    reset_var()
    result = array_serializer_generator(msg_context, 'foo', 'foo/Object[3]',
                                        'data', serialize, is_numpy)
    compare_file(d, 'object_fixed_ser.txt', result)

    serialize = False
    result = array_serializer_generator(msg_context, '', 'uint8[]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'uint8_varlen_deser.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'int16_varlen_deser.txt', result)
    result = array_serializer_generator(msg_context, '', 'uint8[8]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'uint8_fixed_deser.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[10]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'int16_fixed_deser.txt', result)
    result = array_serializer_generator(msg_context, '', 'bool[]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'bool_varlen_deser.txt', result)
    result = array_serializer_generator(msg_context, '', 'bool[3]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'bool_fixed_deser.txt', result)

    # for 'complex' types have to reset the variable generator
    reset_var()
    result = array_serializer_generator(msg_context, '', 'string[]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'string_varlen_deser.txt', result)
    reset_var()
    result = array_serializer_generator(msg_context, '', 'string[2]', 'data',
                                        serialize, is_numpy)
    compare_file(d, 'string_fixed_deser.txt', result)

    reset_var()
    result = array_serializer_generator(msg_context, 'foo', 'foo/Object[]',
                                        'data', serialize, is_numpy)
    compare_file(d, 'object_varlen_deser.txt', result)
    reset_var()
    result = array_serializer_generator(msg_context, 'foo', 'foo/Object[3]',
                                        'data', serialize, is_numpy)
    compare_file(d, 'object_fixed_deser.txt', result)

    # test w/ bad args
    try:
        result = array_serializer_generator(msg_context, '', 'uint8', 'data',
                                            True, False)
        exhaust(result)
        assert False, 'should have raised'
    except MsgGenerationException:
        pass
Esempio n. 39
0
def test_MsgContext():
    from genmsg.msg_loader import MsgContext, load_msg_from_file
    msg_context = MsgContext()
    assert not msg_context.is_registered('time')
    assert not msg_context.is_registered('duration')

    msg_context = MsgContext.create_default()
    # tripwires
    repr(msg_context)
    str(msg_context)

    assert msg_context.is_registered('time'), msg_context._registered_packages
    assert msg_context.is_registered('duration')

    assert not msg_context.is_registered('test_ros/TestString')
    assert not msg_context.is_registered('Header')

    # start loading stuff into context
    test_d = get_test_dir()
    test_ros_dir = os.path.join(test_d, 'test_ros', 'msg')
    test_string_path = os.path.join(test_ros_dir, 'TestString.msg')
    spec = load_msg_from_file(msg_context, test_string_path,
                              'test_ros/TestString')
    msg_context.register('test_ros/TestString', spec)
    assert msg_context.get_registered('test_ros/TestString') == spec
    try:
        msg_context.get_registered('bad/TestString')
        assert False, 'should have raised'
    except KeyError:
        pass

    assert msg_context.is_registered('test_ros/TestString')

    # test Header
    assert not msg_context.is_registered('Header')
    assert not msg_context.is_registered('std_msgs/Header')

    msg_context.register('std_msgs/Header', spec)
    assert msg_context.is_registered('std_msgs/Header')
Esempio n. 40
0
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')
Esempio n. 41
0
def test_MsgContext():
    from genmsg.msg_loader import MsgContext, load_msg_from_file

    msg_context = MsgContext()
    assert not msg_context.is_registered("time")
    assert not msg_context.is_registered("duration")

    msg_context = MsgContext.create_default()
    # tripwires
    repr(msg_context)
    str(msg_context)

    assert msg_context.is_registered("time"), msg_context._registered_packages
    assert msg_context.is_registered("duration")

    assert not msg_context.is_registered("test_ros/TestString")
    assert not msg_context.is_registered("Header")

    # start loading stuff into context
    test_d = get_test_dir()
    test_ros_dir = os.path.join(test_d, "test_ros", "msg")
    test_string_path = os.path.join(test_ros_dir, "TestString.msg")
    spec = load_msg_from_file(msg_context, test_string_path, "test_ros/TestString")
    msg_context.register("test_ros/TestString", spec)
    assert msg_context.get_registered("test_ros/TestString") == spec
    try:
        msg_context.get_registered("bad/TestString")
        assert False, "should have raised"
    except KeyError:
        pass

    assert msg_context.is_registered("test_ros/TestString")

    # test Header
    assert not msg_context.is_registered("Header")
    assert not msg_context.is_registered("std_msgs/Header")

    msg_context.register("std_msgs/Header", spec)
    assert msg_context.is_registered("std_msgs/Header")
def test_MsgContext():
    from genmsg.msg_loader import MsgContext, load_msg_from_file
    msg_context = MsgContext()
    assert not msg_context.is_registered('time')
    assert not msg_context.is_registered('duration')
    
    msg_context = MsgContext.create_default()
    # tripwires
    repr(msg_context)
    str(msg_context)

    assert msg_context.is_registered('time'), msg_context._registered_packages
    assert msg_context.is_registered('duration')

    assert not msg_context.is_registered('test_ros/TestString')
    assert not msg_context.is_registered('Header')
    
    # start loading stuff into context
    test_d = get_test_dir()
    test_ros_dir = os.path.join(test_d, 'test_ros', 'msg')
    test_string_path = os.path.join(test_ros_dir, 'TestString.msg')
    spec = load_msg_from_file(msg_context, test_string_path, 'test_ros/TestString')
    msg_context.register('test_ros/TestString', spec)
    assert msg_context.get_registered('test_ros/TestString') == spec
    try:
        msg_context.get_registered('bad/TestString')
        assert False, 'should have raised'
    except KeyError:
        pass
    
    assert msg_context.is_registered('test_ros/TestString')

    # test Header
    assert not msg_context.is_registered('Header')
    assert not msg_context.is_registered('std_msgs/Header')
    
    msg_context.register('std_msgs/Header', spec)
    assert msg_context.is_registered('std_msgs/Header')
Esempio n. 43
0
def test_load_depends_msg():
    from genmsg.msg_loader import MsgContext, load_msg_by_type, load_depends, MsgNotFound, load_srv_by_type
    test_d = get_test_dir()
    geometry_d = os.path.join(test_d, 'geometry_msgs', 'msg')
    msg_search_path = {
        'test_ros': [os.path.join(test_d, 'test_ros', 'msg')],
        'std_msgs': [os.path.join(test_d, 'std_msgs', 'msg')],
        'geometry_msgs': [geometry_d],
        'sensor_msgs': [os.path.join(test_d, 'sensor_msgs', 'msg')],
        'invalid': [os.path.join(test_d, 'invalid', 'msg')],
    }

    # Test not found
    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'invalid/BadDepend',
                                 msg_search_path)
    try:
        load_depends(msg_context, root_spec, msg_search_path)
        assert False, "should have raised MsgNotFound"
    except MsgNotFound:
        pass
    root_spec = load_msg_by_type(msg_context, 'invalid/BadLocalDepend',
                                 msg_search_path)
    try:
        load_depends(msg_context, root_spec, msg_search_path)
        assert False, "should have raised MsgNotFound"
    except MsgNotFound:
        pass

    # Test with msgs
    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'geometry_msgs/PoseStamped',
                                 msg_search_path)
    load_depends(msg_context, root_spec, msg_search_path)
    file_p = os.path.join(test_d, 'geometry_msgs', 'msg', 'PoseStamped.msg')
    assert file_p == msg_context.get_file('geometry_msgs/PoseStamped')
    val = msg_context.get_all_depends('geometry_msgs/PoseStamped')
    assert set([
        'std_msgs/Header', 'geometry_msgs/Pose', 'geometry_msgs/Point',
        'geometry_msgs/Quaternion'
    ]) == set(val), val
    val = msg_context.get_depends('geometry_msgs/PoseStamped')
    assert set(['std_msgs/Header', 'geometry_msgs/Pose']) == set(val), val

    for s in ['Header']:
        file_p = os.path.join(test_d, 'std_msgs', 'msg', '%s.msg' % s)
        assert file_p == msg_context.get_file('std_msgs/%s' % s)
    for s in ['Pose', 'Point', 'Quaternion']:
        file_p = os.path.join(geometry_d, '%s.msg' % s)
        assert file_p == msg_context.get_file('geometry_msgs/%s' % s)

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'sensor_msgs/Imu',
                                 msg_search_path)
    load_depends(msg_context, root_spec, msg_search_path)
    file_p = os.path.join(test_d, 'sensor_msgs', 'msg', 'Imu.msg')
    assert file_p == msg_context.get_file('sensor_msgs/Imu')
    val = msg_context.get_depends('sensor_msgs/Imu')
    assert set([
        'std_msgs/Header', 'geometry_msgs/Quaternion', 'geometry_msgs/Vector3'
    ]) == set(val), val
    for s in ['Header']:
        file_p = os.path.join(test_d, 'std_msgs', 'msg', '%s.msg' % s)
        assert file_p == msg_context.get_file('std_msgs/%s' % s)
    for s in ['Quaternion', 'Vector3']:
        file_p = os.path.join(geometry_d, '%s.msg' % s)
        assert file_p == msg_context.get_file('geometry_msgs/%s' % s)
Esempio n. 44
0
def test_array_serializer_generator():
    from genmsg.msg_loader import load_msg_by_type
    from genpy.generator import array_serializer_generator, MsgGenerationException, reset_var
    d = os.path.join(get_test_dir(), 'array')
    # generator tests are mainly tripwires/coverage tests

    #array_serializer_generator(msg_context, package, type_, name, serialize, is_numpy):
    msg_context = MsgContext.create_default()
    # load in some objects
    search_path = {'foo': [d]}
    load_msg_by_type(msg_context, 'foo/Object', search_path)
    load_msg_by_type(msg_context, 'foo/ObjectArray', search_path)

    # permutations: var length, unint8
    is_numpy = False

    serialize = True
    result = array_serializer_generator(msg_context, '', 'uint8[]', 'data', serialize, is_numpy)
    compare_file(d, 'uint8_varlen_ser.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[]', 'data', serialize, is_numpy)
    compare_file(d, 'int16_varlen_ser.txt', result)
    result = array_serializer_generator(msg_context, '', 'uint8[8]', 'data', serialize, is_numpy)
    compare_file(d, 'uint8_fixed_ser.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[10]', 'data', serialize, is_numpy)
    compare_file(d, 'int16_fixed_ser.txt', result)
    result = array_serializer_generator(msg_context, '', 'bool[]', 'data', serialize, is_numpy)
    compare_file(d, 'bool_varlen_ser.txt', result)
    result = array_serializer_generator(msg_context, '', 'bool[3]', 'data', serialize, is_numpy)
    compare_file(d, 'bool_fixed_ser.txt', result)

    # for 'complex' types have to reset the variable generator
    reset_var()
    result = array_serializer_generator(msg_context, '', 'string[]', 'data', serialize, is_numpy)
    compare_file(d, 'string_varlen_ser.txt', result)
    reset_var()
    result = array_serializer_generator(msg_context, '', 'string[2]', 'data', serialize, is_numpy)
    compare_file(d, 'string_fixed_ser.txt', result)
    
    reset_var()
    result = array_serializer_generator(msg_context, 'foo', 'foo/Object[]', 'data', serialize, is_numpy)
    compare_file(d, 'object_varlen_ser.txt', result)
    reset_var()
    result = array_serializer_generator(msg_context, 'foo', 'foo/Object[3]', 'data', serialize, is_numpy)
    compare_file(d, 'object_fixed_ser.txt', result)

    serialize = False
    result = array_serializer_generator(msg_context, '', 'uint8[]', 'data', serialize, is_numpy)
    compare_file(d, 'uint8_varlen_deser.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[]', 'data', serialize, is_numpy)
    compare_file(d, 'int16_varlen_deser.txt', result)
    result = array_serializer_generator(msg_context, '', 'uint8[8]', 'data', serialize, is_numpy)
    compare_file(d, 'uint8_fixed_deser.txt', result)
    result = array_serializer_generator(msg_context, '', 'int16[10]', 'data', serialize, is_numpy)
    compare_file(d, 'int16_fixed_deser.txt', result)
    result = array_serializer_generator(msg_context, '', 'bool[]', 'data', serialize, is_numpy)
    compare_file(d, 'bool_varlen_deser.txt', result)
    result = array_serializer_generator(msg_context, '', 'bool[3]', 'data', serialize, is_numpy)
    compare_file(d, 'bool_fixed_deser.txt', result)

    # for 'complex' types have to reset the variable generator
    reset_var()
    result = array_serializer_generator(msg_context, '', 'string[]', 'data', serialize, is_numpy)
    compare_file(d, 'string_varlen_deser.txt', result)
    reset_var()    
    result = array_serializer_generator(msg_context, '', 'string[2]', 'data', serialize, is_numpy)
    compare_file(d, 'string_fixed_deser.txt', result)

    reset_var()
    result = array_serializer_generator(msg_context, 'foo', 'foo/Object[]', 'data', serialize, is_numpy)
    compare_file(d, 'object_varlen_deser.txt', result)
    reset_var()
    result = array_serializer_generator(msg_context, 'foo', 'foo/Object[3]', 'data', serialize, is_numpy)
    compare_file(d, 'object_fixed_deser.txt', result)

    # test w/ bad args
    try:
        result = array_serializer_generator(msg_context, '', 'uint8', 'data', True, False)
        exhaust(result)
        assert False, "should have raised"
    except MsgGenerationException:
        pass
def test_load_msg_depends():
    #TODO: should there just be a 'load_msg, implicit=True?'
    from genmsg.msg_loader import MsgContext, load_msg_by_type, load_msg_depends, MsgNotFound
    test_d = get_test_dir()
    search_path = {
        'test_ros': [ os.path.join(test_d, 'test_ros', 'msg') ],
        'std_msgs': [ os.path.join(test_d, 'std_msgs', 'msg') ],
        'geometry_msgs': [ os.path.join(test_d, 'geometry_msgs', 'msg') ],
        'sensor_msgs': [ os.path.join(test_d, 'sensor_msgs', 'msg') ],
        'invalid': [ os.path.join(test_d, 'invalid', 'msg') ],
        }
    
    # Test not found
    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'invalid/BadDepend', search_path)
    try:
        load_msg_depends(msg_context, root_spec, search_path)
        assert False, "should have raised MsgNotFound"
    except MsgNotFound:
        pass
    root_spec = load_msg_by_type(msg_context, 'invalid/BadLocalDepend', search_path)
    try:
        load_msg_depends(msg_context, root_spec, search_path)
        assert False, "should have raised MsgNotFound"
    except MsgNotFound:
        pass

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'std_msgs/Int32', search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, 'std_msgs', 'msg', 'Int32.msg')
    assert file_p == msg_context.get_file('std_msgs/Int32')
    assert [] == msg_context.get_depends('std_msgs/Int32')

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'std_msgs/Header', search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, 'std_msgs', 'msg', 'Header.msg')
    assert file_p == msg_context.get_file('std_msgs/Header')
    assert [] == msg_context.get_depends('std_msgs/Header')

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'Header', search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, 'std_msgs', 'msg', 'Header.msg')
    assert file_p == msg_context.get_file('std_msgs/Header')
    assert [] == msg_context.get_depends('std_msgs/Header')

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'std_msgs/Int32MultiArray', search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, 'std_msgs', 'msg', 'Int32MultiArray.msg')
    assert file_p == msg_context.get_file('std_msgs/Int32MultiArray')
    val = msg_context.get_all_depends('std_msgs/Int32MultiArray')
    assert set(['std_msgs/MultiArrayLayout', 'std_msgs/MultiArrayDimension']) == set(val), val
    assert 2 == len(val), val

    val = msg_context.get_depends('std_msgs/Int32MultiArray')
    assert set(['std_msgs/MultiArrayLayout']) == set(val), val
    for s in ['MultiArrayLayout', 'MultiArrayDimension']:
        file_p = os.path.join(test_d, 'std_msgs', 'msg', '%s.msg'%s)
        assert file_p == msg_context.get_file('std_msgs/%s'%s)
def test_load_msg_depends_stamped():
    #TODO: should there just be a 'load_msg, implicit=True?'
    from genmsg.msg_loader import MsgContext, load_msg_by_type, load_msg_depends
    test_d = get_test_dir()
    geometry_d = os.path.join(test_d, 'geometry_msgs', 'msg')
    search_path = {
        'test_ros': [ os.path.join(test_d, 'test_ros', 'msg') ],
        'std_msgs': [ os.path.join(test_d, 'std_msgs', 'msg') ],
        'geometry_msgs': [ geometry_d ],
        'sensor_msgs': [ os.path.join(test_d, 'sensor_msgs', 'msg') ],
        }

    # Test with Stamped and deeper hierarchies, Header

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'geometry_msgs/PoseStamped', search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, 'geometry_msgs', 'msg', 'PoseStamped.msg')
    assert file_p == msg_context.get_file('geometry_msgs/PoseStamped')
    val = msg_context.get_all_depends('geometry_msgs/PoseStamped')
    assert set(['std_msgs/Header', 'geometry_msgs/Pose', 'geometry_msgs/Point', 'geometry_msgs/Quaternion']) == set(val), val
    val = msg_context.get_depends('geometry_msgs/PoseStamped')
    assert set(['std_msgs/Header', 'geometry_msgs/Pose']) == set(val), val
    for s in ['Header']:
        file_p = os.path.join(test_d, 'std_msgs', 'msg', '%s.msg'%s)
        assert file_p == msg_context.get_file('std_msgs/%s'%s)
    for s in ['Pose', 'Point', 'Quaternion']:
        file_p = os.path.join(geometry_d, '%s.msg'%s)
        assert file_p == msg_context.get_file('geometry_msgs/%s'%s)

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'geometry_msgs/TwistWithCovarianceStamped', search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, 'geometry_msgs', 'msg', 'TwistWithCovarianceStamped.msg')
    assert file_p == msg_context.get_file('geometry_msgs/TwistWithCovarianceStamped')
    val = msg_context.get_all_depends('geometry_msgs/TwistWithCovarianceStamped')
    assert set(['std_msgs/Header', 'geometry_msgs/TwistWithCovariance', 'geometry_msgs/Twist', 'geometry_msgs/Vector3']) == set(val), val
    val = msg_context.get_depends('geometry_msgs/TwistWithCovarianceStamped')
    assert set(['std_msgs/Header', 'geometry_msgs/TwistWithCovariance']) == set(val), val

    for s in ['Header']:
        file_p = os.path.join(test_d, 'std_msgs', 'msg', '%s.msg'%s)
        assert file_p == msg_context.get_file('std_msgs/%s'%s)
    for s in ['TwistWithCovariance', 'Twist', 'Vector3']:
        file_p = os.path.join(geometry_d, '%s.msg'%s)
        assert file_p == msg_context.get_file('geometry_msgs/%s'%s)

    msg_context = MsgContext.create_default()
    root_spec = load_msg_by_type(msg_context, 'sensor_msgs/Imu', search_path)
    load_msg_depends(msg_context, root_spec, search_path)
    file_p = os.path.join(test_d, 'sensor_msgs', 'msg', 'Imu.msg')
    assert file_p == msg_context.get_file('sensor_msgs/Imu')
    val = msg_context.get_all_depends('sensor_msgs/Imu')
    assert set(['std_msgs/Header', 'geometry_msgs/Quaternion', 'geometry_msgs/Vector3']) == set(val), val
    val = msg_context.get_depends('sensor_msgs/Imu')
    assert set(['std_msgs/Header', 'geometry_msgs/Quaternion', 'geometry_msgs/Vector3']) == set(val), val
    for s in ['Header']:
        file_p = os.path.join(test_d, 'std_msgs', 'msg', '%s.msg'%s)
        assert file_p == msg_context.get_file('std_msgs/%s'%s)
    for s in ['Quaternion', 'Vector3']:
        file_p = os.path.join(geometry_d, '%s.msg'%s)
        assert file_p == msg_context.get_file('geometry_msgs/%s'%s)
Esempio n. 47
0
    print('Unable to import ROS1 tools. Please source a ROS1 installation first.', end='', file=sys.stderr)
    sys.exit(1)

import argparse
import copy
import em
import os
import sys

if sys.version_info[0] > 2:
    from io import StringIO as BufferIO
else:
    from io import BytesIO as BufferIO


g_msg_context = MsgContext()


def get_type_components(full_type):
    if full_type == 'Header':
        return ('std_msgs', 'Header')

    components = full_type.split('/')
    if len(components) != 2:
        raise ValueError('Unexpected value for message type string: {}'.format(full_type))

    package = components[0]
    end = components[1].find('[')
    if end == -1:
      type = components[1]
    else:
Esempio n. 48
0
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 None == default_value(msg_context, "unknown_msgs/Foo", "unknown_msgs")
    # - wrong context
    assert None == default_value(msg_context, 'ThreeNums', 'std_msgs')

    # - 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')
Esempio n. 49
0
def test_load_srv_by_type():
    from genmsg.msg_loader import load_srv_by_type, MsgContext, MsgNotFound

    test_d = get_test_dir()
    test_ros_dir = os.path.join(test_d, 'test_ros', 'srv')
    std_srvs_dir = os.path.join(test_d, 'std_srvs', 'srv')
    empty_path = os.path.join(std_srvs_dir, 'Empty.srv')
    a2i_path = os.path.join(std_srvs_dir, 'AddTwoInts.srv')
    search_path = {
        'test_ros': [test_ros_dir],
        'std_srvs': [std_srvs_dir],
    }
    msg_context = MsgContext.create_default()
    spec = load_srv_by_type(msg_context, 'std_srvs/Empty', search_path)
    assert msg_context.is_registered('std_srvs/EmptyRequest')
    assert msg_context.is_registered('std_srvs/EmptyResponse')
    assert msg_context.get_registered('std_srvs/EmptyRequest') == spec.request
    assert msg_context.get_registered(
        'std_srvs/EmptyResponse') == spec.response
    assert msg_context.get_file(
        'std_srvs/EmptyRequest') == empty_path, msg_context.get_file(
            'std_srvs/EmptyRequest')
    assert msg_context.get_file(
        'std_srvs/EmptyResponse') == empty_path, msg_context.get_file(
            'std_srvs/EmptyResponse')
    assert spec.request.full_name == 'std_srvs/EmptyRequest'
    assert spec.response.full_name == 'std_srvs/EmptyResponse'
    assert spec.request.short_name == 'EmptyRequest'
    assert spec.response.short_name == 'EmptyResponse'
    assert spec.request.package == 'std_srvs'
    assert spec.response.package == 'std_srvs'
    for f in [
            spec.request.names, spec.request.types, spec.response.names,
            spec.response.types
    ]:
        assert [] == f

    spec = load_srv_by_type(msg_context, 'test_ros/AddTwoInts', search_path)
    assert msg_context.is_registered('test_ros/AddTwoIntsRequest')
    assert msg_context.is_registered('test_ros/AddTwoIntsResponse')
    assert msg_context.get_registered(
        'test_ros/AddTwoIntsRequest') == spec.request
    assert msg_context.get_registered(
        'test_ros/AddTwoIntsResponse') == spec.response
    assert spec.request.types == ['int64', 'int64'], spec.request.types
    assert spec.request.names == ['a', 'b'], spec.request.names
    assert spec.response.types == ['int64'], spec.response.types
    assert spec.response.names == ['sum'], spec.response.names

    # test invalid search path
    try:
        load_srv_by_type(msg_context, 'test_ros/AddTwoInts', [std_srvs_dir])
        assert False, "should have raised"
    except ValueError:
        pass
    # test not found
    try:
        load_srv_by_type(msg_context, 'test_ros/Fake', search_path)
        assert False, "should have raised"
    except MsgNotFound:
        pass
Esempio n. 50
0
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')