assertk icon indicating copy to clipboard operation
assertk copied to clipboard

Add support for flat extracting elements

Open 22dasi1bwi opened this issue 3 years ago • 3 comments

I regularly find myself in a position where I have an Iterable of elements, where I want to extract a property which itself is an Iterable.

Up to this point, I needed to do the following:

assertThat(iterable).extracting { it.anotherIterable }.containsExactly(listOf(elementOne, elementTwo))

or in case of more complex logic

assertThat(iterable).transform { it.flatMap { result -> result.added + result.updated }.containsExactly(elementOne, elementTwo)

Having a function for flat mapping would ease that process:

assertThat(iterable).flatExtracting { it.anotherIterable}.containsExactly(elementOne, elementTwo)

or

assertThat(iterable).flatExtracting { result -> result.added + result.updated }.containsExactly(elementOne, elementTwo)

22dasi1bwi avatar Jul 05 '22 08:07 22dasi1bwi

PR at https://github.com/willowtreeapps/assertk/pull/425

22dasi1bwi avatar Jul 05 '22 09:07 22dasi1bwi

Little hesitant to adding something like this, is there some reason

assertThat(iterable.flatMap { it.anotherIterable }).containsExactly(elementOne, elementTwo)

wouldn't work?

evant avatar Nov 30 '23 20:11 evant

No, there isn't. With the variant I provided it just feels more natural in my opinion. The example you provided works, but it feels more like a workaround for something that should be directly included in the library - at least for me.

22dasi1bwi avatar Jan 22 '24 10:01 22dasi1bwi