forked from rentzsch/jrswizzle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClassicSwizzleTest.m
151 lines (125 loc) · 3.29 KB
/
ClassicSwizzleTest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#import "ClassicSwizzleTest.h"
#import <objc/objc-class.h>
// Lifted from http://www.cocoadev.com/index.pl?MethodSwizzling
void ClassicMethodSwizzle(Class aClass, SEL orig_sel, SEL alt_sel) {
Method orig_method = nil, alt_method = nil;
// First, look for the methods
orig_method = class_getInstanceMethod(aClass, orig_sel);
alt_method = class_getInstanceMethod(aClass, alt_sel);
// If both are found, swizzle them
if ((orig_method != nil) && (alt_method != nil))
{
char *temp1;
IMP temp2;
temp1 = orig_method->method_types;
orig_method->method_types = alt_method->method_types;
alt_method->method_types = temp1;
temp2 = orig_method->method_imp;
orig_method->method_imp = alt_method->method_imp;
alt_method->method_imp = temp2;
}
}
BOOL aFooCalled, bFooCalled, bAltFooCalled;
@interface A1 : NSObject {}
- (void)foo1;
@end
@implementation A1
- (void)foo1 {
aFooCalled = YES;
}
@end
@interface B1 : A1 {}
@end
@implementation B1
- (void)foo1 {
bFooCalled = YES;
}
@end
@interface B1 (altFoo1)
- (void)altFoo1;
@end
@implementation B1 (altFoo1)
- (void)altFoo1 {
bAltFooCalled = YES;
}
@end
@interface A2 : NSObject {}
- (void)foo2;
@end
@implementation A2
- (void)foo2 {
aFooCalled = YES;
}
@end
@interface B2 : A2 {}
@end
@implementation B2
@end
@interface B2 (altFoo2)
- (void)altFoo2;
@end
@implementation B2 (altFoo2)
- (void)altFoo2 {
bAltFooCalled = YES;
}
@end
@implementation ClassicSwizzleTest
- (void)testClassicSwizzleOfDirectMethod {
A1 *a = [[[A1 alloc] init] autorelease];
B1 *b = [[[B1 alloc] init] autorelease];
{
aFooCalled = bFooCalled = bAltFooCalled = NO;
[a foo1];
STAssertTrue(aFooCalled, nil);
STAssertFalse(bFooCalled, nil);
STAssertFalse(bAltFooCalled, nil);
aFooCalled = bFooCalled = bAltFooCalled = NO;
[b foo1];
STAssertFalse(aFooCalled, nil);
STAssertTrue(bFooCalled, nil);
STAssertFalse(bAltFooCalled, nil);
}
ClassicMethodSwizzle([B1 class], @selector(foo1), @selector(altFoo1));
{
aFooCalled = bFooCalled = bAltFooCalled = NO;
[a foo1];
STAssertTrue(aFooCalled, nil);
STAssertFalse(bFooCalled, nil);
STAssertFalse(bAltFooCalled, nil);
aFooCalled = bFooCalled = bAltFooCalled = NO;
[b foo1];
STAssertFalse(aFooCalled, nil);
STAssertFalse(bFooCalled, nil);
STAssertTrue(bAltFooCalled, nil);
}
}
- (void)testClassicSwizzleOfInheritedMethod {
A2 *a = [[[A2 alloc] init] autorelease];
B2 *b = [[[B2 alloc] init] autorelease];
{
aFooCalled = bFooCalled = bAltFooCalled = NO;
[a foo2];
STAssertTrue(aFooCalled, nil);
STAssertFalse(bFooCalled, nil);
STAssertFalse(bAltFooCalled, nil);
aFooCalled = bFooCalled = bAltFooCalled = NO;
[b foo2];
STAssertTrue(aFooCalled, nil);
STAssertFalse(bFooCalled, nil);
STAssertFalse(bAltFooCalled, nil);
}
ClassicMethodSwizzle([B2 class], @selector(foo2), @selector(altFoo2));
{
aFooCalled = bFooCalled = bAltFooCalled = NO;
[a foo2];
STAssertFalse(aFooCalled, nil); // KNOWN INCORRECT BEHAVIOR: [a foo2] resulted in calling B2(altFoo2)'s -altFoo2!
STAssertFalse(bFooCalled, nil);
STAssertTrue(bAltFooCalled, nil);
aFooCalled = bFooCalled = bAltFooCalled = NO;
[b foo2];
STAssertFalse(aFooCalled, nil);
STAssertFalse(bFooCalled, nil);
STAssertTrue(bAltFooCalled, nil);
}
}
@end