OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2014-2015 Erik Doernenburg and contributors |
| 3 * |
| 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 */ |
| 16 |
| 17 #import <objc/runtime.h> |
| 18 #import <OCMock/OCMArg.h> |
| 19 #import <OCMock/OCMConstraint.h> |
| 20 #import "OCMPassByRefSetter.h" |
| 21 #import "NSInvocation+OCMAdditions.h" |
| 22 #import "OCMInvocationMatcher.h" |
| 23 #import "OCClassMockObject.h" |
| 24 #import "OCMFunctions.h" |
| 25 |
| 26 |
| 27 @interface NSObject(HCMatcherDummy) |
| 28 - (BOOL)matches:(id)item; |
| 29 @end |
| 30 |
| 31 |
| 32 @implementation OCMInvocationMatcher |
| 33 |
| 34 - (void)dealloc |
| 35 { |
| 36 [recordedInvocation release]; |
| 37 [super dealloc]; |
| 38 } |
| 39 |
| 40 - (void)setInvocation:(NSInvocation *)anInvocation |
| 41 { |
| 42 [recordedInvocation release]; |
| 43 // When the method has a char* argument we do not retain the arguments. This
makes it possible |
| 44 // to match char* args literally and with anyPointer. Not retaining the argu
ment means that |
| 45 // in these cases tests that use their own autorelease pools may fail unexpe
ctedly. |
| 46 if(![anInvocation hasCharPointerArgument]) |
| 47 [anInvocation retainArguments]; |
| 48 recordedInvocation = [anInvocation retain]; |
| 49 } |
| 50 |
| 51 - (void)setRecordedAsClassMethod:(BOOL)flag |
| 52 { |
| 53 recordedAsClassMethod = flag; |
| 54 } |
| 55 |
| 56 - (BOOL)recordedAsClassMethod |
| 57 { |
| 58 return recordedAsClassMethod; |
| 59 } |
| 60 |
| 61 - (void)setIgnoreNonObjectArgs:(BOOL)flag |
| 62 { |
| 63 ignoreNonObjectArgs = flag; |
| 64 } |
| 65 |
| 66 - (NSString *)description |
| 67 { |
| 68 return [recordedInvocation invocationDescription]; |
| 69 } |
| 70 |
| 71 - (NSInvocation *)recordedInvocation |
| 72 { |
| 73 return recordedInvocation; |
| 74 } |
| 75 |
| 76 - (BOOL)matchesSelector:(SEL)sel |
| 77 { |
| 78 if(sel == [recordedInvocation selector]) |
| 79 return YES; |
| 80 if(OCMIsAliasSelector(sel) && |
| 81 OCMOriginalSelectorForAlias(sel) == [recordedInvocation selector]) |
| 82 return YES; |
| 83 |
| 84 return NO; |
| 85 } |
| 86 |
| 87 - (BOOL)matchesInvocation:(NSInvocation *)anInvocation |
| 88 { |
| 89 id target = [anInvocation target]; |
| 90 BOOL isClassMethodInvocation = (target != nil) && (target == [target class])
; |
| 91 if(isClassMethodInvocation != recordedAsClassMethod) |
| 92 return NO; |
| 93 |
| 94 if(![self matchesSelector:[anInvocation selector]]) |
| 95 return NO; |
| 96 |
| 97 NSMethodSignature *signature = [recordedInvocation methodSignature]; |
| 98 NSUInteger n = [signature numberOfArguments]; |
| 99 for(NSUInteger i = 2; i < n; i++) |
| 100 { |
| 101 if(ignoreNonObjectArgs && strcmp([signature getArgumentTypeAtIndex:i], @
encode(id))) |
| 102 { |
| 103 continue; |
| 104 } |
| 105 |
| 106 id recordedArg = [recordedInvocation getArgumentAtIndexAsObject:i]; |
| 107 id passedArg = [anInvocation getArgumentAtIndexAsObject:i]; |
| 108 |
| 109 if([recordedArg isProxy]) |
| 110 { |
| 111 if(![recordedArg isEqual:passedArg]) |
| 112 return NO; |
| 113 continue; |
| 114 } |
| 115 |
| 116 if([recordedArg isKindOfClass:[NSValue class]]) |
| 117 recordedArg = [OCMArg resolveSpecialValues:recordedArg]; |
| 118 |
| 119 if([recordedArg isKindOfClass:[OCMConstraint class]]) |
| 120 { |
| 121 if([recordedArg evaluate:passedArg] == NO) |
| 122 return NO; |
| 123 } |
| 124 else if([recordedArg isKindOfClass:[OCMPassByRefSetter class]]) |
| 125 { |
| 126 id valueToSet = [(OCMPassByRefSetter *)recordedArg value]; |
| 127 // side effect but easier to do here than in handleInvocation |
| 128 if(![valueToSet isKindOfClass:[NSValue class]]) |
| 129 *(id *)[passedArg pointerValue] = valueToSet; |
| 130 else |
| 131 [(NSValue *)valueToSet getValue:[passedArg pointerValue]]; |
| 132 } |
| 133 else if([recordedArg conformsToProtocol:objc_getProtocol("HCMatcher")]) |
| 134 { |
| 135 if([recordedArg matches:passedArg] == NO) |
| 136 return NO; |
| 137 } |
| 138 else |
| 139 { |
| 140 if(([recordedArg class] == [NSNumber class]) && |
| 141 ([(NSNumber*)recordedArg compare:(NSNumber*)passedArg] != NS
OrderedSame)) |
| 142 return NO; |
| 143 if(([recordedArg isEqual:passedArg] == NO) && |
| 144 !((recordedArg == nil) && (passedArg == nil))) |
| 145 return NO; |
| 146 } |
| 147 } |
| 148 return YES; |
| 149 } |
| 150 |
| 151 @end |
OLD | NEW |