assertk icon indicating copy to clipboard operation
assertk copied to clipboard

New assertion for asserting multiple props of an object

Open mbjelac opened this issue 3 years ago • 4 comments

... with a readable specification syntax:

assertThat(MyObject("a", 1, true))
   .propsEqualTo(
     MyObject::name to "a",
     MyObject::amount to 1,
     MyObject::flag to true
   )

mbjelac avatar Sep 08 '21 22:09 mbjelac

Hm, a little hesitant on having multiple ways on doing the same thing, since we have prop() and isDataClassEqualTo() but I don't hate it, will think about it.

evant avatar Sep 12 '21 02:09 evant

Hm, a little hesitant on having multiple ways on doing the same thing, since we have prop() and isDataClassEqualTo() but I don't hate it, will think about it.

Yes, I've tried prop in this way but it can't be chained:

assertThat(something)
  .prop(Something::firstProp).isEqualTo(42)
  .prop(Something::secondProp).isEqualTo("foo")

and I don't see how it could be, since isEqualTo and others are "terminal" methods which execute the actual assertion.

isDataClassEqualTo asserts on all properties and often only some subset of them is important for a specific test.

the only existing tool that could be used is:

tableOf("propGetter", "expected")
  .row(Something::firstProp, 42)
  .row(Something::secondProp, "foo")
  .forAll { propGetter, expected ->
    assertThat(something)
      .prop(propGetter)
      .isEqualTo(expected)
  }

but its a bit too wordy and requires developers to remember that pattern of propGetter+expected.

mbjelac avatar Sep 16 '21 07:09 mbjelac

You'd use all to chain prop fyi

assertThat(something).all {
  prop(Something::firstProp).isEqualTo(42)
  prop(Something::secondProp).isEqualTo("foo")
}

evant avatar Sep 16 '21 14:09 evant

You'd use all to chain prop fyi

assertThat(something).all {
  prop(Something::firstProp).isEqualTo(42)
  prop(Something::secondProp).isEqualTo("foo")
}

indeed ... here's a comparison from my code:

propsEqualTo:

assertThat(foundUser).propsEqualTo(
    FoundUser::role to UserRole.DOCTOR.roleName,
    FoundUser::firstName to firstName,
    FoundUser::lastName to lastName
)

vs. all

assertThat(foundUser).all {
    prop(FoundUser::role).isEqualTo(UserRole.DOCTOR.roleName)
    prop(FoundUser::firstName).isEqualTo(firstName)
    prop(FoundUser::lastName).isEqualTo(lastName)
}

all is a bit wordier, however, it allows a mix of matchers (not forcing isEqualTo on every prop).

I'm currently inclined to drop this PR...

mbjelac avatar Sep 21 '21 06:09 mbjelac