Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added Tests/CFError/TestInfo
Empty file.
73 changes: 73 additions & 0 deletions Tests/CFError/create.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include "CoreFoundation/CFError.h"
#include "CoreFoundation/CFDictionary.h"
#include "../CFTesting.h"

int main (void)
{
CFErrorRef err;
CFDictionaryRef userInfo;
CFDictionaryRef copy;
const void *keys[1];
const void *values[1];

keys[0] = kCFErrorLocalizedDescriptionKey;
values[0] = CFSTR("something went wrong");

userInfo = CFDictionaryCreate (NULL, keys, values, 1,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

err = CFErrorCreate (NULL, kCFErrorDomainPOSIX, 42, userInfo);
PASS_CF(err != NULL, "CFError created.");

PASS_CF(CFErrorGetTypeID () != 0, "CFError type ID is not zero.");
PASS_CF(CFGetTypeID (err) == CFErrorGetTypeID (),
"CFError has the CFError type ID.");

PASS_CFEQ(CFErrorGetDomain (err), kCFErrorDomainPOSIX,
"CFErrorGetDomain returns the domain.");
PASS_CF(CFErrorGetCode (err) == 42, "CFErrorGetCode returns the code.");

copy = CFErrorCopyUserInfo (err);
PASS_CF(copy != NULL, "CFErrorCopyUserInfo returns a dictionary.");
PASS_CFEQ(copy, userInfo, "Copied user info is equal to the original.");
CFRelease (copy);

CFRelease (err);
CFRelease (userInfo);

/* A NULL domain is not allowed. */
err = CFErrorCreate (NULL, NULL, 0, NULL);
PASS_CF(err == NULL, "CFErrorCreate with a NULL domain returns NULL.");

/* With no user info a valid, empty dictionary is substituted. */
err = CFErrorCreate (NULL, kCFErrorDomainCocoa, 7, NULL);
PASS_CF(err != NULL, "CFError created without user info.");
copy = CFErrorCopyUserInfo (err);
PASS_CF(copy != NULL,
"CFErrorCopyUserInfo returns a dictionary even without user info.");
PASS_CF(CFDictionaryGetCount (copy) == 0,
"Substituted user info dictionary is empty.");
CFRelease (copy);
CFRelease (err);

/* CFErrorCreateWithUserInfoKeysAndValues builds the user info for us. */
keys[0] = kCFErrorLocalizedFailureReasonKey;
values[0] = CFSTR("the reason");
err = CFErrorCreateWithUserInfoKeysAndValues (NULL, kCFErrorDomainMach, 13,
keys, values, 1);
PASS_CF(err != NULL,
"CFErrorCreateWithUserInfoKeysAndValues created an error.");
PASS_CFEQ(CFErrorGetDomain (err), kCFErrorDomainMach,
"Keys-and-values error has the requested domain.");
PASS_CF(CFErrorGetCode (err) == 13,
"Keys-and-values error has the requested code.");
copy = CFErrorCopyUserInfo (err);
PASS_CF(CFDictionaryGetCount (copy) == 1,
"Keys-and-values error carries one user info entry.");
PASS_CFEQ(CFDictionaryGetValue (copy, kCFErrorLocalizedFailureReasonKey),
CFSTR("the reason"), "Keys-and-values error preserved the value.");
CFRelease (copy);
CFRelease (err);

return 0;
}
75 changes: 75 additions & 0 deletions Tests/CFError/userinfo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "CoreFoundation/CFError.h"
#include "CoreFoundation/CFDictionary.h"
#include "../CFTesting.h"

/* Build an error whose user info carries the three localized strings that
CFErrorCopy{Description,FailureReason,RecoverySuggestion} read back. */
static CFErrorRef
makeError (CFStringRef domain, CFIndex code)
{
const void *keys[3];
const void *values[3];
CFDictionaryRef userInfo;
CFErrorRef err;

keys[0] = kCFErrorLocalizedDescriptionKey;
keys[1] = kCFErrorLocalizedFailureReasonKey;
keys[2] = kCFErrorLocalizedRecoverySuggestionKey;
values[0] = CFSTR("description");
values[1] = CFSTR("failure reason");
values[2] = CFSTR("recovery suggestion");

userInfo = CFDictionaryCreate (NULL, keys, values, 3,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
err = CFErrorCreate (NULL, domain, code, userInfo);
CFRelease (userInfo);

return err;
}

int main (void)
{
CFErrorRef err;
CFErrorRef equal;
CFErrorRef otherCode;
CFErrorRef otherDomain;
CFStringRef str;

err = makeError (kCFErrorDomainPOSIX, 1);

str = CFErrorCopyDescription (err);
PASS_CFEQ(str, CFSTR("description"),
"CFErrorCopyDescription reads the localized description.");
CFRelease (str);

str = CFErrorCopyFailureReason (err);
PASS_CFEQ(str, CFSTR("failure reason"),
"CFErrorCopyFailureReason reads the localized failure reason.");
CFRelease (str);

str = CFErrorCopyRecoverySuggestion (err);
PASS_CFEQ(str, CFSTR("recovery suggestion"),
"CFErrorCopyRecoverySuggestion reads the localized recovery suggestion.");
CFRelease (str);

/* Two errors with the same domain, code and user info are equal. */
equal = makeError (kCFErrorDomainPOSIX, 1);
PASS_CFEQ(err, equal, "Errors with matching fields are equal.");
PASS_CF(CFHash (err) == CFHash (equal),
"Equal errors have the same hash.");
CFRelease (equal);

/* Differing code breaks equality. */
otherCode = makeError (kCFErrorDomainPOSIX, 2);
PASS_CFNEQ(err, otherCode, "Errors with different codes are not equal.");
CFRelease (otherCode);

/* Differing domain breaks equality. */
otherDomain = makeError (kCFErrorDomainCocoa, 1);
PASS_CFNEQ(err, otherDomain, "Errors with different domains are not equal.");
CFRelease (otherDomain);

CFRelease (err);

return 0;
}