assertk icon indicating copy to clipboard operation
assertk copied to clipboard

Integrate with kotlin 2.x power assert

Open yogurtearl opened this issue 1 year ago • 3 comments

Kotlin 2.x includes power assert. https://kotlinlang.org/docs/power-assert.html

It would be nice if I could use assertk APIs, but get power-assert style error messages when the power assert plugin is applied.

Seems like this might support this sort of thing: https://kotlinlang.org/docs/power-assert.html#beyond-assert-function

yogurtearl avatar May 31 '24 16:05 yogurtearl

Oh that's cool! I had actually considered looking into something similar but decided it would be best as a separate library as it's solving problem of good assertion error messages in different ways. If you use something like power-assert you don't really need something like assertk, ex:

assert(hello.length == world.substring(1, 4).length) { "Incorrect length" }

Incorrect length
assert(hello.length == world.substring(1, 4).length) { "Incorrect length" }
       |     |      |  |     |               |
       |     |      |  |     |               3
       |     |      |  |     orl
       |     |      |  world!
       |     |      false
       |     5
       Hello

vs

assertThat(world.substring(1, 4)).hasSameLengthAs(hello)

expected to have same length as:<"Hello"> (5) but was:<"orl"> (3)

certainly open if someone wants to explore this and show off cases where they would be useful together though!

evant avatar Jun 04 '24 21:06 evant

Unless we're writing a new compiler plugin specific to assertk, the entire design of power assert isn't going to work with this API shape. All the context is lost within the library. Basically what makes assertk so good on its own precludes the use of this tool.

JakeWharton avatar Jun 04 '24 21:06 JakeWharton

Yeah after looking at it a bit more, agreed.

evant avatar Jun 04 '24 21:06 evant