This repository was archived by the owner on Dec 2, 2019. It is now read-only.
forked from specta/specta
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSPTExampleGroup.m
More file actions
370 lines (321 loc) · 10.7 KB
/
SPTExampleGroup.m
File metadata and controls
370 lines (321 loc) · 10.7 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#import "SPTExampleGroup.h"
#import "SPTExample.h"
#import "SPTSenTestCase.h"
#import "SPTSpec.h"
#import "SpectaUtility.h"
#import <libkern/OSAtomic.h>
#import <objc/runtime.h>
static NSTimeInterval asyncSpecTimeout = 10.0;
#ifdef __clang__
static const char *asyncBlockSignature = NULL;
#else
static void (^emptyBlock)(void) = nil;
#endif
static void runExampleBlock(id block, NSString *name) {
if(!SPT_isBlock(block)) {
return;
}
#ifdef __clang__
const char *blockSignature = SPT_getBlockSignature(block);
BOOL isAsyncBlock = strcmp(blockSignature, asyncBlockSignature) == 0;
if(isAsyncBlock) {
__block uint32_t complete = 0;
((SPTAsyncBlock)block)(^{
OSAtomicOr32Barrier(1, &complete);
});
NSTimeInterval timeout = asyncSpecTimeout;
NSDate *timeoutDate = [NSDate dateWithTimeIntervalSinceNow:timeout];
while (!complete && [timeoutDate timeIntervalSinceNow] > 0) {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
}
if (!complete) {
NSString *message = [NSString stringWithFormat:@"\"%@\" failed to invoke done() callback before timeout (%f seconds)", name, timeout];
SPTSenTestCase *currentTestCase = [[[NSThread currentThread] threadDictionary] objectForKey:@"SPT_currentTestCase"];
SPTSpec *spec = [[currentTestCase class] SPT_spec];
NSException *exception = [NSException failureInFile:spec.fileName atLine:(int)spec.lineNumber withDescription:message];
[currentTestCase failWithException: exception];
}
} else {
((SPTVoidBlock)block)();
}
#else
((SPTVoidBlock)block)(emptyBlock);
#endif
}
@interface SPTExampleGroup ()
- (void)incrementExampleCount;
- (void)resetRanExampleCountIfNeeded;
- (void)incrementRanExampleCount;
- (void)runBeforeHooks:(NSString *)compiledName;
- (void)runAfterHooks:(NSString *)compiledName;
@end
@implementation SPTExampleGroup
@synthesize
name=_name
, root=_root
, parent=_parent
, children=_children
, beforeAllArray=_beforeAllArray
, afterAllArray=_afterAllArray
, beforeEachArray=_beforeEachArray
, afterEachArray=_afterEachArray
, sharedExamples=_sharedExamples
, exampleCount=_exampleCount
, ranExampleCount=_ranExampleCount
, focused=_focused
;
- (void)dealloc {
self.name = nil;
self.root = nil;
self.parent = nil;
self.children = nil;
self.beforeAllArray = nil;
self.afterAllArray = nil;
self.beforeEachArray = nil;
self.afterEachArray = nil;
self.sharedExamples = nil;
[super dealloc];
}
+ (void)initialize {
#ifdef __clang__
if (asyncBlockSignature == NULL) {
asyncBlockSignature = SPT_getBlockSignature(^(void (^done)(void)) {});
}
#else
emptyBlock = ^{};
#endif
}
- (id)init {
self = [super init];
if(self) {
self.name = nil;
self.root = nil;
self.parent = nil;
self.children = [NSMutableArray array];
self.beforeAllArray = [NSMutableArray array];
self.afterAllArray = [NSMutableArray array];
self.beforeEachArray = [NSMutableArray array];
self.afterEachArray = [NSMutableArray array];
self.sharedExamples = [NSMutableDictionary dictionary];
self.exampleCount = 0;
self.ranExampleCount = 0;
}
return self;
}
+ (void)setAsyncSpecTimeout:(NSTimeInterval)timeout {
asyncSpecTimeout = timeout;
}
- (id)initWithName:(NSString *)name parent:(SPTExampleGroup *)parent root:(SPTExampleGroup *)root {
self = [self init];
if(self) {
self.name = name;
self.parent = parent;
self.root = root;
}
return self;
}
- (SPTExampleGroup *)addExampleGroupWithName:(NSString *)name {
return [self addExampleGroupWithName:name
focused:NO];
}
- (SPTExampleGroup *)addExampleGroupWithName:(NSString *)name focused:(BOOL)focused {
SPTExampleGroup *group = [[SPTExampleGroup alloc] initWithName:name parent:self root:self.root];
group.focused = focused;
[self.children addObject:group];
return [group autorelease];
}
- (SPTExample *)addExampleWithName:(NSString *)name block:(id)block {
return [self addExampleWithName:name
block:block
focused:NO];
}
- (SPTExample *)addExampleWithName:(NSString *)name block:(id)block focused:(BOOL)focused {
SPTExample *example;
@synchronized(self) {
example = [[SPTExample alloc] initWithName:name block:block];
SPTSpec *spec = [[[NSThread currentThread] threadDictionary] objectForKey:@"SPT_currentSpec"];
example.testCase = spec.testCase;
example.focused = focused;
[self.children addObject:example];
[self incrementExampleCount];
}
return [example autorelease];
}
- (void)incrementExampleCount {
SPTExampleGroup *group = self;
while(group != nil) {
group.exampleCount ++;
group = group.parent;
}
}
- (void)resetRanExampleCountIfNeeded {
SPTExampleGroup *group = self;
while(group != nil) {
if(group.ranExampleCount >= group.exampleCount) {
group.ranExampleCount = 0;
}
group = group.parent;
}
}
- (void)incrementRanExampleCount {
SPTExampleGroup *group = self;
while(group != nil) {
group.ranExampleCount ++;
group = group.parent;
}
}
- (void)addBeforeAllBlock:(SPTVoidBlock)block {
if(!block) return;
[self.beforeAllArray addObject:[[block copy] autorelease]];
}
- (void)addAfterAllBlock:(SPTVoidBlock)block {
if(!block) return;
[self.afterAllArray addObject:[[block copy] autorelease]];
}
- (void)addBeforeEachBlock:(SPTVoidBlock)block {
if(!block) return;
[self.beforeEachArray addObject:[[block copy] autorelease]];
}
- (void)addAfterEachBlock:(SPTVoidBlock)block {
if(!block) return;
[self.afterEachArray addObject:[[block copy] autorelease]];
}
static NSArray * ClassesWithClassMethod(SEL classMethodSelector) {
NSMutableArray * classesWithClassMethod = [[NSMutableArray alloc] init];
int numberOfClasses = objc_getClassList(NULL, 0);
if(numberOfClasses > 0) {
Class * classes = malloc(sizeof(Class) * numberOfClasses);
numberOfClasses = objc_getClassList(classes, numberOfClasses);
for(int classIndex = 0; classIndex < numberOfClasses; classIndex++) {
Class aClass = classes[classIndex];
if (strcmp("UIAccessibilitySafeCategory__NSObject", class_getName(aClass))) {
Method globalMethod = class_getClassMethod(aClass, classMethodSelector);
if(globalMethod) {
[classesWithClassMethod addObject:aClass];
}
}
}
free(classes);
}
return classesWithClassMethod;
}
static void InvokeClassMethod(NSArray * classes, SEL selector) {
for(Class aClass in classes) {
Method globalMethod = class_getClassMethod(aClass, selector);
unsigned numberOfArguments = method_getNumberOfArguments(globalMethod);
if(numberOfArguments == 2) {
IMP globalMethodIMP = method_getImplementation(globalMethod);
globalMethodIMP(aClass, selector);
}
}
}
- (void)runGlobalBeforeEachHooks:(NSString *)compiledName {
static NSArray * globalBeforeEachClasses;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
globalBeforeEachClasses = ClassesWithClassMethod(@selector(beforeEach));
});
InvokeClassMethod(globalBeforeEachClasses, @selector(beforeEach));
}
- (void)runGlobalAfterEachHooks:(NSString *)compiledName {
static NSArray * globalAfterEachClasses;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
globalAfterEachClasses = ClassesWithClassMethod(@selector(afterEach));
});
InvokeClassMethod(globalAfterEachClasses, @selector(afterEach));
}
- (void)runBeforeHooks:(NSString *)compiledName {
NSMutableArray *groups = [NSMutableArray array];
SPTExampleGroup *group = self;
while(group != nil) {
[groups insertObject:group atIndex:0];
group = group.parent;
}
// run beforeAll hooks
for(group in groups) {
if(group.ranExampleCount == 0) {
for(id beforeAllBlock in group.beforeAllArray) {
runExampleBlock(beforeAllBlock, [NSString stringWithFormat:@"%@ - before all block", compiledName]);
}
}
}
// run beforeEach hooks
[self runGlobalBeforeEachHooks:compiledName];
for(group in groups) {
for(id beforeEachBlock in group.beforeEachArray) {
runExampleBlock(beforeEachBlock, [NSString stringWithFormat:@"%@ - before each block", compiledName]);
}
}
}
- (void)runAfterHooks:(NSString *)compiledName {
NSMutableArray *groups = [NSMutableArray array];
SPTExampleGroup *group = self;
while(group != nil) {
[groups addObject:group];
group = group.parent;
}
// run afterEach hooks
for(group in groups) {
for(id afterEachBlock in group.afterEachArray) {
runExampleBlock(afterEachBlock, [NSString stringWithFormat:@"%@ - after each block", compiledName]);
}
}
[self runGlobalAfterEachHooks:compiledName];
// run afterAll hooks
for(group in groups) {
if(group.ranExampleCount == group.exampleCount) {
for(id afterAllBlock in group.afterAllArray) {
runExampleBlock(afterAllBlock, [NSString stringWithFormat:@"%@ - after all block", compiledName]);
}
}
}
}
- (BOOL)isFocusedOrHasFocusedAncestor
{
SPTExampleGroup * ancestor = self;
while (ancestor != nil) {
if (ancestor.focused) {
return YES;
} else {
ancestor = ancestor.parent;
}
}
return NO;
}
- (NSArray *)compileExamplesWithNameStack:(NSArray *)nameStack {
BOOL groupIsFocusedOrHasFocusedAncestor = [self isFocusedOrHasFocusedAncestor];
NSArray *compiled = [NSArray array];
for(id child in self.children) {
if([child isKindOfClass:[SPTExampleGroup class]]) {
SPTExampleGroup *group = child;
NSArray *newNameStack = [nameStack arrayByAddingObject:group.name];
compiled = [compiled arrayByAddingObjectsFromArray:[group compileExamplesWithNameStack:newNameStack]];
} else if([child isKindOfClass:[SPTExample class]]) {
SPTExample *example = child;
NSArray *newNameStack = [nameStack arrayByAddingObject:example.name];
NSString *compiledName = [newNameStack componentsJoinedByString:@" "];
SPTSenTestCase *testCase = example.testCase;
SPTVoidBlock compiledBlock = example.pending ? nil : ^{
@synchronized(self.root) {
[self resetRanExampleCountIfNeeded];
[testCase SPT_setUp];
[self runBeforeHooks:compiledName];
}
runExampleBlock(example.block, compiledName);
@synchronized(self.root) {
[self incrementRanExampleCount];
[self runAfterHooks:compiledName];
[testCase SPT_tearDown];
}
};
SPTExample *compiledExample = [[SPTExample alloc] initWithName:compiledName block:compiledBlock];
compiledExample.pending = example.pending;
compiledExample.focused = (groupIsFocusedOrHasFocusedAncestor || example.focused);
compiled = [compiled arrayByAddingObject:compiledExample];
[compiledExample release];
}
}
return compiled;
}
@end