forked from kiwi-bdd/Kiwi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKWAsyncVerifier.m
99 lines (80 loc) · 2.99 KB
/
KWAsyncVerifier.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
//
// KWAsyncVerifier.m
// iOSFalconCore
//
// Created by Luke Redpath on 13/01/2011.
// Copyright 2011 LJR Software Limited. All rights reserved.
//
#import "KWAsyncVerifier.h"
#import "KWFailure.h"
#import "KWMatching.h"
#import "KWReporting.h"
#import "KWProbePoller.h"
@implementation KWAsyncVerifier
@synthesize timeout;
+ (id)asyncVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite matcherFactory:(KWMatcherFactory *)aMatcherFactory reporter:(id<KWReporting>)aReporter probeTimeout:(NSTimeInterval)probeTimeout;
{
KWAsyncVerifier *verifier = [[self alloc] initWithExpectationType:anExpectationType callSite:aCallSite matcherFactory:aMatcherFactory reporter:aReporter];
verifier.timeout = probeTimeout;
return [verifier autorelease];
}
- (id)initWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite matcherFactory:(KWMatcherFactory *)aMatcherFactory reporter:(id<KWReporting>)aReporter {
if ((self = [super initWithExpectationType:anExpectationType callSite:aCallSite matcherFactory:aMatcherFactory reporter:aReporter])) {
self.timeout = kKW_DEFAULT_PROBE_TIMEOUT;
}
return self;
}
- (void)verifyWithProbe:(KWAsyncMatcherProbe *)aProbe {
@try {
KWProbePoller *poller = [[KWProbePoller alloc] initWithTimeout:self.timeout delay:kKW_DEFAULT_PROBE_DELAY];
if (![poller check:aProbe]) {
if (self.expectationType == KWExpectationTypeShould) {
NSString *message = [aProbe.matcher failureMessageForShould];
KWFailure *failure = [KWFailure failureWithCallSite:self.callSite message:message];
[self.reporter reportFailure:failure];
} else if (self.expectationType == KWExpectationTypeShouldNot) {
NSString *message = [aProbe.matcher failureMessageForShouldNot];
KWFailure *failure = [KWFailure failureWithCallSite:self.callSite message:message];
[self.reporter reportFailure:failure];
} else if (self.expectationType == KWExpectationTypeMaybe) {
// don't do anything
}
}
[poller release];
} @catch (NSException *exception) {
KWFailure *failure = [KWFailure failureWithCallSite:self.callSite message:[exception description]];
[self.reporter reportFailure:failure];
}
}
- (void)verifyWithMatcher:(id<KWMatching>)aMatcher {
KWAsyncMatcherProbe *probe = [[[KWAsyncMatcherProbe alloc] initWithMatcher:aMatcher] autorelease];
[self verifyWithProbe:probe];
}
@end
@implementation KWAsyncMatcherProbe
@synthesize matcher;
- (id)initWithMatcher:(id<KWMatching>)aMatcher;
{
if ((self = [super init])) {
matcher = [aMatcher retain];
// make sure the matcher knows we are going to evaluate it multiple times
if ([aMatcher respondsToSelector:@selector(willEvaluateMultipleTimes)]) {
[aMatcher setWillEvaluateMultipleTimes:YES];
}
}
return self;
}
- (void)dealloc
{
[matcher release];
[super dealloc];
}
- (BOOL)isSatisfied;
{
return matchResult;
}
- (void)sample;
{
matchResult = [matcher evaluate];
}
@end