KWBeEmptyMatcher.m 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //
  2. // Licensed under the terms in License.txt
  3. //
  4. // Copyright 2010 Allen Ding. All rights reserved.
  5. //
  6. #import "KWBeEmptyMatcher.h"
  7. #import "KWFormatter.h"
  8. @interface KWBeEmptyMatcher()
  9. #pragma mark - Properties
  10. @property (nonatomic, readwrite) NSUInteger count;
  11. @end
  12. @implementation KWBeEmptyMatcher
  13. #pragma mark - Getting Matcher Strings
  14. + (NSArray *)matcherStrings {
  15. return @[@"beEmpty"];
  16. }
  17. #pragma mark - Matching
  18. - (BOOL)evaluate {
  19. if ([self.subject respondsToSelector:@selector(count)]) {
  20. self.count = [self.subject count];
  21. return self.count == 0;
  22. }
  23. else if ([self.subject respondsToSelector:@selector(length)]) {
  24. self.count = [self.subject length];
  25. return self.count == 0;
  26. }
  27. [NSException raise:@"KWMatcherException" format:@"subject does not respond to -count or -length"];
  28. return NO;
  29. }
  30. #pragma mark - Getting Failure Messages
  31. - (NSString *)countPhrase {
  32. if (self.count == 1)
  33. return @"1 item";
  34. else
  35. return [NSString stringWithFormat:@"%u items", (unsigned)self.count];
  36. }
  37. - (NSString *)failureMessageForShould {
  38. return [NSString stringWithFormat:@"expected subject to be empty, got %@", [self countPhrase]];
  39. }
  40. - (NSString *)failureMessageForShouldNot {
  41. return @"expected subject not to be empty";
  42. }
  43. - (NSString *)description {
  44. return @"be empty";
  45. }
  46. #pragma mark - Configuring Matchers
  47. - (void)beEmpty {
  48. }
  49. @end