Last active
December 25, 2015 19:14
-
-
Save pxpgraphics/4035e77d373b81ba5035 to your computer and use it in GitHub Desktop.
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
// MARK: PXPAssert | |
/** | |
Traditional C-style assert with an optional message. | |
Use this function for internal sanity checks that are active | |
during testing but do not impact performance of shipping code. | |
* In playgrounds and -Onone builds (the default for Xcode's Debug | |
configuration): if `condition` evaluates to false, stop program | |
execution in a debuggable state after printing `message`. | |
* In -O builds (the default for Xcode's Release configuration), | |
if `condition` evaluates to false, prints `message`. | |
- parameter file: The name of the file; defaults to the current localized file. | |
- parameter function: The name of the function; defaults to the function within which the call is made. | |
- parameter line: The line number; defaults to the line number within the file that the call is made. | |
*/ | |
@transparent | |
@inline(__always) | |
public func PXPAssert( | |
@autoclosure condition: () -> Bool, | |
@autoclosure _ message: () -> String = String(), | |
file: StaticString = __FILE__, | |
function: StaticString = __FUNCTION__, | |
line: UInt = __LINE__ | |
) { | |
func _printStackTrace() { | |
for symbol in NSThread.callStackSymbols() { | |
print("\t" + symbol) | |
} | |
} | |
if !condition() { | |
let localizedFile = NSURL(string: String(file))?.lastPathComponent ?? "<Unknown file>" | |
print("Assertion failed at \(function) in \(localizedFile):\(line) — \(message())") | |
#if DEBUG | |
// Only abort in debug mode: | |
_printStackTrace() | |
abort() | |
#endif | |
} | |
} | |
func testPXPAssert() { | |
let A = 1 | |
let B = 2 | |
PXPAssert(A == B, "A should equal B!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample console output: