-
Notifications
You must be signed in to change notification settings - Fork 37
/
HookOnceTests.swift
74 lines (65 loc) · 1.74 KB
/
HookOnceTests.swift
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
//
// HookOnceTests.swift
// SwiftHookTests
//
// Created by Yanni Wang on 30/6/21.
// Copyright © 2021 Yanni. All rights reserved.
//
import XCTest
import SwiftHook
class HookOnceTests: XCTestCase {
func test_specific_object() throws {
class MyObject {
var run = false
@objc dynamic func myMethod() {
self.run = true
}
}
let obj = MyObject()
var run = false
var token: Token?
token = try hookBefore(object: obj, selector: #selector(MyObject.myMethod)) {
run = true
token?.cancelHook()
}
XCTAssertFalse(obj.run)
XCTAssertFalse(run)
obj.myMethod()
XCTAssertTrue(obj.run)
XCTAssertTrue(run)
obj.run = false
run = false
XCTAssertFalse(obj.run)
XCTAssertFalse(run)
obj.myMethod()
XCTAssertTrue(obj.run)
XCTAssertFalse(run)
}
func test_objects() throws {
class MyObject {
var run = false
@objc dynamic func myMethod() {
self.run = true
}
}
var run = false
var token: Token?
token = try hookBefore(targetClass: MyObject.self, selector: #selector(MyObject.myMethod)) {
run = true
token?.cancelHook()
}
let obj1 = MyObject()
XCTAssertFalse(obj1.run)
XCTAssertFalse(run)
obj1.myMethod()
XCTAssertTrue(obj1.run)
XCTAssertTrue(run)
let obj2 = MyObject()
run = false
XCTAssertFalse(obj2.run)
XCTAssertFalse(run)
obj2.myMethod()
XCTAssertTrue(obj2.run)
XCTAssertFalse(run)
}
}