Last active
December 18, 2015 21:49
-
-
Save alloy/5850206 to your computer and use it in GitHub Desktop.
Meta alert: A Kiwi matcher that allows you to define custom matchers with one block instead of subclassing KWMatcher.
This file contains hidden or 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
#import "KWMatcher.h" | |
typedef BOOL (^FTKiwiCustomMatcherBlock)(id subject); | |
@interface FTKiwiCustomBlockMatcher : KWMatcher | |
// Your custom matcher block will receive the expectation ‘subject’ and must | |
// return `YES` if the expaction passes or `NO` if it fails. | |
- (void)be:(FTKiwiCustomMatcherBlock)block; | |
@end |
This file contains hidden or 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
#import "FTKiwiCustomBlockMatcher.h" | |
@interface FTKiwiCustomBlockMatcher () | |
@property (copy) FTKiwiCustomMatcherBlock customMatcherBlock; | |
@end | |
@implementation FTKiwiCustomBlockMatcher | |
@synthesize customMatcherBlock = _customMatcherBlock; | |
// Enable this if you’re not using ARC. | |
// | |
//- (void)dealloc { | |
//Block_release(_customMatcherBlock); | |
//[super dealloc]; | |
//} | |
+ (NSArray *)matcherStrings { | |
return @[@"be:"]; | |
} | |
- (BOOL)evaluate { | |
return self.customMatcherBlock(self.subject); | |
} | |
- (NSString *)failureMessageForShould { | |
return @"expected the customer matcher to evaluate to `YES`"; | |
} | |
- (NSString *)failureMessageForShouldNot { | |
return @"expected the customer matcher to evaluate to `NO`"; | |
} | |
- (void)be:(FTKiwiCustomMatcherBlock)block { | |
self.customMatcherBlock = block; | |
} | |
@end |
This file contains hidden or 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
#import "Kiwi.h" | |
#import "FTKiwiCustomBlockMatcher.h" | |
typedef FTKiwiCustomMatcherBlock (^StringWithContentMatcher)(NSString *content); | |
SPEC_BEGIN(ExampleSpec) | |
registerMatchers(@"FT"); | |
describe(@"FTKiwiCustomBlockMatcher", ^{ | |
it(@"takes a block that does the actual evaluation", ^{ | |
FTKiwiCustomMatcherBlock stringInstance = ^(id subject) { | |
return [subject isKindOfClass:[NSString class]]; | |
}; | |
[[@"hello, I am string" should] be:stringInstance]; | |
}); | |
it(@"becomes even more interesting with dynamic arguments", ^{ | |
StringWithContentMatcher stringWithContent = ^(NSString *content) { | |
return ^(id subject) { | |
return [subject isKindOfClass:[NSString class]] && [subject isEqualToString:content]; | |
}; | |
}; | |
[[@"hello, I am string" should] be:stringWithContent(@"hello, I am string")]; | |
}); | |
}); | |
SPEC_END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment