I’ve always felt like Kotlin should lend itself ve...
# test
b
I’ve always felt like Kotlin should lend itself very well to assertion DSLs; today I was very happy to stumble upon a nice confluence of language features that resulted in something terse and expressive. This is clearly a skeleton; my question is, has someone published something like this already? Did it work out, and if not, why?
Copy code
inline fun <T> assertAbout(value: T, fn: AssertScope.(T) -> Unit) {
    AssertScope().let { it.fn(value) }
}

class AssertScope() {
    infix fun <T> T.shouldEqual(that: T) {
        // standard kotlin.test assertions
        assertEquals(that, this)
    }

    infix fun <T> T.shouldNotEqual(that: T) {
        assertNotEquals(that, this)
    }
}

assertAbout(“foo”) {
  it.size shouldEqual 3
  it shouldNotEqual “bar”
}