Skip to content

Commit

Permalink
Added reverting changed methods on 'funtime' object dealloc
Browse files Browse the repository at this point in the history
  • Loading branch information
belkevich committed Oct 10, 2013
1 parent 7c4884a commit d21c7b3
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 9 deletions.
6 changes: 5 additions & 1 deletion Classes/OCFMethod.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ - (void)changeImplementationWithBlock:(id)block

- (void)revertImplementation
{
method_setImplementation(method, defaultImplementation);
if (defaultImplementation)
{
method_setImplementation(method, defaultImplementation);
defaultImplementation = NULL;
}
}

@end
5 changes: 5 additions & 0 deletions Classes/OCFuntime.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ - (id)init
return self;
}

- (void)dealloc
{
[self revertAll];
}

#pragma mark - public

- (void)changeClass:(Class)theClass instanceMethod:(SEL)method implementation:(id)block
Expand Down
62 changes: 54 additions & 8 deletions Spec/OCFuntimeSpec/OCFuntimeSpec.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

SPEC_BEGIN(OCFuntimeSpec)

describe(@"OCFuntime", ^{
__block OCFuntime *funtime = nil;
__block OCFMock *mock = nil;
__block OCFuntime *funtime;
__block OCFMock *mock;

describe(@"OCFuntime with changed method", ^{

beforeEach(^
{
Expand Down Expand Up @@ -82,8 +83,8 @@
it(@"should call newly changed instance method if it was changed again", ^
{
[funtime changeClass:[OCFMock class] instanceMethod:@selector(funInstanceMethod)
implementation:^{
// still returns NO
implementation:^
{
NSLog(@"One more time changed FUN instance method");
return 2;
}];
Expand All @@ -94,7 +95,6 @@
{
[funtime changeClass:[OCFMock class] classMethod:@selector(funClassMethod)
implementation:^{
// still returns NO
NSLog(@"One more time changed FUN class method");
return 2;
}];
Expand All @@ -104,7 +104,8 @@
it(@"should revert to default instance method, not previous setted method", ^
{
[funtime changeClass:[OCFMock class] instanceMethod:@selector(funInstanceMethod)
implementation:^{
implementation:^
{
// still returns NO
NSLog(@"One more time changed FUN instance method");
return 2;
Expand All @@ -116,14 +117,59 @@
it(@"should revert to default class method, not previous setted method", ^
{
[funtime changeClass:[OCFMock class] classMethod:@selector(funClassMethod)
implementation:^{
implementation:^
{
// still returns NO
NSLog(@"One more time changed FUN class method");
return 2;
}];
[funtime revertClass:[OCFMock class]];
[OCFMock funClassMethod] should equal(0);
});
});

describe(@"OCFuntime memory management", ^
{
it(@"should call default instance method if 'funtime' instance deallocated", ^
{
funtime = [[OCFuntime alloc] init];
[funtime changeClass:[OCFMock class] instanceMethod:@selector(funInstanceMethod)
implementation:^
{
return 1;
}];
mock = [[OCFMock alloc] init];
[mock funInstanceMethod] should equal(1);
[funtime release];
[mock funInstanceMethod] should equal(0);
[mock release];
});

it(@"should call default class method if 'funtime' instance deallocated", ^
{
funtime = [[OCFuntime alloc] init];
[funtime changeClass:[OCFMock class] classMethod:@selector(funClassMethod)
implementation:^
{
return 1;
}];
[OCFMock funClassMethod] should equal(1);
[funtime release];
[OCFMock funClassMethod] should equal(0);
});
});

describe(@"OCFuntime protection", ^{

beforeEach(^
{
funtime = [[OCFuntime alloc] init];
});

afterEach(^
{
[funtime release];
});

it(@"should throw exception if instance method doesn't exist", ^
{
Expand Down

0 comments on commit d21c7b3

Please sign in to comment.