-
Notifications
You must be signed in to change notification settings - Fork 745
Expand file tree
/
Copy pathxml_tools_test.py
More file actions
75 lines (61 loc) · 2.34 KB
/
xml_tools_test.py
File metadata and controls
75 lines (61 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Copyright 2017 The dm_control Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Tests for utils.xml_tools."""
import io
from absl.testing import absltest
from dm_control.utils import xml_tools
import lxml
etree = lxml.etree
class XmlHelperTest(absltest.TestCase):
def test_nested(self):
element = etree.Element('inserted')
xml_tools.nested_element(element, depth=2)
level_1 = element.find('inserted')
self.assertIsNotNone(level_1)
level_2 = level_1.find('inserted')
self.assertIsNotNone(level_2)
def test_tostring(self):
xml_str = """
<root>
<head>
<content></content>
</head>
</root>"""
tree = xml_tools.parse(io.StringIO(xml_str))
self.assertEqual(b'<root>\n <head>\n <content/>\n </head>\n</root>\n',
etree.tostring(tree, pretty_print=True))
def test_find_element(self):
xml_str = """
<root>
<option name='option_name'>
<content></content>
</option>
<world name='world_name'>
<geom name='geom_name'/>
</world>
</root>"""
tree = xml_tools.parse(io.StringIO(xml_str))
world = xml_tools.find_element(root=tree, tag='world', name='world_name')
self.assertEqual(world.tag, 'world')
self.assertEqual(world.attrib['name'], 'world_name')
geom = xml_tools.find_element(root=tree, tag='geom', name='geom_name')
self.assertEqual(geom.tag, 'geom')
self.assertEqual(geom.attrib['name'], 'geom_name')
with self.assertRaisesRegex(ValueError, 'Element with tag'):
xml_tools.find_element(root=tree, tag='does_not_exist', name='name')
with self.assertRaisesRegex(ValueError, 'Element with tag'):
xml_tools.find_element(root=tree, tag='world', name='does_not_exist')
if __name__ == '__main__':
absltest.main()