Skip to content

Instantly share code, notes, and snippets.

@esmasui
Created May 2, 2012 18:33
Show Gist options
  • Select an option

  • Save esmasui/2579031 to your computer and use it in GitHub Desktop.

Select an option

Save esmasui/2579031 to your computer and use it in GitHub Desktop.
//
// SwizzlingTest.m
// HelloUnitTest
//
// Created by Sosuke Masui on 12/05/03.
// Copyright (c) 2012年 うぴか Inc. All rights reserved.
//
#import "SwizzlingTest.h"
#import </usr/include/objc/objc-class.h>
@interface Swizzlingee : NSObject
- (NSString *) hoge;
- (NSString *) piyo;
- (NSString *) piyoInvoker;
@end
@implementation Swizzlingee
- (NSString *)hoge
{
static NSString *s = @"hoge";
return s;
}
- (NSString *)piyo
{
static NSString *s = @"piyo";
return s;
}
- (NSString *)piyoInvoker
{
NSLog(@"piyo was invoked");
return [self piyo];
}
@end
@implementation SwizzlingTest
- (void)testThatSwizzled
{
//Do swizzle
Method hogeMethod = class_getInstanceMethod([Swizzlingee class], @selector(hoge));
Method piyoInvokerMethod = class_getInstanceMethod([Swizzlingee class], @selector(piyoInvoker));
method_exchangeImplementations(hogeMethod, piyoInvokerMethod);
//Invoke the method
Swizzlingee *underTest = [[Swizzlingee alloc] init];
NSString *s = [underTest hoge];
STAssertEqualObjects(s, @"piyo", @"hoge method should returns piyo after swizzled");
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment