Exemplo n.º 1
0
def test_action_format_to_and_from_dict():
    pkg = "actionlib"
    name = "TwoInts"
    name_goal = "TwoIntsGoal"
    name_result = "TwoIntsResult"
    # it doesn't matter what these are set to
    definition_action = "FOO"
    definition_goal = "BAR"
    definition_result = "COOL"
    d = {
        "package": pkg,
        "name": name,
        "definition": definition_action,
        "goal": {
            "definition":
            definition_goal,
            "fields": [
                {
                    "type": "int64",
                    "name": "a"
                },
                {
                    "type": "int64",
                    "name": "b"
                },
            ],
        },
        "result": {
            "definition": definition_result,
            "fields": [{
                "type": "int64",
                "name": "sum"
            }],
        },
    }
    f = ROS1ActionFormat(
        package=pkg,
        name=name,
        definition=definition_action,
        goal=ROS1MsgFormat(
            definition=definition_goal,
            package=pkg,
            name=name_goal,
            constants=[],
            fields=[Field("int64", "a"),
                    Field("int64", "b")],
        ),
        feedback=None,
        result=ROS1MsgFormat(
            package=pkg,
            definition=definition_result,
            name=name_result,
            constants=[],
            fields=[Field("int64", "sum")],
        ),
    )

    f1 = ROS1ActionFormat.from_dict(d)
    assert f1 == f
    assert ROS1ActionFormat.from_dict(f.to_dict()) == f
Exemplo n.º 2
0
def test_srv_format_to_and_from_dict():
    pkg = "nav_msgs"
    name = "SetMap"
    name_request = "SetMapRequest"
    name_response = "SetMapResponse"
    # it doesn't matter what these are set to
    definition_service = "FOO"
    definition_request = "BAR"
    definition_response = "COOL"
    d = {
        "package": pkg,
        "name": name,
        "definition": definition_service,
        "request": {
            "definition":
            definition_request,
            "fields": [
                {
                    "type": "nav_msgs/OccupancyGrid",
                    "name": "map"
                },
                {
                    "type": "geometry_msgs/PoseWithCovarianceStamped",
                    "name": "initial_pose",
                },
            ],
        },
        "response": {
            "definition": definition_response,
            "fields": [{
                "type": "bool",
                "name": "success"
            }],
        },
    }
    f = ROS1SrvFormat(
        package=pkg,
        name=name,
        definition=definition_service,
        request=ROS1MsgFormat(
            package=pkg,
            definition=definition_request,
            name=name_request,
            constants=[],
            fields=[
                Field("nav_msgs/OccupancyGrid", "map"),
                Field("geometry_msgs/PoseWithCovarianceStamped",
                      "initial_pose"),
            ],
        ),
        response=ROS1MsgFormat(
            package=pkg,
            definition=definition_response,
            name=name_response,
            constants=[],
            fields=[Field("bool", "success")],
        ),
    )
    assert ROS1SrvFormat.from_dict(d) == f
    assert ROS1SrvFormat.from_dict(f.to_dict()) == f
Exemplo n.º 3
0
def test_to_and_from_dict():
    pkg = "tf"
    msg_tf = ROS1MsgFormat.from_dict({
        "package":
        pkg,
        "name":
        "tfMessage",
        "definition":
        "geometry_msgs/TransformStamped[] transforms\n",
        "fields": [{
            "type": "geometry_msgs/TransformStamped[]",
            "name": "transforms",
        }],
    })
    srv_fg = ROS1SrvFormat.from_dict({
        "package": pkg,
        "name": "FrameGraph",
        "definition": "---\nstring dot_graph\n",
        "response": {
            "definition": "string dot_graph\n",
            "fields": [{
                "type": "string",
                "name": "dot_graph"
            }],
        },
    })
    p = ROS1Package(
        name=pkg,
        path="/ros_ws/src/geometry/tf",
        messages=[msg_tf],
        actions=[],
        services=[srv_fg],
    )
    assert p == ROS1Package.from_dict(p.to_dict())
Exemplo n.º 4
0
 def names(fmt: ROS1MsgFormat) -> List[str]:
     buff = []
     for ctx, field in fmt.flatten(register):
         name = field.name
         if ctx:
             name = f"{'.'.join(ctx)}.{name}"
         buff.append(name)
     return buff
Exemplo n.º 5
0
def test_msg_toposort(sut):
    paths = [
        "/ros_ws/src/geometry2/tf2_msgs",
        "/ros_ws/src/geometry/tf",
        "/ros_ws/src/common_msgs/geometry_msgs",
        "/ros_ws/src/std_msgs",
    ]

    db_package = ROS1PackageDatabase.build(sut, paths)
    db_format = ROS1FormatDatabase.from_packages(db_package)

    msgs = db_format.messages.values()
    msgs = ROS1MsgFormat.toposort(msgs)
Exemplo n.º 6
0
def test_msg_from_file(filesystem):
    # read .msg file
    pkg = "tf2_msgs"
    pkg_dir = "/opt/ros/melodic/share/tf2_msgs/"
    fn = os.path.join(pkg_dir, "msg/TFMessage.msg")
    fmt = ROS1MsgFormat.from_file(pkg, fn, filesystem)
    assert fmt.package == pkg
    assert fmt.name == "TFMessage"
    assert fmt.fullname == "tf2_msgs/TFMessage"
    assert not fmt.constants
    assert len(fmt.fields) == 1
    assert (Field("geometry_msgs/TransformStamped[]", "transforms")
            in fmt.fields)

    # attempt to read .action file
    fn = os.path.join(pkg_dir, "action/LookupTransform.action")
    with pytest.raises(AssertionError):
        ROS1SrvFormat.from_file(pkg, fn, filesystem)

    # attempt to read non-existent file
    fn = os.path.join(pkg_dir, "msg/Spooky.msg")
    with pytest.raises(dockerblade.exceptions.ContainerFileNotFound):
        ROS1MsgFormat.from_file(pkg, fn, filesystem)
Exemplo n.º 7
0
def test_msg_format_to_and_from_dict():
    definition = "geometry_msgs/TransformStamped[] transforms\n"
    d = {
        "package":
        "tf",
        "name":
        "tfMessage",
        "definition":
        definition,
        "fields": [{
            "type": "geometry_msgs/TransformStamped[]",
            "name": "transforms"
        }],
    }
    f = ROS1MsgFormat(
        package="tf",
        name="tfMessage",
        definition=definition,
        constants=[],
        fields=[Field("geometry_msgs/TransformStamped[]", "transforms")],
    )
    assert ROS1MsgFormat.from_dict(d) == f
    assert ROS1MsgFormat.from_dict(f.to_dict()) == f
Exemplo n.º 8
0
def test_to_and_from_dict():
    s = """
uint32 seq
time stamp
string frame_id
    """
    fmt = ROS1MsgFormat.from_string("PkgName", "MessageName", s)
    db_fmt = ROS1FormatDatabase({fmt}, {}, {})
    db_type = TypeDatabase.build(db_fmt)

    t = db_type["PkgName/MessageName"]
    m = t(seq=310, stamp=Time(30, 120), frame_id="foo")
    d = {"seq": 310, "stamp": {"secs": 30, "nsecs": 120}, "frame_id": "foo"}
    assert m.to_dict() == d
    assert db_type.from_dict(fmt, d) == m
Exemplo n.º 9
0
def test_msg_from_string():
    s = """
#Standard metadata for higher-level flow data types
#sequence ID: consecutively increasing ID
uint32 seq
#Two-integer timestamp that is expressed as:
# * stamp.secs: seconds (stamp_secs) since epoch
# * stamp.nsecs: nanoseconds since stamp_secs
# time-handling sugar is provided by the client library
time stamp
#Frame this data is associated with
string frame_id

int32 X=123
int32 Y=-123
string FOO=foo
string EXAMPLE="#comments" are ignored, and leading and trailing whitespace removed
    """
    fmt = ROS1MsgFormat.from_string("PkgName", "MessageName", s)
    assert fmt.name == "MessageName"
    assert fmt.package == "PkgName"

    assert len(fmt.constants) == 4
    assert Constant("int32", "X", "123") in fmt.constants
    assert Constant("int32", "Y", "-123") in fmt.constants
    assert Constant("string", "FOO", "foo") in fmt.constants
    assert (Constant(
        "string",
        "EXAMPLE",
        '"#comments" are ignored, and leading and trailing whitespace removed',
    ) in fmt.constants)  # noqa: pycodestyle

    assert len(fmt.fields) == 3
    assert Field("uint32", "seq") in fmt.fields
    assert Field("time", "stamp") in fmt.fields
    assert Field("string", "frame_id") in fmt.fields
Exemplo n.º 10
0
 def mf(name: str, definition: str) -> ROS1MsgFormat:
     f = ROS1MsgFormat.from_string("example_pkg", name, definition)
     register[f.fullname] = f
     return f