ยปCore Development>Code coverage>Lib/test/test_xml_dom_minicompat.py

Python code coverage for Lib/test/test_xml_dom_minicompat.py

#countcontent
1n/a# Tests for xml.dom.minicompat
2n/a
3n/aimport copy
4n/aimport pickle
5n/aimport unittest
6n/a
7n/aimport xml.dom
8n/afrom xml.dom.minicompat import *
9n/a
10n/a
11n/aclass EmptyNodeListTestCase(unittest.TestCase):
12n/a """Tests for the EmptyNodeList class."""
13n/a
14n/a def test_emptynodelist_item(self):
15n/a # Test item access on an EmptyNodeList.
16n/a node_list = EmptyNodeList()
17n/a
18n/a self.assertIsNone(node_list.item(0))
19n/a self.assertIsNone(node_list.item(-1)) # invalid item
20n/a
21n/a with self.assertRaises(IndexError):
22n/a node_list[0]
23n/a with self.assertRaises(IndexError):
24n/a node_list[-1]
25n/a
26n/a def test_emptynodelist_length(self):
27n/a node_list = EmptyNodeList()
28n/a # Reading
29n/a self.assertEqual(node_list.length, 0)
30n/a # Writing
31n/a with self.assertRaises(xml.dom.NoModificationAllowedErr):
32n/a node_list.length = 111
33n/a
34n/a def test_emptynodelist___add__(self):
35n/a node_list = EmptyNodeList() + NodeList()
36n/a self.assertEqual(node_list, NodeList())
37n/a
38n/a def test_emptynodelist___radd__(self):
39n/a node_list = [1,2] + EmptyNodeList()
40n/a self.assertEqual(node_list, [1,2])
41n/a
42n/a
43n/aclass NodeListTestCase(unittest.TestCase):
44n/a """Tests for the NodeList class."""
45n/a
46n/a def test_nodelist_item(self):
47n/a # Test items access on a NodeList.
48n/a # First, use an empty NodeList.
49n/a node_list = NodeList()
50n/a
51n/a self.assertIsNone(node_list.item(0))
52n/a self.assertIsNone(node_list.item(-1))
53n/a
54n/a with self.assertRaises(IndexError):
55n/a node_list[0]
56n/a with self.assertRaises(IndexError):
57n/a node_list[-1]
58n/a
59n/a # Now, use a NodeList with items.
60n/a node_list.append(111)
61n/a node_list.append(999)
62n/a
63n/a self.assertEqual(node_list.item(0), 111)
64n/a self.assertIsNone(node_list.item(-1)) # invalid item
65n/a
66n/a self.assertEqual(node_list[0], 111)
67n/a self.assertEqual(node_list[-1], 999)
68n/a
69n/a def test_nodelist_length(self):
70n/a node_list = NodeList([1, 2])
71n/a # Reading
72n/a self.assertEqual(node_list.length, 2)
73n/a # Writing
74n/a with self.assertRaises(xml.dom.NoModificationAllowedErr):
75n/a node_list.length = 111
76n/a
77n/a def test_nodelist___add__(self):
78n/a node_list = NodeList([3, 4]) + [1, 2]
79n/a self.assertEqual(node_list, NodeList([3, 4, 1, 2]))
80n/a
81n/a def test_nodelist___radd__(self):
82n/a node_list = [1, 2] + NodeList([3, 4])
83n/a self.assertEqual(node_list, NodeList([1, 2, 3, 4]))
84n/a
85n/a def test_nodelist_pickle_roundtrip(self):
86n/a # Test pickling and unpickling of a NodeList.
87n/a
88n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
89n/a # Empty NodeList.
90n/a node_list = NodeList()
91n/a pickled = pickle.dumps(node_list, proto)
92n/a unpickled = pickle.loads(pickled)
93n/a self.assertIsNot(unpickled, node_list)
94n/a self.assertEqual(unpickled, node_list)
95n/a
96n/a # Non-empty NodeList.
97n/a node_list.append(1)
98n/a node_list.append(2)
99n/a pickled = pickle.dumps(node_list, proto)
100n/a unpickled = pickle.loads(pickled)
101n/a self.assertIsNot(unpickled, node_list)
102n/a self.assertEqual(unpickled, node_list)
103n/a
104n/a def test_nodelist_copy(self):
105n/a # Empty NodeList.
106n/a node_list = NodeList()
107n/a copied = copy.copy(node_list)
108n/a self.assertIsNot(copied, node_list)
109n/a self.assertEqual(copied, node_list)
110n/a
111n/a # Non-empty NodeList.
112n/a node_list.append([1])
113n/a node_list.append([2])
114n/a copied = copy.copy(node_list)
115n/a self.assertIsNot(copied, node_list)
116n/a self.assertEqual(copied, node_list)
117n/a for x, y in zip(copied, node_list):
118n/a self.assertIs(x, y)
119n/a
120n/a def test_nodelist_deepcopy(self):
121n/a # Empty NodeList.
122n/a node_list = NodeList()
123n/a copied = copy.deepcopy(node_list)
124n/a self.assertIsNot(copied, node_list)
125n/a self.assertEqual(copied, node_list)
126n/a
127n/a # Non-empty NodeList.
128n/a node_list.append([1])
129n/a node_list.append([2])
130n/a copied = copy.deepcopy(node_list)
131n/a self.assertIsNot(copied, node_list)
132n/a self.assertEqual(copied, node_list)
133n/a for x, y in zip(copied, node_list):
134n/a self.assertIsNot(x, y)
135n/a self.assertEqual(x, y)
136n/a
137n/aif __name__ == '__main__':
138n/a unittest.main()