Skip to content

Instantly share code, notes, and snippets.

@alloy
Last active December 18, 2015 21:49
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alloy/5850206 to your computer and use it in GitHub Desktop.
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.
#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
#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
#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