Source

pyobjc / Lib / objc / test / 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 unittest, sys, os
import objc
from objc.test.identity import *

class TestPythonRoundTrip (unittest.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 (unittest.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.lookUpClass("Protocol")))

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

        cls = objc.lookUpClass("Object")
        container.setStoredObjectAnInstanceOf_(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_(sys.maxint * 2)
        v = container.storedObject()
        self.assert_(container.isSameObjectAsStored_(v), repr(v))

        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 (unittest.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")

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

        cls = objc.lookUpClass("Object")
        container.setStoredObjectAnInstanceOf_(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 (unittest.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 (unittest.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/pyobjc.test.identity"):
            os.unlink("/tmp/pyobjc.test.identity")

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

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

        container.setStoredObject_(value)
        container.writeStoredObjecToFile_("/tmp/pyobjc.test.identity")

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

        

if __name__ == "__main__":
    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.