Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support grouped assertions #7

Open
bitspittle opened this issue Jul 27, 2024 · 0 comments
Open

Support grouped assertions #7

bitspittle opened this issue Jul 27, 2024 · 0 comments
Assignees

Comments

@bitspittle
Copy link
Collaborator

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 bitspittle self-assigned this Jul 27, 2024
CLOVIS-AI added a commit to CLOVIS-AI/Prepared that referenced this issue Aug 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant