Created
December 21, 2014 05:11
-
-
Save jrr/e75902242cc1422662a3 to your computer and use it in GitHub Desktop.
Expecta NSArray-as-NSCountedSet matcher
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
#import "Expecta.h" | |
EXPMatcherInterface(equalInAnyOrder, (id expected)); | |
// 1st argument is the name of the matcher function | |
// 2nd argument is the list of arguments that may be passed in the function | |
// call. | |
// Multiple arguments are fine. (e.g. (int foo, float bar)) | |
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
// based on EXPMatchers+beKindOf example from https://github.com/specta/expecta | |
#import "EXPMatchers+equalInAnyOrder.h" | |
EXPMatcherImplementationBegin(equalInAnyOrder, (id expected)) { | |
BOOL actualIsNil = (actual == nil); | |
BOOL expectedIsNil = (expected == nil); | |
//TODO: handle NSSet, NSCountedSet, ... | |
BOOL paramsAreSupportedTypes = [expected isKindOfClass:[NSArray class]] && [actual isKindOfClass:[NSArray class]]; | |
prerequisite(^BOOL { | |
// Return `NO` if matcher should fail whether or not the result is inverted | |
// using `.Not`. | |
if(actualIsNil || expectedIsNil){ | |
return NO; | |
} | |
if(!paramsAreSupportedTypes){ | |
return NO; | |
} | |
return YES; | |
}); | |
match(^BOOL { | |
NSCountedSet *expectedCountedSet = [NSCountedSet setWithArray:expected]; | |
NSCountedSet *actualCountedSet = [NSCountedSet setWithArray:actual]; | |
return [expectedCountedSet isEqual:actualCountedSet]; | |
// Return `YES` if the matcher should pass, `NO` if it should not. | |
// The actual value/object is passed as `actual`. | |
// Please note that primitive values will be wrapped in NSNumber/NSValue. | |
}); | |
failureMessageForTo(^NSString * { | |
if (actualIsNil) | |
return @"the actual value is nil/null"; | |
if (expectedIsNil) | |
return @"the expected value is nil/null"; | |
if (!paramsAreSupportedTypes) | |
return @"this matcher only supports NSArray type"; | |
return [NSString stringWithFormat:@"expected: %@, got: %@", EXPDescribeObject(expected), EXPDescribeObject(actual)]; | |
// Return the message to be displayed when the match function returns `YES`. | |
}); | |
failureMessageForNotTo(^NSString * { | |
if (actualIsNil) | |
return @"the actual value is nil/null"; | |
if (expectedIsNil) | |
return @"the expected value is nil/null"; | |
if (!paramsAreSupportedTypes) | |
return @"this matcher only supports NSArray type"; | |
return [NSString stringWithFormat:@"expected: %@, got: %@", EXPDescribeObject(expected), EXPDescribeObject(actual)]; | |
// Return the message to be displayed when the match function returns `NO`. | |
}); | |
} | |
EXPMatcherImplementationEnd |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment