assertk icon indicating copy to clipboard operation
assertk copied to clipboard

isNotNullOrEmpty() for Iterables

Open josetorrs opened this issue 1 year ago • 4 comments
trafficstars

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()

josetorrs avatar Jan 17 '24 16:01 josetorrs

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.

JakeWharton avatar Mar 03 '24 13:03 JakeWharton

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

Goooler avatar Mar 03 '24 14:03 Goooler

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.

JakeWharton avatar Mar 03 '24 15:03 JakeWharton

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

Goooler avatar Mar 09 '24 04:03 Goooler