Ejemplo n.º 1
0
    def test_base(self):
        """properties w/ differening access"""
        from IronPythonTest import BaseClass
        a = BaseClass()
        self.assertEqual(a.Area, 0)

        def setA(a, val):
            a.Area = val

        self.assertRaises(AttributeError, setA, a, 16)
        self.assertEqual(a.Area, 0)

        class WrapBaseClass(BaseClass):
            pass

        a = WrapBaseClass()
        self.assertEqual(a.Area, 0)
        a.Area = 16
        self.assertEqual(a.Area, 16)
Ejemplo n.º 2
0
    def test_mutable_Valuetypes(self):
        self.load_iron_python_test()
        from IronPythonTest import MySize, BaseClass

        direct_vt = MySize(1, 2)
        embedded_vt = BaseClass()
        embedded_vt.Width = 3
        embedded_vt.Height = 4

        # Read access should still succeed.
        self.assertEqual(direct_vt.width, 1)
        self.assertEqual(embedded_vt.size.width, 3)
        self.assertEqual(embedded_vt.Size.width, 3)

        # But writes to value type fields should fail with ValueError.
        success = 0
        try:
            direct_vt.width = 5
        except ValueError:
            success = 1
        self.assertTrue(success == 0 and direct_vt.width == 1)

        success = 0
        try:
            embedded_vt.size.width = 5
        except ValueError:
            success = 1
        self.assertTrue(success == 0 and embedded_vt.size.width == 3)

        success = 0
        try:
            embedded_vt.Size.width = 5
        except ValueError:
            success = 1
        self.assertTrue(success == 0 and embedded_vt.Size.width == 3)

        import clr
        # ensure .GetType() and calling the helper w/ the type work
        self.assertEqual(clr.GetClrType(str), ''.GetType())
        # and ensure we're not just auto-converting back on both of them
        self.assertEqual(clr.GetClrType(str), str)
        self.assertEqual(clr.GetClrType(str) != str, False)

        # as well as GetPythonType
        import System
        self.assertEqual(clr.GetPythonType(System.Int32), int)
        self.assertEqual(clr.GetPythonType(clr.GetClrType(int)), int)

        # verify we can't create *Ops classes
        from IronPython.Runtime.Operations import DoubleOps
        self.assertRaises(TypeError, DoubleOps)

        # setting mro to an invalid value should result in
        # bases still being correct
        class foo(object):
            pass

        class bar(foo):
            pass

        class baz(foo):
            pass

        def changeBazBase():
            baz.__bases__ = (foo, bar)  # illegal MRO

        self.assertRaises(TypeError, changeBazBase)

        self.assertEqual(baz.__bases__, (foo, ))
        self.assertEqual(baz.__mro__, (baz, foo, object))

        d = {}
        d[None, 1] = 2
        self.assertEqual(d, {(None, 1): 2})
Ejemplo n.º 3
0
def test_mutable_Valuetypes():
    load_iron_python_test()
    from IronPythonTest import MySize, BaseClass
    
    direct_vt = MySize(1, 2)
    embedded_vt = BaseClass()
    embedded_vt.Width = 3
    embedded_vt.Height = 4
    
    # Read access should still succeed.
    AreEqual(direct_vt.width, 1)
    AreEqual(embedded_vt.size.width, 3)
    AreEqual(embedded_vt.Size.width, 3)
    
    # But writes to value type fields should fail with ValueError.
    success = 0
    try:
        direct_vt.width = 5
    except ValueError:
        success = 1
    Assert(success == 0 and direct_vt.width == 1)
    
    success = 0
    try:
        embedded_vt.size.width = 5
    except ValueError:
        success = 1
    Assert(success == 0 and embedded_vt.size.width == 3)
    
    success = 0
    try:
        embedded_vt.Size.width = 5
    except ValueError:
        success = 1
    Assert(success == 0 and embedded_vt.Size.width == 3)
    
    import clr
    # ensure .GetType() and calling the helper w/ the type work
    AreEqual(clr.GetClrType(str), ''.GetType())
    # and ensure we're not just auto-converting back on both of them
    AreEqual(clr.GetClrType(str), str)
    AreEqual(clr.GetClrType(str) != str, False)
    
    # as well as GetPythonType
    import System
    AreEqual(clr.GetPythonType(System.Int32), int)
    AreEqual(clr.GetPythonType(clr.GetClrType(int)), int)

    # verify we can't create *Ops classes
    from IronPython.Runtime.Operations import DoubleOps
    AssertError(TypeError, DoubleOps)
    
    # setting mro to an invalid value should result in
    # bases still being correct
    class foo(object): pass
    
    class bar(foo): pass
    
    class baz(foo): pass
    
    def changeBazBase():
        baz.__bases__ = (foo, bar)  # illegal MRO
    
    AssertError(TypeError, changeBazBase)
    
    AreEqual(baz.__bases__, (foo, ))
    AreEqual(baz.__mro__, (baz, foo, object))
    
    d = {}
    d[None, 1] = 2
    AreEqual(d, {(None, 1): 2})