Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow KWCaptureSpy to capture nil block arguments #446

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Allow KWCaptureSpy to capture nil block arguments #446

wants to merge 1 commit into from

Conversation

ricketson
Copy link

When trying to capture a block argument that is nil, KWCaptureSpy throws an exception and claims that the argument was never captured.

I believe that the error is due to this method in KWCaptureSpy.m:

- (void)object:(id)anObject didReceiveInvocation:(NSInvocation *)anInvocation {
    if (!_argument) {
        NSMethodSignature *signature = [anInvocation methodSignature];
        const char *objCType = [signature messageArgumentTypeAtIndex:_argumentIndex];
        if (KWObjCTypeIsObject(objCType) || KWObjCTypeIsClass(objCType)) {
            void* argumentBuffer = NULL;
            [anInvocation getMessageArgument:&argumentBuffer atIndex:_argumentIndex];
            id argument = (__bridge id)argumentBuffer;
            if (KWObjCTypeIsBlock(objCType)) {
                _argument = [argument copy];
            } else {
                if(argument == nil) {
                    _argument = [KWNull null];
                } else {
                    _argument = argument;
                }
            }
        } else {
            NSData *data = [anInvocation messageArgumentDataAtIndex:_argumentIndex];
            _argument = [KWValue valueWithBytes:[data bytes] objCType:objCType];
        }
    }
}

Specifically,

if (KWObjCTypeIsBlock(objCType)) {
    _argument = [argument copy];
} else {
    if(argument == nil) {
        _argument = [KWNull null];
    } else {
        _argument = argument;
    }
}

should be...

if (argument == nil) {
    _argument = [KWNull null];
} else if (KWObjCTypeIsBlock(objCType)) {
    _argument = [argument copy];
} else {
    _argument = argument;
}

Since the internal system that KWCaptureSpy uses to tell whether an argument is captured or not is whether it is equal to nil, not setting _argument to [KWNull null] when argument is a block and nil causes it to think that the argument was never captured.

@modocache
Copy link
Member

Looks solid, @mricketson! Could you please add a unit test that demonstrates this fixes the problem (i.e.: one that passes with this change but fails without it)?

@sozorogami
Copy link

@modocache How's that? If you prefer, I could leave the old test alone and implement a stand-alone test for nil blocks.

@modocache
Copy link
Member

@sozorogami Could you add your commit in #555 on top of @mricketson's commit? CI fails in #555 because your test fails without the implementation code in this PR's commit, 74481b2. You can pull down @mricketson's branch using:

git checkout -b mricketson-master master # do all this on a new branch
git pull git@github.com:mricketson/Kiwi.git master # pull @mricketson's work down onto that new branch

Then you can put your commit on top of it using:

git checkout mricketson-master # make sure you're on the new branch
git cherry-pick 5fac65b # apply your commit from #555

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants