KWNotificationMatcher.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // KWNotificationMatcher.m
  3. //
  4. // Created by Paul Zabelin on 7/12/12.
  5. // Copyright (c) 2012 Blazing Cloud, Inc. All rights reserved.
  6. //
  7. #import "KWNotificationMatcher.h"
  8. #import "KWFormatter.h"
  9. @interface KWNotificationMatcher ()
  10. @property (nonatomic, strong) NSNotification *notification;
  11. @property (nonatomic, strong) id observer;
  12. @property (nonatomic, copy) PostedNotificationBlock evaluationBlock;
  13. @property (nonatomic, strong) id expectedObject;
  14. @property (nonatomic, strong) NSDictionary *expectedUserInfo;
  15. @property (nonatomic, assign) BOOL didReceiveNotification;
  16. @end
  17. @implementation KWNotificationMatcher
  18. + (NSArray *)matcherStrings {
  19. return @[@"bePosted",
  20. @"bePostedWithObject:",
  21. @"bePostedWithUserInfo:",
  22. @"bePostedWithObject:andUserInfo:",
  23. @"bePostedWithObject:userInfo:",
  24. @"bePostedEvaluatingBlock:"];
  25. }
  26. - (void)addObserver {
  27. NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
  28. self.observer = [center addObserverForName:self.subject
  29. object:self.expectedObject
  30. queue:nil
  31. usingBlock:^(NSNotification *note) {
  32. self.notification = note;
  33. self.didReceiveNotification = YES;
  34. if (self.expectedObject) {
  35. self.didReceiveNotification &= (self.expectedObject==note.object);
  36. }
  37. if (self.expectedUserInfo) {
  38. self.didReceiveNotification &= [self.expectedUserInfo isEqualToDictionary:[note userInfo]];
  39. }
  40. if (self.evaluationBlock) {
  41. self.evaluationBlock(note);
  42. }
  43. }];
  44. }
  45. #pragma mark - Matching
  46. - (BOOL)evaluate {
  47. [[NSNotificationCenter defaultCenter] removeObserver:self.observer];
  48. return self.didReceiveNotification;
  49. }
  50. - (BOOL)shouldBeEvaluatedAtEndOfExample {
  51. return YES;
  52. }
  53. #pragma mark - Getting Matcher Compatability
  54. + (BOOL)canMatchSubject:(id)anObject {
  55. return [anObject isKindOfClass:[NSString class]];
  56. }
  57. #pragma mark - Getting Failure Messages
  58. - (NSString *)receiveNotificationMessage {
  59. NSMutableString *message = [NSMutableString stringWithFormat:@"receive %@ notification", [KWFormatter formatObject:self.subject]];
  60. if (self.expectedObject && self.expectedUserInfo) {
  61. [message appendFormat:@" with object: %@ and user info: %@", self.expectedObject, self.expectedUserInfo];
  62. } else if (self.expectedObject) {
  63. [message appendFormat:@" with object: %@", self.expectedObject];
  64. } else if (self.expectedUserInfo) {
  65. [message appendFormat:@" with user info: %@", self.expectedUserInfo];
  66. }
  67. return message;
  68. }
  69. - (NSString *)failureMessageForShould {
  70. return [NSString stringWithFormat:@"expect to %@", [self receiveNotificationMessage]];
  71. }
  72. - (NSString *)failureMessageForShouldNot {
  73. return [NSString stringWithFormat:@"expect not to %@, but received: %@",
  74. [self receiveNotificationMessage],
  75. self.notification];
  76. }
  77. - (NSString *)description {
  78. return [NSString stringWithFormat:@"%@ be posted", self.subject];
  79. }
  80. #pragma mark - Configuring Matchers
  81. - (void)bePosted {
  82. [self addObserver];
  83. }
  84. - (void)bePostedWithObject:(id)object {
  85. [self addObserver];
  86. self.expectedObject = object;
  87. }
  88. - (void)bePostedWithUserInfo:(NSDictionary *)userInfo {
  89. [self addObserver];
  90. self.expectedUserInfo = userInfo;
  91. }
  92. - (void)bePostedWithObject:(id)object andUserInfo:(NSDictionary *)userInfo {
  93. [self bePostedWithObject:object userInfo:userInfo];
  94. }
  95. - (void)bePostedWithObject:(id)object userInfo:(NSDictionary *)userInfo {
  96. [self addObserver];
  97. self.expectedObject = object;
  98. self.expectedUserInfo = userInfo;
  99. }
  100. - (void)bePostedEvaluatingBlock:(PostedNotificationBlock)block {
  101. [self addObserver];
  102. self.evaluationBlock = block;
  103. }
  104. @end