Skip to content

Instantly share code, notes, and snippets.

@therealbnut
Created August 20, 2017 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save therealbnut/936b7567d257932c035af7b21a6a941e to your computer and use it in GitHub Desktop.
Save therealbnut/936b7567d257932c035af7b21a6a941e to your computer and use it in GitHub Desktop.
private class Example: NSObject {
@objc func example() {}
}
extension XCTestSuite {
public func addTests<Test: XCTestCase>(
_ tests: [(String, (Test) -> () -> Void)])
{
let method = class_getInstanceMethod(Example.self,
#selector(Example.example))
guard let methodType = method.map(method_getTypeEncoding) else {
return
}
for (name, execute) in tests {
let blockA: @convention(block) (Test?,Selector?)->Void = { obj, _ in
if let obj = obj {
execute(obj)()
}
}
let block = unsafeBitCast(blockA, to: AnyObject.self)
let sel = Selector(name)
let imp = imp_implementationWithBlock(block)
class_addMethod(Test.self, sel, imp, methodType)
self.addTest(Test(selector: sel))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment