- First Commit

This commit is contained in:
giuseppenuc committed 2015-12-17 16:55:03 +01:00
1 parent b34b29256e
commit 3e3fec3773
275 files changed
+11345 -25

No files matched your search

@@ -0,0 +1,4 @@
#import <Foundation/Foundation.h>
BOOL EXPIsValuePointer(NSValue *value);
BOOL EXPIsNumberFloat(NSNumber *number);
@@ -0,0 +1,9 @@
#import "EXPMatcherHelpers.h"
BOOL EXPIsValuePointer(NSValue *value) {
return [value objCType][0] == @encode(void *)[0];
}
BOOL EXPIsNumberFloat(NSNumber *number) {
return strcmp([number objCType], @encode(float)) == 0;
}
@@ -0,0 +1,7 @@
#import "Expecta.h"
EXPMatcherInterface(_beCloseToWithin, (id expected, id within));
EXPMatcherInterface(beCloseToWithin, (id expected, id within));
#define beCloseTo(expected) _beCloseToWithin(EXPObjectify((expected)), nil)
#define beCloseToWithin(expected, range) _beCloseToWithin(EXPObjectify((expected)), EXPObjectify((range)))
@@ -0,0 +1,49 @@
#import "EXPMatchers+beCloseTo.h"
#import "EXPMatcherHelpers.h"
EXPMatcherImplementationBegin(_beCloseToWithin, (id expected, id within)) {
prerequisite(^BOOL{
return [actual isKindOfClass:[NSNumber class]] &&
[expected isKindOfClass:[NSNumber class]] &&
([within isKindOfClass:[NSNumber class]] || (within == nil));
});
match(^BOOL{
double actualValue = [actual doubleValue];
double expectedValue = [expected doubleValue];
if (within != nil) {
double withinValue = [within doubleValue];
double lowerBound = expectedValue - withinValue;
double upperBound = expectedValue + withinValue;
return (actualValue >= lowerBound) && (actualValue <= upperBound);
} else {
double diff = fabs(actualValue - expectedValue);
actualValue = fabs(actualValue);
expectedValue = fabs(expectedValue);
double largest = (expectedValue > actualValue) ? expectedValue : actualValue;
return (diff <= largest * FLT_EPSILON);
}
});
failureMessageForTo(^NSString *{
if (within) {
return [NSString stringWithFormat:@"expected %@ to be close to %@ within %@",
EXPDescribeObject(actual), EXPDescribeObject(expected), EXPDescribeObject(within)];
} else {
return [NSString stringWithFormat:@"expected %@ to be close to %@",
EXPDescribeObject(actual), EXPDescribeObject(expected)];
}
});
failureMessageForNotTo(^NSString *{
if (within) {
return [NSString stringWithFormat:@"expected %@ not to be close to %@ within %@",
EXPDescribeObject(actual), EXPDescribeObject(expected), EXPDescribeObject(within)];
} else {
return [NSString stringWithFormat:@"expected %@ not to be close to %@",
EXPDescribeObject(actual), EXPDescribeObject(expected)];
}
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,3 @@
#import "Expecta.h"
EXPMatcherInterface(beFalsy, (void));
@@ -0,0 +1,24 @@
#import "EXPMatchers+beFalsy.h"
#import "EXPMatcherHelpers.h"
EXPMatcherImplementationBegin(beFalsy, (void)) {
match(^BOOL{
if([actual isKindOfClass:[NSNumber class]]) {
return ![(NSNumber *)actual boolValue];
} else if([actual isKindOfClass:[NSValue class]]) {
if(EXPIsValuePointer((NSValue *)actual)) {
return ![(NSValue *)actual pointerValue];
}
}
return !actual;
});
failureMessageForTo(^NSString *{
return [NSString stringWithFormat:@"expected: a falsy value, got: %@, which is truthy", EXPDescribeObject(actual)];
});
failureMessageForNotTo(^NSString *{
return [NSString stringWithFormat:@"expected: a non-falsy value, got: %@, which is falsy", EXPDescribeObject(actual)];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,6 @@
#import "Expecta.h"
EXPMatcherInterface(_beGreaterThan, (id expected));
EXPMatcherInterface(beGreaterThan, (id expected));
#define beGreaterThan(expected) _beGreaterThan(EXPObjectify((expected)))
@@ -0,0 +1,20 @@
#import "EXPMatchers+beGreaterThan.h"
#import "EXPMatcherHelpers.h"
EXPMatcherImplementationBegin(_beGreaterThan, (id expected)) {
match(^BOOL{
if ([actual respondsToSelector:@selector(compare:)]) {
return [actual compare:expected] == NSOrderedDescending;
}
return NO;
});
failureMessageForTo(^NSString *{
return [NSString stringWithFormat:@"expected: %@ to be greater than %@", EXPDescribeObject(actual), EXPDescribeObject(expected)];
});
failureMessageForNotTo(^NSString *{
return [NSString stringWithFormat:@"expected: %@ not to be greater than %@", EXPDescribeObject(actual), EXPDescribeObject(expected)];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,6 @@
#import "Expecta.h"
EXPMatcherInterface(_beGreaterThanOrEqualTo, (id expected));
EXPMatcherInterface(beGreaterThanOrEqualTo, (id expected));
#define beGreaterThanOrEqualTo(expected) _beGreaterThanOrEqualTo(EXPObjectify((expected)))
@@ -0,0 +1,20 @@
#import "EXPMatchers+beGreaterThanOrEqualTo.h"
#import "EXPMatcherHelpers.h"
EXPMatcherImplementationBegin(_beGreaterThanOrEqualTo, (id expected)) {
match(^BOOL{
if ([actual respondsToSelector:@selector(compare:)]) {
return [actual compare:expected] != NSOrderedAscending;
}
return NO;
});
failureMessageForTo(^NSString *{
return [NSString stringWithFormat:@"expected: %@ to be greater than or equal to %@", EXPDescribeObject(actual), EXPDescribeObject(expected)];
});
failureMessageForNotTo(^NSString *{
return [NSString stringWithFormat:@"expected: %@ not to be greater than or equal to %@", EXPDescribeObject(actual), EXPDescribeObject(expected)];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,10 @@
#import "Expecta.h"
EXPMatcherInterface(_beIdenticalTo, (void *expected));
EXPMatcherInterface(beIdenticalTo, (void *expected)); // to aid code completion
#if __has_feature(objc_arc)
#define beIdenticalTo(expected) _beIdenticalTo((__bridge void*)expected)
#else
#define beIdenticalTo(expected) _beIdenticalTo(expected)
#endif
@@ -0,0 +1,24 @@
#import "EXPMatchers+equal.h"
#import "EXPMatcherHelpers.h"
EXPMatcherImplementationBegin(_beIdenticalTo, (void *expected)) {
match(^BOOL{
if(actual == expected) {
return YES;
} else if([actual isKindOfClass:[NSValue class]] && EXPIsValuePointer((NSValue *)actual)) {
if([(NSValue *)actual pointerValue] == expected) {
return YES;
}
}
return NO;
});
failureMessageForTo(^NSString *{
return [NSString stringWithFormat:@"expected: <%p>, got: <%p>", expected, actual];
});
failureMessageForNotTo(^NSString *{
return [NSString stringWithFormat:@"expected: not <%p>, got: <%p>", expected, actual];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,6 @@
#import "Expecta.h"
EXPMatcherInterface(_beInTheRangeOf, (id expectedLowerBound, id expectedUpperBound));
EXPMatcherInterface(beInTheRangeOf, (id expectedLowerBound, id expectedUpperBound));
#define beInTheRangeOf(expectedLowerBound, expectedUpperBound) _beInTheRangeOf(EXPObjectify((expectedLowerBound)), EXPObjectify((expectedUpperBound)))
@@ -0,0 +1,30 @@
#import "EXPMatchers+beInTheRangeOf.h"
#import "EXPMatcherHelpers.h"
EXPMatcherImplementationBegin(_beInTheRangeOf, (id expectedLowerBound, id expectedUpperBound)) {
match(^BOOL{
if ([actual respondsToSelector:@selector(compare:)]) {
NSComparisonResult compareLowerBound = [expectedLowerBound compare: actual];
NSComparisonResult compareUpperBound = [expectedUpperBound compare: actual];
if (compareLowerBound == NSOrderedSame) {
return YES;
}
if (compareUpperBound == NSOrderedSame) {
return YES;
}
if ((compareLowerBound == NSOrderedAscending) && (compareUpperBound == NSOrderedDescending)) {
return YES;
}
}
return NO;
});
failureMessageForTo(^NSString *{
return [NSString stringWithFormat:@"expected: %@ to be in the range [%@, %@] (inclusive)", EXPDescribeObject(actual), EXPDescribeObject(expectedLowerBound), EXPDescribeObject(expectedUpperBound)];
});
failureMessageForNotTo(^NSString *{
return [NSString stringWithFormat:@"expected: %@ not to be in the range [%@, %@] (inclusive)", EXPDescribeObject(actual), EXPDescribeObject(expectedLowerBound), EXPDescribeObject(expectedUpperBound)];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,6 @@
#import "Expecta.h"
EXPMatcherInterface(beInstanceOf, (Class expected));
EXPMatcherInterface(beAnInstanceOf, (Class expected));
EXPMatcherInterface(beMemberOf, (Class expected));
EXPMatcherInterface(beAMemberOf, (Class expected));
@@ -0,0 +1,31 @@
#import "EXPMatchers+beInstanceOf.h"
EXPMatcherImplementationBegin(beInstanceOf, (Class expected)) {
BOOL actualIsNil = (actual == nil);
BOOL expectedIsNil = (expected == nil);
prerequisite(^BOOL{
return !(actualIsNil || expectedIsNil);
});
match(^BOOL{
return [actual isMemberOfClass:expected];
});
failureMessageForTo(^NSString *{
if(actualIsNil) return @"the actual value is nil/null";
if(expectedIsNil) return @"the expected value is nil/null";
return [NSString stringWithFormat:@"expected: an instance of %@, got: an instance of %@", [expected class], [actual class]];
});
failureMessageForNotTo(^NSString *{
if(actualIsNil) return @"the actual value is nil/null";
if(expectedIsNil) return @"the expected value is nil/null";
return [NSString stringWithFormat:@"expected: not an instance of %@, got: an instance of %@", [expected class], [actual class]];
});
}
EXPMatcherImplementationEnd
EXPMatcherAliasImplementation(beAnInstanceOf, beInstanceOf, (Class expected));
EXPMatcherAliasImplementation(beMemberOf, beInstanceOf, (Class expected));
EXPMatcherAliasImplementation(beAMemberOf, beInstanceOf, (Class expected));
@@ -0,0 +1,4 @@
#import "Expecta.h"
EXPMatcherInterface(beKindOf, (Class expected));
EXPMatcherInterface(beAKindOf, (Class expected));
@@ -0,0 +1,29 @@
#import "EXPMatchers+beKindOf.h"
EXPMatcherImplementationBegin(beKindOf, (Class expected)) {
BOOL actualIsNil = (actual == nil);
BOOL expectedIsNil = (expected == nil);
prerequisite(^BOOL{
return !(actualIsNil || expectedIsNil);
});
match(^BOOL{
return [actual isKindOfClass:expected];
});
failureMessageForTo(^NSString *{
if(actualIsNil) return @"the actual value is nil/null";
if(expectedIsNil) return @"the expected value is nil/null";
return [NSString stringWithFormat:@"expected: a kind of %@, got: an instance of %@, which is not a kind of %@", [expected class], [actual class], [expected class]];
});
failureMessageForNotTo(^NSString *{
if(actualIsNil) return @"the actual value is nil/null";
if(expectedIsNil) return @"the expected value is nil/null";
return [NSString stringWithFormat:@"expected: not a kind of %@, got: an instance of %@, which is a kind of %@", [expected class], [actual class], [expected class]];
});
}
EXPMatcherImplementationEnd
EXPMatcherAliasImplementation(beAKindOf, beKindOf, (Class expected));
@@ -0,0 +1,6 @@
#import "Expecta.h"
EXPMatcherInterface(_beLessThan, (id expected));
EXPMatcherInterface(beLessThan, (id expected));
#define beLessThan(expected) _beLessThan(EXPObjectify((expected)))
@@ -0,0 +1,20 @@
#import "EXPMatchers+beLessThan.h"
#import "EXPMatcherHelpers.h"
EXPMatcherImplementationBegin(_beLessThan, (id expected)) {
match(^BOOL{
if ([actual respondsToSelector:@selector(compare:)]) {
return [actual compare:expected] == NSOrderedAscending;
}
return NO;
});
failureMessageForTo(^NSString *{
return [NSString stringWithFormat:@"expected: %@ to be less than %@", EXPDescribeObject(actual), EXPDescribeObject(expected)];
});
failureMessageForNotTo(^NSString *{
return [NSString stringWithFormat:@"expected: %@ not to be less than %@", EXPDescribeObject(actual), EXPDescribeObject(expected)];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,6 @@
#import "Expecta.h"
EXPMatcherInterface(_beLessThanOrEqualTo, (id expected));
EXPMatcherInterface(beLessThanOrEqualTo, (id expected));
#define beLessThanOrEqualTo(expected) _beLessThanOrEqualTo(EXPObjectify((expected)))
@@ -0,0 +1,20 @@
#import "EXPMatchers+beLessThanOrEqualTo.h"
#import "EXPMatcherHelpers.h"
EXPMatcherImplementationBegin(_beLessThanOrEqualTo, (id expected)) {
match(^BOOL{
if ([actual respondsToSelector:@selector(compare:)]) {
return [actual compare:expected] != NSOrderedDescending;
}
return NO;
});
failureMessageForTo(^NSString *{
return [NSString stringWithFormat:@"expected: %@ to be less than or equal to %@", EXPDescribeObject(actual), EXPDescribeObject(expected)];
});
failureMessageForNotTo(^NSString *{
return [NSString stringWithFormat:@"expected: %@ not to be less than or equal to %@", EXPDescribeObject(actual), EXPDescribeObject(expected)];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,4 @@
#import "Expecta.h"
EXPMatcherInterface(beNil, (void));
EXPMatcherInterface(beNull, (void));
@@ -0,0 +1,18 @@
#import "EXPMatchers+beNil.h"
EXPMatcherImplementationBegin(beNil, (void)) {
match(^BOOL{
return actual == nil;
});
failureMessageForTo(^NSString *{
return [NSString stringWithFormat:@"expected: nil/null, got: %@", EXPDescribeObject(actual)];
});
failureMessageForNotTo(^NSString *{
return [NSString stringWithFormat:@"expected: not nil/null, got: %@", EXPDescribeObject(actual)];
});
}
EXPMatcherImplementationEnd
EXPMatcherAliasImplementation(beNull, beNil, (void));
@@ -0,0 +1,4 @@
#import "Expecta.h"
EXPMatcherInterface(beSubclassOf, (Class expected));
EXPMatcherInterface(beASubclassOf, (Class expected));
@@ -0,0 +1,29 @@
#import "EXPMatchers+beSubclassOf.h"
#import "NSValue+Expecta.h"
#import <objc/runtime.h>
EXPMatcherImplementationBegin(beSubclassOf, (Class expected)) {
__block BOOL actualIsClass = YES;
prerequisite(^BOOL {
actualIsClass = class_isMetaClass(object_getClass(actual));
return actualIsClass;
});
match(^BOOL{
return [actual isSubclassOfClass:expected];
});
failureMessageForTo(^NSString *{
if(!actualIsClass) return @"the actual value is not a Class";
return [NSString stringWithFormat:@"expected: a subclass of %@, got: a class %@, which is not a subclass of %@", [expected class], actual, [expected class]];
});
failureMessageForNotTo(^NSString *{
if(!actualIsClass) return @"the actual value is not a Class";
return [NSString stringWithFormat:@"expected: not a subclass of %@, got: a class %@, which is a subclass of %@", [expected class], actual, [expected class]];
});
}
EXPMatcherImplementationEnd
EXPMatcherAliasImplementation(beASubclassOf, beSubclassOf, (Class expected));
@@ -0,0 +1,4 @@
#import "Expecta.h"
EXPMatcherInterface(beSupersetOf, (id subset));
@@ -0,0 +1,62 @@
#import "EXPMatchers+contain.h"
EXPMatcherImplementationBegin(beSupersetOf, (id subset)) {
BOOL actualIsCompatible = [actual isKindOfClass:[NSDictionary class]] || [actual respondsToSelector:@selector(containsObject:)];
BOOL subsetIsNil = (subset == nil);
// For some instances the isKindOfClass: method returns false, even though
// they are both actually dictionaries. e.g. Comparing a NSCFDictionary and a
// NSDictionary.
// Or in cases when you compare NSMutableArray (which implementation is __NSArrayM:NSMutableArray:NSArray)
// and NSArray (which implementation is __NSArrayI:NSArray)
BOOL bothAreIdenticalCollectionClasses = ([actual isKindOfClass:[NSDictionary class]] && [subset isKindOfClass:[NSDictionary class]]) ||
([actual isKindOfClass:[NSArray class]] && [subset isKindOfClass:[NSArray class]]) ||
([actual isKindOfClass:[NSSet class]] && [subset isKindOfClass:[NSSet class]]) ||
([actual isKindOfClass:[NSOrderedSet class]] && [subset isKindOfClass:[NSOrderedSet class]]);
BOOL classMatches = bothAreIdenticalCollectionClasses || [subset isKindOfClass:[actual class]];
prerequisite(^BOOL{
return actualIsCompatible && !subsetIsNil && classMatches;
});
match(^BOOL{
if(!actualIsCompatible) return NO;
if([actual isKindOfClass:[NSDictionary class]]) {
for (id key in subset) {
id actualValue = [actual valueForKey:key];
id subsetValue = [subset valueForKey:key];
if (![subsetValue isEqual:actualValue]) return NO;
}
} else {
for (id object in subset) {
if (![actual containsObject:object]) return NO;
}
}
return YES;
});
failureMessageForTo(^NSString *{
if(!actualIsCompatible) return [NSString stringWithFormat:@"%@ is not an instance of NSDictionary and does not implement -containsObject:", EXPDescribeObject(actual)];
if(subsetIsNil) return @"the expected value is nil/null";
if(!classMatches) return [NSString stringWithFormat:@"%@ does not match the class of %@", EXPDescribeObject(subset), EXPDescribeObject(actual)];
return [NSString stringWithFormat:@"expected %@ to be a superset of %@", EXPDescribeObject(actual), EXPDescribeObject(subset)];
});
failureMessageForNotTo(^NSString *{
if(!actualIsCompatible) return [NSString stringWithFormat:@"%@ is not an instance of NSDictionary and does not implement -containsObject:", EXPDescribeObject(actual)];
if(subsetIsNil) return @"the expected value is nil/null";
if(!classMatches) return [NSString stringWithFormat:@"%@ does not match the class of %@", EXPDescribeObject(subset), EXPDescribeObject(actual)];
return [NSString stringWithFormat:@"expected %@ not to be a superset of %@", EXPDescribeObject(actual), EXPDescribeObject(subset)];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,3 @@
#import "Expecta.h"
EXPMatcherInterface(beTruthy, (void));
@@ -0,0 +1,24 @@
#import "EXPMatchers+beTruthy.h"
#import "EXPMatcherHelpers.h"
EXPMatcherImplementationBegin(beTruthy, (void)) {
match(^BOOL{
if([actual isKindOfClass:[NSNumber class]]) {
return !![(NSNumber *)actual boolValue];
} else if([actual isKindOfClass:[NSValue class]]) {
if(EXPIsValuePointer((NSValue *)actual)) {
return !![(NSValue *)actual pointerValue];
}
}
return !!actual;
});
failureMessageForTo(^NSString *{
return [NSString stringWithFormat:@"expected: a truthy value, got: %@, which is falsy", EXPDescribeObject(actual)];
});
failureMessageForNotTo(^NSString *{
return [NSString stringWithFormat:@"expected: a non-truthy value, got: %@, which is truthy", EXPDescribeObject(actual)];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,4 @@
#import "Expecta.h"
EXPMatcherInterface(beginWith, (id expected));
EXPMatcherInterface(startWith, (id expected));
@@ -0,0 +1,51 @@
#import "EXPMatchers+beginWith.h"
EXPMatcherImplementationBegin(beginWith, (id expected)) {
BOOL actualIsNil = (actual == nil);
BOOL expectedIsNil = (expected == nil);
//This condition allows the comparison of an immutable string or ordered collection to the mutable type of the same
BOOL actualAndExpectedAreCompatible = (([actual isKindOfClass:[NSString class]] && [expected isKindOfClass:[NSString class]])
|| ([actual isKindOfClass:[NSArray class]] && [expected isKindOfClass:[NSArray class]])
|| ([actual isKindOfClass:[NSOrderedSet class]] && [expected isKindOfClass:[NSOrderedSet class]]));
prerequisite(^BOOL {
return actualAndExpectedAreCompatible;
});
match(^BOOL {
if ([actual isKindOfClass:[NSString class]]) {
return [actual hasPrefix:expected];
} else if ([actual isKindOfClass:[NSArray class]]) {
if ([expected count] > [actual count] || [expected count] == 0) {
return NO;
}
NSArray *subArray = [actual subarrayWithRange:NSMakeRange(0, [expected count])];
return [subArray isEqualToArray:expected];
} else {
if ([expected count] > [actual count] || [expected count] == 0) {
return NO;
}
NSOrderedSet *subset = [NSOrderedSet orderedSetWithOrderedSet:actual range:NSMakeRange(0, [expected count]) copyItems:NO];
return [subset isEqualToOrderedSet:expected];
}
});
failureMessageForTo(^NSString *{
if (actualIsNil) return @"the object is nil/null";
if (expectedIsNil) return @"the expected value is nil/null";
if (!actualAndExpectedAreCompatible) return [NSString stringWithFormat:@"%@ and %@ are not instances of one of %@, %@, or %@", EXPDescribeObject(actual), EXPDescribeObject(expected), [NSString class], [NSArray class], [NSOrderedSet class]];
return [NSString stringWithFormat:@"expected: %@ to begin with %@", EXPDescribeObject(actual), EXPDescribeObject(expected)];
});
failureMessageForNotTo(^NSString *{
if (actualIsNil) return @"the object is nil/null";
if (expectedIsNil) return @"the expected value is nil/null";
if (!actualAndExpectedAreCompatible) return [NSString stringWithFormat:@"%@ and %@ are not instances of one of %@, %@, or %@", EXPDescribeObject(actual), EXPDescribeObject(expected), [NSString class], [NSArray class], [NSOrderedSet class]];
return [NSString stringWithFormat:@"expected: %@ not to begin with %@", EXPDescribeObject(actual), EXPDescribeObject(expected)];
});
}
EXPMatcherImplementationEnd
EXPMatcherAliasImplementation(startWith, beginWith, (id expected));
@@ -0,0 +1,3 @@
#import "Expecta.h"
EXPMatcherInterface(conformTo, (Protocol *expected));
@@ -0,0 +1,33 @@
#import "EXPMatchers+conformTo.h"
#import "NSValue+Expecta.h"
#import <objc/runtime.h>
EXPMatcherImplementationBegin(conformTo, (Protocol *expected)) {
BOOL actualIsNil = (actual == nil);
BOOL expectedIsNil = (expected == nil);
prerequisite(^BOOL{
return !(actualIsNil || expectedIsNil);
});
match(^BOOL{
return [actual conformsToProtocol:expected];
});
failureMessageForTo(^NSString *{
if(actualIsNil) return @"the object is nil/null";
if(expectedIsNil) return @"the protocol is nil/null";
NSString *name = NSStringFromProtocol(expected);
return [NSString stringWithFormat:@"expected: %@ to conform to %@", actual, name];
});
failureMessageForNotTo(^NSString *{
if(actualIsNil) return @"the object is nil/null";
if(expectedIsNil) return @"the protocol is nil/null";
NSString *name = NSStringFromProtocol(expected);
return [NSString stringWithFormat:@"expected: %@ not to conform to %@", actual, name];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,5 @@
#import "Expecta.h"
EXPMatcherInterface(_contain, (id expected));
EXPMatcherInterface(contain, (id expected)); // to aid code completion
#define contain(expected) _contain(EXPObjectify((expected)))
@@ -0,0 +1,38 @@
#import "EXPMatchers+contain.h"
EXPMatcherImplementationBegin(_contain, (id expected)) {
BOOL actualIsCompatible = [actual isKindOfClass:[NSString class]] || [actual conformsToProtocol:@protocol(NSFastEnumeration)];
BOOL expectedIsNil = (expected == nil);
prerequisite(^BOOL{
return actualIsCompatible && !expectedIsNil;
});
match(^BOOL{
if(actualIsCompatible) {
if([actual isKindOfClass:[NSString class]]) {
return [(NSString *)actual rangeOfString:[expected description]].location != NSNotFound;
} else {
for (id object in actual) {
if ([object isEqual:expected]) {
return YES;
}
}
}
}
return NO;
});
failureMessageForTo(^NSString *{
if(!actualIsCompatible) return [NSString stringWithFormat:@"%@ is not an instance of NSString or NSFastEnumeration", EXPDescribeObject(actual)];
if(expectedIsNil) return @"the expected value is nil/null";
return [NSString stringWithFormat:@"expected %@ to contain %@", EXPDescribeObject(actual), EXPDescribeObject(expected)];
});
failureMessageForNotTo(^NSString *{
if(!actualIsCompatible) return [NSString stringWithFormat:@"%@ is not an instance of NSString or NSFastEnumeration", EXPDescribeObject(actual)];
if(expectedIsNil) return @"the expected value is nil/null";
return [NSString stringWithFormat:@"expected %@ not to contain %@", EXPDescribeObject(actual), EXPDescribeObject(expected)];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,3 @@
#import "Expecta.h"
EXPMatcherInterface(endWith, (id expected));
@@ -0,0 +1,49 @@
#import "EXPMatchers+endWith.h"
EXPMatcherImplementationBegin(endWith, (id expected)) {
BOOL actualIsNil = (actual == nil);
BOOL expectedIsNil = (expected == nil);
//This condition allows the comparison of an immutable string or ordered collection to the mutable type of the same
BOOL actualAndExpectedAreCompatible = (([actual isKindOfClass:[NSString class]] && [expected isKindOfClass:[NSString class]])
|| ([actual isKindOfClass:[NSArray class]] && [expected isKindOfClass:[NSArray class]])
|| ([actual isKindOfClass:[NSOrderedSet class]] && [expected isKindOfClass:[NSOrderedSet class]]));
prerequisite(^BOOL {
return actualAndExpectedAreCompatible;
});
match(^BOOL {
if ([actual isKindOfClass:[NSString class]]) {
return [actual hasSuffix:expected];
} else if ([actual isKindOfClass:[NSArray class]]) {
if ([expected count] > [actual count] || [expected count] == 0) {
return NO;
}
NSArray *subArray = [actual subarrayWithRange:NSMakeRange([actual count] - [expected count], [expected count])];
return [subArray isEqualToArray:expected];
} else {
if ([expected count] > [actual count] || [expected count] == 0) {
return NO;
}
NSOrderedSet *subset = [NSOrderedSet orderedSetWithOrderedSet:actual range:NSMakeRange([actual count] - [expected count], [expected count]) copyItems:NO];
return [subset isEqualToOrderedSet:expected];
}
});
failureMessageForTo(^NSString *{
if (actualIsNil) return @"the object is nil/null";
if (expectedIsNil) return @"the expected value is nil/null";
if (!actualAndExpectedAreCompatible) return [NSString stringWithFormat:@"%@ and %@ are not instances of one of %@, %@, or %@", EXPDescribeObject(actual), EXPDescribeObject(expected), [NSString class], [NSArray class], [NSOrderedSet class]];
return [NSString stringWithFormat:@"expected: %@ to end with %@", EXPDescribeObject(actual), EXPDescribeObject(expected)];
});
failureMessageForNotTo(^NSString *{
if (actualIsNil) return @"the object is nil/null";
if (expectedIsNil) return @"the expected value is nil/null";
if (!actualAndExpectedAreCompatible) return [NSString stringWithFormat:@"%@ and %@ are not instances of one of %@, %@, or %@", EXPDescribeObject(actual), EXPDescribeObject(expected), [NSString class], [NSArray class], [NSOrderedSet class]];
return [NSString stringWithFormat:@"expected: %@ not to end with %@", EXPDescribeObject(actual), EXPDescribeObject(expected)];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,5 @@
#import "Expecta.h"
EXPMatcherInterface(_equal, (id expected));
EXPMatcherInterface(equal, (id expected)); // to aid code completion
#define equal(...) _equal(EXPObjectify((__VA_ARGS__)))
@@ -0,0 +1,38 @@
#import "EXPMatchers+equal.h"
#import "EXPMatcherHelpers.h"
EXPMatcherImplementationBegin(_equal, (id expected)) {
match(^BOOL{
if((actual == expected) || [actual isEqual:expected]) {
return YES;
} else if([actual isKindOfClass:[NSNumber class]] && [expected isKindOfClass:[NSNumber class]]) {
if([actual isKindOfClass:[NSDecimalNumber class]] || [expected isKindOfClass:[NSDecimalNumber class]]) {
NSDecimalNumber *actualDecimalNumber = [NSDecimalNumber decimalNumberWithDecimal:[(NSNumber *) actual decimalValue]];
NSDecimalNumber *expectedDecimalNumber = [NSDecimalNumber decimalNumberWithDecimal:[(NSNumber *) expected decimalValue]];
return [actualDecimalNumber isEqualToNumber:expectedDecimalNumber];
}
else {
if(EXPIsNumberFloat((NSNumber *)actual) || EXPIsNumberFloat((NSNumber *)expected)) {
return [(NSNumber *)actual floatValue] == [(NSNumber *)expected floatValue];
}
}
}
return NO;
});
failureMessageForTo(^NSString *{
NSString *expectedDescription = EXPDescribeObject(expected);
NSString *actualDescription = EXPDescribeObject(actual);
if (![expectedDescription isEqualToString:actualDescription]) {
return [NSString stringWithFormat:@"expected: %@, got: %@", EXPDescribeObject(expected), EXPDescribeObject(actual)];
} else {
return [NSString stringWithFormat:@"expected (%@): %@, got (%@): %@", NSStringFromClass([expected class]), EXPDescribeObject(expected), NSStringFromClass([actual class]), EXPDescribeObject(actual)];
}
});
failureMessageForNotTo(^NSString *{
return [NSString stringWithFormat:@"expected: not %@, got: %@", EXPDescribeObject(expected), EXPDescribeObject(actual)];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,10 @@
#import "Expecta.h"
EXPMatcherInterface(haveCountOf, (NSUInteger expected));
EXPMatcherInterface(haveCount, (NSUInteger expected));
EXPMatcherInterface(haveACountOf, (NSUInteger expected));
EXPMatcherInterface(haveLength, (NSUInteger expected));
EXPMatcherInterface(haveLengthOf, (NSUInteger expected));
EXPMatcherInterface(haveALengthOf, (NSUInteger expected));
#define beEmpty() haveCountOf(0)
@@ -0,0 +1,42 @@
#import "EXPMatchers+haveCountOf.h"
EXPMatcherImplementationBegin(haveCountOf, (NSUInteger expected)) {
BOOL actualIsStringy = [actual isKindOfClass:[NSString class]] || [actual isKindOfClass:[NSAttributedString class]];
BOOL actualIsCompatible = actualIsStringy || [actual respondsToSelector:@selector(count)];
prerequisite(^BOOL{
return actualIsCompatible;
});
NSUInteger (^count)(id) = ^(id actual) {
if(actualIsStringy) {
return [actual length];
} else {
return [actual count];
}
};
match(^BOOL{
if(actualIsCompatible) {
return count(actual) == expected;
}
return NO;
});
failureMessageForTo(^NSString *{
if(!actualIsCompatible) return [NSString stringWithFormat:@"%@ is not an instance of NSString, NSAttributedString, NSArray, NSSet, NSOrderedSet, or NSDictionary", EXPDescribeObject(actual)];
return [NSString stringWithFormat:@"expected %@ to have a count of %zi but got %zi", EXPDescribeObject(actual), expected, count(actual)];
});
failureMessageForNotTo(^NSString *{
if(!actualIsCompatible) return [NSString stringWithFormat:@"%@ is not an instance of NSString, NSAttributedString, NSArray, NSSet, NSOrderedSet, or NSDictionary", EXPDescribeObject(actual)];
return [NSString stringWithFormat:@"expected %@ not to have a count of %zi", EXPDescribeObject(actual), expected];
});
}
EXPMatcherImplementationEnd
EXPMatcherAliasImplementation(haveCount, haveCountOf, (NSUInteger expected));
EXPMatcherAliasImplementation(haveACountOf, haveCountOf, (NSUInteger expected));
EXPMatcherAliasImplementation(haveLength, haveCountOf, (NSUInteger expected));
EXPMatcherAliasImplementation(haveLengthOf, haveCountOf, (NSUInteger expected));
EXPMatcherAliasImplementation(haveALengthOf, haveCountOf, (NSUInteger expected));
@@ -0,0 +1,3 @@
#import "Expecta.h"
EXPMatcherInterface(match, (NSString *expected));
@@ -0,0 +1,38 @@
#import "EXPMatchers+match.h"
#import "EXPMatcherHelpers.h"
EXPMatcherImplementationBegin(match, (NSString *expected)) {
BOOL actualIsNil = (actual == nil);
BOOL expectedIsNil = (expected == nil);
__block NSRegularExpression *regex = nil;
__block NSError *regexError = nil;
prerequisite (^BOOL {
BOOL nilInput = (actualIsNil || expectedIsNil);
if (!nilInput) {
regex = [NSRegularExpression regularExpressionWithPattern:expected options:0 error:&regexError];
}
return !nilInput && regex;
});
match(^BOOL {
NSRange range = [regex rangeOfFirstMatchInString:actual options:0 range:NSMakeRange(0, [actual length])];
return !NSEqualRanges(range, NSMakeRange(NSNotFound, 0));
});
failureMessageForTo(^NSString *{
if (actualIsNil) return @"the object is nil/null";
if (expectedIsNil) return @"the expression is nil/null";
if (regexError) return [NSString stringWithFormat:@"unable to create regular expression from given parameter: %@", [regexError localizedDescription]];
return [NSString stringWithFormat:@"expected: %@ to match to %@", EXPDescribeObject(actual), expected];
});
failureMessageForNotTo(^NSString *{
if (actualIsNil) return @"the object is nil/null";
if (expectedIsNil) return @"the expression is nil/null";
if (regexError) return [NSString stringWithFormat:@"unable to create regular expression from given parameter: %@", [regexError localizedDescription]];
return [NSString stringWithFormat:@"expected: %@ not to match to %@", EXPDescribeObject(actual), expected];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,4 @@
#import "Expecta.h"
EXPMatcherInterface(postNotification, (id expectedNotification));
EXPMatcherInterface(notify, (id expectedNotification));
@@ -0,0 +1,88 @@
#import "EXPMatchers+postNotification.h"
@implementation NSNotification (EXPEquality)
- (BOOL)exp_isFunctionallyEqualTo:(NSNotification *)otherNotification
{
if (![otherNotification isKindOfClass:[NSNotification class]]) return NO;
BOOL namesMatch = [otherNotification.name isEqualToString:self.name];
BOOL objectsMatch = YES;
if (otherNotification.object || self.object) {
objectsMatch = [otherNotification.object isEqual:self.object];
}
BOOL userInfoMatches = YES;
if (otherNotification.userInfo || self.userInfo) {
userInfoMatches = [otherNotification.userInfo isEqual:self.userInfo];
}
return (namesMatch && objectsMatch && userInfoMatches);
}
@end
EXPMatcherImplementationBegin(postNotification, (id expected)){
BOOL actualIsNil = (actual == nil);
BOOL expectedIsNil = (expected == nil);
BOOL isNotification = [expected isKindOfClass:[NSNotification class]];
BOOL isName = [expected isKindOfClass:[NSString class]];
__block NSString *expectedName;
__block BOOL expectedNotificationOccurred = NO;
__block id observer;
prerequisite(^BOOL{
expectedNotificationOccurred = NO;
if (actualIsNil || expectedIsNil) return NO;
if (isNotification) {
expectedName = [expected name];
}else if(isName) {
expectedName = expected;
}else{
return NO;
}
observer = [[NSNotificationCenter defaultCenter] addObserverForName:expectedName object:nil queue:nil usingBlock:^(NSNotification *note){
if (isNotification) {
expectedNotificationOccurred |= [expected exp_isFunctionallyEqualTo:note];
}else{
expectedNotificationOccurred = YES;
}
}];
((EXPBasicBlock)actual)();
return YES;
});
match(^BOOL{
if(expectedNotificationOccurred) {
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}
return expectedNotificationOccurred;
});
failureMessageForTo(^NSString *{
if (observer) {
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}
if(actualIsNil) return @"the actual value is nil/null";
if(expectedIsNil) return @"the expected value is nil/null";
if(!(isNotification || isName)) return @"the actual value is not a notification or string";
return [NSString stringWithFormat:@"expected: %@, got: none",expectedName];
});
failureMessageForNotTo(^NSString *{
if (observer) {
[[NSNotificationCenter defaultCenter] removeObserver:observer];
}
if(actualIsNil) return @"the actual value is nil/null";
if(expectedIsNil) return @"the expected value is nil/null";
if(!(isNotification || isName)) return @"the actual value is not a notification or string";
return [NSString stringWithFormat:@"expected: none, got: %@", expectedName];
});
}
EXPMatcherImplementationEnd
EXPMatcherAliasImplementation(notify, postNotification, (id expectedNotification))
@@ -0,0 +1,4 @@
#import "Expecta.h"
EXPMatcherInterface(raise, (NSString *expectedExceptionName));
#define raiseAny() raise(nil)
@@ -0,0 +1,30 @@
#import "EXPMatchers+raise.h"
#import "EXPDefines.h"
EXPMatcherImplementationBegin(raise, (NSString *expectedExceptionName)) {
__block NSException *exceptionCaught = nil;
match(^BOOL{
BOOL expectedExceptionCaught = NO;
@try {
((EXPBasicBlock)actual)();
} @catch(NSException *e) {
exceptionCaught = e;
expectedExceptionCaught = (expectedExceptionName == nil) || [[exceptionCaught name] isEqualToString:expectedExceptionName];
}
return expectedExceptionCaught;
});
failureMessageForTo(^NSString *{
return [NSString stringWithFormat:@"expected: %@, got: %@",
expectedExceptionName ? expectedExceptionName : @"any exception",
exceptionCaught ? [exceptionCaught name] : @"no exception"];
});
failureMessageForNotTo(^NSString *{
return [NSString stringWithFormat:@"expected: %@, got: %@",
expectedExceptionName ? [NSString stringWithFormat:@"not %@", expectedExceptionName] : @"no exception",
exceptionCaught ? [exceptionCaught name] : @"no exception"];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,3 @@
#import "Expecta.h"
EXPMatcherInterface(raiseWithReason, (NSString *expectedExceptionName, NSString *expectedReason));
@@ -0,0 +1,35 @@
#import "EXPMatchers+raiseWithReason.h"
#import "EXPDefines.h"
EXPMatcherImplementationBegin(raiseWithReason, (NSString *expectedExceptionName, NSString *expectedReason)) {
__block NSException *exceptionCaught = nil;
match(^BOOL{
BOOL expectedExceptionCaught = NO;
@try {
((EXPBasicBlock)actual)();
} @catch(NSException *e) {
exceptionCaught = e;
expectedExceptionCaught = (((expectedExceptionName == nil) || [[exceptionCaught name] isEqualToString:expectedExceptionName]) &&
((expectedReason == nil) || ([[exceptionCaught reason] isEqualToString:expectedReason])));
}
return expectedExceptionCaught;
});
failureMessageForTo(^NSString *{
return [NSString stringWithFormat:@"expected: %@ (%@), got: %@ (%@)",
expectedExceptionName ?: @"any exception",
expectedReason ?: @"any reason",
exceptionCaught ? [exceptionCaught name] : @"no exception",
exceptionCaught ? [exceptionCaught reason] : @""];
});
failureMessageForNotTo(^NSString *{
return [NSString stringWithFormat:@"expected: %@ (%@), got: %@ (%@)",
expectedExceptionName ? [NSString stringWithFormat:@"not %@", expectedExceptionName] : @"no exception",
expectedReason ? [NSString stringWithFormat:@"not '%@'", expectedReason] : @"no reason",
exceptionCaught ? [exceptionCaught name] : @"no exception",
exceptionCaught ? [exceptionCaught reason] : @"no reason"];
});
}
EXPMatcherImplementationEnd
@@ -0,0 +1,3 @@
#import "Expecta.h"
EXPMatcherInterface(respondTo, (SEL expected));
@@ -0,0 +1,28 @@
#import "EXPMatchers+respondTo.h"
#import "EXPMatcherHelpers.h"
EXPMatcherImplementationBegin(respondTo, (SEL expected)) {
BOOL actualIsNil = (actual == nil);
BOOL expectedIsNull = (expected == NULL);
prerequisite (^BOOL {
return !(actualIsNil || expectedIsNull);
});
match(^BOOL {
return [actual respondsToSelector:expected];
});
failureMessageForTo(^NSString *{
if (actualIsNil) return @"the object is nil/null";
if (expectedIsNull) return @"the selector is null";
return [NSString stringWithFormat:@"expected: %@ to respond to %@", EXPDescribeObject(actual), NSStringFromSelector(expected)];
});
failureMessageForNotTo(^NSString *{
if (actualIsNil) return @"the object is nil/null";
if (expectedIsNull) return @"the selector is null";
return [NSString stringWithFormat:@"expected: %@ not to respond to %@", EXPDescribeObject(actual), NSStringFromSelector(expected)];
});
}
EXPMatcherImplementationEnd
+25
View File
@@ -0,0 +1,25 @@
#import "EXPMatchers+beNil.h"
#import "EXPMatchers+equal.h"
#import "EXPMatchers+beInstanceOf.h"
#import "EXPMatchers+beKindOf.h"
#import "EXPMatchers+beSubclassOf.h"
#import "EXPMatchers+conformTo.h"
#import "EXPMatchers+beTruthy.h"
#import "EXPMatchers+beFalsy.h"
#import "EXPMatchers+contain.h"
#import "EXPMatchers+beSupersetOf.h"
#import "EXPMatchers+haveCountOf.h"
#import "EXPMatchers+beIdenticalTo.h"
#import "EXPMatchers+beGreaterThan.h"
#import "EXPMatchers+beGreaterThanOrEqualTo.h"
#import "EXPMatchers+beLessThan.h"
#import "EXPMatchers+beLessThanOrEqualTo.h"
#import "EXPMatchers+beInTheRangeOf.h"
#import "EXPMatchers+beCloseTo.h"
#import "EXPMatchers+raise.h"
#import "EXPMatchers+raiseWithReason.h"
#import "EXPMatchers+respondTo.h"
#import "EXPMatchers+postNotification.h"
#import "EXPMatchers+beginWith.h"
#import "EXPMatchers+endWith.h"
#import "EXPMatchers+match.h"