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 { |
13 return [[[self alloc] init] autorelease]; | 24 return [[[self alloc] init] autorelease]; |
14 } | 25 } |
15 | 26 |
16 - (BOOL)evaluate:(id)value | 27 - (BOOL)evaluate:(id)value |
17 { | 28 { |
18 return NO; | 29 return NO; |
19 } | 30 } |
20 | 31 |
21 - (id)copyWithZone:(NSZone *)zone | 32 - (id)copyWithZone:(struct _NSZone *)zone |
22 { | 33 { |
23 » return [self retain]; | 34 return [self retain]; |
24 } | 35 } |
25 | 36 |
26 + (id)constraintWithSelector:(SEL)aSelector onObject:(id)anObject | 37 + (instancetype)constraintWithSelector:(SEL)aSelector onObject:(id)anObject |
27 { | 38 { |
28 OCMInvocationConstraint *constraint = [OCMInvocationConstraint constrain
t]; | 39 OCMInvocationConstraint *constraint = [OCMInvocationConstraint constrain
t]; |
29 NSMethodSignature *signature = [anObject methodSignatureForSelector:aSel
ector]; | 40 NSMethodSignature *signature = [anObject methodSignatureForSelector:aSel
ector]; |
30 if(signature == nil) | 41 if(signature == nil) |
31 [NSException raise:NSInvalidArgumentException format:@"Unkown se
lector %@ used in constraint.", NSStringFromSelector(aSelector)]; | 42 [NSException raise:NSInvalidArgumentException format:@"Unkown se
lector %@ used in constraint.", NSStringFromSelector(aSelector)]; |
32 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; | 43 NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:s
ignature]; |
33 [invocation setTarget:anObject]; | 44 [invocation setTarget:anObject]; |
34 [invocation setSelector:aSelector]; | 45 [invocation setSelector:aSelector]; |
35 constraint->invocation = invocation; | 46 constraint->invocation = invocation; |
36 return constraint; | 47 return constraint; |
37 } | 48 } |
38 | 49 |
39 + (id)constraintWithSelector:(SEL)aSelector onObject:(id)anObject withValue:(id)
aValue | 50 + (instancetype)constraintWithSelector:(SEL)aSelector onObject:(id)anObject with
Value:(id)aValue |
40 { | 51 { |
41 OCMInvocationConstraint *constraint = [self constraintWithSelector:aSele
ctor onObject:anObject]; | 52 OCMInvocationConstraint *constraint = [self constraintWithSelector:aSele
ctor onObject:anObject]; |
42 if([[constraint->invocation methodSignature] numberOfArguments] < 4) | 53 if([[constraint->invocation methodSignature] numberOfArguments] < 4) |
43 [NSException raise:NSInvalidArgumentException format:@"Constrain
t with value requires selector with two arguments."]; | 54 [NSException raise:NSInvalidArgumentException format:@"Constrain
t with value requires selector with two arguments."]; |
44 [constraint->invocation setArgument:&aValue atIndex:3]; | 55 [constraint->invocation setArgument:&aValue atIndex:3]; |
45 return constraint; | 56 return constraint; |
46 } | 57 } |
47 | 58 |
48 | 59 |
49 @end | 60 @end |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 [invocation invoke]; | 123 [invocation invoke]; |
113 BOOL returnValue; | 124 BOOL returnValue; |
114 [invocation getReturnValue:&returnValue]; | 125 [invocation getReturnValue:&returnValue]; |
115 return returnValue; | 126 return returnValue; |
116 } | 127 } |
117 | 128 |
118 @end | 129 @end |
119 | 130 |
120 #pragma mark - | 131 #pragma mark - |
121 | 132 |
122 #if NS_BLOCKS_AVAILABLE | |
123 | |
124 @implementation OCMBlockConstraint | 133 @implementation OCMBlockConstraint |
125 | 134 |
126 - (id)initWithConstraintBlock:(BOOL (^)(id))aBlock; | 135 - (instancetype)initWithConstraintBlock:(BOOL (^)(id))aBlock |
127 { | 136 { |
128 » self = [super init]; | 137 if ((self = [super init])) |
129 » block = aBlock; | 138 { |
| 139 block = [aBlock copy]; |
| 140 } |
| 141 » |
130 return self; | 142 return self; |
131 } | 143 } |
132 | 144 |
| 145 - (void)dealloc { |
| 146 [block release]; |
| 147 [super dealloc]; |
| 148 } |
| 149 |
133 - (BOOL)evaluate:(id)value | 150 - (BOOL)evaluate:(id)value |
134 { | 151 { |
135 » return block(value); | 152 return block ? block(value) : NO; |
136 } | 153 } |
137 | 154 |
| 155 |
138 @end | 156 @end |
139 | |
140 #endif | |
OLD | NEW |