1from __future__ import unicode_literals 2from PyObjCTools.TestSupport import * 3import objc 4import sys 5 6# Most useful systems will at least have 'NSObject'. 7#NSObject = objc.lookUpClass('NSObject') 8 9# Use a class that isn't used in the rest of the testsuite, 10# should write a native class for this! 11BaseName = 'NSAttributedString' 12BaseClass = objc.lookUpClass(BaseName) 13 14if sys.maxsize >= 2 ** 32: 15 # -poseAsClass: is not supported in 64-bit mode (the functionality is 16 # not present in the 64-bit runtime and will never be because it 17 # conflicts with new functionality such as non-fragile class layouts) 18 pass 19 20else: 21 class TestPosing(TestCase): 22 def testPosing(self): 23 24 class PoseClass(BaseClass): 25 __slots__ = () # Don't add instance variables, not even __dict__ 26 def testPosingMethod(self): 27 return "<PoseClass instance>" 28 29 30 PoseClass.poseAsClass_(BaseClass) 31 32 # BaseClass still refers to the old class, if we look it up again 33 # we get to see the new value. There's not much we can do about that. 34 obj = objc.lookUpClass(BaseName).new() 35 self.assertEqual(obj.testPosingMethod(), "<PoseClass instance>") 36 37 # XXX: next assertion fails because the runtime seems to copy the 38 # original class. 39 #self.assertIsInstance(obj, PoseClass) 40 self.assertNotEqual(BaseClass.__name__, BaseName) 41 self.assertEqual(PoseClass.__name__, BaseName) 42 del obj 43 44 45 46if __name__ == '__main__': 47 main() 48