1from PyObjCTools.TestSupport import * 2import objc 3 4from Foundation import * 5import Foundation 6 7try: 8 unicode 9except NameError: 10 unicode = str 11 12class TestNSExceptionInteraction(TestCase): 13 def testRepeatedAllocInit(self): 14 for i in range(1,1000): 15 a = NSException.alloc().initWithName_reason_userInfo_( b"Bogus".decode('ascii'), b"A bad reason".decode('ascii'), { b"foo".decode('ascii') : b"bar".decode('ascii') } ) 16 17 def testFormat(self): 18 try: 19 NSException.raise_format_('ExceptionName', 'Format: %s %d', b'hello', 42) 20 21 except TypeError: 22 raise 23 24 except objc.error as e: 25 self.assertEqual(e._pyobjc_info_['name'], 'ExceptionName') 26 self.assertEqual(e._pyobjc_info_['reason'], 'Format: hello 42') 27 28 29class TestNSException (TestCase): 30 def testConstants(self): 31 self.assertIsInstance(NSGenericException, unicode) 32 self.assertIsInstance(NSRangeException, unicode) 33 self.assertIsInstance(NSInvalidArgumentException, unicode) 34 self.assertIsInstance(NSInternalInconsistencyException, unicode) 35 self.assertIsInstance(NSMallocException, unicode) 36 self.assertIsInstance(NSObjectInaccessibleException, unicode) 37 self.assertIsInstance(NSObjectNotAvailableException, unicode) 38 self.assertIsInstance(NSDestinationInvalidException, unicode) 39 self.assertIsInstance(NSPortTimeoutException, unicode) 40 self.assertIsInstance(NSInvalidSendPortException, unicode) 41 self.assertIsInstance(NSInvalidReceivePortException, unicode) 42 self.assertIsInstance(NSPortSendException, unicode) 43 self.assertIsInstance(NSPortReceiveException, unicode) 44 self.assertIsInstance(NSOldStyleException, unicode) 45 46 @min_os_level('10.6') 47 def testConstants10_6(self): 48 self.assertIsInstance(NSAssertionHandlerKey, unicode) 49 50 @expectedFailure 51 def testUncaughtExceptionHandler(self): 52 self.fail("NSSetUncaughtExceptionHandler") 53 self.fail("NSGetUncaughtExceptionHandler") 54 55 def testNoAssert(self): 56 self.assertNotHasAttr(Foundation, 'NSAssert5') 57 self.assertNotHasAttr(Foundation, 'NSAssert4') 58 self.assertNotHasAttr(Foundation, 'NSAssert3') 59 self.assertNotHasAttr(Foundation, 'NSAssert2') 60 self.assertNotHasAttr(Foundation, 'NSAssert1') 61 self.assertNotHasAttr(Foundation, 'NSAssert') 62 self.assertNotHasAttr(Foundation, 'NSParameterAssert') 63 self.assertNotHasAttr(Foundation, 'NSCAssert5') 64 self.assertNotHasAttr(Foundation, 'NSCAssert4') 65 self.assertNotHasAttr(Foundation, 'NSCAssert3') 66 self.assertNotHasAttr(Foundation, 'NSCAssert2') 67 self.assertNotHasAttr(Foundation, 'NSCAssert1') 68 self.assertNotHasAttr(Foundation, 'NSCAssert') 69 self.assertNotHasAttr(Foundation, 'NSCParameterAssert') 70 71 def testMethods(self): 72 self.assertArgIsPrintf(NSException.raise_format_, 1) 73 74 self.assertArgIsPrintf(NSAssertionHandler.handleFailureInMethod_object_file_lineNumber_description_, 4) 75 self.assertArgIsPrintf(NSAssertionHandler.handleFailureInFunction_file_lineNumber_description_, 3) 76 77 78if __name__ == '__main__': 79 main() 80