KWChangeMatcher.m 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //
  2. // KWChangeMatcher.m
  3. // Kiwi
  4. //
  5. // Copyright (c) 2013 Eloy Durán <eloy.de.enige@gmail.com>.
  6. // All rights reserved.
  7. //
  8. #import "KWChangeMatcher.h"
  9. #import "KWBlock.h"
  10. @interface KWChangeMatcher ()
  11. @property (nonatomic, copy) KWChangeMatcherCountBlock countBlock;
  12. @property (nonatomic, assign) BOOL anyChange;
  13. @property (nonatomic, assign) NSInteger expectedDifference, expectedTotal, actualTotal;
  14. @end
  15. @implementation KWChangeMatcher
  16. + (NSArray *)matcherStrings {
  17. return @[@"change:by:", @"change:"];
  18. }
  19. - (NSString *)failureMessageForShould {
  20. if (self.anyChange) {
  21. return @"expected subject to change the count";
  22. } else {
  23. return [NSString stringWithFormat:@"expected subject to change the count to %d, got %d", (int)self.expectedTotal, (int)self.actualTotal];
  24. }
  25. }
  26. - (NSString *)failureMessageForShouldNot {
  27. if (self.anyChange) {
  28. return @"expected subject to not change the count";
  29. } else {
  30. return [NSString stringWithFormat:@"expected subject not to change the count to %d", (int)self.actualTotal];
  31. }
  32. }
  33. - (NSString *)description {
  34. if (self.anyChange) {
  35. return @"change count";
  36. } else {
  37. return [NSString stringWithFormat:@"change count by %d", (int)self.expectedDifference];
  38. }
  39. }
  40. - (BOOL)evaluate {
  41. NSInteger before = self.countBlock();
  42. // Perform actual work, which is expected to change the result of countBlock.
  43. [self.subject call];
  44. self.actualTotal = self.countBlock();
  45. if (self.anyChange) {
  46. return before != self.actualTotal;
  47. } else {
  48. self.expectedTotal = before + self.expectedDifference;
  49. return self.expectedTotal == self.actualTotal;
  50. }
  51. }
  52. - (void)change:(KWChangeMatcherCountBlock)countBlock by:(NSInteger)expectedDifference {
  53. self.anyChange = NO;
  54. self.expectedDifference = expectedDifference;
  55. self.countBlock = countBlock;
  56. }
  57. - (void)change:(KWChangeMatcherCountBlock)countBlock {
  58. self.anyChange = YES;
  59. self.countBlock = countBlock;
  60. }
  61. @end