forked from kiwi-bdd/Kiwi
-
Notifications
You must be signed in to change notification settings - Fork 55
/
KWExampleSuite.m
86 lines (63 loc) · 2.19 KB
/
KWExampleSuite.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
//
// KWExampleSuite.m
// Kiwi
//
// Created by Luke Redpath on 17/10/2011.
// Copyright (c) 2011 Allen Ding. All rights reserved.
//
#import "KWExampleSuite.h"
#import "KWAfterAllNode.h"
#import "KWBeforeAllNode.h"
#import "KWContextNode.h"
#import "KWExample.h"
#import "KWStringUtilities.h"
#import "NSMethodSignature+KiwiAdditions.h"
#import <objc/runtime.h>
#define kKWINVOCATION_EXAMPLE_GROUP_KEY @"__KWExampleGroupKey"
@interface KWExampleSuite()
@property (nonatomic, strong) KWContextNode *rootNode;
@property (nonatomic, strong) NSMutableArray *examples;
@end
@implementation KWExampleSuite
- (id)initWithRootNode:(KWContextNode *)contextNode {
self = [super init];
if (self) {
_rootNode = contextNode;
_examples = [[NSMutableArray alloc] init];
}
return self;
}
- (void)addExample:(KWExample *)example {
[self.examples addObject:example];
example.suite = self;
}
- (void)markLastExampleAsLastInContext:(KWContextNode *)context
{
if ([self.examples count] > 0) {
KWExample *lastExample = (KWExample *)[self.examples lastObject];
[lastExample.lastInContexts addObject:context];
}
}
- (NSArray *)invocationsForTestCase {
NSMutableArray *invocations = [NSMutableArray array];
// Add a single dummy invocation for each example group
for (KWExample *exampleGroup in self.examples) {
NSMethodSignature *methodSignature = [NSMethodSignature signatureWithObjCTypes:[KWEncodingForDefaultMethod() UTF8String]];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocations addObject:invocation];
[invocation kw_setExample:exampleGroup];
}
return invocations;
}
@end
#pragma mark -
// because SenTest will modify the invocation target, we'll have to store
// another reference to the example group so we can retrieve it later
@implementation NSInvocation (KWExampleGroup)
- (void)kw_setExample:(KWExample *)exampleGroup {
objc_setAssociatedObject(self, kKWINVOCATION_EXAMPLE_GROUP_KEY, exampleGroup, OBJC_ASSOCIATION_RETAIN);
}
- (KWExample *)kw_example {
return objc_getAssociatedObject(self, kKWINVOCATION_EXAMPLE_GROUP_KEY);
}
@end