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 "OCMRecorder.h" |
| 19 #import "OCMockObject.h" |
| 20 #import "OCMInvocationMatcher.h" |
| 21 #import "OCClassMockObject.h" |
| 22 |
| 23 @implementation OCMRecorder |
| 24 |
| 25 - (instancetype)init |
| 26 { |
| 27 // no super, we're inheriting from NSProxy |
| 28 return self; |
| 29 } |
| 30 |
| 31 - (instancetype)initWithMockObject:(OCMockObject *)aMockObject |
| 32 { |
| 33 [self init]; |
| 34 [self setMockObject:aMockObject]; |
| 35 return self; |
| 36 } |
| 37 |
| 38 - (void)setMockObject:(OCMockObject *)aMockObject |
| 39 { |
| 40 mockObject = aMockObject; |
| 41 } |
| 42 |
| 43 - (void)dealloc |
| 44 { |
| 45 [invocationMatcher release]; |
| 46 [super dealloc]; |
| 47 } |
| 48 |
| 49 - (NSString *)description |
| 50 { |
| 51 return [invocationMatcher description]; |
| 52 } |
| 53 |
| 54 - (OCMInvocationMatcher *)invocationMatcher |
| 55 { |
| 56 return invocationMatcher; |
| 57 } |
| 58 |
| 59 |
| 60 #pragma mark Modifying the matcher |
| 61 |
| 62 - (id)classMethod |
| 63 { |
| 64 // should we handle the case where this is called with a mock that isn't a c
lass mock? |
| 65 [invocationMatcher setRecordedAsClassMethod:YES]; |
| 66 return self; |
| 67 } |
| 68 |
| 69 - (id)ignoringNonObjectArgs |
| 70 { |
| 71 [invocationMatcher setIgnoreNonObjectArgs:YES]; |
| 72 return self; |
| 73 } |
| 74 |
| 75 |
| 76 #pragma mark Recording the actual invocation |
| 77 |
| 78 - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector |
| 79 { |
| 80 if([invocationMatcher recordedAsClassMethod]) |
| 81 return [[(OCClassMockObject *)mockObject mockedClass] methodSignatureFor
Selector:aSelector]; |
| 82 |
| 83 NSMethodSignature *signature = [mockObject methodSignatureForSelector:aSelec
tor]; |
| 84 if(signature == nil) |
| 85 { |
| 86 // if we're a working with a class mock and there is a class method, aut
o-switch |
| 87 if(([object_getClass(mockObject) isSubclassOfClass:[OCClassMockObject cl
ass]]) && |
| 88 ([[(OCClassMockObject *)mockObject mockedClass] respondsToSelector:aS
elector])) |
| 89 { |
| 90 [self classMethod]; |
| 91 signature = [self methodSignatureForSelector:aSelector]; |
| 92 } |
| 93 } |
| 94 return signature; |
| 95 } |
| 96 |
| 97 - (void)forwardInvocation:(NSInvocation *)anInvocation |
| 98 { |
| 99 [anInvocation setTarget:nil]; |
| 100 [invocationMatcher setInvocation:anInvocation]; |
| 101 } |
| 102 |
| 103 - (void)doesNotRecognizeSelector:(SEL)aSelector |
| 104 { |
| 105 [NSException raise:NSInvalidArgumentException format:@"%@: cannot stub/expec
t/verify method '%@' because no such method exists in the mocked class.", mockOb
ject, NSStringFromSelector(aSelector)]; |
| 106 } |
| 107 |
| 108 |
| 109 @end |
OLD | NEW |