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