1from PyObjCTools.TestSupport import *
2import objc
3import sys
4
5NSObject = objc.lookUpClass('NSObject')
6#_NSZombie = objc.lookUpClass('_NSZombie')
7NSProxy = objc.lookUpClass('NSProxy')
8
9
10
11class MethodAccessTest (TestCase):
12
13    def testObjCObject(self):
14        # Trying to access the methods of objc.objc_object should not
15        # crash the interpreter.
16        self.assertRaises(AttributeError, getattr, objc.objc_object.pyobjc_classMethods, 'func_code')
17        self.assertRaises(AttributeError, getattr, objc.objc_object.pyobjc_instanceMethods, 'func_code')
18
19    def testNSProxyStuff(self):
20        # NSProxy is incompatitble with pyobjc_{class,instance}Methods, but
21        # this should not crash the interpreter
22        self.assertRaises(AttributeError, getattr, NSProxy.pyobjc_instanceMethods, 'foobar')
23        self.assertRaises(AttributeError, getattr, NSProxy.pyobjc_classMethods, 'foobar')
24        self.assertRaises(AttributeError, getattr, NSProxy, 'foobar')
25#
26#    if sys.platform == 'darwin':
27#        def testNSZombie(self):
28#            self.assertRaises(AttributeError, getattr, _NSZombie.pyobjc_instanceMethods, "foobar")
29#            self.assertRaises(AttributeError, getattr, _NSZombie.pyobjc_classMethods, "foobar")
30#            self.assertRaises(AttributeError, getattr, _NSZombie, "foobar")
31#
32
33    def testDir(self):
34        o = NSObject.new()
35
36        d = dir(o.pyobjc_instanceMethods)
37        self.assertGreaterThan(len(d), 10)
38        self.assertIn("init", d)
39
40        d = dir(NSObject.pyobjc_classMethods)
41        self.assertGreaterThan(len(d), 10)
42        self.assertIn("alloc", d)
43
44    def testDict(self):
45        o = NSObject.new()
46
47        d = o.pyobjc_instanceMethods.__dict__.keys()
48        self.assertGreaterThan(len(d), 10)
49        self.assertIn("init", d)
50
51        d = NSObject.pyobjc_classMethods.__dict__.keys()
52        self.assertGreaterThan(len(d), 10)
53        self.assertIn("alloc", d)
54
55        #d = o.pyobjc_classMethods.__dict__.keys()
56        #self.assertGreaterThan(len(d), 10)
57        #self.assertIn("alloc", d)
58
59    def testAttributes(self):
60        o = NSObject.new()
61
62        self.assertHasAttr(o.pyobjc_instanceMethods, "init")
63        #self.assertHasAttr(o.pyobjc_classMethods, "alloc")
64
65        self.assertHasAttr(NSObject.pyobjc_classMethods, "alloc")
66
67class ClassAndInstanceMethods(TestCase):
68    def testClassThroughInstance(self):
69        # Class methods are not accessible through instances.
70        self.assertRaises(AttributeError, getattr, NSObject.new(), 'alloc')
71
72if __name__ == "__main__":
73    main()
74