Skip to content

Instantly share code, notes, and snippets.

@MarioIannotta
Last active August 22, 2016 08:49
Show Gist options
  • Save MarioIannotta/8d189f2c4cafc24dc4b74394751d57c0 to your computer and use it in GitHub Desktop.
Save MarioIannotta/8d189f2c4cafc24dc4b74394751d57c0 to your computer and use it in GitHub Desktop.
Handle swift logs and testing safely and painless
/*
This gist is part of the post "Handle swift logs and testing safely and painless"
More info here:
https://medium.com/@MarioIannotta/handle-swift-test-and-log-safely-c825d6006bc4#.363uu3a94
*/
import Foundation
enum Test {
case kittensVC
case kittensNetworkService
case kittensTableView
typealias TestBlock = () -> ()
static private let defaultDelay: NSTimeInterval = 0.01
func isEnabled() -> Bool {
#if !DEBUG
return false
#endif
switch self {
case kittensVC:
return true
case kittensNetworkService:
return true
case kittensTableView:
return true
default:
return false
}
}
func performBlock(withDelay delay: NSTimeInterval = defaultDelay, block: TestBlock) -> Bool {
if self.isEnabled() {
Test.performBlockAfterDelay(delay, block: block)
return true
}
return false
}
static func performBlockFor(sections: [Test], withDelay delay: NSTimeInterval = defaultDelay, block: TestBlock) {
for section in sections {
if section.performBlock(withDelay: delay, block: block) {
return
}
}
}
static private func performBlockAfterDelay(delay: NSTimeInterval, block: TestBlock) {
let delayTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
dispatch_after(delayTime, dispatch_get_main_queue()) {
block()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment