Last active
August 29, 2015 13:56
-
-
Save samdods/8979831 to your computer and use it in GitHub Desktop.
Expanded use of Foundation's assertions (modified from https://gist.github.com/krzysztofzablocki/5921645)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef NS_ENUM(NSInteger, kErrorCode) { | |
kErrorCodeInternal = 431432, | |
}; | |
extern NSError *NSErrorMake(NSString *message, NSInteger code, NSDictionary *aUserInfo, NSString *methodOrFunction); | |
#define NSObjcAssert NSAssert | |
#define InvalidConditionString(condition) (@"Invalid condition not satisfying: " #condition) | |
#define GenericAssertCondition(ctype, condition) NS ## ctype ## Assert((condition), InvalidConditionString((condition))) | |
#define GenericErrorMake(condition, func) NSErrorMake(InvalidConditionString((condition)), kErrorCodeInternal, nil, func) | |
#define GenericAssertTrueOrPerformAction(ctype, condition, action) do{ ctype ## AssertCondition(condition); \ | |
if (!(condition)) { ctype ## ErrorMake(condition); action; } } while(0) | |
#pragma mark - Objective-C methods | |
#define ObjcAssertCondition(condition) GenericAssertCondition(Objc, condition) | |
#define ObjcErrorMake(condition) GenericErrorMake((condition), NSStringFromSelector(_cmd)) | |
#define AssertTrueOrReturnError(condition) do{ ObjcAssertCondition(condition); \ | |
if (!(condition)) { return ErrorMake(condition); } } while(0) | |
#define AssertTrueOrPerformAction(condition, action) GenericAssertTrueOrPerformAction(Objc, condition, action) | |
#define AssertTrueOrReturn(condition) AssertTrueOrPerformAction(condition, return) | |
#define AssertTrueOrReturnNo(condition) AssertTrueOrPerformAction(condition, return NO) | |
#define AssertTrueOrReturnNil(condition) AssertTrueOrPerformAction(condition, return nil) | |
#pragma mark - C-based Functions | |
#define CAssertCondition(condition) GenericAssertCondition(C, condition) | |
#define CErrorMake(condition) GenericErrorMake((condition), [NSString stringWithUTF8String:__PRETTY_FUNCTION__]) | |
#define CAssertTrueOrReturnError(condition) do{ CAssertCondition(condition); \ | |
if (!(condition)) { return CErrorMake(condition); } } while(0) | |
#define CAssertTrueOrPerformAction(condition, action) GenericAssertTrueOrPerformAction(C, condition, action) | |
#define CAssertTrueOrReturn(condition) CAssertTrueOrPerformAction(condition, return) | |
#define CAssertTrueOrReturnNo(condition) CAssertTrueOrPerformAction(condition, return NO) | |
#define CAssertTrueOrReturnNil(condition) CAssertTrueOrPerformAction(condition, return nil) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment