| OLD | NEW |
| 1 //------------------------------------------------------------------------------
--------- | 1 /* |
| 2 // $Id$ | 2 * Copyright (c) 2007-2015 Erik Doernenburg and contributors |
| 3 // Copyright (c) 2007-2010 by Mulle Kybernetik. See License file for details. | 3 * |
| 4 //------------------------------------------------------------------------------
--------- | 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 * not use these files except in compliance with the License. You may obtain |
| 6 * a copy of the License at |
| 7 * |
| 8 * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 * |
| 10 * Unless required by applicable law or agreed to in writing, software |
| 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 * License for the specific language governing permissions and limitations |
| 14 * under the License. |
| 15 */ |
| 5 | 16 |
| 6 #import <OCMock/OCMConstraint.h> | 17 #import <OCMock/OCMConstraint.h> |
| 7 | 18 |
| 8 | 19 |
| 9 @implementation OCMConstraint | 20 @implementation OCMConstraint |
| 10 | 21 |
| 11 + (id)constraint | 22 + (instancetype)constraint { |
| 12 { | 23 return [[[self alloc] init] autorelease]; |
| 13 » return [[[self alloc] init] autorelease]; | |
| 14 } | 24 } |
| 15 | 25 |
| 16 - (BOOL)evaluate:(id)value | 26 - (BOOL)evaluate:(id)value |
| 17 { | 27 { |
| 18 return NO; | 28 return NO; |
| 19 } | 29 } |
| 20 | 30 |
| 21 - (id)copyWithZone:(NSZone *)zone | 31 - (id)copyWithZone:(struct _NSZone*)zone { |
| 22 { | 32 return [self retain]; |
| 23 » return [self retain]; | |
| 24 } | 33 } |
| 25 | 34 |
| 26 + (id)constraintWithSelector:(SEL)aSelector onObject:(id)anObject | 35 + (instancetype)constraintWithSelector:(SEL)aSelector onObject:(id)anObject { |
| 27 { | 36 OCMInvocationConstraint* constraint = [OCMInvocationConstraint constraint]; |
| 28 » OCMInvocationConstraint *constraint = [OCMInvocationConstraint constrain
t]; | 37 NSMethodSignature* signature = |
| 29 » NSMethodSignature *signature = [anObject methodSignatureForSelector:aSel
ector]; | 38 [anObject methodSignatureForSelector:aSelector]; |
| 30 » if(signature == nil) | 39 if (signature == nil) |
| 31 » » [NSException raise:NSInvalidArgumentException format:@"Unkown se
lector %@ used in constraint.", NSStringFromSelector(aSelector)]; | 40 [NSException raise:NSInvalidArgumentException |
| 32 » NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | 41 format:@"Unkown selector %@ used in constraint.", |
| 33 » [invocation setTarget:anObject]; | 42 NSStringFromSelector(aSelector)]; |
| 34 » [invocation setSelector:aSelector]; | 43 NSInvocation* invocation = |
| 35 » constraint->invocation = invocation; | 44 [NSInvocation invocationWithMethodSignature:signature]; |
| 36 » return constraint; | 45 [invocation setTarget:anObject]; |
| 46 [invocation setSelector:aSelector]; |
| 47 constraint->invocation = invocation; |
| 48 return constraint; |
| 37 } | 49 } |
| 38 | 50 |
| 39 + (id)constraintWithSelector:(SEL)aSelector onObject:(id)anObject withValue:(id)
aValue | 51 + (instancetype)constraintWithSelector:(SEL)aSelector |
| 40 { | 52 onObject:(id)anObject |
| 41 » OCMInvocationConstraint *constraint = [self constraintWithSelector:aSele
ctor onObject:anObject]; | 53 withValue:(id)aValue { |
| 42 » if([[constraint->invocation methodSignature] numberOfArguments] < 4) | 54 OCMInvocationConstraint* constraint = |
| 43 » » [NSException raise:NSInvalidArgumentException format:@"Constrain
t with value requires selector with two arguments."]; | 55 [self constraintWithSelector:aSelector onObject:anObject]; |
| 44 » [constraint->invocation setArgument:&aValue atIndex:3]; | 56 if ([[constraint->invocation methodSignature] numberOfArguments] < 4) |
| 45 » return constraint; | 57 [NSException |
| 58 raise:NSInvalidArgumentException |
| 59 format:@"Constraint with value requires selector with two arguments."]; |
| 60 [constraint->invocation setArgument:&aValue atIndex:3]; |
| 61 return constraint; |
| 46 } | 62 } |
| 47 | 63 |
| 48 | 64 |
| 49 @end | 65 @end |
| 50 | 66 |
| 51 | 67 |
| 52 | 68 |
| 53 #pragma mark - | 69 #pragma mark - |
| 54 | 70 |
| 55 @implementation OCMAnyConstraint | 71 @implementation OCMAnyConstraint |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 [invocation invoke]; | 128 [invocation invoke]; |
| 113 BOOL returnValue; | 129 BOOL returnValue; |
| 114 [invocation getReturnValue:&returnValue]; | 130 [invocation getReturnValue:&returnValue]; |
| 115 return returnValue; | 131 return returnValue; |
| 116 } | 132 } |
| 117 | 133 |
| 118 @end | 134 @end |
| 119 | 135 |
| 120 #pragma mark - | 136 #pragma mark - |
| 121 | 137 |
| 122 #if NS_BLOCKS_AVAILABLE | |
| 123 | |
| 124 @implementation OCMBlockConstraint | 138 @implementation OCMBlockConstraint |
| 125 | 139 |
| 126 - (id)initWithConstraintBlock:(BOOL (^)(id))aBlock; | 140 - (instancetype)initWithConstraintBlock:(BOOL (^)(id))aBlock { |
| 127 { | 141 if ((self = [super init])) { |
| 128 » self = [super init]; | 142 block = [aBlock copy]; |
| 129 » block = aBlock; | 143 } |
| 130 » return self; | 144 |
| 145 return self; |
| 146 } |
| 147 |
| 148 - (void)dealloc { |
| 149 [block release]; |
| 150 [super dealloc]; |
| 131 } | 151 } |
| 132 | 152 |
| 133 - (BOOL)evaluate:(id)value | 153 - (BOOL)evaluate:(id)value |
| 134 { | 154 { |
| 135 » return block(value); | 155 return block ? block(value) : NO; |
| 136 } | 156 } |
| 137 | 157 |
| 138 @end | 158 @end |
| 139 | |
| 140 #endif | |
| OLD | NEW |