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