forked from kiwi-bdd/Kiwi
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathKWExample.m
More file actions
393 lines (306 loc) · 13.6 KB
/
Copy pathKWExample.m
File metadata and controls
393 lines (306 loc) · 13.6 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//
// Licensed under the terms in License.txt
//
// Copyright 2010 Allen Ding. All rights reserved.
//
#import "KWExample.h"
#import "KWExampleSuiteBuilder.h"
#import "KWContextNode.h"
#import "KWMatcherFactory.h"
#import "KWExistVerifier.h"
#import "KWMatchVerifier.h"
#import "KWAsyncVerifier.h"
#import "KWFailure.h"
#import "KWContextNode.h"
#import "KWBeforeEachNode.h"
#import "KWBeforeAllNode.h"
#import "KWLetNode.h"
#import "KWItNode.h"
#import "KWAfterEachNode.h"
#import "KWAfterAllNode.h"
#import "KWPendingNode.h"
#import "KWRegisterMatchersNode.h"
#import "KWWorkarounds.h"
#import "KWIntercept.h"
#import "KWExampleNode.h"
#import "KWExampleSuite.h"
#import "KWCallSite.h"
#import "KWSymbolicator.h"
@interface KWExample ()
@property (nonatomic, readonly) NSMutableArray *verifiers;
@property (nonatomic, readonly) KWMatcherFactory *matcherFactory;
@property (nonatomic, weak) id<KWExampleDelegate> delegate;
@property (nonatomic, assign) BOOL didNotFinish;
@property (nonatomic, strong) id<KWExampleNode> exampleNode;
@property (nonatomic, assign) BOOL passed;
- (void)reportResultForExampleNodeWithLabel:(NSString *)label;
@end
@implementation KWExample
- (id)initWithExampleNode:(id<KWExampleNode>)node {
self = [super init];
if (self) {
_exampleNode = node;
_matcherFactory = [[KWMatcherFactory alloc] init];
_verifiers = [[NSMutableArray alloc] init];
_lastInContexts = [[NSMutableArray alloc] init];
_passed = YES;
}
return self;
}
- (BOOL)isLastInContext:(KWContextNode *)context {
for (KWContextNode *contextWhereItLast in self.lastInContexts) {
if (context == contextWhereItLast) {
return YES;
}
}
return NO;
}
- (NSString *)description {
return [NSString stringWithFormat:@"<KWExample: %@>", self.exampleNode.description];
}
#pragma mark - Adding Verifiers
- (id)addVerifier:(id<KWVerifying>)aVerifier {
if (![self.verifiers containsObject:aVerifier])
[self.verifiers addObject:aVerifier];
return aVerifier;
}
- (id)addExistVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite {
id verifier = [KWExistVerifier existVerifierWithExpectationType:anExpectationType callSite:aCallSite reporter:self];
[self addVerifier:verifier];
return verifier;
}
- (id)addMatchVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite {
if (self.unresolvedVerifier) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"Trying to add another verifier without specifying a matcher for the previous one."
userInfo:nil];
}
id<KWVerifying> verifier = [KWMatchVerifier matchVerifierWithExpectationType:anExpectationType callSite:aCallSite matcherFactory:self.matcherFactory reporter:self];
[self addVerifier:verifier];
self.unresolvedVerifier = verifier;
return verifier;
}
- (id)addAsyncVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite timeout:(NSInteger)timeout shouldWait:(BOOL)shouldWait {
id verifier = [KWAsyncVerifier asyncVerifierWithExpectationType:anExpectationType callSite:aCallSite matcherFactory:self.matcherFactory reporter:self probeTimeout:timeout shouldWait: shouldWait];
[self addVerifier:verifier];
return verifier;
}
#pragma mark - Running examples
- (void)runWithDelegate:(id<KWExampleDelegate>)delegate; {
self.delegate = delegate;
[self.matcherFactory registerMatcherClassesWithNamespacePrefix:@"KW"];
[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setCurrentExample:self];
[self.exampleNode acceptExampleNodeVisitor:self];
}
#pragma mark - Reporting failure
- (NSString *)descriptionForExampleContext {
NSMutableArray *parts = [NSMutableArray array];
for (KWContextNode *context in [[self.exampleNode contextStack] reverseObjectEnumerator]) {
if ([context description] != nil) {
[parts addObject:[[context description] stringByAppendingString:@","]];
}
}
return [parts componentsJoinedByString:@" "];
}
- (KWFailure *)outputReadyFailureWithFailure:(KWFailure *)aFailure {
NSString *annotatedFailureMessage = [NSString stringWithFormat:@"'%@ %@' [FAILED], %@",
[self descriptionForExampleContext], [self.exampleNode description],
aFailure.message];
#if TARGET_IPHONE_SIMULATOR
// \uff1a is the unicode for a fill width colon, as opposed to a regular
// colon character (':'). This escape is performed so that Xcode doesn't
// truncate the error output in the build results window, which is running
// build time specs.
annotatedFailureMessage = [annotatedFailureMessage stringByReplacingOccurrencesOfString:@":" withString:@"\uff1a"];
#endif // #if TARGET_IPHONE_SIMULATOR
return [KWFailure failureWithCallSite:aFailure.callSite message:annotatedFailureMessage];
}
- (void)reportFailure:(KWFailure *)failure {
self.passed = NO;
[self.delegate example:self didFailWithFailure:[self outputReadyFailureWithFailure:failure]];
}
- (void)reportResultForExampleNodeWithLabel:(NSString *)label {
NSLog(@"+ '%@ %@' [%@]", [self descriptionForExampleContext], [self.exampleNode description], label);
}
#pragma mark - Full description with context
/** Pending cases will be marked yellow by XCode as not finished, because their description differs for -[SenTestCaseRun start] and -[SenTestCaseRun stop] methods
*/
- (NSString *)pendingNotFinished {
BOOL reportPending = self.didNotFinish;
self.didNotFinish = YES;
return reportPending ? @"(PENDING)" : @"";
}
- (NSString *)descriptionWithContext {
NSString *descriptionWithContext = [NSString stringWithFormat:@"%@ %@",
[self descriptionForExampleContext],
[self.exampleNode description] ? [self.exampleNode description] : @""];
BOOL isPending = [self.exampleNode isKindOfClass:[KWPendingNode class]];
return isPending ? [descriptionWithContext stringByAppendingString:[self pendingNotFinished]] : descriptionWithContext;
}
#pragma mark - Visiting Nodes
- (void)visitRegisterMatchersNode:(KWRegisterMatchersNode *)aNode {
[self.matcherFactory registerMatcherClassesWithNamespacePrefix:aNode.namespacePrefix];
}
- (void)visitBeforeAllNode:(KWBeforeAllNode *)aNode {
if (aNode.block == nil)
return;
aNode.block();
}
- (void)visitAfterAllNode:(KWAfterAllNode *)aNode {
if (aNode.block == nil)
return;
aNode.block();
}
- (void)visitBeforeEachNode:(KWBeforeEachNode *)aNode {
if (aNode.block == nil)
return;
aNode.block();
}
- (void)visitAfterEachNode:(KWAfterEachNode *)aNode {
if (aNode.block == nil)
return;
aNode.block();
}
- (void)visitLetNode:(KWLetNode *)aNode
{
[aNode evaluateTree];
}
- (void)visitItNode:(KWItNode *)aNode {
if (aNode.block == nil || aNode != self.exampleNode)
return;
aNode.example = self;
[aNode.context performExample:self withBlock:^{
@try {
aNode.block();
#if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
NSException *invocationException = KWGetAndClearExceptionFromAcrossInvocationBoundary();
[invocationException raise];
#endif // #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
// Finish verifying and clear
for (id<KWVerifying> verifier in self.verifiers) {
[verifier exampleWillEnd];
}
if (self.unresolvedVerifier) {
KWFailure *failure = [KWFailure failureWithCallSite:self.unresolvedVerifier.callSite format:@"expected subject not to be nil"];
[self reportFailure:failure];
}
} @catch (NSException *exception) {
KWFailure *failure = [KWFailure failureWithCallSite:aNode.callSite format:@"%@ \"%@\" raised",
[exception name],
[exception reason]];
[self reportFailure:failure];
}
if (self.passed) {
[self reportResultForExampleNodeWithLabel:@"PASSED"];
}
// Always clear stubs and spies at the end of it blocks
KWClearStubsAndSpies();
}];
}
- (void)visitPendingNode:(KWPendingNode *)aNode {
if (aNode != self.exampleNode)
return;
[self reportResultForExampleNodeWithLabel:@"PENDING"];
}
- (NSString *)generateDescriptionForAnonymousItNode {
// anonymous specify blocks should only have one verifier, but use the first in any case
return [(self.verifiers)[0] descriptionForAnonymousItNode];
}
@end
#pragma mark - Looking up CallSites
KWCallSite *callSiteWithAddress(long address);
KWCallSite *callSiteAtAddressIfNecessary(long address);
KWCallSite *callSiteAtAddressIfNecessary(long address){
BOOL shouldLookup = [[KWExampleSuiteBuilder sharedExampleSuiteBuilder] isFocused] && ![[KWExampleSuiteBuilder sharedExampleSuiteBuilder] foundFocus];
return shouldLookup ? callSiteWithAddress(address) : nil;
}
KWCallSite *callSiteWithAddress(long address){
NSArray *args = @[@"-d",
@"-p", @(getpid()).stringValue, [NSString stringWithFormat:@"%lx", address]];
NSString *callSite = [NSString stringWithShellCommand:@"/usr/bin/atos" arguments:args];
NSString *pattern = @".+\\((.+):([0-9]+)\\)";
NSError *e;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&e];
NSArray *res = [regex matchesInString:callSite options:0 range:NSMakeRange(0, callSite.length)];
NSString *fileName = nil;
NSInteger lineNumber = 0;
for (NSTextCheckingResult *ntcr in res) {
fileName = [callSite substringWithRange:[ntcr rangeAtIndex:1]];
NSString *lineNumberMatch = [callSite substringWithRange:[ntcr rangeAtIndex:2]];
lineNumber = lineNumberMatch.integerValue;
}
return [KWCallSite callSiteWithFilename:fileName lineNumber:lineNumber];
}
#pragma mark - Building Example Groups
void describe(NSString *aDescription, void (^block)(void)) {
KWCallSite *callSite = callSiteAtAddressIfNecessary(kwCallerAddress());
describeWithCallSite(callSite, aDescription, block);
}
void context(NSString *aDescription, void (^block)(void)) {
KWCallSite *callSite = callSiteAtAddressIfNecessary(kwCallerAddress());
contextWithCallSite(callSite, aDescription, block);
}
void registerMatchers(NSString *aNamespacePrefix) {
registerMatchersWithCallSite(nil, aNamespacePrefix);
}
void beforeAll(void (^block)(void)) {
beforeAllWithCallSite(nil, block);
}
void afterAll(void (^block)(void)) {
afterAllWithCallSite(nil, block);
}
void beforeEach(void (^block)(void)) {
beforeEachWithCallSite(nil, block);
}
void afterEach(void (^block)(void)) {
afterEachWithCallSite(nil, block);
}
void it(NSString *aDescription, void (^block)(void)) {
KWCallSite *callSite = callSiteAtAddressIfNecessary(kwCallerAddress());
itWithCallSite(callSite, aDescription, block);
}
void let_(__autoreleasing id *anObjectRef, const char *aSymbolName, id (^block)(void))
{
NSString *aDescription = [NSString stringWithUTF8String:aSymbolName];
letWithCallSite(nil, anObjectRef, aDescription, block);
}
void specify(void (^block)(void))
{
itWithCallSite(nil, nil, block);
}
void pending_(NSString *aDescription, void (^ignoredBlock)(void)) {
pendingWithCallSite(nil, aDescription, ignoredBlock);
}
void describeWithCallSite(KWCallSite *aCallSite, NSString *aDescription, void (^block)(void)) {
contextWithCallSite(aCallSite, aDescription, block);
}
void contextWithCallSite(KWCallSite *aCallSite, NSString *aDescription, void (^block)(void)) {
[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] pushContextNodeWithCallSite:aCallSite description:aDescription];
block();
[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] popContextNode];
}
void registerMatchersWithCallSite(KWCallSite *aCallSite, NSString *aNamespacePrefix) {
[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setRegisterMatchersNodeWithCallSite:aCallSite namespacePrefix:aNamespacePrefix];
}
void beforeAllWithCallSite(KWCallSite *aCallSite, void (^block)(void)) {
[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setBeforeAllNodeWithCallSite:aCallSite block:block];
}
void afterAllWithCallSite(KWCallSite *aCallSite, void (^block)(void)) {
[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setAfterAllNodeWithCallSite:aCallSite block:block];
}
void beforeEachWithCallSite(KWCallSite *aCallSite, void (^block)(void)) {
[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setBeforeEachNodeWithCallSite:aCallSite block:block];
}
void afterEachWithCallSite(KWCallSite *aCallSite, void (^block)(void)) {
[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] setAfterEachNodeWithCallSite:aCallSite block:block];
}
void letWithCallSite(KWCallSite *aCallSite, __autoreleasing id *anObjectRef, NSString *aSymbolName, id (^block)(void))
{
[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] addLetNodeWithCallSite:aCallSite objectRef:anObjectRef symbolName:aSymbolName block:block];
}
void itWithCallSite(KWCallSite *aCallSite, NSString *aDescription, void (^block)(void)) {
[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] addItNodeWithCallSite:aCallSite description:aDescription block:block];
}
void pendingWithCallSite(KWCallSite *aCallSite, NSString *aDescription, void (^ignoredBlock)(void)) {
[[KWExampleSuiteBuilder sharedExampleSuiteBuilder] addPendingNodeWithCallSite:aCallSite description:aDescription];
}