KWMessageTracker.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // Licensed under the terms in License.txt
  3. //
  4. // Copyright 2010 Allen Ding. All rights reserved.
  5. //
  6. #import "KWMessageTracker.h"
  7. #import "KWMessagePattern.h"
  8. #import "NSObject+KiwiStubAdditions.h"
  9. @interface KWMessageTracker()
  10. #pragma mark - Properties
  11. @property (nonatomic, assign) NSUInteger receivedCount;
  12. @end
  13. @implementation KWMessageTracker
  14. #pragma mark - Initializing
  15. - (id)initWithSubject:(id)anObject messagePattern:(KWMessagePattern *)aMessagePattern countType:(KWCountType)aCountType count:(NSUInteger)aCount {
  16. self = [super init];
  17. if (self) {
  18. _subject = anObject;
  19. _messagePattern = aMessagePattern;
  20. _countType = aCountType;
  21. _count = aCount;
  22. [anObject addMessageSpy:self forMessagePattern:aMessagePattern];
  23. }
  24. return self;
  25. }
  26. + (id)messageTrackerWithSubject:(id)anObject messagePattern:(KWMessagePattern *)aMessagePattern countType:(KWCountType)aCountType count:(NSUInteger)aCount {
  27. return [[self alloc] initWithSubject:anObject messagePattern:aMessagePattern countType:aCountType count:aCount];
  28. }
  29. #pragma mark - Spying on Messages
  30. - (void)object:(id)anObject didReceiveInvocation:(NSInvocation *)anInvocation {
  31. if (![self.messagePattern matchesInvocation:anInvocation])
  32. return;
  33. ++self.receivedCount;
  34. }
  35. #pragma mark - Stopping Tracking
  36. - (void)stopTracking {
  37. [self.subject removeMessageSpy:self forMessagePattern:self.messagePattern];
  38. }
  39. #pragma mark - Getting Message Tracker Status
  40. - (BOOL)succeeded {
  41. switch (self.countType) {
  42. case KWCountTypeExact:
  43. return self.receivedCount == self.count;
  44. case KWCountTypeAtLeast:
  45. return self.receivedCount >= self.count;
  46. case KWCountTypeAtMost:
  47. return self.receivedCount <= self.count;
  48. default:
  49. break;
  50. }
  51. assert(0 && "should never reach here");
  52. return NO;
  53. }
  54. #pragma mark - Getting Phrases
  55. - (NSString *)phraseForCount:(NSUInteger)aCount {
  56. if (aCount == 1)
  57. return @"1 time";
  58. return [NSString stringWithFormat:@"%d times", (int)aCount];
  59. }
  60. - (NSString *)expectedCountPhrase {
  61. NSString *countPhrase = [self phraseForCount:self.count];
  62. switch (self.countType) {
  63. case KWCountTypeExact:
  64. return [NSString stringWithFormat:@"exactly %@", countPhrase];
  65. case KWCountTypeAtLeast:
  66. return [NSString stringWithFormat:@"at least %@", countPhrase];
  67. case KWCountTypeAtMost:
  68. return [NSString stringWithFormat:@"at most %@", countPhrase];
  69. default:
  70. break;
  71. }
  72. assert(0 && "should never reach here");
  73. return nil;
  74. }
  75. - (NSString *)receivedCountPhrase {
  76. return [self phraseForCount:self.receivedCount];
  77. }
  78. #pragma mark - Debugging
  79. - (NSString *)modeString {
  80. switch (self.countType) {
  81. case KWCountTypeExact:
  82. return @"KWCountTypeExact";
  83. case KWCountTypeAtLeast:
  84. return @"KWCountTypeAtLeast";
  85. case KWCountTypeAtMost:
  86. return @"KWCountTypeAtMost";
  87. default:
  88. break;
  89. }
  90. assert(0 && "should never reach here");
  91. return nil;
  92. }
  93. - (NSString *)description {
  94. return [NSString stringWithFormat:@"messagePattern: %@\nmode: %@\ncount: %d\nreceiveCount: %d",
  95. self.messagePattern,
  96. self.modeString,
  97. (int)self.count,
  98. (int)self.receivedCount];
  99. }
  100. @end