test_urdf.py
Go to the documentation of this file.
1 from __future__ import print_function
2 
3 import unittest
4 import mock
5 import os
6 import sys
7 
8 # Add path to import xml_matching
9 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '.')))
10 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),
11  '../src')))
12 
13 from xml.dom import minidom # noqa
14 from xml_matching import xml_matches # noqa
15 from urdf_parser_py import urdf # noqa
16 
17 
18 class ParseException(Exception):
19  pass
20 
21 
22 class TestURDFParser(unittest.TestCase):
23  @mock.patch('urdf_parser_py.xml_reflection.on_error',
24  mock.Mock(side_effect=ParseException))
25  def parse(self, xml):
26  return urdf.Robot.from_xml_string(xml)
27 
28  def parse_and_compare(self, orig):
29  xml = minidom.parseString(orig)
30  robot = urdf.Robot.from_xml_string(orig)
31  rewritten = minidom.parseString(robot.to_xml_string())
32  self.assertTrue(xml_matches(xml, rewritten))
33 
35  xml = '''<?xml version="1.0"?>
36 <robot name="test">
37  <transmission name="simple_trans">
38  <type>transmission_interface/SimpleTransmission</type>
39  <joint name="foo_joint">
40  <hardwareInterface>EffortJointInterface</hardwareInterface>
41  </joint>
42  <actuator name="foo_motor">
43  <mechanicalReduction>50.0</mechanicalReduction>
44  </actuator>
45  </transmission>
46 </robot>'''
47  self.parse_and_compare(xml)
48 
50  xml = '''<?xml version="1.0"?>
51 <robot name="test">
52  <transmission name="simple_trans">
53  <type>transmission_interface/SimpleTransmission</type>
54  <joint name="foo_joint">
55  <hardwareInterface>EffortJointInterface</hardwareInterface>
56  </joint>
57  <joint name="bar_joint">
58  <hardwareInterface>EffortJointInterface</hardwareInterface>
59  <hardwareInterface>EffortJointInterface</hardwareInterface>
60  </joint>
61  <actuator name="foo_motor">
62  <mechanicalReduction>50.0</mechanicalReduction>
63  </actuator>
64  </transmission>
65 </robot>'''
66  self.parse_and_compare(xml)
67 
69  xml = '''<?xml version="1.0"?>
70 <robot name="test">
71  <transmission name="simple_trans">
72  <type>transmission_interface/SimpleTransmission</type>
73  <joint name="foo_joint">
74  <hardwareInterface>EffortJointInterface</hardwareInterface>
75  </joint>
76  <actuator name="foo_motor">
77  <mechanicalReduction>50.0</mechanicalReduction>
78  </actuator>
79  <actuator name="bar_motor"/>
80  </transmission>
81 </robot>'''
82  self.parse_and_compare(xml)
83 
85  xml = '''<?xml version="1.0"?>
86 <robot name="test">
87  <transmission name="simple_trans">
88  <type>transmission_interface/SimpleTransmission</type>
89  </transmission>
90 </robot>'''
91  self.assertRaises(Exception, self.parse, xml)
92 
94  xml = '''<?xml version="1.0"?>
95 <robot name="test">
96  <transmission name="simple_trans">
97  <type>transmission_interface/SimpleTransmission</type>
98  <joint name="foo_joint">
99  <hardwareInterface>EffortJointInterface</hardwareInterface>
100  </joint>
101  </transmission>
102 </robot>'''
103  self.assertRaises(Exception, self.parse, xml)
104 
106  xml = '''<?xml version="1.0"?>
107 <robot name="test">
108  <transmission name="PR2_trans" type="SimpleTransmission">
109  <joint name="foo_joint"/>
110  <actuator name="foo_motor"/>
111  <mechanicalReduction>1.0</mechanicalReduction>
112  </transmission>
113 </robot>'''
114  self.parse_and_compare(xml)
115 
117  xml = '''<?xml version="1.0"?>
118 <robot name="test">
119  <link name="link">
120  <visual>
121  <geometry>
122  <cylinder length="1" radius="1"/>
123  </geometry>
124  <material name="mat"/>
125  </visual>
126  </link>
127 </robot>'''
128  self.parse_and_compare(xml)
129 
131  xml = '''<?xml version="1.0"?>
132 <robot name="test">
133  <material name="mat">
134  <color rgba="0.0 0.0 0.0 1.0"/>
135  </material>
136 </robot>'''
137  self.parse_and_compare(xml)
138 
140  xml = '''<?xml version="1.0"?>
141 <robot name="test">
142  <material name="mat"/>
143 </robot>'''
144  self.assertRaises(ParseException, self.parse, xml)
145 
146 
147 class LinkOriginTestCase(unittest.TestCase):
148  @mock.patch('urdf_parser_py.xml_reflection.on_error',
149  mock.Mock(side_effect=ParseException))
150  def parse(self, xml):
151  return urdf.Robot.from_xml_string(xml)
152 
154  xml = '''<?xml version="1.0"?>
155 <robot name="test">
156  <link name="test_link">
157  <inertial>
158  <mass value="10.0"/>
159  <origin/>
160  </inertial>
161  </link>
162 </robot>'''
163  robot = self.parse(xml)
164  origin = robot.links[0].inertial.origin
165  self.assertEquals(origin.xyz, [0, 0, 0])
166  self.assertEquals(origin.rpy, [0, 0, 0])
167 
169  xml = '''<?xml version="1.0"?>
170 <robot name="test">
171  <link name="test_link">
172  <inertial>
173  <mass value="10.0"/>
174  <origin xyz="1 2 3"/>
175  </inertial>
176  </link>
177 </robot>'''
178  robot = self.parse(xml)
179  origin = robot.links[0].inertial.origin
180  self.assertEquals(origin.xyz, [1, 2, 3])
181  self.assertEquals(origin.rpy, [0, 0, 0])
182 
183 
184 if __name__ == '__main__':
185  unittest.main()
def test_robot_material(self)
Definition: test_urdf.py:130
def test_new_transmission_multiple_joints(self)
Definition: test_urdf.py:49
def test_old_transmission(self)
Definition: test_urdf.py:105
def test_robot_link_defaults_xyz_set(self)
Definition: test_urdf.py:168
def parse(self, xml)
Definition: test_urdf.py:25
def xml_matches(a, b, ignore_nodes=[])
Definition: xml_matching.py:96
def test_new_transmission_missing_actuator(self)
Definition: test_urdf.py:93
def test_new_transmission(self)
Definition: test_urdf.py:34
def test_link_material_missing_color_and_texture(self)
Definition: test_urdf.py:116
def test_new_transmission_missing_joint(self)
Definition: test_urdf.py:84
def test_robot_link_defaults(self)
Definition: test_urdf.py:153
def test_robot_material_missing_color_and_texture(self)
Definition: test_urdf.py:139
def test_new_transmission_multiple_actuators(self)
Definition: test_urdf.py:68
def parse_and_compare(self, orig)
Definition: test_urdf.py:28


urdfdom_py
Author(s): Thomas Moulard, David Lu, Kelsey Hawkins, Antonio El Khoury, Eric Cousineau, Ioan Sucan , Jackie Kay
autogenerated on Fri Jun 7 2019 21:42:12