forked from kiwi-bdd/Kiwi
/
KWExampleSuite.m
87 lines (68 loc) · 2.02 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
86
//
// KWExampleSuite.m
// Kiwi
//
// Created by Luke Redpath on 17/10/2011.
// Copyright (c) 2011 Allen Ding. All rights reserved.
//
#import "KWExampleSuite.h"
#import "KWExample.h"
#import "KWStringUtilities.h"
#import "KWBeforeAllNode.h"
#import "KWAfterAllNode.h"
#import "NSMethodSignature+KiwiAdditions.h"
#import <objc/runtime.h>
#define kKWINVOCATION_EXAMPLE_GROUP_KEY @"__KWExampleGroupKey"
@implementation KWExampleSuite
- (id)initWithRootNode:(KWContextNode *)contextNode
{
if ((self = [super init])) {
rootNode = [contextNode retain];
examples = [[NSMutableArray alloc] init];
}
return self;
}
- (void)dealloc
{
[examples release];
[rootNode release];
[super dealloc];
}
- (void)addExample:(KWExample *)example
{
[examples addObject:example];
[example setSuite:self];
}
- (void)markLastExampleAsLastInContext:(KWContextNode *)context
{
if ([examples count] > 0) {
KWExample *lastExample = (KWExample *)[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 examples) {
NSMethodSignature *methodSignature = [NSMethodSignature signatureWithObjCTypes:[KWEncodingForVoidMethod() 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