在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):bnorm/kotlin-power-assert开源软件地址(OpenSource Url):https://github.com/bnorm/kotlin-power-assert开源编程语言(OpenSource Language):Kotlin 100.0%开源软件介绍(OpenSource Introduction):kotlin-power-assertKotlin Compiler Plugin which high-jacks Kotlin assert function calls and transforms them similar to Groovy's Power Assert feature. This plugin uses the IR backend for the Kotlin compiler and supports all platforms: JVM, JS, and Native! ExampleGiven following code: val hello = "Hello"
assert(hello.length == "World".substring(1, 4).length) Normally the assertion message would look like:
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:
With
Complex, multi-line, boolean expression are also supported:
Beyond AssertThe plugin by default will transform Functions which can be transformed have specific requirements. A function must
have a form which allows taking a For example, the
If the first function definition is called, it will be transformed into calling the second definition with the diagram message supplied as the last parameter. If the second definition is called, it will be transformed into calling the same function but with the diagram message appended to the last parameter. This transformed function call doesn't need to throw an exception either. See Advanced Usage for some examples. Gradle PluginBuilds of the Gradle plugin are available through the Gradle Plugin Portal. plugins {
kotlin("multiplatform") version "1.6.0"
id("com.bnorm.power.kotlin-power-assert") version "0.11.0"
} The Gradle plugin allows configuring the functions which should be transformed with a list of fully-qualified function names. // Kotlin DSL
configure<com.bnorm.power.PowerAssertGradleExtension> {
functions = listOf("kotlin.assert", "kotlin.test.assertTrue")
} // Groovy
kotlinPowerAssert {
functions = ["kotlin.assert", "kotlin.test.assertTrue"]
} You can also exclude Gradle source sets from being transformed by the plugin, where those source sets can be specified by name. // Kotlin DSL
configure<com.bnorm.power.PowerAssertGradleExtension> {
excludedSourceSets = listOf(
"commonMain",
"jvmMain",
"jsMain",
"nativeMain"
)
} // Groovy
kotlinPowerAssert {
excludedSourceSets = [
"commonMain",
"jvmMain",
"jsMain",
"nativeMain"
]
} CompatibilityThe Kotlin compiler plugin API is unstable and each new version of Kotlin can bring breaking changes to the APIs used by this compiler plugin. Make sure you are using the correct version of this plugin for whatever version of Kotlin used. Check the table below to find when support for a particular version of Kotlin was first introduced. If a version of Kotlin or this plugin is not listed it can be assumed to maintain compatibility with the next oldest version listed.
Kotlin IRThis plugin supports all IR based compiler backends: JVM, JS, and Native! Only Kotlin/JS still uses the legacy compiler backend by default, use the following to make sure IR is enabled. target {
js(IR) {
}
} Advanced UsageFunction Call TracingSimilar to Rust's fun <T> dbg(value: T): T = value
fun <T> dbg(value: T, msg: String): T {
println(msg)
return value
}
fun main() {
println(dbg(1 + 2 + 3))
} Prints the following:
Soft AssertionTo achieve soft assertion, the following template can be implemented: typealias LazyMessage = () -> Any
interface AssertScope {
fun assert(assertion: Boolean, lazyMessage: LazyMessage? = null)
}
fun <R> assertSoftly(block: AssertScope.() -> R): R = TODO("implement") You can then use the template as follows: val jane: Person = TODO()
assertSoftly {
assert(jane.firstName == "Jane")
assert(jane.lastName == "Doe")
} A working example is available in this repository in the sample directory. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论