truthish icon indicating copy to clipboard operation
truthish copied to clipboard

Support grouped assertions

Open bitspittle opened this issue 1 year ago • 0 comments

External comment from a dev:


Assertion frameworks often provide a way to combine multiple assertions into a single one.

For example, with Kotest:

assertSoftly(Person("foo", 12)) {
    name shouldBe "foo"
    age shouldBe 12
}

With AssertK:

assertThat(Person("foo", 12))
    .prop(Person::name).isEqualTo("foo")
    .prop(Person::age).isEqualTo(12)

It doesn't seem like Google Truth provides a way to do this itself, so I don't have any explicit guidance in this case. We can take a page out of JUnit's book using assertAll(...)

which in Truthish might look like:

assertAll {
   with(Person("foo", 12)) {
      assertThat(name).isEqualTo("foo")
      assertThat(age).isEqualTo(12)
   }
} // would report here if at least one failure occured

In this case, assertAll would change the default strategy employed by the assertThat method to collect instead of throw.

This is great in that it's pretty flexible, but it will add extra nesting.

We could also add assertSoftly. Although I don't think I like that name, so maybe assertAllWith(...) instead:

assertAllWith(Person("foo", 12)) {
   assertThat(name).isEqualTo("foo")
   assertThat(age).isEqualTo(12)
}

but I wonder if that's even necessary, as the Kotlin-idiomatic thing to do may be to allow users to just use Kotlin directly (the first example) instead of trying to wrap everything ourselves.

bitspittle avatar Jul 27 '24 22:07 bitspittle