Skip to content

Instantly share code, notes, and snippets.

@Andrew0000
Last active October 13, 2023 13:14
Show Gist options
  • Save Andrew0000/4f298e9ea42e8bf49504e14a6fc71347 to your computer and use it in GitHub Desktop.
Save Andrew0000/4f298e9ea42e8bf49504e14a6fc71347 to your computer and use it in GitHub Desktop.
DebugAssert
@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