Skip to content

Commit

Permalink
Make sure to add the version when creating a new URDF. (#62)
Browse files Browse the repository at this point in the history
It turns out that when I added the version stuff, I completely
forgot about creating a new URDF.  This PR fixes it so that
we can successfully round-trip through the URDF parser, and
adds tests to ensure the same.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
  • Loading branch information
clalancette authored Jul 13, 2020
1 parent a47a778 commit b44187f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/urdf_parser_py/urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,10 +471,16 @@ def check_valid(self):


class Robot(xmlr.Object):
def __init__(self, name=None):
SUPPORTED_VERSIONS = ["1.0"]

def __init__(self, name=None, version="1.0"):
self.aggregate_init()

self.name = name
if version not in self.SUPPORTED_VERSIONS:
raise ValueError("Invalid version; only %s is supported" % (','.join(self.SUPPORTED_VERSIONS)))

self.version = version
self.joints = []
self.links = []
self.materials = []
Expand Down Expand Up @@ -547,8 +553,8 @@ def post_read_xml(self):
if int(split[0]) < 0 or int(split[1]) < 0:
raise ValueError("Version number must be positive")

if self.version != "1.0":
raise ValueError("Invalid version; only 1.0 is supported")
if self.version not in self.SUPPORTED_VERSIONS:
raise ValueError("Invalid version; only %s is supported" % (','.join(self.SUPPORTED_VERSIONS)))


xmlr.reflect(Robot, tag='robot', params=[
Expand Down
16 changes: 16 additions & 0 deletions test/test_urdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,21 @@ def test_multi_collision_access(self):
self.assertEqual(id(dummyObject), id(robot.links[0].collisions[0]))


class TestCreateNew(unittest.TestCase):
def test_new_urdf(self):
testcase = urdf.URDF('robot_name').to_xml()
self.assertTrue('name' in testcase.keys())
self.assertTrue('version' in testcase.keys())
self.assertEqual(testcase.get('name'), 'robot_name')
self.assertEqual(testcase.get('version'), '1.0')

def test_new_urdf_with_version(self):
testcase = urdf.URDF('robot_name', '1.0').to_xml()
self.assertTrue('name' in testcase.keys())
self.assertTrue('version' in testcase.keys())
self.assertEqual(testcase.get('name'), 'robot_name')
self.assertEqual(testcase.get('version'), '1.0')


if __name__ == '__main__':
unittest.main()

0 comments on commit b44187f

Please sign in to comment.