Skip to content

Commit

Permalink
Extracted the creation of matchers and moved it into the matcher fact…
Browse files Browse the repository at this point in the history
…ory which feels like the

correct place for matchers to be created.

The matcher factory can then try and look up a user-defined matcher if it's unable to create a
regular matcher, before giving up.
  • Loading branch information
Luke Redpath committed Jun 17, 2011
1 parent 0b80c7b commit 5605114
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
17 changes: 6 additions & 11 deletions Kiwi/KWMatchVerifier.m
Expand Up @@ -125,19 +125,14 @@ - (void)forwardInvocation:(NSInvocation *)anInvocation {
#if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
@try {
#endif // #if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG

id matcher = [self.matcherFactory matcherFromInvocation:anInvocation subject:self.subject];

SEL selector = [anInvocation selector];
Class matcherClass = [self.matcherFactory matcherClassForSelector:selector subject:self.subject];

if (matcherClass == nil) {
KWFailure *failure = [KWFailure failureWithCallSite:self.callSite format:@"could not create matcher for -%@",
NSStringFromSelector(selector)];
[self.reporter reportFailure:failure];
return;
if (matcher == nil) {
KWFailure *failure = [KWFailure failureWithCallSite:self.callSite format:@"could not create matcher for -%@",
NSStringFromSelector(anInvocation.selector)];
[self.reporter reportFailure:failure];
}

// Create a matcher and pass it the message that came after 'should'.
id matcher = [[matcherClass alloc] initWithSubject:self.subject];
[anInvocation invokeWithTarget:matcher];

#if KW_TARGET_HAS_INVOCATION_EXCEPTION_BUG
Expand Down
13 changes: 11 additions & 2 deletions Kiwi/KWMatcherFactory.h
Expand Up @@ -7,6 +7,10 @@
#import "KiwiConfiguration.h"
#import "KWMatching.h"

@class KWFailure;
@class KWMatcher;
@class KWUserDefinedMatcherBuilder;

@interface KWMatcherFactory : NSObject {
@private
NSMutableArray *registeredMatcherClasses;
Expand All @@ -29,14 +33,19 @@
- (void)registerMatcherClass:(Class)aClass;
- (void)registerMatcherClassesWithNamespacePrefix:(NSString *)aNamespacePrefix;

#pragma mark -
#pragma mark Registering User Defined Matchers

//- (void)registerUserDefinedMatcherWithBuilder:(KWUserDefinedMatcherBuilder *)aBuilder;

#pragma mark -
#pragma mark Getting Method Signatures

- (NSMethodSignature *)methodSignatureForMatcherSelector:(SEL)aSelector;

#pragma mark -
#pragma mark Getting Matcher Classes
#pragma mark Getting Matchers

- (Class)matcherClassForSelector:(SEL)aSelector subject:(id)anObject;
- (KWMatcher *)matcherFromInvocation:(NSInvocation *)anInvocation subject:(id)subject;

@end
33 changes: 32 additions & 1 deletion Kiwi/KWMatcherFactory.m
Expand Up @@ -8,6 +8,12 @@
#import </usr/include/objc/runtime.h>
#import "KWMatching.h"
#import "KWStringUtilities.h"
#import "KWUserDefinedMatcher.h"
#import "KWMatchers.h"

@interface KWMatcherFactory()
- (Class)matcherClassForSelector:(SEL)aSelector subject:(id)anObject;
@end

@implementation KWMatcherFactory

Expand All @@ -24,6 +30,7 @@ - (id)init {
}

- (void)dealloc {
[registeredMatcherClasses release];
[matcherClassChains release];
[super dealloc];
}
Expand Down Expand Up @@ -92,6 +99,14 @@ - (void)registerMatcherClassesWithNamespacePrefix:(NSString *)aNamespacePrefix {
}
}

#pragma mark -
#pragma mark Registering User Defined Matchers

//- (void)registerUserDefinedMatcherWithBuilder:(KWUserDefinedMatcherBuilder *)aBuilder
//{
//
//}

#pragma mark -
#pragma mark Getting Method Signatures

Expand All @@ -106,7 +121,23 @@ - (NSMethodSignature *)methodSignatureForMatcherSelector:(SEL)aSelector {
}

#pragma mark -
#pragma mark Getting Matcher Classes
#pragma mark Getting Matchers

- (KWMatcher *)matcherFromInvocation:(NSInvocation *)anInvocation subject:(id)subject {
SEL selector = [anInvocation selector];

// try and match a built-in or registered matcher class
Class matcherClass = [self matcherClassForSelector:selector subject:subject];

if (matcherClass == nil) {
// see if we can match with a user-defined matcher instead
return [[KWMatchers matchers] matcherForSelector:selector subject:subject];
}
return [[[matcherClass alloc] initWithSubject:subject] autorelease];
}

#pragma mark -
#pragma mark Private methods

- (Class)matcherClassForSelector:(SEL)aSelector subject:(id)anObject {
NSArray *matcherClassChain = [matcherClassChains objectForKey:NSStringFromSelector(aSelector)];
Expand Down

0 comments on commit 5605114

Please sign in to comment.