-
Notifications
You must be signed in to change notification settings - Fork 437
/
OptionSpec.kt
50 lines (45 loc) · 1.32 KB
/
OptionSpec.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package arrow.core.continuations
import arrow.core.None
import arrow.core.toOption
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.orNull
import io.kotest.property.checkAll
class OptionSpec : StringSpec({
"ensureNotNull in option computation" {
fun square(i: Int): Int = i * i
checkAll(Arb.int().orNull()) { i: Int? ->
option {
val ii = i
ensureNotNull(ii)
square(ii) // Smart-cast by contract
} shouldBe i.toOption().map(::square)
}
}
"short circuit option" {
option {
val number: Int = "s".length
ensureNotNull(number.takeIf { it > 1 })
throw IllegalStateException("This should not be executed")
} shouldBe None
}
"ensureNotNull in eager option computation" {
fun square(i: Int): Int = i * i
checkAll(Arb.int().orNull()) { i: Int? ->
option {
val ii = i
ensureNotNull(ii)
square(ii) // Smart-cast by contract
} shouldBe i.toOption().map(::square)
}
}
"eager short circuit null" {
option.eager {
val number: Int = "s".length
ensureNotNull(number.takeIf { it > 1 })
throw IllegalStateException("This should not be executed")
} shouldBe None
}
})