KWAsyncVerifier.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #import "KWAsyncVerifier.h"
  9. #import "KWFailure.h"
  10. #import "KWMatching.h"
  11. #import "KWReporting.h"
  12. #import "KWProbePoller.h"
  13. @implementation KWAsyncVerifier
  14. + (id)asyncVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite matcherFactory:(KWMatcherFactory *)aMatcherFactory reporter:(id<KWReporting>)aReporter probeTimeout:(NSTimeInterval)probeTimeout shouldWait:(BOOL)shouldWait {
  15. KWAsyncVerifier *verifier = [[self alloc] initWithExpectationType:anExpectationType callSite:aCallSite matcherFactory:aMatcherFactory reporter:aReporter];
  16. verifier.timeout = probeTimeout;
  17. verifier.shouldWait = shouldWait;
  18. return verifier;
  19. }
  20. - (id)initWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite matcherFactory:(KWMatcherFactory *)aMatcherFactory reporter:(id<KWReporting>)aReporter {
  21. self = [super initWithExpectationType:anExpectationType callSite:aCallSite matcherFactory:aMatcherFactory reporter:aReporter];
  22. if (self) {
  23. self.timeout = kKW_DEFAULT_PROBE_TIMEOUT;
  24. }
  25. return self;
  26. }
  27. - (void)verifyWithProbe:(KWAsyncMatcherProbe *)aProbe {
  28. @try {
  29. KWProbePoller *poller = [[KWProbePoller alloc] initWithTimeout:self.timeout delay:kKW_DEFAULT_PROBE_DELAY shouldWait: self.shouldWait];
  30. if (![poller check:aProbe]) {
  31. if (self.expectationType == KWExpectationTypeShould) {
  32. NSString *message = [aProbe.matcher failureMessageForShould];
  33. KWFailure *failure = [KWFailure failureWithCallSite:self.callSite message:message];
  34. [self.reporter reportFailure:failure];
  35. }
  36. } else {
  37. // poller returned YES -- fail if expectation is NOT
  38. if (self.expectationType == KWExpectationTypeShouldNot) {
  39. NSString *message = [aProbe.matcher failureMessageForShouldNot];
  40. KWFailure *failure = [KWFailure failureWithCallSite:self.callSite message:message];
  41. [self.reporter reportFailure:failure];
  42. }
  43. }
  44. } @catch (NSException *exception) {
  45. KWFailure *failure = [KWFailure failureWithCallSite:self.callSite message:[exception description]];
  46. [self.reporter reportFailure:failure];
  47. }
  48. }
  49. - (void)verifyWithMatcher:(id<KWMatching>)aMatcher {
  50. KWAsyncMatcherProbe *probe = [[KWAsyncMatcherProbe alloc] initWithMatcher:aMatcher];
  51. [self verifyWithProbe:probe];
  52. }
  53. @end
  54. @implementation KWAsyncMatcherProbe
  55. - (id)initWithMatcher:(id<KWMatching>)aMatcher {
  56. self = [super init];
  57. if (self) {
  58. _matcher = aMatcher;
  59. // make sure the matcher knows we are going to evaluate it multiple times
  60. if ([aMatcher respondsToSelector:@selector(willEvaluateMultipleTimes)]) {
  61. [aMatcher setWillEvaluateMultipleTimes:YES];
  62. }
  63. }
  64. return self;
  65. }
  66. - (BOOL)isSatisfied {
  67. return self.matchResult;
  68. }
  69. - (void)sample {
  70. self.matchResult = [self.matcher evaluate];
  71. }
  72. @end