Skip to content

Commit ae5f8e8

Browse files
committed
Migrated test for multiple apply/revert calls
1 parent dbc260e commit ae5f8e8

2 files changed

Lines changed: 65 additions & 50 deletions

File tree

Tests/InterposeKitTests/ClassHookTests.swift

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,120 @@ import XCTest
33

44
fileprivate class ExampleClass: NSObject {
55
@objc dynamic func doSomething() {}
6+
@objc dynamic var intValue = 1
67
}
78

89
fileprivate class ExampleSubclass: ExampleClass {}
910

1011
final class ClassHookTests: InterposeKitTestCase {
1112

12-
func testSuccess_applyHook() throws {
13+
func testLifecycle_applyHook() throws {
1314
let hook = try Interpose.applyHook(
1415
on: ExampleClass.self,
15-
for: #selector(ExampleClass.doSomething),
16-
methodSignature: (@convention(c) (NSObject, Selector) -> Void).self,
17-
hookSignature: (@convention(block) (NSObject) -> Void).self
16+
for: #selector(getter: ExampleClass.intValue),
17+
methodSignature: (@convention(c) (NSObject, Selector) -> Int).self,
18+
hookSignature: (@convention(block) (NSObject) -> Int).self
1819
) { hook in
19-
return { `self` in }
20+
return { `self` in
21+
1 + hook.original(self, hook.selector)
22+
}
2023
}
2124

25+
XCTAssertEqual(ExampleClass().intValue, 2)
2226
XCTAssertEqual(hook.state, .active)
2327
XCTAssertMatchesRegex(
2428
hook.debugDescription,
25-
#"^Active hook for -\[ExampleClass doSomething\] \(originalIMP: 0x[0-9a-fA-F]+\)$"#
29+
#"^Active hook for -\[ExampleClass intValue\] \(originalIMP: 0x[0-9a-fA-F]+\)$"#
2630
)
2731

2832
try hook.revert()
2933

34+
XCTAssertEqual(ExampleClass().intValue, 1)
3035
XCTAssertEqual(hook.state, .pending)
3136
XCTAssertMatchesRegex(
3237
hook.debugDescription,
33-
#"^Pending hook for -\[ExampleClass doSomething\]$"#
38+
#"^Pending hook for -\[ExampleClass intValue\]$"#
3439
)
3540
}
3641

37-
func testSuccess_prepareHook() throws {
42+
func testLifecycle_prepareHook() throws {
3843
let hook = try Interpose.prepareHook(
3944
on: ExampleClass.self,
40-
for: #selector(ExampleClass.doSomething),
41-
methodSignature: (@convention(c) (NSObject, Selector) -> Void).self,
42-
hookSignature: (@convention(block) (NSObject) -> Void).self
45+
for: #selector(getter: ExampleClass.intValue),
46+
methodSignature: (@convention(c) (NSObject, Selector) -> Int).self,
47+
hookSignature: (@convention(block) (NSObject) -> Int).self
4348
) { hook in
44-
return { `self` in }
49+
return { `self` in
50+
1 + hook.original(self, hook.selector)
51+
}
4552
}
4653

54+
XCTAssertEqual(ExampleClass().intValue, 1)
4755
XCTAssertEqual(hook.state, .pending)
4856
XCTAssertMatchesRegex(
4957
hook.debugDescription,
50-
#"^Pending hook for -\[ExampleClass doSomething\]$"#
58+
#"^Pending hook for -\[ExampleClass intValue\]$"#
5159
)
5260

5361
try hook.apply()
5462

63+
XCTAssertEqual(ExampleClass().intValue, 2)
5564
XCTAssertEqual(hook.state, .active)
5665
XCTAssertMatchesRegex(
5766
hook.debugDescription,
58-
#"^Active hook for -\[ExampleClass doSomething\] \(originalIMP: 0x[0-9a-fA-F]+\)$"#
67+
#"^Active hook for -\[ExampleClass intValue\] \(originalIMP: 0x[0-9a-fA-F]+\)$"#
5968
)
6069

6170
try hook.revert()
6271

72+
XCTAssertEqual(ExampleClass().intValue, 1)
6373
XCTAssertEqual(hook.state, .pending)
6474
XCTAssertMatchesRegex(
6575
hook.debugDescription,
66-
#"^Pending hook for -\[ExampleClass doSomething\]$"#
76+
#"^Pending hook for -\[ExampleClass intValue\]$"#
6777
)
6878
}
6979

80+
func testLifecycle_idempotentApplyAndRevert() throws {
81+
let object = ExampleClass()
82+
83+
let hook = try Interpose.prepareHook(
84+
on: ExampleClass.self,
85+
for: #selector(getter: ExampleClass.intValue),
86+
methodSignature: (@convention(c) (NSObject, Selector) -> Int).self,
87+
hookSignature: (@convention(block) (NSObject) -> Int).self
88+
) { hook in
89+
return { `self` in
90+
1 + hook.original(self, hook.selector)
91+
}
92+
}
93+
94+
XCTAssertEqual(object.intValue, 1)
95+
XCTAssertEqual(hook.state, .pending)
96+
97+
try hook.apply()
98+
try hook.apply() // noop
99+
100+
XCTAssertEqual(object.intValue, 2)
101+
XCTAssertEqual(hook.state, .active)
102+
103+
try hook.revert()
104+
try hook.revert() // noop
105+
106+
XCTAssertEqual(object.intValue, 1)
107+
XCTAssertEqual(hook.state, .pending)
108+
109+
try hook.apply()
110+
111+
XCTAssertEqual(object.intValue, 2)
112+
XCTAssertEqual(hook.state, .active)
113+
114+
try hook.revert()
115+
116+
XCTAssertEqual(object.intValue, 1)
117+
XCTAssertEqual(hook.state, .pending)
118+
}
119+
70120
func testValidationFailure_methodNotFound() throws {
71121
XCTAssertThrowsError(
72122
try Interpose.prepareHook(

Tests/InterposeKitTests/ToBePolished/InterposeKitTests.swift

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,6 @@ import XCTest
33

44
final class InterposeKitTests: InterposeKitTestCase {
55

6-
func testClassOverrideAndRevert() throws {
7-
let testObj = TestClass()
8-
XCTAssertEqual(testObj.sayHi(), testClassHi)
9-
10-
// Functions need to be `@objc dynamic` to be hookable.
11-
let hook = try Interpose.applyHook(
12-
on: TestClass.self,
13-
for: #selector(TestClass.sayHi),
14-
methodSignature: (@convention(c) (NSObject, Selector) -> String).self,
15-
hookSignature: (@convention(block) (NSObject) -> String).self) { store in { bSelf in
16-
// You're free to skip calling the original implementation.
17-
print("Before Interposing \(bSelf)")
18-
let string = store.original(bSelf, store.selector)
19-
print("After Interposing \(bSelf)")
20-
21-
return string + testString
22-
}
23-
}
24-
print(TestClass().sayHi())
25-
26-
// Test various apply/revert's
27-
XCTAssertEqual(testObj.sayHi(), testClassHi + testString)
28-
try hook.revert()
29-
XCTAssertEqual(testObj.sayHi(), testClassHi)
30-
try hook.apply()
31-
XCTAssertEqual(testObj.sayHi(), testClassHi + testString)
32-
try hook.apply() // noop
33-
try hook.apply() // noop
34-
try hook.revert()
35-
try hook.revert() // noop
36-
try hook.apply()
37-
try hook.revert()
38-
XCTAssertEqual(testObj.sayHi(), testClassHi)
39-
}
40-
416
func testSubclassOverride() throws {
427
let testObj = TestSubclass()
438
XCTAssertEqual(testObj.sayHi(), testClassHi + testSubclass)

0 commit comments

Comments
 (0)