Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extracted the creation of matchers and moved it into the matcher fact…

…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...
commit 560511491df0ca3964425cef6ad3708da0204153 1 parent 0b80c7b
Luke Redpath authored
View
17 Kiwi/KWMatchVerifier.m
@@ -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
View
13 Kiwi/KWMatcherFactory.h
@@ -7,6 +7,10 @@
#import "KiwiConfiguration.h"
#import "KWMatching.h"
+@class KWFailure;
+@class KWMatcher;
+@class KWUserDefinedMatcherBuilder;
+
@interface KWMatcherFactory : NSObject {
@private
NSMutableArray *registeredMatcherClasses;
@@ -30,13 +34,18 @@
- (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
View
33 Kiwi/KWMatcherFactory.m
@@ -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
@@ -24,6 +30,7 @@ - (id)init {
}
- (void)dealloc {
+ [registeredMatcherClasses release];
[matcherClassChains release];
[super dealloc];
}
@@ -93,6 +100,14 @@ - (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 {
@@ -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)];
Please sign in to comment.
Something went wrong with that request. Please try again.