Ezio Melotti | cd514cf | 2013-04-10 19:30:11 +0300 | [diff] [blame] | 1 | # Tests for xml.dom.minicompat |
| 2 | |
| 3 | import pickle |
| 4 | import unittest |
| 5 | |
| 6 | import xml.dom |
| 7 | from xml.dom.minicompat import * |
| 8 | |
| 9 | |
| 10 | class EmptyNodeListTestCase(unittest.TestCase): |
| 11 | """Tests for the EmptyNodeList class.""" |
| 12 | |
| 13 | def test_emptynodelist_item(self): |
| 14 | # Test item access on an EmptyNodeList. |
| 15 | node_list = EmptyNodeList() |
| 16 | |
| 17 | self.assertIsNone(node_list.item(0)) |
| 18 | self.assertIsNone(node_list.item(-1)) # invalid item |
| 19 | |
| 20 | with self.assertRaises(IndexError): |
| 21 | node_list[0] |
| 22 | with self.assertRaises(IndexError): |
| 23 | node_list[-1] |
| 24 | |
| 25 | def test_emptynodelist_length(self): |
| 26 | node_list = EmptyNodeList() |
| 27 | # Reading |
| 28 | self.assertEqual(node_list.length, 0) |
| 29 | # Writing |
| 30 | with self.assertRaises(xml.dom.NoModificationAllowedErr): |
| 31 | node_list.length = 111 |
| 32 | |
| 33 | def test_emptynodelist___add__(self): |
| 34 | node_list = EmptyNodeList() + NodeList() |
| 35 | self.assertEqual(node_list, NodeList()) |
| 36 | |
| 37 | def test_emptynodelist___radd__(self): |
| 38 | node_list = [1,2] + EmptyNodeList() |
| 39 | self.assertEqual(node_list, [1,2]) |
| 40 | |
| 41 | |
| 42 | class NodeListTestCase(unittest.TestCase): |
| 43 | """Tests for the NodeList class.""" |
| 44 | |
| 45 | def test_nodelist_item(self): |
| 46 | # Test items access on a NodeList. |
| 47 | # First, use an empty NodeList. |
| 48 | node_list = NodeList() |
| 49 | |
| 50 | self.assertIsNone(node_list.item(0)) |
| 51 | self.assertIsNone(node_list.item(-1)) |
| 52 | |
| 53 | with self.assertRaises(IndexError): |
| 54 | node_list[0] |
| 55 | with self.assertRaises(IndexError): |
| 56 | node_list[-1] |
| 57 | |
| 58 | # Now, use a NodeList with items. |
| 59 | node_list.append(111) |
| 60 | node_list.append(999) |
| 61 | |
| 62 | self.assertEqual(node_list.item(0), 111) |
| 63 | self.assertIsNone(node_list.item(-1)) # invalid item |
| 64 | |
| 65 | self.assertEqual(node_list[0], 111) |
| 66 | self.assertEqual(node_list[-1], 999) |
| 67 | |
| 68 | def test_nodelist_length(self): |
| 69 | node_list = NodeList([1, 2]) |
| 70 | # Reading |
| 71 | self.assertEqual(node_list.length, 2) |
| 72 | # Writing |
| 73 | with self.assertRaises(xml.dom.NoModificationAllowedErr): |
| 74 | node_list.length = 111 |
| 75 | |
| 76 | def test_nodelist___add__(self): |
| 77 | node_list = NodeList([3, 4]) + [1, 2] |
| 78 | self.assertEqual(node_list, NodeList([3, 4, 1, 2])) |
| 79 | |
| 80 | def test_nodelist___radd__(self): |
| 81 | node_list = [1, 2] + NodeList([3, 4]) |
| 82 | self.assertEqual(node_list, NodeList([1, 2, 3, 4])) |
| 83 | |
| 84 | def test_nodelist_pickle_roundtrip(self): |
| 85 | # Test pickling and unpickling of a NodeList. |
| 86 | |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame^] | 87 | for proto in range(pickle.HIGHEST_PROTOCOL + 1): |
| 88 | # Empty NodeList. |
| 89 | node_list = NodeList() |
| 90 | pickled = pickle.dumps(node_list, proto) |
| 91 | unpickled = pickle.loads(pickled) |
| 92 | self.assertEqual(unpickled, node_list) |
Ezio Melotti | cd514cf | 2013-04-10 19:30:11 +0300 | [diff] [blame] | 93 | |
Serhiy Storchaka | bad1257 | 2014-12-15 14:03:42 +0200 | [diff] [blame^] | 94 | # Non-empty NodeList. |
| 95 | node_list.append(1) |
| 96 | node_list.append(2) |
| 97 | pickled = pickle.dumps(node_list, proto) |
| 98 | unpickled = pickle.loads(pickled) |
| 99 | self.assertEqual(unpickled, node_list) |
Ezio Melotti | cd514cf | 2013-04-10 19:30:11 +0300 | [diff] [blame] | 100 | |
| 101 | if __name__ == '__main__': |
| 102 | unittest.main() |