tests for xml dom minicompat tests for the emptynodelist class def testemptynodelistitemself test item access on an emptynodelist nodelist emptynodelist self assertisnonenodelist item0 self assertisnonenodelist item1 invalid item with self assertraisesindexerror nodelist0 with self assertraisesindexerror nodelist1 def testemptynodelistlengthself nodelist emptynodelist reading self assertequalnodelist length 0 writing with self assertraisesxml dom nomodificationallowederr nodelist length 111 def testemptynodelistaddself nodelist emptynodelist nodelist self assertequalnodelist nodelist def testemptynodelistraddself nodelist 1 2 emptynodelist self assertequalnodelist 1 2 class nodelisttestcaseunittest testcase test items access on a nodelist first use an empty nodelist now use a nodelist with items reading writing test pickling and unpickling of a nodelist empty nodelist nonempty nodelist empty nodelist nonempty nodelist empty nodelist nonempty nodelist tests for xml dom minicompat tests for the emptynodelist class test item access on an emptynodelist invalid item reading writing tests for the nodelist class test items access on a nodelist first use an empty nodelist now use a nodelist with items invalid item reading writing test pickling and unpickling of a nodelist empty nodelist non empty nodelist empty nodelist non empty nodelist empty nodelist non empty nodelist
import copy
import pickle
import unittest

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


class EmptyNodeListTestCase(unittest.TestCase):
    

    def test_emptynodelist_item(self):
        
        node_list = EmptyNodeList()

        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]

    def test_emptynodelist_length(self):
        node_list = EmptyNodeList()
        
        self.assertEqual(node_list.length, 0)
        
        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):
    

    def test_nodelist_item(self):
        
        
        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]

        
        node_list.append(111)
        node_list.append(999)

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

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

    def test_nodelist_length(self):
        node_list = NodeList([1, 2])
        
        self.assertEqual(node_list.length, 2)
        
        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):
        

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

            
            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):
        
        node_list = NodeList()
        copied = copy.copy(node_list)
        self.assertIsNot(copied, node_list)
        self.assertEqual(copied, node_list)

        
        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):
        
        node_list = NodeList()
        copied = copy.deepcopy(node_list)
        self.assertIsNot(copied, node_list)
        self.assertEqual(copied, node_list)

        
        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()