
import unittest
from blivet.devices import ParentList

class ParentListTestCase(unittest.TestCase):
    def testParentList(self):
        items = range(5)
        length = len(items)
        pl = ParentList(items=items)
        self.assertEqual(len(pl), length)
        self.assertEqual(list(pl), items)

        self.assertEqual(hasattr(pl, "index"), False)
        self.assertEqual(hasattr(pl, "insert"), False)
        self.assertEqual(hasattr(pl, "pop"), False)

        self.assertEqual(pl[:], pl.items)

        with self.assertRaises(TypeError):
            pl[2] = 99

        newval = 99
        self.assertEqual(99 in pl, False)
        pl.append(newval)
        length += 1
        self.assertEqual(len(pl), length)
        self.assertEqual(newval in pl, True)

        val = 3
        self.assertEqual(val in pl, True)
        pl.remove(val)
        length -= 1
        self.assertEqual(len(pl), length)
        self.assertEqual(val in pl, False)

        #
        # verify that add/remove functions work as expected
        #
        def pre_add(item):
            if item > 32:
                raise ValueError("only numbers less than 32 are allowed")

        def pre_remove(item):
            if len(pl) - 1 < 3:
                raise RuntimeError("list can never have fewer than 3 items")

        pl = ParentList(items=items, appendfunc=pre_add, removefunc=pre_remove)

        self.assertRaises(ValueError, pl.append, 33)

        pl.remove(4)
        pl.remove(3)
        self.assertRaises(RuntimeError, pl.remove, 2)


def suite():
    return unittest.TestLoader().loadTestsFromTestCase(ParentListTestCase)


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


@@ -47,16 +47,50 @@
 rentList
+%0Afrom blivet.devices import Device
 %0A%0Aclass 
@@ -1568,16 +1568,611 @@
 ve, 2)%0A%0A
+    def testDeviceParents(self):%0A        %22%22%22 Verify that Device.parents functions as expected. %22%22%22%0A        dev1 = Device(%22dev1%22, %5B%5D)%0A        self.assertEqual(len(dev1.parents), 0)%0A        self.assertIsInstance(dev1.parents, ParentList)%0A        dev2 = Device(%22dev2%22)%0A        dev3 = Device(%22dev3%22, %5Bdev1%5D)%0A        self.assertEqual(len(dev3.parents), 1)%0A        dev3.parents.remove(dev1)%0A        self.assertEqual(len(dev3.parents), 0)%0A        dev3.parents.append(dev1)%0A        self.assertEqual(len(dev3.parents), 1)%0A        dev3.parents.append(dev2)%0A        self.assertEqual(len(dev3.parents), 2)%0A%0A
 %0Adef sui

Add tests that use ParentList as part of Device.