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