-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTestYTXModuleSpec.m
More file actions
335 lines (264 loc) · 9.35 KB
/
TestYTXModuleSpec.m
File metadata and controls
335 lines (264 loc) · 9.35 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//
// TestYTXModuleSpec.m
// TestYTXModuleSpec
//
// Created by caojun on 08/12/2016.
// Copyright (c) 2016 caojun. All rights reserved.
//
// https://github.com/kiwi-bdd/Kiwi
#import <YTXModule/YTXModule.h>
static BOOL isLoad = NO;
static BOOL applicationLifCycle1 = NO;
static BOOL applicationLifCycle2 = NO;
static BOOL applicationLifCycle3 = NO;
static BOOL isCallRouterMacro1 = NO;
static BOOL isCallRouterMacro2 = NO;
static BOOL isCallRouterMacro3 = NO;
static BOOL isCallRouterObjectMacro1 = NO;
static BOOL isCallRouterObjectMacro2 = NO;
static BOOL isCallRouterObjectMacro3 = NO;
static NSString * testQueryStringQueryValue = nil;
static NSString * testQueryStringNameValue = nil;
static NSString * testQueryStringAgeValue = nil;
static NSString * testRegisterTargetSelector = nil;
static id completionExits = nil;
static id userInfoExits = nil;
static id completionExits1 = nil;
static id userInfoExits1 = nil;
static BOOL isCallNOParametersAndCompletion = NO;
static id testUserInfoNoExits = nil;
static id testCompletionNoExits = nil;
static id testUserInfoNoExits1 = nil;
static id testCompletionNoExits1 = nil;
static id testCover;
static id testCoverA;
static id testCoverB;
static id testCoverANotExits;
static int testWillCallObjectForUrl = 0;
static int testOnceWillCallObjectForUrl = 0;
static int testWillCallOpenUrl = 0;
static int testOnceWillCallOpenUrl = 0;
@interface YTXTestModuleA : YTXModule
@end
@implementation YTXTestModuleA
YTXMODULE_EXTERN()
{
//相当于load
isLoad = YES;
}
YTXMODULE_EXTERN_ROUTER_METHOD(@"URL")
{
YTXMODULE_EXAPAND_PARAMETERS(parameters)
NSLog(@"%@ %@", userInfo, completion);
isCallRouterMacro1 = YES;
completionExits = completion;
userInfoExits = userInfo;
completion(@"Success");
}
YTXMODULE_EXTERN_ROUTER_METHOD(@"URL1")
{
YTXMODULE_EXAPAND_PARAMETERS(parameters)
NSLog(@"%@ %@", userInfo, completion);
isCallRouterMacro2 = YES;
completion(@"Success");
}
YTXMODULE_EXTERN_ROUTER_METHOD(@"URL1_NO_CALL")
{
YTXMODULE_EXAPAND_PARAMETERS(parameters)
NSLog(@"%@ %@", userInfo, completion);
isCallRouterMacro3 = YES;
completion(@"Success");
}
YTXMODULE_EXTERN_ROUTER_OBJECT_METHOD(@"object")
{
YTXMODULE_EXAPAND_PARAMETERS(parameters)
NSLog(@"%@ %@", userInfo, completion);
isCallRouterObjectMacro1 = YES;
completionExits1 = completion;
userInfoExits1 = userInfo;
return @"我是个类型";
}
YTXMODULE_EXTERN_ROUTER_OBJECT_METHOD(@"object1")
{
YTXMODULE_EXAPAND_PARAMETERS(parameters)
NSLog(@"%@ %@", userInfo, completion);
isCallRouterObjectMacro2 = YES;
return @"我是个类型";
}
YTXMODULE_EXTERN_ROUTER_OBJECT_METHOD(@"object1_NO_CALL")
{
YTXMODULE_EXAPAND_PARAMETERS(parameters)
NSLog(@"%@ %@", userInfo, completion);
isCallRouterObjectMacro3 = YES;
return @"我是个类型";
}
YTXMODULE_EXTERN_ROUTER_METHOD(@"YTX://QUERY/:query")
{
YTXMODULE_EXAPAND_PARAMETERS(parameters)
NSLog(@"%@ %@", userInfo, completion);
testQueryStringQueryValue = parameters[@"query"];;
testQueryStringNameValue = parameters[@"name"];
testQueryStringAgeValue = parameters[@"age"];
}
YTXMODULE_EXTERN_ROUTER_METHOD(@"NOParametersAndCompletion")
{
YTXMODULE_EXAPAND_PARAMETERS(parameters)
isCallNOParametersAndCompletion = YES;
testUserInfoNoExits = userInfo;
testCompletionNoExits = completion;
}
YTXMODULE_EXTERN_ROUTER_METHOD(@"YTX://Test/A")
{
testCoverANotExits=@1;
}
YTXMODULE_EXTERN_ROUTER_METHOD(@"YTX://Test/A")
{
testCoverA=@1;
}
YTXMODULE_EXTERN_ROUTER_METHOD(@"YTX://Test/B")
{
testCoverB=@1;
}
YTXMODULE_EXTERN_ROUTER_METHOD(@"YTX://Test")
{
testCover=@1;
}
+ (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions
{
applicationLifCycle1 = YES;
[YTXModule registerURLPattern:@"YTX://Test/targetSelector" withTarget:self withSelector:@selector(testRegisterTargetSelector)];
testRegisterTargetSelector = [YTXModule objectForURL:@"YTX://Test/targetSelector"];
return YES;
}
+ (NSString *)testRegisterTargetSelector {
return @"1024 Hello World";
}
+ (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions
{
applicationLifCycle2 = YES;
return YES;
}
+ (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
NSLog(@"----------------------");
return YES;
}
+ (void)applicationDidBecomeActive:(UIApplication *)application
{
applicationLifCycle3 = YES;
}
+ (void)onceWillCallOpenUrl {
testOnceWillCallOpenUrl++;
}
+ (void)willCallOpenUrl {
testWillCallOpenUrl++;
}
+ (void)onceWillCallObjectForUrl {
testOnceWillCallObjectForUrl++;
}
+ (void)willCallObjectForUrl {
testWillCallObjectForUrl++;
}
@end
//@interface YTXTestModuleB : NSObject
//
//@end
//
//@implementation YTXTestModuleB
//
//- (void)applicationDidBecomeActive:(UIApplication *)application
//{
//
//}
//
//@end
SPEC_BEGIN(InitialTestYTXModuleSpec)
describe(@"测试YTXModule", ^{
// YTXTestModuleB * b = [YTXTestModuleB new];
// [YTXModule registerAppDelegateObject:b];
context(@"测试YTXMODULE_EXTERN宏", ^{
it(@"检查YTXMODULE_EXTERN这个宏的包体会被调用", ^{
[[@(isLoad) should] equal:@1];
});
});
context(@"测试YTXMODULE注册方法", ^{
it(@"检查注册方法是否被调用", ^{
[[testRegisterTargetSelector should] equal:@"1024 Hello World"];
});
});
context(@"测试runtime扩展Application生命周期的方法", ^{
it(@"检查方法被调用:+ (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions", ^{
[[@(applicationLifCycle1) should] equal:@1];
});
it(@"检查方法被调用:+ (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions", ^{
[[@(applicationLifCycle2) should] equal:@1];
});
// it(@"检查方法被调用:+ (void)applicationDidBecomeActive:(UIApplication *)application", ^{
// [[@(applicationLifCycle3) should] equal:@1];
// });
});
context(@"测试Router", ^{
it(@"检查宏被调用:YTXMODULE_EXTERN_ROUTER_METHOD", ^{
[[@(isCallRouterMacro1) should] equal:@1];
[[@(isCallRouterMacro2) should] equal:@1];
[[@(isCallRouterMacro3) should] equal:@0];
});
it(@"检查宏被调用:YTXMODULE_EXTERN_ROUTER_OBJECT_METHOD", ^{
[[@(isCallRouterObjectMacro1) should] equal:@1];
[[@(isCallRouterObjectMacro2) should] equal:@1];
[[@(isCallRouterObjectMacro3) should] equal:@0];
});
it(@"检查URL Query Paramete语法会被自动解析:YTX://QUERY/query?age=18&name=CJ", ^{
[[testQueryStringQueryValue should] equal:@"query"];
[[testQueryStringNameValue should] equal:@"CJ"];
[[testQueryStringAgeValue should] equal:@"18"];
});
it(@"检查router中parameters中的userInfo和completion不为空", ^{
[[userInfoExits shouldNot] beNil];
[[completionExits shouldNot] beNil];
});
it(@"检查object中parameters中的userInfo不为空,completion为空", ^{
[[userInfoExits1 shouldNot] beNil];
[[completionExits1 should] beNil];
});
it(@"检查不传UserInfo和Completion确实取不到", ^{
[[@(isCallNOParametersAndCompletion) should] equal:@1];
[[testUserInfoNoExits should] beNonNil];
[[testCompletionNoExits should] beNil];
[[testUserInfoNoExits1 should] beNil];
[[testCompletionNoExits1 should] beNil];
});
it(@"检查router不会覆盖", ^{
[testCover shouldNotBeNil];
[testCoverANotExits shouldBeNil];
[testCoverA shouldNotBeNil];
[testCoverB shouldNotBeNil];
});
it(@"register unregister url", ^{
NSString * url = @"method://testregister";
UIViewController * vc = [UIViewController new];
[YTXModule registerURLPattern:url withTarget:vc withSelector:@selector(viewDidLoad)];
BOOL ret = [YTXModule canOpenURL:url];
[[@(ret) should] equal:@YES];
[YTXModule deregisterURLPattern:url];
ret = [YTXModule canOpenURL:url];
[[@(ret) should] equal:@NO];
});
it(@"generateURLWithPattern", ^{
NSString * url = [YTXModule generateURLWithPattern:@"object://test/:id/:kk" parameters:@[@10, @"name"]];
[[url should] equal:@"object://test/10/name"];
});
it(@"unregisterAppDelegate", ^{
[YTXModule registerAppDelegateModule:[YTXTestModuleA class]];
[YTXModule unregisterAppDelegateObject:[YTXTestModuleA class]];
});
});
context(@"测试willCallObjectForUrl", ^{
it(@"检查多次willCallObjectForUrl", ^{
[[@(testWillCallObjectForUrl) should] beGreaterThan:@1];
});
it(@"检查一次willOnceCallObjectForUrl", ^{
[[@(testOnceWillCallObjectForUrl) should] equal:@1];
});
});
});
SPEC_END