Created
August 20, 2017 23:35
-
-
Save therealbnut/936b7567d257932c035af7b21a6a941e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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