Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class NullableBooleansTest {
enum class Result { OK, NOT }
private fun testNullableBooleanWhen(
nullableBoolean: Boolean?,
check: (Boolean?) -> Boolean,
resulting: Result
) {
val result = if (check(nullableBoolean)) {
Result.OK
} else {
Result.NOT
}
assertEquals(resulting, result)
}
@Test fun forEqualsTrue() {
val check: (Boolean?) -> Boolean = { nb -> nb == true }
testNullableBooleanWhen(true, check, Result.OK)
testNullableBooleanWhen(false, check, Result.NOT)
testNullableBooleanWhen(null, check, Result.NOT)
}
@Test fun forEqualsFalse() {
val check: (Boolean?) -> Boolean = { nb -> nb == false }
testNullableBooleanWhen(false, check, Result.OK)
testNullableBooleanWhen(true, check, Result.NOT)
testNullableBooleanWhen(null, check, Result.NOT)
}
@Test fun forNotEqualsTrue() {
val check: (Boolean?) -> Boolean = { nb -> nb != true }
testNullableBooleanWhen(false, check, Result.OK)
testNullableBooleanWhen(null, check, Result.OK)
testNullableBooleanWhen(true, check, Result.NOT)
}
@Test fun forNotEqualsFalse() {
val check: (Boolean?) -> Boolean = { nb -> nb != false }
testNullableBooleanWhen(true, check, Result.OK)
testNullableBooleanWhen(null, check, Result.OK)
testNullableBooleanWhen(false, check, Result.NOT)
}
@Test fun forEqualsNull() {
val check: (Boolean?) -> Boolean = { nb -> nb == null }
testNullableBooleanWhen(null, check, Result.OK)
testNullableBooleanWhen(false, check, Result.NOT)
testNullableBooleanWhen(true, check, Result.NOT)
}
@Test fun forNotEqualsNull() {
val check: (Boolean?) -> Boolean = { nb -> nb != null }
testNullableBooleanWhen(false, check, Result.OK)
testNullableBooleanWhen(true, check, Result.OK)
testNullableBooleanWhen(null, check, Result.NOT)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment