-
Notifications
You must be signed in to change notification settings - Fork 37
/
HookClassMethodsOCTests.m
237 lines (206 loc) · 7.65 KB
/
HookClassMethodsOCTests.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// HookClassMethodsOCTests.m
// SwiftHookTests
//
// Created by Yanni Wang on 28/6/21.
// Copyright © 2021 Yanni. All rights reserved.
//
#import <XCTest/XCTest.h>
@import SwiftHook;
BOOL run_HookClassMethodsOCTests = NO;
OCToken *token_HookClassMethodsOCTests = nil;
@interface MyObject_HookClassMethodsOCTests : NSObject
@end
@implementation MyObject_HookClassMethodsOCTests
+ (void)myMethod
{
run_HookClassMethodsOCTests = YES;
}
+ (NSInteger)myMethodWithNumber:(NSInteger)number url:(NSURL *)URL
{
run_HookClassMethodsOCTests = YES;
return number * 2;
}
@end
@interface HookClassMethodsOCTests : XCTestCase
@end
@implementation HookClassMethodsOCTests
- (void)setUp
{
[super setUp];
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertNil(token_HookClassMethodsOCTests);
}
- (void)tearDown
{
[super tearDown];
run_HookClassMethodsOCTests = NO;
[token_HookClassMethodsOCTests cancelHook];
token_HookClassMethodsOCTests = nil;
}
// MARK: - empty closure
// before
- (void)test_before
{
__block BOOL run = NO;
XCTAssertFalse(run);
NSError *error = nil;
token_HookClassMethodsOCTests = [MyObject_HookClassMethodsOCTests sh_hookClassMethodBeforeSelector:@selector(myMethod) error:&error closure:^{
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
run = YES;
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertTrue(run);
}];
XCTAssertNil(error);
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
[MyObject_HookClassMethodsOCTests myMethod];
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertTrue(run);
}
// after
- (void)test_after
{
__block BOOL run = NO;
XCTAssertFalse(run);
NSError *error = nil;
token_HookClassMethodsOCTests = [MyObject_HookClassMethodsOCTests sh_hookClassMethodAfterSelector:@selector(myMethod) error:&error closure:^{
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
run = YES;
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertTrue(run);
}];
XCTAssertNil(error);
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
[MyObject_HookClassMethodsOCTests myMethod];
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertTrue(run);
}
// MARK: - self and selector closure
// before
- (void)test_before_obj_sel
{
__block BOOL run = NO;
XCTAssertFalse(run);
NSError *error = nil;
token_HookClassMethodsOCTests = [MyObject_HookClassMethodsOCTests sh_hookClassMethodBeforeSelector:@selector(myMethod) error:&error closureObjSel:^(Class _Nonnull __unsafe_unretained class, SEL _Nonnull sel) {
XCTAssertTrue(MyObject_HookClassMethodsOCTests.class == class);
XCTAssertEqual(sel, @selector(myMethod));
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
run = YES;
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertTrue(run);
}];
XCTAssertNil(error);
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
[MyObject_HookClassMethodsOCTests myMethod];
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertTrue(run);
}
// after
- (void)test_after_obj_sel
{
__block BOOL run = NO;
XCTAssertFalse(run);
NSError *error = nil;
token_HookClassMethodsOCTests = [MyObject_HookClassMethodsOCTests sh_hookClassMethodAfterSelector:@selector(myMethod) error:&error closureObjSel:^(Class _Nonnull __unsafe_unretained class, SEL _Nonnull sel) {
XCTAssertTrue(MyObject_HookClassMethodsOCTests.class == class);
XCTAssertEqual(sel, @selector(myMethod));
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
run = YES;
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertTrue(run);
}];
XCTAssertNil(error);
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
[MyObject_HookClassMethodsOCTests myMethod];
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertTrue(run);
}
// MARK: - custom closure
// before
- (void)test_before_custom
{
__block BOOL run = NO;
XCTAssertFalse(run);
NSError *error = nil;
token_HookClassMethodsOCTests = [MyObject_HookClassMethodsOCTests sh_hookClassMethodBeforeSelector:@selector(myMethodWithNumber:url:) closure:^(NSObject * _Nonnull __unsafe_unretained class, SEL _Nonnull sel) {
XCTAssertTrue(MyObject_HookClassMethodsOCTests.class == (Class)class);
XCTAssertEqual(sel, @selector(myMethodWithNumber:url:));
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
run = YES;
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertTrue(run);
} error:&error];
XCTAssertNil(error);
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
NSInteger result = [MyObject_HookClassMethodsOCTests myMethodWithNumber:77 url:[NSURL URLWithString:@"https://google.com"]];
XCTAssertEqual(result, 154);
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertTrue(run);
}
// after
- (void)test_after_custom
{
__block BOOL run = NO;
XCTAssertFalse(run);
NSError *error = nil;
token_HookClassMethodsOCTests = [MyObject_HookClassMethodsOCTests sh_hookClassMethodAfterSelector:@selector(myMethodWithNumber:url:) closure:^(NSObject * _Nonnull __unsafe_unretained class, SEL _Nonnull sel, NSInteger number, NSURL *url) {
XCTAssertTrue(MyObject_HookClassMethodsOCTests.class == (Class)class);
XCTAssertEqual(sel, @selector(myMethodWithNumber:url:));
XCTAssertEqual(number, 77);
XCTAssertEqualObjects(url, [NSURL URLWithString:@"https://google.com"]);
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
run = YES;
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertTrue(run);
} error:&error];
XCTAssertNil(error);
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
NSInteger result = [MyObject_HookClassMethodsOCTests myMethodWithNumber:77 url:[NSURL URLWithString:@"https://google.com"]];
XCTAssertEqual(result, 154);
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertTrue(run);
}
// instead
- (void)test_instead_custom
{
__block BOOL run = NO;
XCTAssertFalse(run);
NSError *error = nil;
token_HookClassMethodsOCTests = [MyObject_HookClassMethodsOCTests sh_hookClassMethodInsteadWithSelector:@selector(myMethodWithNumber:url:) closure:^NSInteger (NSInteger(^original)(NSObject * _Nonnull __unsafe_unretained class, SEL _Nonnull sel, NSInteger number, NSURL *url), NSObject * _Nonnull __unsafe_unretained class, SEL _Nonnull sel, NSInteger number, NSURL *url) {
XCTAssertTrue(MyObject_HookClassMethodsOCTests.class == (Class)class);
XCTAssertEqual(sel, @selector(myMethodWithNumber:url:));
XCTAssertEqual(number, 77);
XCTAssertEqualObjects(url, [NSURL URLWithString:@"https://google.com"]);
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
NSInteger result = original(class, sel, 11, url);
XCTAssertEqual(result, 22);
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
run = YES;
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertTrue(run);
return 999;
} error:&error];
XCTAssertNil(error);
XCTAssertFalse(run_HookClassMethodsOCTests);
XCTAssertFalse(run);
NSInteger result = [MyObject_HookClassMethodsOCTests myMethodWithNumber:77 url:[NSURL URLWithString:@"https://google.com"]];
XCTAssertEqual(result, 999);
XCTAssertTrue(run_HookClassMethodsOCTests);
XCTAssertTrue(run);
}
@end