Kotlin Compiler Plugin which high-jacks Kotlin assert function calls and transforms them similar to Groovy's Power Assert feature. This plugin uses the new IR backend for the Kotlin compiler.
Given following code:
val hello = "Hello"
assert(hello.length == "World".substring(1, 4).length)
Normally the assertion message would look like:
java.lang.AssertionError: Assertion failed
at <stacktrace>
A custom assertion message can be provided:
val hello = "Hello"
assert(hello.length == "World".substring(1, 4).length) { "Incorrect length" }
But this just replaces the message:
java.lang.AssertionError: Incorrect length
at <stacktrace>
With kotlin-power-assert
included, the error message for the previous example
will be transformed:
java.lang.AssertionError: Incorrect length
assert(hello.length == "World".substring(1, 4).length)
| | | | |
| | | | 3
| | | orl
| | false
| 5
Hello
at <stacktrace>
Complex, multi-line, boolean expression are also supported:
Assertion failed
assert(
(text != null && text.toLowerCase() == text) ||
| |
| false
null
text == "Hello"
| |
| false
null
)
Builds of the Gradle plugin are available through the Gradle Plugin Portal.
plugins {
id("com.bnorm.power.kotlin-power-assert") version "0.2.0"
}
The plugin by default will transform assert
function call but can also
transform other functions like require
, check
, and/or assertTrue
. The
function needs to validate the Boolean expression evaluates to true
and has a
form which also takes a String or String producing lambda.
configure<com.bnorm.power.PowerAssertGradleExtension> {
functions = listOf("kotlin.test.AssertionsKt.assertTrue", "kotlin.PreconditionsKt.require")
}
Using this compiler plugin only works if the code is compiled using IR. This can be enabled only when compiling the test SourceSet if desired. As Kotlin IR is still experimental, mileage may vary.
compileTestKotlin {
kotlinOptions {
useIR = true
}
}