-
Notifications
You must be signed in to change notification settings - Fork 609
Expand file tree
/
Copy pathOCClassMockObject.m
More file actions
317 lines (256 loc) · 9.37 KB
/
OCClassMockObject.m
File metadata and controls
317 lines (256 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* Copyright (c) 2005-2016 Erik Doernenburg and contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use these files except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
#import <objc/runtime.h>
#import "OCClassMockObject.h"
#import "NSObject+OCMAdditions.h"
#import "OCMFunctionsPrivate.h"
#import "OCMInvocationStub.h"
#import "NSMethodSignature+OCMAdditions.h"
#import "OCProtocolsProxy.h"
@implementation OCClassMockObject
{
OCProtocolsProxy *protocolsProxy;
}
#pragma mark Initialisers, description, accessors, etc.
- (id)initWithClass:(Class)aClass protocols:(NSArray *)protocols
{
NSParameterAssert(aClass != nil);
[super init];
mockedClass = aClass;
protocolsProxy = [[OCProtocolsProxy alloc] initWithProtocols:protocols];
[self prepareClassForClassMethodMocking];
return self;
}
- (void)dealloc
{
[self stopMocking];
[protocolsProxy release];
[super dealloc];
}
- (NSString *)description
{
NSArray *protocolNames = [protocolsProxy protocolNames];
if (protocolNames) {
return [NSString stringWithFormat:@"OCMockObject(%@ <%@>)",
NSStringFromClass(mockedClass),
[protocolNames componentsJoinedByString:@", "]];
}
return [NSString stringWithFormat:@"OCMockObject(%@)", NSStringFromClass(mockedClass)];
}
- (Class)mockedClass
{
return mockedClass;
}
#pragma mark Extending/overriding superclass behaviour
- (void)stopMocking
{
if(originalMetaClass != nil)
{
/* The mocked class has the meta class of a dynamically created subclass as its meta class,
but we need a reference to the subclass to dispose it. Asking the meta class for its
class name returns the actual class name, which we can then use to look up the class...
*/
const char *createdSubclassName = object_getClassName(mockedClass);
Class createdSubclass = objc_lookUpClass(createdSubclassName);
[self restoreMetaClass];
objc_disposeClassPair(createdSubclass);
}
[super stopMocking];
}
- (void)restoreMetaClass
{
OCMSetAssociatedMockForClass(nil, mockedClass);
object_setClass(mockedClass, originalMetaClass);
originalMetaClass = nil;
}
- (void)addStub:(OCMInvocationStub *)aStub
{
[super addStub:aStub];
if([aStub recordedAsClassMethod])
[self setupForwarderForClassMethodSelector:[[aStub recordedInvocation] selector]];
}
#pragma mark Class method mocking
- (void)prepareClassForClassMethodMocking
{
/* the runtime and OCMock depend on string and array; we don't intercept methods on them to avoid endless loops */
if([[mockedClass class] isSubclassOfClass:[NSString class]] || [[mockedClass class] isSubclassOfClass:[NSArray class]])
return;
/* if there is another mock for this exact class, stop it */
id otherMock = OCMGetAssociatedMockForClass(mockedClass, NO);
if(otherMock != nil)
[otherMock restoreMetaClass];
OCMSetAssociatedMockForClass(self, mockedClass);
/* dynamically create a subclass and use its meta class as the meta class for the mocked class */
Class subclass = OCMCreateSubclass(mockedClass, mockedClass);
originalMetaClass = object_getClass(mockedClass);
id newMetaClass = object_getClass(subclass);
/* create a dummy initialize method */
Method myDummyInitializeMethod = class_getInstanceMethod([self mockObjectClass], @selector(initializeForClassObject));
const char *initializeTypes = method_getTypeEncoding(myDummyInitializeMethod);
IMP myDummyInitializeIMP = method_getImplementation(myDummyInitializeMethod);
class_addMethod(newMetaClass, @selector(initialize), myDummyInitializeIMP, initializeTypes);
object_setClass(mockedClass, newMetaClass); // only after dummy initialize is installed (iOS9)
/* point forwardInvocation: of the object to the implementation in the mock */
Method myForwardMethod = class_getInstanceMethod([self mockObjectClass], @selector(forwardInvocationForClassObject:));
IMP myForwardIMP = method_getImplementation(myForwardMethod);
class_addMethod(newMetaClass, @selector(forwardInvocation:), myForwardIMP, method_getTypeEncoding(myForwardMethod));
/* adding forwarder for most class methods (instance methods on meta class) to allow for verify after run */
NSArray *methodBlackList = @[@"class", @"forwardingTargetForSelector:", @"methodSignatureForSelector:", @"forwardInvocation:", @"isBlock",
@"instanceMethodForwarderForSelector:", @"instanceMethodSignatureForSelector:"];
[NSObject enumerateMethodsInClass:originalMetaClass usingBlock:^(Class cls, SEL sel) {
if((cls == object_getClass([NSObject class])) || (cls == [NSObject class]) || (cls == object_getClass(cls)))
return;
NSString *className = NSStringFromClass(cls);
NSString *selName = NSStringFromSelector(sel);
if(([className hasPrefix:@"NS"] || [className hasPrefix:@"UI"]) &&
([selName hasPrefix:@"_"] || [selName hasSuffix:@"_"]))
return;
if([methodBlackList containsObject:selName])
return;
@try
{
[self setupForwarderForClassMethodSelector:sel];
}
@catch(NSException *e)
{
// ignore for now
}
}];
}
- (void)setupForwarderForClassMethodSelector:(SEL)selector
{
SEL aliasSelector = OCMAliasForOriginalSelector(selector);
if(class_getClassMethod(mockedClass, aliasSelector) != NULL)
return;
Method originalMethod = class_getClassMethod(mockedClass, selector);
IMP originalIMP = method_getImplementation(originalMethod);
const char *types = method_getTypeEncoding(originalMethod);
Class metaClass = object_getClass(mockedClass);
IMP forwarderIMP = [originalMetaClass instanceMethodForwarderForSelector:selector];
class_replaceMethod(metaClass, selector, forwarderIMP, types);
class_addMethod(metaClass, aliasSelector, originalIMP, types);
}
- (void)forwardInvocationForClassObject:(NSInvocation *)anInvocation
{
// in here "self" is a reference to the real class, not the mock
OCClassMockObject *mock = OCMGetAssociatedMockForClass((Class) self, YES);
if(mock == nil)
{
[NSException raise:NSInternalInconsistencyException format:@"No mock for class %@", NSStringFromClass((Class)self)];
}
if([mock handleInvocation:anInvocation] == NO)
{
[anInvocation setSelector:OCMAliasForOriginalSelector([anInvocation selector])];
[anInvocation invoke];
}
}
- (void)initializeForClassObject
{
// we really just want to have an implementation so that the superclass's is not called
}
#pragma mark Proxy API
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
NSMethodSignature *signature = [mockedClass instanceMethodSignatureForSelector:aSelector];
if(signature == nil)
{
signature = [NSMethodSignature signatureForDynamicPropertyAccessedWithSelector:aSelector inClass:mockedClass];
}
if(signature == nil)
{
signature = [protocolsProxy methodSignatureForSelector:aSelector];
}
return signature;
}
- (Class)mockObjectClass
{
return [super class];
}
- (Class)class
{
return mockedClass;
}
- (BOOL)respondsToSelector:(SEL)selector
{
return [mockedClass instancesRespondToSelector:selector] || [protocolsProxy respondsToSelector:selector];
}
- (BOOL)isKindOfClass:(Class)aClass
{
return [mockedClass isSubclassOfClass:aClass];
}
- (BOOL)conformsToProtocol:(Protocol *)aProtocol
{
return class_conformsToProtocol(mockedClass, aProtocol) || [protocolsProxy conformsToProtocol:aProtocol];
}
@end
#pragma mark -
/**
taken from:
`class-dump -f isNS /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk/System/Library/Frameworks/CoreFoundation.framework`
@interface NSObject (__NSIsKinds)
- (_Bool)isNSValue__;
- (_Bool)isNSTimeZone__;
- (_Bool)isNSString__;
- (_Bool)isNSSet__;
- (_Bool)isNSOrderedSet__;
- (_Bool)isNSNumber__;
- (_Bool)isNSDictionary__;
- (_Bool)isNSDate__;
- (_Bool)isNSData__;
- (_Bool)isNSArray__;
*/
@implementation OCClassMockObject(NSIsKindsImplementation)
- (BOOL)isNSValue__
{
return [mockedClass isSubclassOfClass:[NSValue class]];
}
- (BOOL)isNSTimeZone__
{
return [mockedClass isSubclassOfClass:[NSTimeZone class]];
}
- (BOOL)isNSSet__
{
return [mockedClass isSubclassOfClass:[NSSet class]];
}
- (BOOL)isNSOrderedSet__
{
return [mockedClass isSubclassOfClass:[NSOrderedSet class]];
}
- (BOOL)isNSNumber__
{
return [mockedClass isSubclassOfClass:[NSNumber class]];
}
- (BOOL)isNSDate__
{
return [mockedClass isSubclassOfClass:[NSDate class]];
}
- (BOOL)isNSString__
{
return [mockedClass isSubclassOfClass:[NSString class]];
}
- (BOOL)isNSDictionary__
{
return [mockedClass isSubclassOfClass:[NSDictionary class]];
}
- (BOOL)isNSData__
{
return [mockedClass isSubclassOfClass:[NSData class]];
}
- (BOOL)isNSArray__
{
return [mockedClass isSubclassOfClass:[NSArray class]];
}
@end