KWBeBetweenMatcher.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 "KWBeBetweenMatcher.h"
  7. #import "KWFormatter.h"
  8. @interface KWBeBetweenMatcher()
  9. #pragma mark - Properties
  10. @property (nonatomic, strong) id lowerEndpoint;
  11. @property (nonatomic, strong) id upperEndpoint;
  12. @end
  13. @implementation KWBeBetweenMatcher
  14. #pragma mark - Getting Matcher Strings
  15. + (NSArray *)matcherStrings {
  16. return @[@"beBetween:and:", @"beInTheIntervalFrom:to:"];
  17. }
  18. #pragma mark - Matching
  19. - (BOOL)evaluate {
  20. if (![self.subject respondsToSelector:@selector(compare:)])
  21. [NSException raise:@"KWMatcherException" format:@"subject does not respond to -compare:"];
  22. NSComparisonResult lowerResult = [self.subject compare:self.lowerEndpoint];
  23. NSComparisonResult upperResult = [self.subject compare:self.upperEndpoint];
  24. return (lowerResult == NSOrderedDescending || lowerResult == NSOrderedSame) &&
  25. (upperResult == NSOrderedAscending || upperResult == NSOrderedSame);
  26. }
  27. #pragma mark - Getting Failure Messages
  28. - (NSString *)failureMessageForShould {
  29. return [NSString stringWithFormat:@"expected subject to be in the interval [%@, %@], got %@",
  30. [KWFormatter formatObject:self.lowerEndpoint],
  31. [KWFormatter formatObject:self.upperEndpoint],
  32. [KWFormatter formatObject:self.subject]];
  33. }
  34. - (NSString *)description {
  35. return [NSString stringWithFormat:@"be between %@ and %@", self.lowerEndpoint, self.upperEndpoint];
  36. }
  37. #pragma mark - Configuring Matchers
  38. - (void)beBetween:(id)aLowerEndpoint and:(id)anUpperEndpoint {
  39. [self beInTheIntervalFrom:aLowerEndpoint to:anUpperEndpoint];
  40. }
  41. - (void)beInTheIntervalFrom:(id)aLowerEndpoint to:(id)anUpperEndpoint {
  42. self.lowerEndpoint = aLowerEndpoint;
  43. self.upperEndpoint = anUpperEndpoint;
  44. }
  45. @end