KWExistVerifier.m 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //
  2. // Licensed under the terms in License.txt
  3. //
  4. // Copyright 2010 Allen Ding. All rights reserved.
  5. //
  6. #import "KWExistVerifier.h"
  7. #import "KWCallSite.h"
  8. #import "KWFailure.h"
  9. #import "KWFormatter.h"
  10. #import "KWReporting.h"
  11. @interface KWExistVerifier()
  12. @property (nonatomic, readonly) KWExpectationType expectationType;
  13. @property (nonatomic, readonly) id<KWReporting> reporter;
  14. @property (nonatomic, strong) KWCallSite *callSite;
  15. @end
  16. @implementation KWExistVerifier
  17. #pragma mark - Initializing
  18. - (id)initWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite reporter:(id<KWReporting>)aReporter {
  19. self = [super init];
  20. if (self) {
  21. _expectationType = anExpectationType;
  22. _callSite = aCallSite;
  23. _reporter = aReporter;
  24. }
  25. return self;
  26. }
  27. + (id)existVerifierWithExpectationType:(KWExpectationType)anExpectationType callSite:(KWCallSite *)aCallSite reporter:(id<KWReporting>)aReporter {
  28. return [[self alloc] initWithExpectationType:anExpectationType callSite:aCallSite reporter:aReporter];
  29. }
  30. - (NSString *)descriptionForAnonymousItNode {
  31. if (self.expectationType == KWExpectationTypeShould) {
  32. return @"should exist";
  33. }
  34. return @"should not exist";
  35. }
  36. #pragma mark - Ending Examples
  37. - (void)exampleWillEnd {
  38. if (self.expectationType == KWExpectationTypeShould && self.subject == nil) {
  39. KWFailure *failure = [KWFailure failureWithCallSite:self.callSite message:@"expected subject not to be nil"];
  40. [self.reporter reportFailure:failure];
  41. } else if (self.expectationType == KWExpectationTypeShouldNot && self.subject != nil) {
  42. KWFailure *failure = [KWFailure failureWithCallSite:self.callSite format:@"expected subject to be nil, got %@",
  43. [KWFormatter formatObject:self.subject]];
  44. [self.reporter reportFailure:failure];
  45. }
  46. }
  47. @end