Skip to content

Commit

Permalink
Merge pull request #2 from fcy/develop
Browse files Browse the repository at this point in the history
Version 1.2.0, fix the bogus v1.1.0
  • Loading branch information
fcy committed Jul 24, 2013
2 parents baf6197 + e821adb commit b98c035
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 34 deletions.
4 changes: 2 additions & 2 deletions FCYAsserts.podspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Pod::Spec.new do |s|
s.name = 'FCYAsserts'
s.version = '1.1.0'
s.version = '1.2.0'
s.license = { :type => 'MIT' }
s.homepage = 'https://github.com/fcy/FCYAsserts'
s.authors = { 'Felipe Cypriano' => '[email protected]' }
s.summary = 'Collection of assert macros that logs useful messages.'
s.source = { :git => 'https://github.com/fcy/FCYAsserts.git', :tag => "#{s.version}" }
s.source = { :git => 'https://github.com/fcy/FCYAsserts.git', :tag => "v#{s.version}" }
s.source_files = 'FCYAsserts/*.{h,m}'
s.requires_arc = true
end
2 changes: 1 addition & 1 deletion FCYAsserts/FCYAssertHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ extern NSString *const FCYAssertErrorDomain;
function:(NSString *)function
file:(NSString *)file
line:(NSInteger)line
description:(NSString *)format, ... NS_FORMAT_FUNCTION(6, 7);
description:(NSString *)description;

@end
9 changes: 3 additions & 6 deletions FCYAsserts/FCYAssertHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,12 @@ - (void)assertFailureOrReturnWithExpression:(NSString *)expression function:(NSS
va_end(args);
}

- (void)assertFailureOrReturnBlock:(FCYAssertReturnBlock)returnBlock withExpression:(NSString *)expression function:(NSString *)function file:(NSString *)file line:(NSInteger)line description:(NSString *)format, ... {
va_list args;
va_start(args, format);
NSString *logMessage = [self _assertFailureShouldAbort:__FCYShouldAbort withExpression:expression function:function file:file line:line description:format arguments:args];
va_end(args);
- (void)assertFailureOrReturnBlock:(FCYAssertReturnBlock)returnBlock withExpression:(NSString *)expression function:(NSString *)function file:(NSString *)file line:(NSInteger)line description:(NSString *)description {
[self _assertFailureShouldAbort:__FCYShouldAbort withExpression:expression function:function file:file line:line description:description arguments:NULL];

NSError *error = [NSError errorWithDomain:FCYAssertErrorDomain
code:0
userInfo:@{ NSLocalizedDescriptionKey : logMessage }];
userInfo:@{ NSLocalizedDescriptionKey : description }];
returnBlock(error);
}

Expand Down
58 changes: 36 additions & 22 deletions FCYAsserts/FCYAsserts.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,44 @@
/// @name FCYAssertOrReturn
/// -----------------------

#define __FCYAssertOrReturnHandler \
[[FCYAssertHandler handler] assertFailureOrReturnWithExpression:[NSString stringWithUTF8String:#condition] \
function:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
file:[NSString stringWithUTF8String:__FILE__] \
line:__LINE__ \
description:(desc), ##__VA_ARGS__];


#define FCYAssertOrReturn(condition, desc, ...) \
do { if (!(condition)) { __FCYAssertOrReturnHandler return; } } while(0)
do { if (!(condition)) { \
[[FCYAssertHandler handler] assertFailureOrReturnWithExpression:[NSString stringWithUTF8String:#condition] \
function:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
file:[NSString stringWithUTF8String:__FILE__] \
line:__LINE__ \
description:(desc), ##__VA_ARGS__]; \
return; \
} } while(0)

#define FCYAssertOrReturnNil(condition, desc, ...) \
do { if (!(condition)) { __FCYAssertOrReturnHandler return nil; } } while(0)

#define __FCYAssertOrReturnBlockHandler \
[[FCYAssertHandler handler] assertFailureOrReturnBlock:block \
withExpression:[NSString stringWithUTF8String:#condition] \
function:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
file:[NSString stringWithUTF8String:__FILE__] \
line:__LINE__ \
description:(desc), ##__VA_ARGS__];
do { if (!(condition)) { \
[[FCYAssertHandler handler] assertFailureOrReturnWithExpression:[NSString stringWithUTF8String:#condition] \
function:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
file:[NSString stringWithUTF8String:__FILE__] \
line:__LINE__ \
description:(desc), ##__VA_ARGS__]; \
return nil; \
} } while(0)

#define FCYAssertOrReturnBlock(condition, block, desc, ...) \
do { if (!(condition)) { __FCYAssertOrReturnBlockHandler return; } } while(0)
#define FCYAssertOrReturnBlock(condition, desc, block) \
do { if (!(condition)) { \
[[FCYAssertHandler handler] assertFailureOrReturnBlock:(block) \
withExpression:[NSString stringWithUTF8String:#condition] \
function:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
file:[NSString stringWithUTF8String:__FILE__] \
line:__LINE__ \
description:(desc)]; \
return; \
} } while(0)

#define FCYAssertOrReturnNilBlock(condition, block, desc, ...) \
do { if (!(condition)) { __FCYAssertOrReturnBlockHandler return nil; } } while(0)
#define FCYAssertOrReturnNilBlock(condition, desc, block) \
do { if (!(condition)) { \
[[FCYAssertHandler handler] assertFailureOrReturnBlock:(block) \
withExpression:[NSString stringWithUTF8String:#condition] \
function:[NSString stringWithUTF8String:__PRETTY_FUNCTION__] \
file:[NSString stringWithUTF8String:__FILE__] \
line:__LINE__ \
description:(desc)]; \
return nil; \
} } while(0)
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,17 @@ id json = [NSJSONSerialization JSONObjectWithData:dataFromServer options:0 error
if (error) {
completionBlock(nil, error);
} else {
FCYAssertOrReturnBlock([json isKindOfClass:[NSDictionary class]], ^(NSError *assertError){
FCYAssertOrReturnBlock([json isKindOfClass:[NSDictionary class]], @"Invalid Server Data", ^(NSError *assertError){
completionBlock(nil, assertError);
}, @"Should have received a NSDictionary from the server. Got %@ instead.", [json class]);
});
completionBlock(json, nil);
}
```

If json is not a NSDictionary in debug code it would abort and log and in release it would just log:

> -[MyClass callServiceWithCompletionBlock:]: Assertion '[json isKindOfClass:[NSDictionary class]]' failed on line /Users/you/dev/Project/MyClass.m:42. Should have received a NSDictionary from the server. Got NSArray instead.
> -[MyClass callServiceWithCompletionBlock:]: Assertion '[json isKindOfClass:[NSDictionary class]]' failed on line /Users/you/dev/Project/MyClass.m:42. Invalid Server Data
## Log messages with CocoaLumberjack

Expand Down

0 comments on commit b98c035

Please sign in to comment.