pyobjc / pyobjc-core / PyObjCTest /

from PyObjCTools.TestSupport import *
import objc
import objc._convenience as convenience

class TestConvenienceHelpers (TestCase):
    def test_add_for_selector(self):
        methods = [
            ('add', lambda self, x: self.testMethod_(x))

        with filterWarnings("error", DeprecationWarning):
            self.assertRaises(DeprecationWarning, objc.addConvenienceForSelector, b'testMethod:', methods)
            if b'testMethod' in convenience._CONVENIENCE_METHODS:
                del convenience._CONVENIENCE_METHODS[b'testMethods:']

        with filterWarnings("ignore", DeprecationWarning):
            self.assertNotIn(b'testMethod:', convenience._CONVENIENCE_METHODS)
                objc.addConvenienceForSelector(b'testMethod:', methods)

                self.assertEqual(convenience._CONVENIENCE_METHODS[b'testMethod:'], methods)

                if b'testMethod' in convenience._CONVENIENCE_METHODS:
                    del convenience._CONVENIENCE_METHODS[b'testMethods:']

    def test_add_for_class(self):
        self.assertNotIn("MyObject", convenience.CLASS_METHODS)

        methods = [
            ('info', lambda self: self.description())

            objc.addConvenienceForClass("MyObject", methods)
            self.assertEqual(convenience.CLASS_METHODS["MyObject"], methods)

            if 'MyObject' in convenience.CLASS_METHODS:
                del convenience.CLASS_METHODS["MyObject"]

class TestBasicConveniences (TestCase):
    def testBundleForClass(self):
        orig = convenience.currentBundle
            the_bundle = object()
            def currentBundle():
                return the_bundle
            convenience.currentBundle = currentBundle

            class OC_Test_Basic_Convenience_1 (objc.lookUpClass("NSObject")):

            self.assertIs(OC_Test_Basic_Convenience_1.bundleForClass(), the_bundle)
            convenience.currentBundle = orig

    def test_kvc_helper(self):
        o = objc.lookUpClass('NSURL').URLWithString_('')
        self.assertEqual(, '')

        self.assertEqual(, '')
        self.assertEqual(o._['host'], '')
        self.assertRaises(TypeError, lambda: o._[42])
        self.assertEqual(repr(o._), '<KVC accessor for %r>'%(o,))
        self.assertRaises(AttributeError, getattr, o._, 'nosuchattr')
        self.assertRaises(TypeError, o._.__setitem__, 42) 

        o = objc.lookUpClass('NSMutableDictionary').dictionary()
        o._.key1 = 1
        o._['key2'] = 2

        self.assertEqual(o, {'key1': 1, 'key2': 2 })
        self.assertRaises(AttributeError, o._.nosuchattr)
        self.assertRaises(TypeError, o._.__setitem__, 42) 

# TODO: Explicit tests for add_convenience_methods.

if __name__ == "__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
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.