Created
November 13, 2012 14:44
-
-
Save MattesGroeger/4066084 to your computer and use it in GitHub Desktop.
Matcher for Kiwi testing framework that allows testing notifications.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "KWHCMatcher.h" | |
#import "KWMessageTracker.h" | |
#import "Kiwi.h" | |
@interface NSNotificationMatcher : NSObject <HCMatcher> | |
{ | |
NSDictionary *_userInfo; | |
} | |
+ (id)matcherWithUserInfo:(NSDictionary *)dictionary; | |
@end | |
@interface NotificationMatcher : KWMatcher | |
{ | |
@private | |
KWMessageTracker *_messageTracker; | |
NSString *_notificationName; | |
} | |
- (void)receiveNotification:(NSString *)name; | |
- (void)receiveNotification:(NSString *)name withCount:(NSUInteger)aCount; | |
- (void)receiveNotification:(NSString *)name withCountAtLeast:(NSUInteger)aCount; | |
- (void)receiveNotification:(NSString *)name withCountAtMost:(NSUInteger)aCount; | |
- (void)receiveNotification:(NSString *)name withUserInfo:(NSDictionary *)userInfo; | |
- (void)receiveNotification:(NSString *)name withCount:(NSUInteger)aCount userInfo:(NSDictionary *)userInfo; | |
- (void)receiveNotification:(NSString *)name withCountAtLeast:(NSUInteger)aCount userInfo:(NSDictionary *)userInfo; | |
- (void)receiveNotification:(NSString *)name withCountAtMost:(NSUInteger)aCount userInfo:(NSDictionary *)userInfo; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "MatcherFixtures.h" | |
@implementation NSNotificationMatcher | |
+ (id)matcherWithUserInfo:(NSDictionary *)userInfo | |
{ | |
return [[NSNotificationMatcher alloc] initWithUserInfo:userInfo]; | |
} | |
- (id)initWithUserInfo:(NSDictionary *)userInfo | |
{ | |
self = [super init]; | |
if (self) | |
{ | |
_userInfo = userInfo; | |
} | |
return self; | |
} | |
- (BOOL)matches:(id)item | |
{ | |
NSNotification *notification = item; | |
return ([notification.userInfo isEqual:_userInfo]); | |
} | |
@end | |
@implementation NotificationMatcher | |
+ (NSArray *)matcherStrings | |
{ | |
return @[@"receiveNotification:", | |
@"receiveNotification:withCount:", | |
@"receiveNotification:withCountAtLeast:", | |
@"receiveNotification:withCountAtMost:", | |
@"receiveNotification:withUserInfo:", | |
@"receiveNotification:withCount:userInfo:", | |
@"receiveNotification:withCountAtLeast:userInfo:", | |
@"receiveNotification:withCountAtMost:userInfo:"]; | |
} | |
- (void)receiveNotification:(NSString *)name | |
{ | |
return [self addObserver:name countType:KWCountTypeExact count:1 userInfo:nil]; | |
} | |
- (void)receiveNotification:(NSString *)name withCount:(NSUInteger)aCount | |
{ | |
return [self addObserver:name countType:KWCountTypeExact count:aCount userInfo:nil]; | |
} | |
- (void)receiveNotification:(NSString *)name withCountAtLeast:(NSUInteger)aCount | |
{ | |
return [self addObserver:name countType:KWCountTypeAtLeast count:aCount userInfo:nil]; | |
} | |
- (void)receiveNotification:(NSString *)name withCountAtMost:(NSUInteger)aCount | |
{ | |
return [self addObserver:name countType:KWCountTypeAtMost count:aCount userInfo:nil]; | |
} | |
- (void)receiveNotification:(NSString *)name withUserInfo:(NSDictionary *)userInfo | |
{ | |
return [self addObserver:name countType:KWCountTypeExact count:1 userInfo:userInfo]; | |
} | |
- (void)receiveNotification:(NSString *)name withCount:(NSUInteger)aCount userInfo:(NSDictionary *)userInfo | |
{ | |
return [self addObserver:name countType:KWCountTypeExact count:aCount userInfo:userInfo]; | |
} | |
- (void)receiveNotification:(NSString *)name withCountAtLeast:(NSUInteger)aCount userInfo:(NSDictionary *)userInfo | |
{ | |
return [self addObserver:name countType:KWCountTypeAtLeast count:aCount userInfo:userInfo]; | |
} | |
- (void)receiveNotification:(NSString *)name withCountAtMost:(NSUInteger)aCount userInfo:(NSDictionary *)userInfo | |
{ | |
return [self addObserver:name countType:KWCountTypeAtMost count:aCount userInfo:userInfo]; | |
} | |
- (void)addObserver:(NSString *)notificationName countType:(KWCountType)aCountType count:(NSUInteger)aCount userInfo:(NSDictionary *)userInfo | |
{ | |
if (![self.subject isKindOfClass:[NSNotificationCenter class]]) | |
[NSException raise:@"KWMatcherException" format:@"subject is not of type -NSNotificationCenter"]; | |
_notificationName = notificationName; | |
KWMessagePattern *messagePattern; | |
if (userInfo) | |
{ | |
messagePattern = [KWMessagePattern messagePatternWithSelector:@selector(handleNotification:) argumentFilters:[NSArray arrayWithObject:[NSNotificationMatcher matcherWithUserInfo:userInfo]]]; | |
} | |
else | |
{ | |
messagePattern = [KWMessagePattern messagePatternWithSelector:@selector(handleNotification:)]; | |
} | |
[subject addObserver:self | |
selector:@selector(handleNotification:) | |
name:_notificationName | |
object:nil]; | |
@try | |
{ | |
_messageTracker = [KWMessageTracker messageTrackerWithSubject:self messagePattern:messagePattern countType:aCountType count:aCount]; | |
} | |
@catch (NSException *exception) | |
{ | |
// KWSetExceptionFromAcrossInvocationBoundary(exception); | |
} | |
} | |
- (void)handleNotification:(NSNotification *)notification | |
{ | |
NSLog(@"NSNotification received!"); | |
} | |
- (NSString *)failureMessageForShould { | |
return [NSString stringWithFormat:@"expected subject to send notification -%@ %@, but received it %@", | |
[_messageTracker expectedCountPhrase], | |
_notificationName, | |
[_messageTracker receivedCountPhrase]]; | |
} | |
- (NSString *)failureMessageForShouldNot { | |
return [NSString stringWithFormat:@"expected subject not to send notification -%@, but received it %@", | |
_notificationName, | |
[_messageTracker receivedCountPhrase]]; | |
} | |
- (BOOL)evaluate | |
{ | |
BOOL succeeded = [_messageTracker succeeded]; | |
[_messageTracker stopTracking]; | |
[subject removeObserver:self]; | |
return succeeded; | |
} | |
- (BOOL)shouldBeEvaluatedAtEndOfExample | |
{ | |
return YES; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment