Skip to content

Instantly share code, notes, and snippets.

@pk
Created February 16, 2011 17:17
Show Gist options
  • Save pk/829750 to your computer and use it in GitHub Desktop.
Save pk/829750 to your computer and use it in GitHub Desktop.
OCHamcrest RespondsTo matcher: assertThat(anObject, respondsTo(@"someMethod:")); Instead of something like: assertTrue([anObject respondsToSelector:@selector(someMethod:)]);
//
// PKRespondsToMatcher.h
//
// Created by Pavel Kunc on 11/02/2011.
// Copyright 2011 Pavel Kunc. All rights reserved.
//
#import <OCHamcrestIOS/HCBaseMatcher.h>
#import <OCHamcrestIOS/HCDescription.h>
#import <objc/objc-api.h>
@interface PKRespondsToMatcher : HCBaseMatcher {
NSString *selectorString_;
}
+ (NXRRespondsToMatcher *)respondsToMatcher:(NSString *)aSelectorString;
- (id)initWithString:(NSString *)aSelectorString;
@end
OBJC_EXPORT id<HCMatcher> PK_respondsTo(NSString *aString);
#ifdef HC_SHORTHAND
#define respondsTo PK_respondsTo
#endif
//
// PKRespondsToMatcher.m
//
// Created by Pavel Kunc on 11/02/2011.
// Copyright 2011 Pavel Kunc. All rights reserved.
//
#import "PKRespondsToMatcher.h"
@implementation PKRespondsToMatcher
+ (PKRespondsToMatcher *)respondsToMatcher:(NSString *)aSelectorString {
return [[[self alloc] initWithString:aSelectorString] autorelease];
}
- (id) initWithString:(NSString *)aSelectorString {
if ((self = [super init])) {
selectorString_ = [aSelectorString copy];
}
return self;
}
- (void)dealloc {
[selectorString_ release];
[super dealloc];
}
- (BOOL) matches:(id)item {
return [item respondsToSelector:NSSelectorFromString(selectorString_)];
}
- (void) describeMismatchOf:(id)item to:(id<HCDescription>)mismatchDescription {
[mismatchDescription appendText:@"it doesn't."];
}
- (void)describeTo:(id<HCDescription>)description {
[[description appendText:@"respond to "] appendValue:selectorString_];
}
@end
id<HCMatcher> PK_respondsTo(NSString *aString) {
return [PKRespondsToMatcher respondsToMatcher:aString];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment