summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_xml_dom_minicompat.py
blob: 3b03dfc53995a5bb9b98e636611ab71c69475e32 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Tests for xml.dom.minicompat

import copy
import pickle
import unittest

import xml.dom
from xml.dom.minicompat import *


class EmptyNodeListTestCase(unittest.TestCase):
    """Tests for the EmptyNodeList class."""

    def test_emptynodelist_item(self):
        # Test item access on an EmptyNodeList.
        node_list = EmptyNodeList()

        self.assertIsNone(node_list.item(0))
        self.assertIsNone(node_list.item(-1)) # invalid item

        with self.assertRaises(IndexError):
            node_list[0]
        with self.assertRaises(IndexError):
            node_list[-1]

    def test_emptynodelist_length(self):
        node_list = EmptyNodeList()
        # Reading
        self.assertEqual(node_list.length, 0)
        # Writing
        with self.assertRaises(xml.dom.NoModificationAllowedErr):
            node_list.length = 111

    def test_emptynodelist___add__(self):
        node_list = EmptyNodeList() + NodeList()
        self.assertEqual(node_list, NodeList())

    def test_emptynodelist___radd__(self):
        node_list = [1,2] + EmptyNodeList()
        self.assertEqual(node_list, [1,2])


class NodeListTestCase(unittest.TestCase):
    """Tests for the NodeList class."""

    def test_nodelist_item(self):
        # Test items access on a NodeList.
        # First, use an empty NodeList.
        node_list = NodeList()

        self.assertIsNone(node_list.item(0))
        self.assertIsNone(node_list.item(-1))

        with self.assertRaises(IndexError):
            node_list[0]
        with self.assertRaises(IndexError):
            node_list[-1]

        # Now, use a NodeList with items.
        node_list.append(111)
        node_list.append(999)

        self.assertEqual(node_list.item(0), 111)
        self.assertIsNone(node_list.item(-1)) # invalid item

        self.assertEqual(node_list[0], 111)
        self.assertEqual(node_list[-1], 999)

    def test_nodelist_length(self):
        node_list = NodeList([1, 2])
        # Reading
        self.assertEqual(node_list.length, 2)
        # Writing
        with self.assertRaises(xml.dom.NoModificationAllowedErr):
            node_list.length = 111

    def test_nodelist___add__(self):
        node_list = NodeList([3, 4]) + [1, 2]
        self.assertEqual(node_list, NodeList([3, 4, 1, 2]))

    def test_nodelist___radd__(self):
        node_list = [1, 2] + NodeList([3, 4])
        self.assertEqual(node_list, NodeList([1, 2, 3, 4]))

    def test_nodelist_pickle_roundtrip(self):
        # Test pickling and unpickling of a NodeList.

        for proto in range(pickle.HIGHEST_PROTOCOL + 1):
            # Empty NodeList.
            node_list = NodeList()
            pickled = pickle.dumps(node_list, proto)
            unpickled = pickle.loads(pickled)
            self.assertIsNot(unpickled, node_list)
            self.assertEqual(unpickled, node_list)

            # Non-empty NodeList.
            node_list.append(1)
            node_list.append(2)
            pickled = pickle.dumps(node_list, proto)
            unpickled = pickle.loads(pickled)
            self.assertIsNot(unpickled, node_list)
            self.assertEqual(unpickled, node_list)

    def test_nodelist_copy(self):
        # Empty NodeList.
        node_list = NodeList()
        copied = copy.copy(node_list)
        self.assertIsNot(copied, node_list)
        self.assertEqual(copied, node_list)

        # Non-empty NodeList.
        node_list.append([1])
        node_list.append([2])
        copied = copy.copy(node_list)
        self.assertIsNot(copied, node_list)
        self.assertEqual(copied, node_list)
        for x, y in zip(copied, node_list):
            self.assertIs(x, y)

    def test_nodelist_deepcopy(self):
        # Empty NodeList.
        node_list = NodeList()
        copied = copy.deepcopy(node_list)
        self.assertIsNot(copied, node_list)
        self.assertEqual(copied, node_list)

        # Non-empty NodeList.
        node_list.append([1])
        node_list.append([2])
        copied = copy.deepcopy(node_list)
        self.assertIsNot(copied, node_list)
        self.assertEqual(copied, node_list)
        for x, y in zip(copied, node_list):
            self.assertIsNot(x, y)
            self.assertEqual(x, y)

if __name__ == '__main__':
    unittest.main()