forked from Quick/Quick
-
Notifications
You must be signed in to change notification settings - Fork 9
/
QuickSpec.m
86 lines (67 loc) · 2.53 KB
/
QuickSpec.m
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
//
// QCKSpec.m
// Quick
//
// Created by Brian Ivan Gesiak on 6/6/14.
// Copyright (c) 2014 Brian Ivan Gesiak. All rights reserved.
//
#import "QuickSpec.h"
#import "NSString+QCKSelectorName.h"
#import <Quick/Quick-Swift.h>
#import <objc/runtime.h>
const void * const QCKExampleKey = &QCKExampleKey;
@interface QuickSpec ()
@property (nonatomic, strong) Example *example;
@end
@implementation QuickSpec
#pragma mark - XCTestCase Overrides
+ (void)initialize {
[World setCurrentExampleGroup:[World rootExampleGroupForSpecClass:[self class]]];
[self exampleGroups];
}
+ (NSArray *)testInvocations {
NSArray *examples = [World rootExampleGroupForSpecClass:[self class]].examples;
NSMutableArray *invocations = [NSMutableArray arrayWithCapacity:[examples count]];
for (Example *example in examples) {
SEL selector = [self addInstanceMethodForExample:example];
NSInvocation *invocation = [self invocationForInstanceMethodWithSelector:selector
example:example];
[invocations addObject:invocation];
}
return invocations;
}
- (void)setInvocation:(NSInvocation *)invocation {
self.example = objc_getAssociatedObject(invocation, QCKExampleKey);
[super setInvocation:invocation];
}
- (NSString *)name {
return [NSString stringWithFormat:@"%@: %@",
NSStringFromClass([self class]), self.example.name];
}
#pragma mark - Public Interface
+ (void)exampleGroups { }
+ (BOOL)isConcreteSpec {
return NO;
}
#pragma mark - Internal Methods
+ (SEL)addInstanceMethodForExample:(Example *)example {
IMP implementation = imp_implementationWithBlock(^(id self){
[example run];
});
const char *types = [[NSString stringWithFormat:@"%s%s%s", @encode(id), @encode(id), @encode(SEL)] UTF8String];
SEL selector = NSSelectorFromString(example.name.selectorName);
class_addMethod(self, selector, implementation, types);
return selector;
}
+ (NSInvocation *)invocationForInstanceMethodWithSelector:(SEL)selector
example:(Example *)example {
NSMethodSignature *signature = [self instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.selector = selector;
objc_setAssociatedObject(invocation,
QCKExampleKey,
example,
OBJC_ASSOCIATION_RETAIN_NONATOMIC);
return invocation;
}
@end