https://kotlinlang.org logo
#kotest
Title
# kotest
h

Henning B

08/10/2021, 10:07 AM
any nicer way to write something like this?
Copy code
list.none { it is State.StateA } shouldBe true
Goal is, to check if the list does not contain an element which matches a given predicate
r

raulraja

08/10/2021, 10:15 AM
Hi @Henning B, that looks good to me but perhaps if you have a known value then you may alternatively use
!in
Copy code
val x: Boolean = listOf(1, 2, 3).none { it == 1 } //false
val y: Boolean = 1 !in listOf(1, 2, 3) //false
t

Tobi M.

08/10/2021, 10:41 AM
Maybe:
Copy code
list shouldNot singleElement { it is State.StateA }
You could also extract this into another function like
Copy code
infix fun <T> Collection<T>.shouldNotHaveSingleElement(t: T) = this shouldNot singleElement(t)
s

sam

08/10/2021, 12:47 PM
There is always .forNone too
h

Henning B

08/10/2021, 2:58 PM
Thanks!
I ended up with:
Copy code
states.forAll { state -> state.shouldNotBeTypeOf<State.StateA>() }
e

Emil Kantis

08/16/2021, 12:32 PM
somewhat related, perhaps you have some opinions to add https://github.com/kotest/kotest/pull/2321 🙂