willowtreeapps / assertk

assertions for kotlin inspired by assertj
MIT License
748 stars 83 forks source link

Integrate with kotlin 2.x power assert #539

Open yogurtearl opened 1 month ago

yogurtearl commented 1 month ago

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

evant commented 1 month ago

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!

JakeWharton commented 1 month ago

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.

evant commented 1 month ago

Yeah after looking at it a bit more, agreed.