Skip to content

Instantly share code, notes, and snippets.

@CassiusPacheco
Created April 29, 2020 06:57
Show Gist options
  • Save CassiusPacheco/b0a660b1d2d922d9e0720449cae1141e to your computer and use it in GitHub Desktop.
Save CassiusPacheco/b0a660b1d2d922d9e0720449cae1141e to your computer and use it in GitHub Desktop.
Assert helper that provides a few functions for asserting while debugging
public final class Assert {
public static var shouldCrashOnDebug = true
fileprivate static var isUnitTesting = NSClassFromString("XCTest") != nil
}
public func debugAssertion(_ condition: @autoclosure () -> Bool, message: String = "") {
#if DEBUG || STAGE
if !condition() {
if Assert.shouldCrashOnDebug {
assertionFailure(message)
} else {
print("\n\nπŸ’₯πŸ’£πŸ’₯Assert DEBUG fatal error: \(message)\n\n")
}
}
#endif
}
public func debugFailure(_ message: String) {
#if DEBUG || STAGE
if !Assert.shouldCrashOnDebug {
print("\n\nπŸ’₯πŸ’£πŸ’₯Assert DEBUG fatal error: \(message)\n\n")
} else if !Assert.isUnitTesting {
assertionFailure("\n\nπŸ’₯πŸ’£πŸ’₯ " + message)
}
#endif
}
public func debugAssertNotMainThread() {
#if DEBUG || STAGE
if !Assert.isUnitTesting && Thread.isMainThread {
assertionFailure("😱😱😱 Some unexpected code is running ON the Main Thread 😱😱😱")
}
#endif
}
public func debugAssertMainThread() {
#if DEBUG || STAGE
if !Assert.isUnitTesting && !Thread.isMainThread {
assertionFailure("😱😱😱 Some unexpected code is running OFF the Main Thread 😱😱😱")
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment