Source

pyobjc / pyobjc-framework-Cocoa / PyObjCTest / test_cferror.py

from PyObjCTools.TestSupport import *
from CoreFoundation import *
from Foundation import *

try:
    unicode
except NameError:
    unicode = str


try:
    long
except NameError:
    long = int

try:
    unichr
except NameError:
    unichr = chr


class TestError (TestCase):

    @min_os_level('10.5')
    def testTypes(self):
        try:
            NSCFError = objc.lookUpClass('__NSCFError')
        except objc.error:
            NSCFError = objc.lookUpClass('NSCFError')

        self.assertIs(CFErrorRef, NSCFError)

    @min_os_level('10.5')
    def testTypeID(self):
        self.assertIsInstance(CFErrorGetTypeID(), (int, long))

    @min_os_level('10.5')
    def testCreation(self):
        userInfo = {
                b'foo'.decode('ascii'): b'bar'.decode('ascii'),
        }
        err = CFErrorCreate(None, kCFErrorDomainPOSIX, 42,  userInfo)
        self.assertIsInstance(err, CFErrorRef)
        self.assertResultIsCFRetained(CFErrorCopyUserInfo)
        dct = CFErrorCopyUserInfo(err)
        self.assertEqual(dct, userInfo)

        keys = [ b"key1".decode('ascii'), b"key2".decode('ascii')]
        values = [ b"value1".decode('ascii'), b"value2".decode('ascii')]

        keys = [ NSString.stringWithString_(v) for v in keys ]
        values = [ NSString.stringWithString_(v) for v in values ]

        self.assertArgHasType(CFErrorCreateWithUserInfoKeysAndValues, 3, b'n^@')
        self.assertArgSizeInArg(CFErrorCreateWithUserInfoKeysAndValues, 3, 5)
        self.assertArgHasType(CFErrorCreateWithUserInfoKeysAndValues, 4, b'n^@')
        self.assertArgSizeInArg(CFErrorCreateWithUserInfoKeysAndValues, 4, 5)
        err = CFErrorCreateWithUserInfoKeysAndValues(None, kCFErrorDomainPOSIX, 42, keys, values, 2)
        self.assertIsInstance(err, CFErrorRef)
        dct = CFErrorCopyUserInfo(err)
        self.assertEqual(dct, {b'key1'.decode('ascii'): b'value1'.decode('ascii'), b'key2'.decode('ascii'):b'value2'.decode('ascii')})

    @min_os_level('10.5')
    def testInspection(self):
        userInfo = {
                b'foo'.decode('ascii'): b'bar'.decode('ascii'),
                kCFErrorLocalizedFailureReasonKey: "failure reason",
                kCFErrorLocalizedRecoverySuggestionKey: 'recovery suggestion',
        }
        self.assertResultIsCFRetained(CFErrorCreate)
        err = CFErrorCreate(None, kCFErrorDomainPOSIX, 42,  userInfo)
        self.assertIsInstance(err, CFErrorRef)
        dom = CFErrorGetDomain(err)
        self.assertEqual(dom, kCFErrorDomainPOSIX)

        code = CFErrorGetCode(err)
        self.assertEqual(code, 42)

        self.assertResultIsCFRetained(CFErrorCopyDescription)
        v = CFErrorCopyDescription(err)
        self.assertIn(v, (
            b'Operation could not be completed. failure reason'.decode('ascii'),
            b'The operation couldn%st be completed. failure reason'.decode('ascii')%(unichr(0x2019),)))

        self.assertResultIsCFRetained(CFErrorCopyFailureReason)
        v = CFErrorCopyFailureReason(err)
        self.assertEqual(v, b'failure reason'.decode('ascii'))

        self.assertResultIsCFRetained(CFErrorCopyRecoverySuggestion)
        v = CFErrorCopyRecoverySuggestion(err)
        self.assertEqual(v, b'recovery suggestion'.decode('ascii'))

    @min_os_level('10.5')
    def testConstants(self):
        self.assertIsInstance(kCFErrorDomainPOSIX, unicode)
        self.assertIsInstance(kCFErrorDomainOSStatus, unicode)
        self.assertIsInstance(kCFErrorDomainMach, unicode)
        self.assertIsInstance(kCFErrorDomainCocoa, unicode)
        self.assertIsInstance(kCFErrorLocalizedDescriptionKey, unicode)
        self.assertIsInstance(kCFErrorLocalizedFailureReasonKey, unicode)
        self.assertIsInstance(kCFErrorLocalizedRecoverySuggestionKey, unicode)
        self.assertIsInstance(kCFErrorDescriptionKey, unicode)
        self.assertIsInstance(kCFErrorUnderlyingErrorKey, unicode)

    @min_os_level('10.7')
    def testConstants10_7(self):
        self.assertIsInstance(kCFErrorURLKey, unicode)
        self.assertIsInstance(kCFErrorFilePathKey, unicode)

if __name__ == "__main__":
    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 ProjectModifiedEvent.java.
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.