Skip to content

Commit 8a0929c

Browse files
committed
Added test for steipete/InterposeKit#37
1 parent 9635fe8 commit 8a0929c

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

Tests/InterposeKitTests/ObjectInterposeTests.swift

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,64 @@ final class ObjectInterposeTests: InterposeKitTestCase {
150150
try hook.revert()
151151
XCTAssertEqual(testObj.doubleString(string: str), str + str)
152152
}
153-
153+
154+
func testHook_getPoint() throws {
155+
let object = TestClass()
156+
XCTAssertEqual(object.getPoint(), CGPoint(x: -1, y: 1))
157+
158+
let hook = try object.hook(
159+
#selector(TestClass.getPoint),
160+
methodSignature: (@convention(c) (NSObject, Selector) -> CGPoint).self,
161+
hookSignature: (@convention(block) (NSObject) -> CGPoint).self
162+
) { hook in
163+
return { `self` in
164+
var point = hook.original(self, hook.selector)
165+
point.x += 2
166+
point.y += 2
167+
return point
168+
}
169+
}
170+
171+
XCTAssertEqual(object.getPoint(), CGPoint(x: 1, y: 3))
172+
173+
try hook.revert()
174+
XCTAssertEqual(object.getPoint(), CGPoint(x: -1, y: 1))
175+
}
176+
177+
func testHook_passthroughPoint() throws {
178+
let object = TestClass()
179+
180+
XCTAssertEqual(
181+
object.passthroughPoint(CGPoint(x: 1, y: 1)),
182+
CGPoint(x: 1, y: 1)
183+
)
184+
185+
let hook = try object.hook(
186+
#selector(TestClass.passthroughPoint(_:)),
187+
methodSignature: (@convention(c) (NSObject, Selector, CGPoint) -> CGPoint).self,
188+
hookSignature: (@convention(block) (NSObject, CGPoint) -> CGPoint).self
189+
) { hook in
190+
return { `self`, inPoint in
191+
var outPoint = hook.original(self, hook.selector, inPoint)
192+
outPoint.x += 1
193+
outPoint.y += 1
194+
return outPoint
195+
}
196+
}
197+
198+
XCTAssertEqual(
199+
object.passthroughPoint(CGPoint(x: 1, y: 1)),
200+
CGPoint(x: 2, y: 2)
201+
)
202+
203+
try hook.revert()
204+
205+
XCTAssertEqual(
206+
object.passthroughPoint(CGPoint(x: 1, y: 1)),
207+
CGPoint(x: 1, y: 1)
208+
)
209+
}
210+
154211
// func testLargeStructReturn() throws {
155212
// let testObj = TestClass()
156213
// let transform = CATransform3D()

Tests/InterposeKitTests/TestClass.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ class TestClass: NSObject {
5050
@objc dynamic func calculate2(var1: Int, var2: Int, var3: Int, var4: Int, var5: Int, var6: Int) -> Int {
5151
var1 + var2 + var3 + var4 + var5 + var6
5252
}
53-
53+
54+
@objc dynamic func getPoint() -> CGPoint {
55+
CGPoint(x: -1, y: 1)
56+
}
57+
58+
@objc dynamic func passthroughPoint(_ point: CGPoint) -> CGPoint {
59+
point
60+
}
61+
5462
// This requires _objc_msgSendSuper_stret on x64, returns a large struct
5563
@objc dynamic func invert3DTransform(_ input: CATransform3D) -> CATransform3D {
5664
input.inverted

0 commit comments

Comments
 (0)