Exemplo n.º 1
0
    def test_chain02(self):
        "Inheritance."

        class Klass1:
            pass

        class Klass2(Klass1):
            pass

        class Klass3(Klass2):
            pass

        class InnerClass:
            data = None

            def __repr__(self):
                return 'InnerClass(data={})'.format(self.data)

        idc = InheritedDataChain(InnerClass)
        instance1 = idc[Klass1]
        instance1.data = Klass1.__name__
        instance2 = idc[Klass2]
        instance2.data = Klass2.__name__
        self.assertListEqual([instance1, instance2], [*idc.chain(Klass2)])

        instance3 = idc[Klass3]
        instance3.data = Klass3.__name__
        self.assertListEqual([instance1, instance2, instance3],
                             [*idc.chain(Klass3)])

        # Reversed
        self.assertListEqual([instance3, instance2, instance1],
                             [*idc.chain(Klass3, parent_first=False)])
Exemplo n.º 2
0
    def test_chain01(self):
        class Klass1:
            pass

        class Klass2:
            pass

        class InnerClass:
            pass

        idc = InheritedDataChain(InnerClass)

        with self.assertNoException():
            chain1 = [*idc.chain(Klass1)]

        self.assertFalse(chain1)

        instance1 = idc[Klass1]
        chain1 = [*idc.chain(Klass1)]
        self.assertEqual(1, len(chain1))
        self.assertIs(instance1, chain1[0])
        self.assertIs(instance1, next(idc.chain(Klass1)))

        instance2 = idc[Klass2]
        self.assertListEqual([instance2], [*idc.chain(Klass2)])