test_identity.py :  » Development » PyObjC » trunk » pyobjc » pyobjc-core » PyObjCTest » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » PyObjC 
PyObjC » trunk » pyobjc » pyobjc core » PyObjCTest » test_identity.py
# Some tests that verify that object identity is correctly retained 
# accross the bridge
# 
# TODO:
# - Add unittests here for every class that is treated specially by 
#   the bridge.
# - Add unittests that test for "real-world" scenarios (writing stuff
#   to plists, ...)
# - Implement the required functionality

import sys, os
import objc
from PyObjCTest.identity import *
from PyObjCTools.TestSupport import *

class TestPythonRoundTrip (TestCase):
    # TODO: verify

    def testBasicPython(self):

        container = OC_TestIdentity.alloc().init()

        for v in (self, object(), list, sum):
            container.setStoredObject_(v)
            self.assert_(v is container.storedObject(), repr(v))


    def testPythonContainer(self):

        container = OC_TestIdentity.alloc().init()

        for v in ( [1, 2,3], (1,2,3), {1:2, 3:4} ):
            container.setStoredObject_(v)
            self.assert_(v is container.storedObject(), repr(v))

    def dont_testPythonStrings(self):
        # XXX: this test would always fail, this is by design.

        container = OC_TestIdentity.alloc().init()

        for v in ( u"Hello world", "Hello world" ):
            container.setStoredObject_(v)
            self.assert_(v is container.storedObject(), repr(v))

    def dont_testPythonNumber(self):
        # XXX: this test would always fail, need to move some code to C
        # to fix (but not now)
        container = OC_TestIdentity.alloc().init()

        for v in (99999, 99999L, 10.0):
            container.setStoredObject_(v)
            self.assert_(v is container.storedObject, repr(v))


class ObjCRoundTrip (TestCase):
    # TODO: NSProxy

    def testNSObject(self):
        container = OC_TestIdentity.alloc().init()

        cls = objc.lookUpClass("NSObject")
        container.setStoredObjectToResultOf_on_("new", cls)

        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))

        container.setStoredObjectToResultOf_on_("class", cls)
        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))

    def testNSString(self):

        container = OC_TestIdentity.alloc().init()

        cls = objc.lookUpClass("NSObject")
        container.setStoredObjectToResultOf_on_("description", cls)
        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))
        self.assert_(isinstance(v, unicode))

        cls = objc.lookUpClass("NSMutableString")
        container.setStoredObjectToResultOf_on_("new", cls)
        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))
        self.assert_(isinstance(v, unicode))

    def testProtocol(self):
        container = OC_TestIdentity.alloc().init()

        container.setStoredObjectToAProtocol()
        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))
        self.assert_(isinstance(v, objc.formal_protocol))

    if sys.maxint < 2 ** 32:
        def testObject(self):
            container = OC_TestIdentity.alloc().init()
            cls = objc.lookUpClass("Object")
            container.setStoredObjectAnInstanceOfClassic_(cls)
            v = container.storedObject()
            self.assert_(container.isSameObjectAsStored_(v), repr(v))
            self.assert_(isinstance(v, cls))

    def testNSNumber(self):
        container = OC_TestIdentity.alloc().init()

        container.setStoredObjectToInteger_(10)
        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))

        container.setStoredObjectToInteger_(-40)
        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))

        container.setStoredObjectToUnsignedInteger_(40)
        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))

        container.setStoredObjectToUnsignedInteger_(2 ** 32 - 4)
        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))

        if sys.maxint < 2 ** 32:
            container.setStoredObjectToLongLong_(sys.maxint * 2)
            v = container.storedObject()
            self.assert_(container.isSameObjectAsStored_(v), repr(v))

        container.setStoredObjectToLongLong_(-sys.maxint)
        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))

        container.setStoredObjectToUnsignedLongLong_(sys.maxint * 2)
        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))

        container.setStoredObjectToFloat_(10.0)
        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))

        container.setStoredObjectToDouble_(9999.0)
        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))

    def testNSDecimalNumber(self):
        container = OC_TestIdentity.alloc().init()
        cls = objc.lookUpClass("NSDecimalNumber")
        container.setStoredObjectToResultOf_on_("zero", cls)
        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))


class ObjCtoPython (TestCase):
    # TODO: NSProxy

    def assertFetchingTwice(self, container, message = None):
            v1 = container.storedObject()
            v2 = container.storedObject()

            self.assert_(v1 is v2, message)

    def testNSObject(self):
        container = OC_TestIdentity.alloc().init()

        cls = objc.lookUpClass("NSObject")
        container.setStoredObjectToResultOf_on_("new", cls)

        self.assertFetchingTwice(container, repr(cls))

        container.setStoredObjectToResultOf_on_("new", cls)
        v1 = container.storedObject()
        container.setStoredObjectToResultOf_on_("new", cls)
        v2 = container.storedObject()
        self.assert_(v1 is not v2, "different objects")

    def dont_testNSString(self):
        # This would always fail, NSStrings get a new proxy everytime
        # they cross the bridge, otherwise it would be unnecessarily hard
        # to get at the current value of NSMutableStrings.

        container = OC_TestIdentity.alloc().init()

        cls = objc.lookUpClass("NSObject")
        container.setStoredObjectToResultOf_on_("description", cls)
        self.assertFetchingTwice(container, "string")

        cls = objc.lookUpClass("NSMutableString")
        container.setStoredObjectToResultOf_on_("new", cls)
        self.assertFetchingTwice(container, "mutable string")

    def testProtocol(self):
        container = OC_TestIdentity.alloc().init()

        container.setStoredObjectToAProtocol()
        v = container.storedObject()
        self.assertFetchingTwice(container, "protocol")

    if sys.maxint < 2**32:
        def testObject(self):
            container = OC_TestIdentity.alloc().init()

            cls = objc.lookUpClass("Object")
            container.setStoredObjectAnInstanceOfClassic_(cls)
            self.assertFetchingTwice(container, "object")

    def dont_testNSNumber(self):
        # No unique proxies yet, due to the way these proxies are
        # implemented. This needs to be fixed, but that does not have
        # a high priority.
        container = OC_TestIdentity.alloc().init()

        container.setStoredObjectToInteger_(10)
        self.assertFetchingTwice(container, "int 10")

        container.setStoredObjectToInteger_(-40)
        self.assertFetchingTwice(container, "int -40")

        container.setStoredObjectToUnsignedInteger_(40)
        self.assertFetchingTwice(container, "unsigned int 40")

        container.setStoredObjectToUnsignedInteger_(sys.maxint * 2)
        self.assertFetchingTwice(container, "unsigned int sys.maxint*2")

        container.setStoredObjectToLongLong_(sys.maxint * 2)
        self.assertFetchingTwice(container, "long long sys.maxint*2")

        container.setStoredObjectToLongLong_(-sys.maxint)
        self.assertFetchingTwice(container, "long long -sys.maxint")

        container.setStoredObjectToUnsignedLongLong_(sys.maxint * 2)
        self.assertFetchingTwice(container, "unsigned long long sys.maxint*2")

        container.setStoredObjectToFloat_(10.0)
        self.assertFetchingTwice(container, "float")

        container.setStoredObjectToDouble_(9999.0)
        self.assertFetchingTwice(container, "double")

    def testNSDecimalNumber(self):
        container = OC_TestIdentity.alloc().init()

        cls = objc.lookUpClass("NSDecimalNumber")
        container.setStoredObjectToResultOf_on_("zero", cls)
        self.assertFetchingTwice(container, "decimal")

class PythonToObjC (TestCase):
    # TODO: verify

    def testPlainObjects(self):
        container = OC_TestIdentity.alloc().init()

        for v in (self, object(), list, sum):
            container.setStoredObject_(v)

            self.assert_(container.isSameObjectAsStored_(v), repr(v))


    def testContainers(self):
        container = OC_TestIdentity.alloc().init()

        for v in ([1,2,3], (1,2,3), {1:2, 3:4}):
            container.setStoredObject_(v)

            self.assert_(container.isSameObjectAsStored_(v), repr(v))

    def dont_testStrings(self):
        # These get converted, not proxied
        container = OC_TestIdentity.alloc().init()

        for v in ("hello world", u"a unicode string"):
            container.setStoredObject_(v)

            self.assert_(container.isSameObjectAsStored_(v), repr(v))

    def dont_testNumbers(self):
        # These get converted, not proxied
        container = OC_TestIdentity.alloc().init()

        for v in (1, 666666, 1L, 66666L, 1.0,):
            container.setStoredObject_(v)

            self.assert_(container.isSameObjectAsStored_(v), repr(v))

class TestSerializingDataStructures (TestCase):
    # OC_Python{Array,Dictionary} used to contain specialized 
    # identity-preservation code. It is unclear why this was added, it might
    # have to do with writing data to plist files.
    # This TestCase tries to trigger the problem that caused the addition of
    # said code, although unsuccesfully...

    def tearDown(self):
        if os.path.exists("/tmp/pyPyObjCTest.identity"):
            os.unlink("/tmp/pyPyObjCTest.identity")

    def testMakePlist(self):
        container = OC_TestIdentity.alloc().init()

        value = [ 1, 2, 3, [ u"hello", [u"world", (u"in", 9 ) ], True, {u"aap":3}]]
        value.append(value[3])

        container.setStoredObject_(value)
        container.writeStoredObjectToFile_(u"/tmp/pyPyObjCTest.identity")

        value = {
            u"hello": [ 1, 2, 3],
            u"world": {
                u"nl": u"wereld",
                u"de": u"Welt",
            }
        }
        container.setStoredObject_(value)
        container.writeStoredObjectToFile_(u"/tmp/pyPyObjCTest.identity")

        

if __name__ == "__main__":
    main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.