forked from sepeth/pugixmltodict
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
103 lines (85 loc) · 3.26 KB
/
tests.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import unittest
from pugixmltodict import parse, unparse
class XmlToDictTestCase(unittest.TestCase):
def test_minimal(self):
self.assertEqual(parse('<a/>'), {'a': None})
def test_simple(self):
self.assertEqual(parse('<a>data</a>'), {'a': 'data'})
def test_list(self):
self.assertEqual(parse('<a><b>1</b><b>2</b><b>3</b></a>'),
{'a': {'b': ['1', '2', '3']}})
def test_attrib(self):
self.assertEqual(parse('<a href="xyz"/>'),
{'a': {'@href': 'xyz'}})
def test_attrib_and_text(self):
self.assertEqual(parse('<a href="xyz">123</a>'),
{'a': {'@href': 'xyz', '#text': '123'}})
def test_semi_structured(self):
self.assertEqual(parse('<a>abc<b/>def</a>'),
{'a': {'b': None, '#text': 'abcdef'}})
def test_nested_semi_structured(self):
self.assertEqual(parse('<a>abc<b>123<c/>456</b>def</a>'),
{'a': {'#text': 'abcdef', 'b': {
'#text': '123456', 'c': None}}})
def test_skip_whitespace(self):
xml = """
<root>
<emptya> </emptya>
<emptyb attr="attrvalue">
</emptyb>
<value>hello</value>
</root>
"""
self.assertEqual(
parse(xml),
{'root': {'emptya': None,
'emptyb': {'@attr': 'attrvalue'},
'value': 'hello'}})
def test_namespace_ignore(self):
xml = """
<root xmlns="http://defaultns.com/"
xmlns:a="http://a.com/"
xmlns:b="http://b.com/">
<x>1</x>
<a:y>2</a:y>
<b:z>3</b:z>
</root>
"""
d = {
'root': {
'@xmlns': 'http://defaultns.com/',
'@xmlns:a': 'http://a.com/',
'@xmlns:b': 'http://b.com/',
'x': '1',
'a:y': '2',
'b:z': '3',
},
}
self.assertEqual(parse(xml), d)
def test_with_broken_attribute(self):
with self.assertRaises(ValueError):
parse('<root attr>foo</root>')
def test_with_mismatched_tag(self):
with self.assertRaises(ValueError):
parse('<root attr="val">text</wrong>')
class DictToXmlTestCase(unittest.TestCase):
def test_root(self):
obj = {'a': None}
self.assertEqual(obj, parse(unparse(obj)))
self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))
def test_simple_text(self):
obj = {'a': 'b'}
self.assertEqual(obj, parse(unparse(obj)))
self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))
def test_attrib(self):
obj = {'a': {'@href': 'x'}}
self.assertEqual(obj, parse(unparse(obj)))
self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))
def test_attrib_and_text(self):
obj = {'a': {'@href': 'x', '#text': 'y'}}
self.assertEqual(obj, parse(unparse(obj)))
self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))
def test_list(self):
obj = {'a': {'b': ['1', '2', '3']}}
self.assertEqual(obj, parse(unparse(obj)))
self.assertEqual(unparse(obj), unparse(parse(unparse(obj))))