Created
April 29, 2020 06:57
-
-
Save CassiusPacheco/b0a660b1d2d922d9e0720449cae1141e to your computer and use it in GitHub Desktop.
Assert helper that provides a few functions for asserting while debugging
This file contains hidden or 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
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