Created
August 7, 2010 12:49
-
-
Save Mon-Ouie/512779 to your computer and use it in GitHub Desktop.
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 <Foundation/Foundation.h> | |
| #import <Block.h> | |
| typedef BOOL (^SpecTestBlock)(NSObject *obj); | |
| typedef NSString *(^SpecErrorBlock)(NSObject *obj); | |
| @interface SpecMatcher : NSObject { | |
| SpecTestBlock testBlock; | |
| SpecErrorBlock errorBlock; | |
| SpecErrorBlock shouldNotErrorBlock; | |
| } | |
| + (SpecMatcher*)matcherWithTest:(SpecTestBlock)test | |
| error:(SpecErrorBlock)error | |
| errorNot:(SpecErrorBlock)notError; | |
| - (id)initWithTest:(SpecTestBlock)test | |
| error:(SpecErrorBlock)error | |
| errorNot:(SpecErrorBlock)notError; | |
| - (BOOL)executeWith:(NSObject*)object; | |
| - (NSString*)errorFor:(NSObject*)object; | |
| - (NSString*)shouldNotErrorFor:(NSObject*)object; | |
| - (void)dealloc; | |
| @end | |
| @implementation SpecMatcher | |
| + (SpecMatcher*)matcherWithTest:(SpecTestBlock)test | |
| error:(SpecErrorBlock)error | |
| errorNot:(SpecErrorBlock)notError { | |
| return [[[self alloc] initWithTest:test error:error errorNot:notError] | |
| autorelease]; | |
| } | |
| - (id)initWithTest:(SpecTestBlock)test | |
| error:(SpecErrorBlock)error | |
| errorNot:(SpecErrorBlock)notError { | |
| if (self = [super init]) { | |
| testBlock = Block_copy(test); | |
| errorBlock = Block_copy(error); | |
| shouldNotErrorBlock = Block_copy(notError); | |
| } | |
| return self; | |
| } | |
| - (BOOL)executeWith:(NSObject*)object { | |
| return testBlock(object); | |
| } | |
| - (NSString*)errorFor:(NSObject*)object { | |
| return errorBlock(object); | |
| } | |
| - (NSString*)shouldNotErrorFor:(NSObject*)object { | |
| return shouldNotErrorBlock(object); | |
| } | |
| - (void)dealloc { | |
| Block_release(testBlock); | |
| Block_release(errorBlock); | |
| Block_release(shouldNotErrorBlock); | |
| [super dealloc]; | |
| } | |
| @end | |
| @interface NSObject (Spec) | |
| - (void)should:(SpecMatcher*)matcher; | |
| - (void)shouldNot:(SpecMatcher*)matcher; | |
| @end | |
| @implementation NSObject (Spec) | |
| - (void)should:(SpecMatcher*)matcher { | |
| BOOL res = [matcher executeWith:self]; | |
| NSAssert(res, [matcher errorFor:self]); | |
| } | |
| - (void)shouldNot:(SpecMatcher*)matcher { | |
| BOOL res = [matcher executeWith:self]; | |
| NSAssert(!res, [matcher shouldNotErrorFor:self]); | |
| } | |
| @end | |
| SpecMatcher *beEqualTo(NSObject *arg) { | |
| return [SpecMatcher matcherWithTest:^(NSObject *obj) { | |
| return [obj isEqual:arg]; | |
| } | |
| error:^(NSObject *obj) { | |
| return [NSString stringWithFormat:@"%@ should be equal to %@", | |
| obj, arg]; | |
| } | |
| errorNot:^(NSObject *obj) { | |
| return [NSString stringWithFormat:@"%@ should not be equal to %@", | |
| obj, arg]; | |
| }]; | |
| } | |
| int main(int argc, char *argv[]) { | |
| NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
| NSNumber *object = [NSNumber numberWithInt:30]; | |
| [object should:beEqualTo([NSNumber numberWithInt:29])]; | |
| [pool drain]; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment