Permalink
Browse files

Adding stub:withBlock: method to Mock objects. It let's you implement…

… any method on the Mock object with a block.
  • Loading branch information...
roustem committed Jul 13, 2012
1 parent 3a29914 commit a39aea7ba53f5a4bc3c9baf809556e98dd6224a5
Showing with 428 additions and 0 deletions.
  1. +12 −0 Kiwi.xcodeproj/project.pbxproj
  2. +1 −0 Kiwi/KWMock.h
  3. +12 −0 Kiwi/KWMock.m
  4. +3 −0 Kiwi/KWStub.h
  5. +29 −0 Kiwi/KWStub.m
  6. +34 −0 Kiwi/NSInvocation+OCMAdditions.h
  7. +337 −0 Kiwi/NSInvocation+OCMAdditions.m
@@ -388,6 +388,10 @@
A3EB8065131EA574001860F5 /* KWHamrestMatchingAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = A352EA0D12EDC6F20049C691 /* KWHamrestMatchingAdditions.m */; };
A3FABFAF13CBB187002003F7 /* KiwiBlockMacros.h in Headers */ = {isa = PBXBuildFile; fileRef = A3FABFAE13CBB187002003F7 /* KiwiBlockMacros.h */; settings = {ATTRIBUTES = (Public, ); }; };
ADBF4B031511C6B300784E9E /* KWAny.h in Headers */ = {isa = PBXBuildFile; fileRef = F5FC83B611B100B100BF98A7 /* KWAny.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ B36B276415AFD5120056C31D /* NSInvocation+OCMAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = B36B276215AFD5120056C31D /* NSInvocation+OCMAdditions.h */; };
+ B36B276515AFD5120056C31D /* NSInvocation+OCMAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = B36B276215AFD5120056C31D /* NSInvocation+OCMAdditions.h */; };
+ B36B276615AFD5120056C31D /* NSInvocation+OCMAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = B36B276315AFD5120056C31D /* NSInvocation+OCMAdditions.m */; };
+ B36B276715AFD5120056C31D /* NSInvocation+OCMAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = B36B276315AFD5120056C31D /* NSInvocation+OCMAdditions.m */; };
C922D1DA1580438300995B43 /* KWStringContainsMatcher.m in Sources */ = {isa = PBXBuildFile; fileRef = C922D1D81580438300995B43 /* KWStringContainsMatcher.m */; };
C922D1DD158045FC00995B43 /* KWStringContainsMatcherTest.m in Sources */ = {isa = PBXBuildFile; fileRef = C922D1DC158045FC00995B43 /* KWStringContainsMatcherTest.m */; };
C922D1DE1580484300995B43 /* KWStringContainsMatcher.h in Headers */ = {isa = PBXBuildFile; fileRef = C922D1D71580438300995B43 /* KWStringContainsMatcher.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -668,6 +672,8 @@
A3CB75E0144C3479002D1F7A /* KWExampleSuite.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KWExampleSuite.h; sourceTree = "<group>"; };
A3CB75E1144C3479002D1F7A /* KWExampleSuite.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWExampleSuite.m; sourceTree = "<group>"; };
A3FABFAE13CBB187002003F7 /* KiwiBlockMacros.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KiwiBlockMacros.h; sourceTree = "<group>"; };
+ B36B276215AFD5120056C31D /* NSInvocation+OCMAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSInvocation+OCMAdditions.h"; sourceTree = "<group>"; };
+ B36B276315AFD5120056C31D /* NSInvocation+OCMAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSInvocation+OCMAdditions.m"; sourceTree = "<group>"; };
C922D1D71580438300995B43 /* KWStringContainsMatcher.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = KWStringContainsMatcher.h; sourceTree = "<group>"; };
C922D1D81580438300995B43 /* KWStringContainsMatcher.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWStringContainsMatcher.m; sourceTree = "<group>"; };
C922D1DC158045FC00995B43 /* KWStringContainsMatcherTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = KWStringContainsMatcherTest.m; sourceTree = "<group>"; };
@@ -1180,6 +1186,8 @@
F5BDB7A511C0C04200DD3474 /* KWWorkarounds.m */,
F52CD2A7115E1E760038A6A2 /* NSInvocation+KiwiAdditions.h */,
F52CD2A8115E1E760038A6A2 /* NSInvocation+KiwiAdditions.m */,
+ B36B276215AFD5120056C31D /* NSInvocation+OCMAdditions.h */,
+ B36B276315AFD5120056C31D /* NSInvocation+OCMAdditions.m */,
F5015B6D1158398E002E9A98 /* NSMethodSignature+KiwiAdditions.h */,
F5015B6E1158398E002E9A98 /* NSMethodSignature+KiwiAdditions.m */,
F5B3FDC41178B15E00ECF9E8 /* NSNumber+KiwiAdditions.h */,
@@ -1420,6 +1428,7 @@
832C837A157263B300F160D5 /* KWCaptureSpy.h in Headers */,
C922D1EA15805AEB00995B43 /* KWStringPrefixMatcher.h in Headers */,
C922D1EB15805AF000995B43 /* KWStringContainsMatcher.h in Headers */,
+ B36B276515AFD5120056C31D /* NSInvocation+OCMAdditions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1515,6 +1524,7 @@
4B45D6BD1548765800793B1E /* KWCaptureSpy.h in Headers */,
C922D1DE1580484300995B43 /* KWStringContainsMatcher.h in Headers */,
C922D1DF1580487700995B43 /* KWStringPrefixMatcher.h in Headers */,
+ B36B276415AFD5120056C31D /* NSInvocation+OCMAdditions.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1782,6 +1792,7 @@
832C83C2157263B300F160D5 /* KWCaptureSpy.m in Sources */,
C922D1E915805ADC00995B43 /* KWStringPrefixMatcher.m in Sources */,
C922D1EC15805AF600995B43 /* KWStringContainsMatcher.m in Sources */,
+ B36B276715AFD5120056C31D /* NSInvocation+OCMAdditions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1880,6 +1891,7 @@
4B45D6BE1548765800793B1E /* KWCaptureSpy.m in Sources */,
C922D1DA1580438300995B43 /* KWStringContainsMatcher.m in Sources */,
C922D1E715805ADB00995B43 /* KWStringPrefixMatcher.m in Sources */,
+ B36B276615AFD5120056C31D /* NSInvocation+OCMAdditions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -59,6 +59,7 @@
#pragma mark Stubbing Methods
- (void)stub:(SEL)aSelector;
+- (void)stub:(SEL)aSelector withBlock:(id (^)(NSArray *params))block;
- (void)stub:(SEL)aSelector withArguments:(id)firstArgument, ...;
- (void)stub:(SEL)aSelector andReturn:(id)aValue;
- (void)stub:(SEL)aSelector andReturn:(id)aValue withArguments:(id)firstArgument, ...;
View
@@ -217,6 +217,11 @@ - (void)stub:(SEL)aSelector {
[self stubMessagePattern:messagePattern andReturn:nil];
}
+- (void)stub:(SEL)aSelector withBlock:(id (^)(NSArray *params))block {
+ KWMessagePattern *messagePattern = [KWMessagePattern messagePatternWithSelector:aSelector];
+ [self stubMessagePattern:messagePattern withBlock:block];
+}
+
- (void)stub:(SEL)aSelector withArguments:(id)firstArgument, ... {
va_list argumentList;
va_start(argumentList, firstArgument);
@@ -259,6 +264,13 @@ - (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern andReturn:(id)aVa
[self.stubs addObject:stub];
}
+- (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern withBlock:(id (^)(NSArray *params))block {
+ [self expectMessagePattern:aMessagePattern];
+ [self removeStubWithMessagePattern:aMessagePattern];
+ KWStub *stub = [KWStub stubWithMessagePattern:aMessagePattern block:block];
+ [self.stubs addObject:stub];
+}
+
- (void)stubMessagePattern:(KWMessagePattern *)aMessagePattern andReturn:(id)aValue times:(id)times afterThatReturn:(id)aSecondValue {
[self expectMessagePattern:aMessagePattern];
[self removeStubWithMessagePattern:aMessagePattern];
View
@@ -15,17 +15,20 @@
id returnValueTimes;
int returnedValueTimes;
id secondValue;
+ id (^block)(NSArray *params);
}
#pragma mark -
#pragma mark Initializing
- (id)initWithMessagePattern:(KWMessagePattern *)aMessagePattern;
- (id)initWithMessagePattern:(KWMessagePattern *)aMessagePattern value:(id)aValue;
+- (id)initWithMessagePattern:(KWMessagePattern *)aMessagePattern block:(id (^)(NSArray *params))aBlock;
- (id)initWithMessagePattern:(KWMessagePattern *)aMessagePattern value:(id)aValue times:(id)times afterThatReturn:(id)aSecondValue;
+ (id)stubWithMessagePattern:(KWMessagePattern *)aMessagePattern;
+ (id)stubWithMessagePattern:(KWMessagePattern *)aMessagePattern value:(id)aValue;
++ (id)stubWithMessagePattern:(KWMessagePattern *)aMessagePattern block:(id (^)(NSArray *params))aBlock;
+ (id)stubWithMessagePattern:(KWMessagePattern *)aMessagePattern value:(id)aValue times:(id)times afterThatReturn:(id)aSecondValue;
#pragma mark -
View
@@ -10,6 +10,8 @@
#import "KWStringUtilities.h"
#import "KWValue.h"
+#import "NSInvocation+OCMAdditions.h"
+
@implementation KWStub
#pragma mark -
@@ -28,6 +30,15 @@ - (id)initWithMessagePattern:(KWMessagePattern *)aMessagePattern value:(id)aValu
return self;
}
+- (id)initWithMessagePattern:(KWMessagePattern *)aMessagePattern block:(id (^)(NSArray *params))aBlock {
+ if ((self = [super init])) {
+ messagePattern = [aMessagePattern retain];
+ block = [aBlock copy];
+ }
+
+ return self;
+}
+
- (id)initWithMessagePattern:(KWMessagePattern *)aMessagePattern value:(id)aValue times:(id)times afterThatReturn:(id)aSecondValue {
if ((self = [super init])) {
messagePattern = [aMessagePattern retain];
@@ -47,6 +58,10 @@ + (id)stubWithMessagePattern:(KWMessagePattern *)aMessagePattern value:(id)aValu
return [[[self alloc] initWithMessagePattern:aMessagePattern value:aValue] autorelease];
}
++ (id)stubWithMessagePattern:(KWMessagePattern *)aMessagePattern block:(id (^)(NSArray *params))aBlock {
+ return [[[self alloc] initWithMessagePattern:aMessagePattern block:aBlock] autorelease];
+}
+
+ (id)stubWithMessagePattern:(KWMessagePattern *)aMessagePattern value:(id)aValue times:(id)times afterThatReturn:(id)aSecondValue {
return [[[self alloc] initWithMessagePattern:aMessagePattern value:aValue times:times afterThatReturn:aSecondValue] autorelease];
}
@@ -56,6 +71,7 @@ - (void)dealloc {
[value release];
[returnValueTimes release];
[secondValue release];
+ [block release];
[super dealloc];
}
@@ -160,6 +176,19 @@ - (void)writeObjectValueToInvocationReturnValue:(NSInvocation *)anInvocation {
- (BOOL)processInvocation:(NSInvocation *)anInvocation {
if (![self.messagePattern matchesInvocation:anInvocation])
return NO;
+
+ if (block) {
+ NSUInteger numberOfArguments = [[anInvocation methodSignature] numberOfArguments];
+ NSMutableArray *params = [NSMutableArray arrayWithCapacity:(numberOfArguments-2)];
+ for (NSUInteger i = 2; i < numberOfArguments; ++i) {
+ id param = [anInvocation getArgumentAtIndexAsObject:i];
+ [params addObject:param];
+ }
+
+ value = block(params);
+
+ [params removeAllObjects]; // We don't want these objects to be in autorelease pool
+ }
if (self.value == nil)
[self writeZerosToInvocationReturnValue:anInvocation];
@@ -0,0 +1,34 @@
+//---------------------------------------------------------------------------------------
+// $Id$
+// Copyright (c) 2006-2009 by Mulle Kybernetik. See License file for details.
+//---------------------------------------------------------------------------------------
+
+#import <Foundation/Foundation.h>
+
+@interface NSInvocation(OCMAdditions)
+
+- (id)getArgumentAtIndexAsObject:(int)argIndex;
+
+- (NSString *)invocationDescription;
+
+- (NSString *)argumentDescriptionAtIndex:(int)argIndex;
+
+- (NSString *)objectDescriptionAtIndex:(int)anInt;
+- (NSString *)charDescriptionAtIndex:(int)anInt;
+- (NSString *)unsignedCharDescriptionAtIndex:(int)anInt;
+- (NSString *)intDescriptionAtIndex:(int)anInt;
+- (NSString *)unsignedIntDescriptionAtIndex:(int)anInt;
+- (NSString *)shortDescriptionAtIndex:(int)anInt;
+- (NSString *)unsignedShortDescriptionAtIndex:(int)anInt;
+- (NSString *)longDescriptionAtIndex:(int)anInt;
+- (NSString *)unsignedLongDescriptionAtIndex:(int)anInt;
+- (NSString *)longLongDescriptionAtIndex:(int)anInt;
+- (NSString *)unsignedLongLongDescriptionAtIndex:(int)anInt;
+- (NSString *)doubleDescriptionAtIndex:(int)anInt;
+- (NSString *)floatDescriptionAtIndex:(int)anInt;
+- (NSString *)structDescriptionAtIndex:(int)anInt;
+- (NSString *)pointerDescriptionAtIndex:(int)anInt;
+- (NSString *)cStringDescriptionAtIndex:(int)anInt;
+- (NSString *)selectorDescriptionAtIndex:(int)anInt;
+
+@end
Oops, something went wrong.

0 comments on commit a39aea7

Please sign in to comment.