Skip to content

Commit 8dee337

Browse files
committed
Refactored test for accessing original implementation on hook
1 parent 4791fde commit 8dee337

File tree

3 files changed

+50
-52
lines changed

3 files changed

+50
-52
lines changed

Tests/InterposeKitTests/ClassHookTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import XCTest
44
fileprivate class ExampleClass: NSObject {
55
@objc static dynamic func doSomethingStatic() {}
66
@objc dynamic func doSomething() {}
7-
@objc dynamic var intValue: Int { 1 }
7+
@objc dynamic var intValue = 1
88
@objc dynamic var arrayValue: [String] { ["base"] }
99
}
1010

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
@testable import InterposeKit
2+
import XCTest
3+
4+
fileprivate class ExampleClass: NSObject {
5+
@objc dynamic var intValue: Int { 1 }
6+
}
7+
8+
final class HookTests: XCTestCase {
9+
10+
func testAccessingOriginalIMP() throws {
11+
typealias MethodSignature = @convention(c) (ExampleClass, Selector) -> Int
12+
typealias HookSignature = @convention(block) (ExampleClass) -> Int
13+
14+
let object = ExampleClass()
15+
16+
let hook = try Hook(
17+
target: .object(object),
18+
selector: #selector(getter: ExampleClass.intValue),
19+
build: { (hook: HookProxy<MethodSignature>) -> HookSignature in
20+
return { `self` in 2 }
21+
}
22+
)
23+
24+
// Before applying the hook, originalIMP performs a dynamic lookup of the current method
25+
// implementation (which is still the unmodified original).
26+
do {
27+
let original = unsafeBitCast(
28+
hook.originalIMP,
29+
to: MethodSignature.self
30+
)
31+
32+
XCTAssertEqual(original(object, #selector(getter: ExampleClass.intValue)), 1)
33+
}
34+
35+
// After applying the hook, originalIMP returns the stored implementation that was
36+
// replaced. This avoids another dynamic lookup.
37+
do {
38+
try hook.apply()
39+
40+
let original = unsafeBitCast(
41+
hook.originalIMP,
42+
to: MethodSignature.self
43+
)
44+
45+
XCTAssertEqual(original(object, #selector(getter: ExampleClass.intValue)), 1)
46+
}
47+
}
48+
49+
}

Tests/InterposeKitTests/ToBePolished/HookDynamicLookupTests.swift

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)