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