-
Notifications
You must be signed in to change notification settings - Fork 37
/
HookOnceOCTests.m
83 lines (68 loc) · 1.77 KB
/
HookOnceOCTests.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
//
// HookOnceOCTests.m
// SwiftHookTests
//
// Created by Yanni Wang on 30/6/21.
// Copyright © 2021 Yanni. All rights reserved.
//
#import <XCTest/XCTest.h>
@import SwiftHook;
@interface MyObject_HookOnceOCTests : NSObject
@property (nonatomic, assign) BOOL run;
@end
@implementation MyObject_HookOnceOCTests
-(void)myMethod
{
self.run = YES;
}
@end
@interface HookOnceOCTests : XCTestCase
@end
@implementation HookOnceOCTests
- (void)test_specific_object
{
MyObject_HookOnceOCTests *obj = [[MyObject_HookOnceOCTests alloc] init];
__block BOOL run = NO;
NSError *error = nil;
__block OCToken *token = [obj sh_hookBeforeSelector:@selector(myMethod) error:&error closure:^{
run = YES;
[token cancelHook];
}];
XCTAssertNil(error);
XCTAssertFalse(obj.run);
XCTAssertFalse(run);
[obj myMethod];
XCTAssertTrue(obj.run);
XCTAssertTrue(run);
obj.run = NO;
run = NO;
XCTAssertFalse(obj.run);
XCTAssertFalse(run);
[obj myMethod];
XCTAssertTrue(obj.run);
XCTAssertFalse(run);
}
- (void)test_objects
{
__block BOOL run = NO;
NSError *error = nil;
__block OCToken *token = [MyObject_HookOnceOCTests sh_hookBeforeSelector:@selector(myMethod) error:&error closure:^{
run = YES;
[token cancelHook];
}];
XCTAssertNil(error);
MyObject_HookOnceOCTests *obj1 = [[MyObject_HookOnceOCTests alloc] init];
XCTAssertFalse(obj1.run);
XCTAssertFalse(run);
[obj1 myMethod];
XCTAssertTrue(obj1.run);
XCTAssertTrue(run);
run = NO;
MyObject_HookOnceOCTests *obj2 = [[MyObject_HookOnceOCTests alloc] init];
XCTAssertFalse(obj2.run);
XCTAssertFalse(run);
[obj2 myMethod];
XCTAssertTrue(obj2.run);
XCTAssertFalse(run);
}
@end