I have a function like this:
fun foo(a: Boolean, b: Boolean, c: Boolean) { /*...*/ }
and I wanted to test it with all possible argument values:
for (a in false..true)
for (b in false..true)
for (c in false..true) {
val expected = ...
assertThat(foo(a, b, c)).isEqualTo(expected)
}
But unfortunately Kotlin doesn't have a BooleanRange type.
I can do this instead:
for (a in 0..1)
for (b in 0..1)
for (c in 0..1) {
val expected = ...
assertThat(foo(a == 1, b == 1, c == 1)).isEqualTo(expected)
}
But this is not at all clear.
I can do this:
for (a in arrayOf(false, true))
for (b in arrayOf(false, true))
for (c in arrayOf(false, true)) {
val expected = ...
assertThat(foo(a, b, c)).isEqualTo(expected)
}
But this is still too verbose. Is there a neater way, maybe Kotlin has a function that iterates directly through the two values false and true? I see that it has a BooleanIterator type, but I'm not sure if that's any use here.
Thanks for all your answers (upvoted). In the end, I took broot's comment and settled for:
Although if I wanted it to be shorter, I could have done this: