forked from kiwi-bdd/Kiwi
-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathKWContextNode.m
More file actions
147 lines (114 loc) · 4.05 KB
/
KWContextNode.m
File metadata and controls
147 lines (114 loc) · 4.05 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
//
// Licensed under the terms in License.txt
//
// Copyright 2010 Allen Ding. All rights reserved.
//
#import "KWAfterAllNode.h"
#import "KWAfterEachNode.h"
#import "KWBeforeAllNode.h"
#import "KWBeforeEachNode.h"
#import "KWLetNode.h"
#import "KWCallSite.h"
#import "KWContextNode.h"
#import "KWExampleNodeVisitor.h"
#import "KWExample.h"
#import "KWFailure.h"
#import "KWRegisterMatchersNode.h"
#import "KWSymbolicator.h"
@interface KWContextNode()
@property (nonatomic, assign) NSUInteger performedExampleCount;
@end
@implementation KWContextNode
#pragma mark - Initializing
- (id)initWithCallSite:(KWCallSite *)aCallSite parentContext:(KWContextNode *)node description:(NSString *)aDescription {
self = [super init];
if (self) {
_parentContext = node;
_callSite = aCallSite;
_description = [aDescription copy];
_nodes = [[NSMutableArray alloc] init];
_letNodes = [[NSMutableArray alloc] init];
_performedExampleCount = 0;
}
return self;
}
+ (id)contextNodeWithCallSite:(KWCallSite *)aCallSite parentContext:(KWContextNode *)contextNode description:(NSString *)aDescription {
return [[self alloc] initWithCallSite:aCallSite parentContext:contextNode description:aDescription];
}
- (void)addContextNode:(KWContextNode *)aNode {
[(NSMutableArray *)self.nodes addObject:aNode];
}
- (void)setRegisterMatchersNode:(KWRegisterMatchersNode *)aNode {
if (self.registerMatchersNode != nil)
[NSException raise:@"KWContextNodeException" format:@"a register matchers node already exists"];
_registerMatchersNode = aNode;
}
- (void)setBeforeEachNode:(KWBeforeEachNode *)aNode {
if (self.beforeEachNode != nil)
[NSException raise:@"KWContextNodeException" format:@"a before each node already exists"];
_beforeEachNode = aNode;
}
- (void)setAfterEachNode:(KWAfterEachNode *)aNode {
if (self.afterEachNode != nil)
[NSException raise:@"KWContextNodeException" format:@"an after each node already exists"];
_afterEachNode = aNode;
}
- (void)addLetNode:(KWLetNode *)aNode
{
[(NSMutableArray *)self.letNodes addObject:aNode];
}
- (KWLetNode *)letNodeTree
{
KWLetNode *tree = [self.parentContext letNodeTree];
for (KWLetNode *letNode in self.letNodes) {
if (!tree) {
tree = letNode;
}
else {
[tree addLetNode:letNode];
}
}
return tree;
}
- (void)addItNode:(KWItNode *)aNode {
[(NSMutableArray *)self.nodes addObject:aNode];
}
- (void)addPendingNode:(KWPendingNode *)aNode {
[(NSMutableArray *)self.nodes addObject:aNode];
}
- (void)performExample:(KWExample *)example withBlock:(void (^)(void))exampleBlock
{
void (^innerExampleBlock)(void) = [exampleBlock copy];
void (^outerExampleBlock)(void) = ^{
@try {
[self.registerMatchersNode acceptExampleNodeVisitor:example];
if (self.performedExampleCount == 0) {
[self.beforeAllNode acceptExampleNodeVisitor:example];
}
KWLetNode *letNodeTree = [self letNodeTree];
[letNodeTree acceptExampleNodeVisitor:example];
[self.beforeEachNode acceptExampleNodeVisitor:example];
innerExampleBlock();
[self.afterEachNode acceptExampleNodeVisitor:example];
if ([example isLastInContext:self]) {
[self.afterAllNode acceptExampleNodeVisitor:example];
[letNodeTree unlink];
}
} @catch (NSException *exception) {
KWFailure *failure = [KWFailure failureWithCallSite:self.callSite format:@"%@ \"%@\" raised", [exception name], [exception reason]];
[example reportFailure:failure];
}
self.performedExampleCount++;
};
if (self.parentContext == nil) {
outerExampleBlock();
}
else {
[self.parentContext performExample:example withBlock:outerExampleBlock];
}
}
#pragma mark - Accepting Visitors
- (void)acceptExampleNodeVisitor:(id<KWExampleNodeVisitor>)aVisitor {
[aVisitor visitContextNode:self];
}
@end