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