2020-02-10 09:17:03 -06:00
2019-10-31 11:42:43 -05:00
2020-02-05 23:22:16 -05:00
2020-02-10 09:06:51 -06:00
2020-02-05 23:22:05 -05:00
2020-02-05 23:22:05 -05:00
2019-10-31 11:42:43 -05:00

kotlin-power-assert

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.

Example

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>

Kotlin IR

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
    }
}

Project Snapshots

Snapshot builds of the Kotlin compiler plugin are available through Sonatype Snapshot repository. Builds of the Gradle plugin are also available through the Gradle Plugin Portal.

buildscript {
  repositories {
    maven {
      url "https://oss.sonatype.org/content/repositories/snapshots"
    }
  }
  dependencies {
    classpath "com.bnorm.power:kotlin-power-assert-gradle:0.1.0-SNAPSHOT"
  }
}

apply plugin: "com.bnorm.power.kotlin-power-assert"

repositories {
  // Required to find Kotlin compiler plugin as it is not bundled with Gradle plugin
  maven {
    url "https://oss.sonatype.org/content/repositories/snapshots"
  }
}
S
Description
The Kotlin Programming Language.
Readme 2.1 GiB
Languages
Kotlin 79.9%
Java 10.4%
Swift 4.3%
C 2.8%
C++ 2.1%
Other 0.3%