forked from Quick/Quick
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSpec.m
More file actions
174 lines (134 loc) · 6.03 KB
/
QuickSpec.m
File metadata and controls
174 lines (134 loc) · 6.03 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
#import "QuickSpec.h"
#import "QuickConfiguration.h"
#if __has_include("Quick-Swift.h")
#import "Quick-Swift.h"
#else
#import <Quick/Quick-Swift.h>
#endif
static QuickSpec *currentSpec = nil;
@interface QuickSpec ()
@property (nonatomic, strong) Example *example;
@end
@implementation QuickSpec
#pragma mark - XCTestCase Overrides
/**
QuickSpec hooks into this event to compile the example groups for this spec subclass.
If an exception occurs when compiling the examples, report it to the user. Chances are they
included an expectation outside of a "it", "describe", or "context" block.
*/
+ (XCTestSuite *)defaultTestSuite {
[self buildExamplesIfNeeded];
// Add instance methods for this class' examples.
NSArray *examples = [[World sharedWorld] examplesForSpecClass:[self class]];
NSMutableSet<NSString*> *selectorNames = [NSMutableSet set];
for (Example *example in examples) {
[self addInstanceMethodForExample:example classSelectorNames:selectorNames];
}
return [super defaultTestSuite];
}
/**
Invocations for each test method in the test case. QuickSpec overrides this method to define a
new method for each example defined in +[QuickSpec spec].
@return An array of invocations that execute the newly defined example methods.
*/
+ (NSArray *)testInvocations {
[self buildExamplesIfNeeded];
NSArray *examples = [[World sharedWorld] examplesForSpecClass:[self class]];
NSMutableArray *invocations = [NSMutableArray arrayWithCapacity:[examples count]];
NSMutableSet<NSString*> *selectorNames = [NSMutableSet set];
for (Example *example in examples) {
SEL selector = [self addInstanceMethodForExample:example classSelectorNames:selectorNames];
NSMethodSignature *signature = [self instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.selector = selector;
[invocations addObject:invocation];
}
return invocations;
}
#pragma mark - Public Interface
- (void)spec { }
+ (QuickSpec*) current {
return currentSpec;
}
#pragma mark - Internal Methods
/**
Runs the `spec` method and builds the examples for this class.
It's safe to call this method multiple times. If the examples for the class have been built, invocation
of this method has no effect.
*/
+ (void)buildExamplesIfNeeded {
[QuickConfiguration class];
World *world = [World sharedWorld];
if ([world isRootExampleGroupInitializedForSpecClass:[self class]]) {
// The examples fot this subclass have been already built. Skipping.
return;
}
ExampleGroup *rootExampleGroup = [world rootExampleGroupForSpecClass:[self class]];
[world performWithCurrentExampleGroup:rootExampleGroup closure:^{
QuickSpec *spec = [self new];
@try {
[spec spec];
}
@catch (NSException *exception) {
[NSException raise:NSInternalInconsistencyException
format:@"An exception occurred when building Quick's example groups.\n"
@"Some possible reasons this might happen include:\n\n"
@"- An 'expect(...).to' expectation was evaluated outside of "
@"an 'it', 'context', or 'describe' block\n"
@"- 'sharedExamples' was called twice with the same name\n"
@"- 'itBehavesLike' was called with a name that is not registered as a shared example\n\n"
@"Here's the original exception: '%@', reason: '%@', userInfo: '%@'",
exception.name, exception.reason, exception.userInfo];
}
}];
}
/**
QuickSpec uses this method to dynamically define a new instance method for the
given example. The instance method runs the example, catching any exceptions.
The exceptions are then reported as test failures.
In order to report the correct file and line number, examples must raise exceptions
containing following keys in their userInfo:
- "SenTestFilenameKey": A String representing the file name
- "SenTestLineNumberKey": An Int representing the line number
These keys used to be used by SenTestingKit, and are still used by some testing tools
in the wild. See: https://github.com/Quick/Quick/pull/41
@return The selector of the newly defined instance method.
*/
+ (SEL)addInstanceMethodForExample:(Example *)example classSelectorNames:(NSMutableSet<NSString*> *)selectorNames {
IMP implementation = imp_implementationWithBlock(^(QuickSpec *self){
self.example = example;
currentSpec = self;
[example run];
});
const char *types = [[NSString stringWithFormat:@"%s%s%s", @encode(void), @encode(id), @encode(SEL)] UTF8String];
NSString *originalName = [QCKObjCStringUtils c99ExtendedIdentifierFrom:example.name];
NSString *selectorName = originalName;
NSUInteger i = 2;
while ([selectorNames containsObject:selectorName]) {
selectorName = [NSString stringWithFormat:@"%@_%tu", originalName, i++];
}
[selectorNames addObject:selectorName];
SEL selector = NSSelectorFromString(selectorName);
class_addMethod(self, selector, implementation, types);
return selector;
}
/**
This method is used to record failures, whether they represent example
expectations that were not met, or exceptions raised during test setup
and teardown. By default, the failure will be reported as an
XCTest failure, and the example will be highlighted in Xcode.
*/
- (void)recordFailureWithDescription:(NSString *)description
inFile:(NSString *)filePath
atLine:(NSUInteger)lineNumber
expected:(BOOL)expected {
if (self.example.isSharedExample) {
filePath = self.example.callsite.file;
lineNumber = self.example.callsite.line;
}
[currentSpec.testRun recordFailureWithDescription:description
inFile:filePath
atLine:lineNumber
expected:expected];
}
@end