Brian Norman 6de1f2ac1f Remove carriage returns from source file text
The Kotlin compiler removes carriage returns from the text of the file
when calculating the startOffset and endOffset properties for an
IrExpression. This causes the source snippet extraction to not work for
files with Windows-style line-separators.

Before pull substrings out of the file source string, remove all
carriages returns so the IrExpression offsets match the compiler.
2020-09-14 20:01:01 -05:00
2020-08-29 19:13:52 -05:00
2020-02-05 23:22:16 -05:00
2020-08-29 19:13:52 -05:00
2020-08-29 10:13:18 -05:00
2020-08-29 10:13:18 -05:00
2019-10-31 11:42:43 -05:00

kotlin-power-assert

Maven Central

Kotlin 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!

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>

Complex, multi-line, boolean expression are also supported:

Assertion failed
assert(
  (text != null && text.toLowerCase() == text) ||
   |    |
   |    false
   null
      text == "Hello"
      |    |
      |    false
      null
)

Gradle Plugin

Builds of the Gradle plugin are available through the Gradle Plugin Portal.

plugins {
  kotlin("jvm") version "1.4.0"
  id("com.bnorm.power.kotlin-power-assert") version "0.5.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.assertTrue", "kotlin.require")
}

Kotlin IR

Using this compiler plugin only works if the code is compiled using Kotlin 1.4.0 and IR is enabled. This includes all IR based compiler backends: JVM, JS, and Native! As Kotlin IR is still experimental, mileage may vary.

Kotlin/JVM
tasks.withType<KotlinCompile>().configureEach {
  kotlinOptions {
    useIR = true
  }
}
Kotlin/JS
target {
  js(IR) {
  }
}
Kotlin/Native

IR already enabled by default!

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%