Last active
October 13, 2023 13:14
-
-
Save Andrew0000/4f298e9ea42e8bf49504e14a6fc71347 to your computer and use it in GitHub Desktop.
DebugAssert
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
@Suppress("Unused") | |
object DebugAssert { | |
fun ensure(predicate: () -> Boolean) { | |
ensure(predicate, null) | |
} | |
fun ensure(predicate: () -> Boolean, description: String? = null) { | |
if (BuildConfig.DEBUG && !predicate()) { | |
throw DebugAssertException(description ?: "DebugAssert") | |
} | |
} | |
fun ensure(predicate: Boolean, description: String? = null) { | |
if (BuildConfig.DEBUG && !predicate) { | |
throw DebugAssertException(description ?: "DebugAssert") | |
} | |
} | |
fun ensureBackgroundThread() { | |
ensure({ isBackgroundThread() }, "Background thread expected") | |
} | |
fun fail(description: String? = null) { | |
if (BuildConfig.DEBUG) { | |
throw DebugAssertException(description ?: "DebugAssert") | |
} | |
} | |
} | |
class DebugAssertException( | |
description: String = "DebugAssert", | |
) : IllegalStateException(description) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment