kotlin-result/src/test/kotlin/com/github/michaelbull/result/OrTest.kt

34 lines
929 B
Kotlin
Raw Normal View History

2017-10-21 23:59:16 +00:00
package com.github.michaelbull.result
2017-10-21 02:51:30 +00:00
import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import org.junit.jupiter.api.Test
internal class OrTest {
private object OrError
@Test
internal fun `or should return the result value if ok`() {
val value = ok(500).or(ok(1000)).get()
2017-10-21 02:51:30 +00:00
assertThat(value, equalTo(500))
}
@Test
internal fun `or should return the default value if not ok`() {
2017-10-21 18:04:23 +00:00
val value = err(OrError).or(ok(5000)).get()
assertThat(value, equalTo(5000))
2017-10-21 02:51:30 +00:00
}
@Test
internal fun `orElse should return the result value if ok`() {
val value = ok(3000).orElse { ok(4000) }.get()
assertThat(value, equalTo(3000))
2017-10-21 02:51:30 +00:00
}
@Test
internal fun `orElse should return the transformed value if not ok`() {
2017-10-21 18:04:23 +00:00
val value = err(4000).orElse { ok(2000) }.get()
assertThat(value, equalTo(2000))
2017-10-21 02:51:30 +00:00
}
}