Add support for flat extracting elements
22dasi1bwi opened this issue · 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)
PR at #425
Little hesitant to adding something like this, is there some reason
assertThat(iterable.flatMap { it.anotherIterable }).containsExactly(elementOne, elementTwo)
wouldn't work?
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.