Skip to content

Instantly share code, notes, and snippets.

@jrr
Created December 21, 2014 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrr/e75902242cc1422662a3 to your computer and use it in GitHub Desktop.
Save jrr/e75902242cc1422662a3 to your computer and use it in GitHub Desktop.
Expecta NSArray-as-NSCountedSet matcher
#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))
// 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