Esempio n. 1
0
    def test_extra_generics(self):
        from IronPythonTest.StaticTest import B, GD, KD
        b = B()
        self.assertEqual(GD.M1(1), 'GD.M1')
        #self.assertEqual(GD.M1('s'), 'GB.M1')
        self.assertRaises(TypeError, GD.M1, b)

        #self.assertEqual(GD.M2('s'), 'GB.M2')
        self.assertEqual(GD.M2[str]('s'), 'GD.M2')
        self.assertEqual(GD.M2[int](2), 'GD.M2')
        self.assertEqual(GD.M2(1), 'GD.M2')

        self.assertEqual(GD.M3('s'), 'GD.M3')
        self.assertRaises(TypeError, GD.M3, 1)

        #self.assertEqual(GD.M4('s'), ('GB.M4-B', 's'))
        #self.assertEqual(GD.M4(), 'GB.M4-A')
        self.assertEqual(GD.M4(1), ('GD.M4', 1))

        self.assertEqual(GD.M21(1), 'GD.M21')
        #self.assertEqual(GD.M21(1,2), 'GB.M21')

        self.assertEqual(GD.M22(1), 'GD.M22')
        self.assertEqual(GD.M22(1, 2), 'GD.M22')

        self.assertRaises(TypeError, GD.M23)
        self.assertEqual(GD.M23(1), 'GD.M23')

        self.assertEqual(GD.M24(), 'GD.M24')
        #self.assertEqual(GD.M24(1), 'GB.M24')

        self.assertEqual(GD.M25(), 'GD.M25')
        self.assertEqual(GD.M25(1), 'GD.M25')

        self.assertEqual(KD[int].M1(10), 'KD.M1')
        #self.assertEqual(KD[int].M1('s'), 'KB.M1')

        self.assertRaises(TypeError, KD[str].M1, 10)
        self.assertEqual(KD[str].M1('s'), 'KD.M1')
Esempio n. 2
0
    def test_method(self):
        from IronPythonTest.StaticTest import B, Base, D, OverrideAll, OverrideNothing

        self.assertEqual(Base.Method_None(), 'Base.Method_None')
        self.assertEqual(OverrideNothing.Method_None(), 'Base.Method_None')
        self.assertEqual(OverrideAll.Method_None(), 'OverrideAll.Method_None')

        for type in self.allTypes:
            self.assertRaises(TypeError, type.Method_None, None)
            self.assertRaises(TypeError, type.Method_None, 1)

        self.assertEqual(Base.Method_OneArg(1), 'Base.Method_OneArg')
        self.assertEqual(OverrideNothing.Method_OneArg(1),
                         'Base.Method_OneArg')
        self.assertEqual(OverrideAll.Method_OneArg(1),
                         'OverrideAll.Method_OneArg')

        for type in self.allTypes:
            self.assertRaises(TypeError, type.Method_OneArg)
            self.assertRaises(TypeError, type.Method_OneArg, None)

        #==============================================================

        b, d1, d2 = Base(), OverrideNothing(), OverrideAll()
        for x in [b, d1, d2]:
            self.assertEqual(Base.Method_Base(x), 'Base.Method_Base')
            self.assertEqual(OverrideNothing.Method_Base(x),
                             'Base.Method_Base')

        self.assertRaisesMessage(TypeError, 'expected OverrideAll, got Base',
                                 OverrideAll.Method_Base, b)
        self.assertRaisesMessage(TypeError,
                                 'expected OverrideAll, got OverrideNothing',
                                 OverrideAll.Method_Base, d1)
        self.assertEqual(OverrideAll.Method_Base(d2),
                         'OverrideAll.Method_Base')

        #==============================================================

        b, d = B(), D()

        self.assertEqual(Base.Method_Inheritance1(b),
                         'Base.Method_Inheritance1')
        self.assertEqual(OverrideNothing.Method_Inheritance1(b),
                         'Base.Method_Inheritance1')
        self.assertRaisesMessage(TypeError, 'expected D, got B',
                                 OverrideAll.Method_Inheritance1, b)

        self.assertEqual(Base.Method_Inheritance1(d),
                         'Base.Method_Inheritance1')
        self.assertEqual(OverrideNothing.Method_Inheritance1(d),
                         'Base.Method_Inheritance1')
        self.assertEqual(OverrideAll.Method_Inheritance1(d),
                         'OverrideAll.Method_Inheritance1')

        self.assertRaisesMessage(TypeError, 'expected D, got B',
                                 Base.Method_Inheritance2, b)
        self.assertRaisesMessage(TypeError, 'expected D, got B',
                                 OverrideNothing.Method_Inheritance2, b)
        self.assertEqual(OverrideAll.Method_Inheritance2(b),
                         'OverrideAll.Method_Inheritance2')

        self.assertEqual(Base.Method_Inheritance2(d),
                         'Base.Method_Inheritance2')
        self.assertEqual(OverrideNothing.Method_Inheritance2(d),
                         'Base.Method_Inheritance2')
        self.assertEqual(OverrideAll.Method_Inheritance2(d),
                         'OverrideAll.Method_Inheritance2')

        self.assertRaises(TypeError, OverrideAll.Method_Inheritance3, b, b)
        # OverrideAll only gets the (D, B) overload because (B, D) would cause a conflict
        self.assertRaises(TypeError, OverrideAll.Method_Inheritance3, b, d)
        self.assertEqual(OverrideAll.Method_Inheritance3(d, b),
                         'OverrideAll.Method_Inheritance3')
        self.assertEqual(OverrideAll.Method_Inheritance3(d, d),
                         'OverrideAll.Method_Inheritance3')

        # play with instance
        b, o1, o2 = Base(), OverrideNothing(), OverrideAll()
        self.assertEqual(b.Method_None(), 'Base.Method_None')
        self.assertEqual(o1.Method_None(), 'Base.Method_None')
        self.assertEqual(o2.Method_None(), 'OverrideAll.Method_None')

        self.assertEqual(b.Method_Base(b), 'Base.Method_Base')
        self.assertEqual(o1.Method_Base(b), 'Base.Method_Base')
        self.assertRaisesMessage(TypeError, 'expected OverrideAll, got Base',
                                 o2.Method_Base, b)

        self.assertEqual(b.Method_Base(o1), 'Base.Method_Base')
        self.assertEqual(o1.Method_Base(o1), 'Base.Method_Base')
        self.assertRaisesMessage(TypeError,
                                 'expected OverrideAll, got OverrideNothing',
                                 o2.Method_Base, o1)

        self.assertEqual(b.Method_Base(o2), 'Base.Method_Base')
        self.assertEqual(o1.Method_Base(o2), 'Base.Method_Base')
        self.assertEqual(o2.Method_Base(o2), 'OverrideAll.Method_Base')

        # del
        def f(target):
            del target.Method_None

        self.assertRaisesMessage(
            AttributeError,
            "cannot delete attribute 'Method_None' of builtin type 'Base'", f,
            Base)
        self.assertRaisesMessage(
            AttributeError,
            "cannot delete attribute 'Method_None' of builtin type 'Base'", f,
            OverrideNothing)
        self.assertRaisesMessage(
            AttributeError,
            "cannot delete attribute 'Method_None' of builtin type 'OverrideAll'",
            f, OverrideAll)

        self.assertRaisesMessage(
            AttributeError,
            "cannot delete attribute 'Method_None' of builtin type 'Base'", f,
            b)
        self.assertRaisesMessage(
            AttributeError,
            "cannot delete attribute 'Method_None' of builtin type 'Base'", f,
            o1)
        self.assertRaisesMessage(
            AttributeError,
            "cannot delete attribute 'Method_None' of builtin type 'OverrideAll'",
            f, o2)