-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathEXPExpect.m
214 lines (182 loc) · 5.22 KB
/
EXPExpect.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
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
#import "EXPExpect.h"
#import "NSObject+Expecta.h"
#import "Expecta.h"
#import "EXPUnsupportedObject.h"
#import "EXPMatcher.h"
#import "EXPBlockDefinedMatcher.h"
#import <libkern/OSAtomic.h>
@implementation EXPExpect
@dynamic
actual,
to,
toNot,
notTo,
will,
willNot,
after;
@synthesize
actualBlock=_actualBlock,
testCase=_testCase,
negative=_negative,
asynchronous=_asynchronous,
timeout=_timeout,
lineNumber=_lineNumber,
fileName=_fileName;
- (instancetype)initWithActualBlock:(id)actualBlock testCase:(id)testCase lineNumber:(int)lineNumber fileName:(const char *)fileName {
self = [super init];
if(self) {
self.actualBlock = actualBlock;
self.testCase = testCase;
self.negative = NO;
self.asynchronous = NO;
self.timeout = [Expecta asynchronousTestTimeout];
self.lineNumber = lineNumber;
self.fileName = fileName;
}
return self;
}
- (void)dealloc
{
_actualBlock = nil;
[super dealloc];
}
+ (EXPExpect *)expectWithActualBlock:(id)actualBlock testCase:(id)testCase lineNumber:(int)lineNumber fileName:(const char *)fileName {
return [[[EXPExpect alloc] initWithActualBlock:actualBlock testCase:(id)testCase lineNumber:lineNumber fileName:fileName] autorelease];
}
#pragma mark -
- (EXPExpect *)to {
return self;
}
- (EXPExpect *)toNot {
self.negative = !self.negative;
return self;
}
- (EXPExpect *)notTo {
return [self toNot];
}
- (EXPExpect *)will {
self.asynchronous = YES;
return self;
}
- (EXPExpect *)willNot {
return self.will.toNot;
}
- (EXPExpect *(^)(NSTimeInterval))after
{
EXPExpect * (^block)(NSTimeInterval) = [^EXPExpect *(NSTimeInterval timeout) {
self.asynchronous = YES;
self.timeout = timeout;
return self;
} copy];
return [block autorelease];
}
#pragma mark -
- (id)actual {
if(self.actualBlock) {
return self.actualBlock();
}
return nil;
}
- (void)applyMatcher:(id<EXPMatcher>)matcher
{
id actual = [self actual];
[self applyMatcher:matcher to:&actual];
}
- (void)applyMatcher:(id<EXPMatcher>)matcher to:(NSObject **)actual {
if([*actual isKindOfClass:[EXPUnsupportedObject class]]) {
EXPFail(self.testCase, self.lineNumber, self.fileName,
[NSString stringWithFormat:@"expecting a %@ is not supported", ((EXPUnsupportedObject *)*actual).type]);
} else {
BOOL failed = NO;
if([matcher respondsToSelector:@selector(meetsPrerequesiteFor:)] &&
![matcher meetsPrerequesiteFor:*actual]) {
failed = YES;
} else {
BOOL matchResult = NO;
if(self.asynchronous) {
NSTimeInterval timeOut = self.timeout;
NSDate *expiryDate = [NSDate dateWithTimeIntervalSinceNow:timeOut];
while(1) {
matchResult = [matcher matches:*actual];
failed = self.negative ? matchResult : !matchResult;
if(!failed || ([(NSDate *)[NSDate date] compare:expiryDate] == NSOrderedDescending)) {
break;
}
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
OSMemoryBarrier();
*actual = self.actual;
}
} else {
matchResult = [matcher matches:*actual];
}
failed = self.negative ? matchResult : !matchResult;
}
if(failed) {
NSString *message = nil;
if(self.negative) {
if ([matcher respondsToSelector:@selector(failureMessageForNotTo:)]) {
message = [matcher failureMessageForNotTo:*actual];
}
} else {
if ([matcher respondsToSelector:@selector(failureMessageForTo:)]) {
message = [matcher failureMessageForTo:*actual];
}
}
if (message == nil) {
message = @"Match Failed.";
}
EXPFail(self.testCase, self.lineNumber, self.fileName, message);
}
}
self.negative = NO;
}
#pragma mark - Dynamic predicate dispatch
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
if ([self.actual respondsToSelector:aSelector]) {
return [self.actual methodSignatureForSelector:aSelector];
}
return [super methodSignatureForSelector:aSelector];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
if ([self.actual respondsToSelector:anInvocation.selector]) {
EXPDynamicPredicateMatcher *matcher = [[EXPDynamicPredicateMatcher alloc] initWithExpectation:self selector:anInvocation.selector];
[anInvocation setSelector:@selector(dispatch)];
[anInvocation invokeWithTarget:matcher];
[matcher release];
}
else {
[super forwardInvocation:anInvocation];
}
}
@end
@implementation EXPDynamicPredicateMatcher
- (instancetype)initWithExpectation:(EXPExpect *)expectation selector:(SEL)selector
{
if ((self = [super init])) {
_expectation = expectation;
_selector = selector;
}
return self;
}
- (BOOL)matches:(id)actual
{
return (BOOL)[actual performSelector:_selector];
}
- (NSString *)failureMessageForTo:(id)actual
{
return [NSString stringWithFormat:@"expected %@ to be true", NSStringFromSelector(_selector)];
}
- (NSString *)failureMessageForNotTo:(id)actual
{
return [NSString stringWithFormat:@"expected %@ to be false", NSStringFromSelector(_selector)];
}
- (void (^)(void))dispatch
{
__block id blockExpectation = _expectation;
return [[^{
[blockExpectation applyMatcher:self];
} copy] autorelease];
}
@end