Created
May 2, 2012 18:22
-
-
Save esmasui/2578933 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "SwizzlingTest.h" | |
#import </usr/include/objc/objc-class.h> | |
@interface Swizzlingee : NSObject | |
- (NSString *) hoge; | |
- (NSString *) piyo; | |
@end | |
@implementation Swizzlingee | |
- (NSString *)hoge | |
{ | |
static NSString *s = @"hoge"; | |
return s; | |
} | |
- (NSString *)piyo | |
{ | |
static NSString *s = @"piyo"; | |
return s; | |
} | |
@end | |
@implementation SwizzlingTest | |
- (void)testThatSwizzled | |
{ | |
//Do swizzle | |
Method hogeMethod = class_getInstanceMethod([Swizzlingee class], @selector(hoge)); | |
Method piyoMethod = class_getInstanceMethod([Swizzlingee class], @selector(piyo)); | |
method_exchangeImplementations(hogeMethod, piyoMethod); | |
//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