assertk icon indicating copy to clipboard operation
assertk copied to clipboard

Should there be an extension method version of assert?

Open dalewking opened this issue 3 years ago • 2 comments
trafficstars

The standard syntax is to pass the value as a parameter to assertThat as in

    assertThat(someExpressionToGetTheValue())

But depending on the complexity of the expression this can get harder to read with indentation and parentheses. It occurs to me that an extension method could be useful as well. Here is what I am thinking:

    someExpressionToGetTheValue()
        .assertIt()
        .isNotNull()

dalewking avatar Apr 05 '22 14:04 dalewking

That reminds me of early RSpec. They had the ability to say a.should == b, which read much nicer.

Due to some kind of issues relating to typing or metaclass stuff, they decided that this was deprecated and replaced with expect(a).to equal(b).

It was too bad because I did like that.

The equivalents here might be:

    a.shouldEqual(b)
    a.shouldNotBeNull()

But at some point it almost becomes a completely different assertion library so it's rough. Would the two be mixed in the same test suite? At work, we usually insist on choosing one way to do a given thing and sticking to it.

And actually,

With infix functions, you could get some really devious syntax here.

Example:

infix fun <T> T.should(Condition<T> condition) {
    assertThat(this).satisfies(condition)
}

// usage
a should equal(b)

hakanai avatar Apr 06 '22 04:04 hakanai

I'm actually coming from KoTest which let's you use methods like those but also let's you say a shouldBe b

dalewking avatar Apr 06 '22 23:04 dalewking

No plans to support alternate assertion syntax as it takes away from the focus and simplicity of the lib imo. There's other options out there if this is important to you.

evant avatar Mar 16 '23 04:03 evant