Skip to content
This repository has been archived by the owner on Aug 17, 2019. It is now read-only.

Commit

Permalink
Allow capturing of nil arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
eraserhd authored and Marin Usalj committed Oct 10, 2012
1 parent 4b6860b commit c1a1a0b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions Kiwi/KWCaptureSpy.h
Expand Up @@ -3,6 +3,7 @@
#import "KWMessageSpying.h"

@interface KWCaptureSpy : NSObject<KWMessageSpying> {
BOOL _argumentCaptured;
id _argument;
NSUInteger _argumentIndex;
}
Expand Down
6 changes: 4 additions & 2 deletions Kiwi/KWCaptureSpy.m
Expand Up @@ -10,19 +10,20 @@ @implementation KWCaptureSpy
- (id)initWithArgumentIndex:(NSUInteger)index {
if ((self = [super init])) {
_argumentIndex = index;
_argumentCaptured = NO;
}
return self;
}

- (id)argument {
if (!_argument) {
if (!_argumentCaptured) {
@throw [NSException exceptionWithName:NSInternalInconsistencyException reason:@"Argument requested has yet to be captured." userInfo:nil];
}
return [[_argument retain] autorelease];
}

- (void)object:(id)anObject didReceiveInvocation:(NSInvocation *)anInvocation {
if (!_argument) {
if (!_argumentCaptured) {
NSMethodSignature *signature = [anInvocation methodSignature];
const char *objCType = [signature messageArgumentTypeAtIndex:_argumentIndex];
if (KWObjCTypeIsObject(objCType)) {
Expand All @@ -37,6 +38,7 @@ - (void)object:(id)anObject didReceiveInvocation:(NSInvocation *)anInvocation {
NSData *data = [anInvocation messageArgumentDataAtIndex:_argumentIndex];
_argument = [[KWValue valueWithBytes:[data bytes] objCType:objCType] retain];
}
_argumentCaptured = YES;
}
}

Expand Down
9 changes: 9 additions & 0 deletions Tests/KWCaptureTest.m
Expand Up @@ -52,6 +52,15 @@ - (void)testShouldBeAbleToCaptureValues {
STAssertEqualObjects(spy.argument, [KWValue valueWithDouble:2], @"Captured argument should be equal to '2'");
}

- (void)testShouldBeAbleToCaptureNils {
id robotMock = [KWMock nullMockForClass:[Robot class]];
KWCaptureSpy *spy = [robotMock captureArgument:@selector(speak:afterDelay:whenDone:) atIndex:0];

[robotMock speak:nil afterDelay:2 whenDone:^{}];

STAssertNil(spy.argument, @"Captured argument should be nil");
}

- (void)testShouldRaiseAnExceptionIfArgumentHasNotBeenCaptured {
id robotMock = [KWMock nullMockForClass:[Robot class]];
KWCaptureSpy *spy = [robotMock captureArgument:@selector(speak:afterDelay:whenDone:) atIndex:1];
Expand Down

0 comments on commit c1a1a0b

Please sign in to comment.