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