OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2004-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 "OCMStubRecorder.h" |
| 18 #import "OCClassMockObject.h" |
| 19 #import "OCMReturnValueProvider.h" |
| 20 #import "OCMBoxedReturnValueProvider.h" |
| 21 #import "OCMExceptionReturnValueProvider.h" |
| 22 #import "OCMIndirectReturnValueProvider.h" |
| 23 #import "OCMNotificationPoster.h" |
| 24 #import "OCMBlockCaller.h" |
| 25 #import "OCMRealObjectForwarder.h" |
| 26 #import "OCMFunctions.h" |
| 27 #import "OCMInvocationStub.h" |
| 28 |
| 29 |
| 30 @implementation OCMStubRecorder |
| 31 |
| 32 #pragma mark Initialisers, description, accessors, etc. |
| 33 |
| 34 - (id)init |
| 35 { |
| 36 self = [super init]; |
| 37 invocationMatcher = [[OCMInvocationStub alloc] init]; |
| 38 return self; |
| 39 } |
| 40 |
| 41 - (OCMInvocationStub *)stub |
| 42 { |
| 43 return (OCMInvocationStub *)invocationMatcher; |
| 44 } |
| 45 |
| 46 |
| 47 #pragma mark Recording invocation actions |
| 48 |
| 49 - (id)andReturn:(id)anObject |
| 50 { |
| 51 [[self stub] addInvocationAction:[[[OCMReturnValueProvider alloc] initWi
thValue:anObject] autorelease]]; |
| 52 return self; |
| 53 } |
| 54 |
| 55 - (id)andReturnValue:(NSValue *)aValue |
| 56 { |
| 57 [[self stub] addInvocationAction:[[[OCMBoxedReturnValueProvider alloc] initW
ithValue:aValue] autorelease]]; |
| 58 return self; |
| 59 } |
| 60 |
| 61 - (id)andThrow:(NSException *)anException |
| 62 { |
| 63 [[self stub] addInvocationAction:[[[OCMExceptionReturnValueProvider alloc] i
nitWithValue:anException] autorelease]]; |
| 64 return self; |
| 65 } |
| 66 |
| 67 - (id)andPost:(NSNotification *)aNotification |
| 68 { |
| 69 [[self stub] addInvocationAction:[[[OCMNotificationPoster alloc] initWithNot
ification:aNotification] autorelease]]; |
| 70 return self; |
| 71 } |
| 72 |
| 73 - (id)andCall:(SEL)selector onObject:(id)anObject |
| 74 { |
| 75 [[self stub] addInvocationAction:[[[OCMIndirectReturnValueProvider alloc] in
itWithProvider:anObject andSelector:selector] autorelease]]; |
| 76 return self; |
| 77 } |
| 78 |
| 79 - (id)andDo:(void (^)(NSInvocation *))aBlock |
| 80 { |
| 81 [[self stub] addInvocationAction:[[[OCMBlockCaller alloc] initWithCallBlock:
aBlock] autorelease]]; |
| 82 return self; |
| 83 } |
| 84 |
| 85 - (id)andForwardToRealObject |
| 86 { |
| 87 [[self stub] addInvocationAction:[[[OCMRealObjectForwarder alloc] init] auto
release]]; |
| 88 return self; |
| 89 } |
| 90 |
| 91 |
| 92 #pragma mark Finishing recording |
| 93 |
| 94 - (void)forwardInvocation:(NSInvocation *)anInvocation |
| 95 { |
| 96 [super forwardInvocation:anInvocation]; |
| 97 [mockObject addStub:[self stub]]; |
| 98 } |
| 99 |
| 100 |
| 101 @end |
| 102 |
| 103 |
| 104 @implementation OCMStubRecorder (Properties) |
| 105 |
| 106 @dynamic _andReturn; |
| 107 |
| 108 - (OCMStubRecorder *(^)(NSValue *))_andReturn |
| 109 { |
| 110 id (^theBlock)(id) = ^ (NSValue *aValue) |
| 111 { |
| 112 if(OCMIsObjectType([aValue objCType])) |
| 113 { |
| 114 NSValue *objValue = nil; |
| 115 [aValue getValue:&objValue]; |
| 116 return [self andReturn:objValue]; |
| 117 } |
| 118 else |
| 119 { |
| 120 return [self andReturnValue:aValue]; |
| 121 } |
| 122 }; |
| 123 return [[theBlock copy] autorelease]; |
| 124 } |
| 125 |
| 126 |
| 127 @dynamic _andThrow; |
| 128 |
| 129 - (OCMStubRecorder *(^)(NSException *))_andThrow |
| 130 { |
| 131 id (^theBlock)(id) = ^ (NSException * anException) |
| 132 { |
| 133 return [self andThrow:anException]; |
| 134 }; |
| 135 return [[theBlock copy] autorelease]; |
| 136 } |
| 137 |
| 138 |
| 139 @dynamic _andPost; |
| 140 |
| 141 - (OCMStubRecorder *(^)(NSNotification *))_andPost |
| 142 { |
| 143 id (^theBlock)(id) = ^ (NSNotification * aNotification) |
| 144 { |
| 145 return [self andPost:aNotification]; |
| 146 }; |
| 147 return [[theBlock copy] autorelease]; |
| 148 } |
| 149 |
| 150 |
| 151 @dynamic _andCall; |
| 152 |
| 153 - (OCMStubRecorder *(^)(id, SEL))_andCall |
| 154 { |
| 155 id (^theBlock)(id, SEL) = ^ (id anObject, SEL aSelector) |
| 156 { |
| 157 return [self andCall:aSelector onObject:anObject]; |
| 158 }; |
| 159 return [[theBlock copy] autorelease]; |
| 160 } |
| 161 |
| 162 |
| 163 @dynamic _andDo; |
| 164 |
| 165 - (OCMStubRecorder *(^)(void (^)(NSInvocation *)))_andDo |
| 166 { |
| 167 id (^theBlock)(void (^)(NSInvocation *)) = ^ (void (^ blockToCall)(NSInvocat
ion *)) |
| 168 { |
| 169 return [self andDo:blockToCall]; |
| 170 }; |
| 171 return [[theBlock copy] autorelease]; |
| 172 } |
| 173 |
| 174 |
| 175 @dynamic _andForwardToRealObject; |
| 176 |
| 177 - (OCMStubRecorder *(^)(void))_andForwardToRealObject |
| 178 { |
| 179 id (^theBlock)(void) = ^ (void) |
| 180 { |
| 181 return [self andForwardToRealObject]; |
| 182 }; |
| 183 return [[theBlock copy] autorelease]; |
| 184 } |
| 185 |
| 186 |
| 187 @end |
OLD | NEW |