Skip to content

Instantly share code, notes, and snippets.

@HugoSay
Created July 25, 2018 15:05
Show Gist options
  • Save HugoSay/bfafc9e1a14528440a651de1d8d52599 to your computer and use it in GitHub Desktop.
Save HugoSay/bfafc9e1a14528440a651de1d8d52599 to your computer and use it in GitHub Desktop.
calls all calculated variables from a class for unit tests
//: [Previous](@previous)
import Foundation
class Foo {
@objc static var prop1: String? {
print("accessed prop1")
return "Hello I am prop1!" }
@objc static var prop2: String? {
print("accessed prop2")
return "Hello I am prop2!" }
}
extension Foo {
class Tutu {
@objc static var prop4: String? {
print("accessed prop4")
return "Hello I'm prop4"
}
}
@objc static var prop3: String? {
print("accessed prop3")
return "Hello I am prop13" }
}
typealias MyCFunction = @convention(c) (Selector) -> AnyObject
func checkAllCalculatedVariablesFor(myClass: AnyClass) {
let myclass = object_getClass(myClass)
var count: CUnsignedInt = 0
let methods = class_copyMethodList(myclass, &count)!
for i in 0 ..< count {
let selector = method_getName(methods.advanced(by: Int(i)).pointee)
let implem = method_getImplementation(methods.advanced(by: Int(i)).pointee)
let curriedImplementation = unsafeBitCast(implem, to: MyCFunction.self)
//calls the static var
curriedImplementation(selector)
}
}
let myclass = object_getClass(Foo.self)
print("PRINTING SELECTORS ! ")
checkAllCalculatedVariablesFor(myClass: Foo.self)
print("DONE ! :)")
checkAllCalculatedVariablesFor(myClass: Foo.Tutu.self)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment