1//--------------------------------------------------------------------------------------- 2// $Id$ 3// Copyright (c) 2007-2010 by Mulle Kybernetik. See License file for details. 4//--------------------------------------------------------------------------------------- 5 6#import <OCMock/OCMConstraint.h> 7 8 9@implementation OCMConstraint 10 11+ (id)constraint 12{ 13 return [[[self alloc] init] autorelease]; 14} 15 16- (BOOL)evaluate:(id)value 17{ 18 return NO; 19} 20 21- (id)copyWithZone:(NSZone *)zone 22{ 23 return [self retain]; 24} 25 26+ (id)constraintWithSelector:(SEL)aSelector onObject:(id)anObject 27{ 28 OCMInvocationConstraint *constraint = [OCMInvocationConstraint constraint]; 29 NSMethodSignature *signature = [anObject methodSignatureForSelector:aSelector]; 30 if(signature == nil) 31 [NSException raise:NSInvalidArgumentException format:@"Unkown selector %@ used in constraint.", NSStringFromSelector(aSelector)]; 32 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature]; 33 [invocation setTarget:anObject]; 34 [invocation setSelector:aSelector]; 35 constraint->invocation = invocation; 36 return constraint; 37} 38 39+ (id)constraintWithSelector:(SEL)aSelector onObject:(id)anObject withValue:(id)aValue 40{ 41 OCMInvocationConstraint *constraint = [self constraintWithSelector:aSelector onObject:anObject]; 42 if([[constraint->invocation methodSignature] numberOfArguments] < 4) 43 [NSException raise:NSInvalidArgumentException format:@"Constraint with value requires selector with two arguments."]; 44 [constraint->invocation setArgument:&aValue atIndex:3]; 45 return constraint; 46} 47 48 49@end 50 51 52 53#pragma mark - 54 55@implementation OCMAnyConstraint 56 57- (BOOL)evaluate:(id)value 58{ 59 return YES; 60} 61 62@end 63 64 65 66#pragma mark - 67 68@implementation OCMIsNilConstraint 69 70- (BOOL)evaluate:(id)value 71{ 72 return value == nil; 73} 74 75@end 76 77 78 79#pragma mark - 80 81@implementation OCMIsNotNilConstraint 82 83- (BOOL)evaluate:(id)value 84{ 85 return value != nil; 86} 87 88@end 89 90 91 92#pragma mark - 93 94@implementation OCMIsNotEqualConstraint 95 96- (BOOL)evaluate:(id)value 97{ 98 return ![value isEqual:testValue]; 99} 100 101@end 102 103 104 105#pragma mark - 106 107@implementation OCMInvocationConstraint 108 109- (BOOL)evaluate:(id)value 110{ 111 [invocation setArgument:&value atIndex:2]; // should test if constraint takes arg 112 [invocation invoke]; 113 BOOL returnValue; 114 [invocation getReturnValue:&returnValue]; 115 return returnValue; 116} 117 118@end 119 120#pragma mark - 121 122#if NS_BLOCKS_AVAILABLE 123 124@implementation OCMBlockConstraint 125 126- (id)initWithConstraintBlock:(BOOL (^)(id))aBlock; 127{ 128 self = [super init]; 129 block = aBlock; 130 return self; 131} 132 133- (BOOL)evaluate:(id)value 134{ 135 return block(value); 136} 137 138@end 139 140#endif 141