main
1//
2// KWAsyncVerifier.m
3// iOSFalconCore
4//
5// Created by Luke Redpath on 13/01/2011.
6// Copyright 2011 LJR Software Limited. All rights reserved.
7//
8
9#import "KWAsyncVerifier.h"
10#import "KWFailure.h"
11#import "KWMatching.h"
12#import "KWReporting.h"
13#import "KWProbePoller.h"
14
15@implementation KWAsyncVerifier
16
17@synthesize timeout;
18
19+ (id)asyncVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite matcherFactory:(KWMatcherFactory *)aMatcherFactory reporter:(id<KWReporting>)aReporter probeTimeout:(NSTimeInterval)probeTimeout;
20{
21 KWAsyncVerifier *verifier = [[self alloc] initWithExpectationType:anExpectationType callSite:aCallSite matcherFactory:aMatcherFactory reporter:aReporter];
22 verifier.timeout = probeTimeout;
23 return [verifier autorelease];
24}
25
26- (id)initWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite matcherFactory:(KWMatcherFactory *)aMatcherFactory reporter:(id<KWReporting>)aReporter {
27 if ((self = [super initWithExpectationType:anExpectationType callSite:aCallSite matcherFactory:aMatcherFactory reporter:aReporter])) {
28 self.timeout = kKW_DEFAULT_PROBE_TIMEOUT;
29 }
30 return self;
31}
32
33- (void)verifyWithProbe:(KWAsyncMatcherProbe *)aProbe {
34 @try {
35 KWProbePoller *poller = [[KWProbePoller alloc] initWithTimeout:self.timeout delay:kKW_DEFAULT_PROBE_DELAY];
36
37 if (![poller check:aProbe]) {
38 if (self.expectationType == KWExpectationTypeShould) {
39 NSString *message = [aProbe.matcher failureMessageForShould];
40 KWFailure *failure = [KWFailure failureWithCallSite:self.callSite message:message];
41 [self.reporter reportFailure:failure];
42 } else if (self.expectationType == KWExpectationTypeShouldNot) {
43 NSString *message = [aProbe.matcher failureMessageForShouldNot];
44 KWFailure *failure = [KWFailure failureWithCallSite:self.callSite message:message];
45 [self.reporter reportFailure:failure];
46 } else if (self.expectationType == KWExpectationTypeMaybe) {
47 // don't do anything
48 }
49 }
50 [poller release];
51
52 } @catch (NSException *exception) {
53 KWFailure *failure = [KWFailure failureWithCallSite:self.callSite message:[exception description]];
54 [self.reporter reportFailure:failure];
55 }
56}
57
58- (void)verifyWithMatcher:(id<KWMatching>)aMatcher {
59 KWAsyncMatcherProbe *probe = [[[KWAsyncMatcherProbe alloc] initWithMatcher:aMatcher] autorelease];
60 [self verifyWithProbe:probe];
61}
62
63@end
64
65@implementation KWAsyncMatcherProbe
66
67@synthesize matcher;
68
69- (id)initWithMatcher:(id<KWMatching>)aMatcher;
70{
71 if ((self = [super init])) {
72 matcher = [aMatcher retain];
73
74 // make sure the matcher knows we are going to evaluate it multiple times
75 if ([aMatcher respondsToSelector:@selector(willEvaluateMultipleTimes)]) {
76 [aMatcher setWillEvaluateMultipleTimes:YES];
77 }
78 }
79 return self;
80}
81
82- (void)dealloc
83{
84 [matcher release];
85 [super dealloc];
86}
87
88- (BOOL)isSatisfied;
89{
90 return matchResult;
91}
92
93- (void)sample;
94{
95 matchResult = [matcher evaluate];
96}
97
98@end
99