willowtreeapps / assertk

assertions for kotlin inspired by assertj
MIT License
757 stars 84 forks source link

isNotNullOrEmpty() for Iterables #512

Open josetorrs opened 7 months ago

josetorrs commented 7 months ago

Currently the official way in the documentation is to first call .isNotNull() and then .isNotEmpty() . Can there be a new method that combines them? I know you can write a custom assertion but would be nice to have it built-in. Happy to open a pull request.

Example:

from:

val foo = listOf("hello", "world")

assertThat(foo).isNotNull().isNotEmpty()

to:

val foo = listOf("hello", "world")

assertThat(foo).isNotNullOrEmpty()
JakeWharton commented 5 months ago

I don't think they should be combined. One is a general assertion which refines the type, whereas the other is an assertion on the contents of a collection.

Goooler commented 5 months ago

I believe it could be treated as the inverted of isNullOrEmpty.

JakeWharton commented 5 months ago

That assertion is impossible to otherwise represent though. It requires #450.

Asserting not null or empty is worded as not(null or empty) but that is logically equivalent to not(null) and not(empty). And and assertions are expressed separate functions because you can use as many in succession as you want.

Goooler commented 5 months ago

Reasonable. It looks like isNullOrEmpty().not(), this might be why isNotNullOrEmpty hasn't been added to Kotlin stdlib.