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 com.natpryce.hamkrest.sameInstance
|
|
|
|
import org.junit.jupiter.api.Test
|
|
|
|
|
|
|
|
internal class AndTest {
|
|
|
|
private object AndError
|
|
|
|
|
2017-10-21 15:52:29 +00:00
|
|
|
@Test
|
|
|
|
internal fun `and should return the result value if ok`() {
|
2017-10-22 14:05:02 +00:00
|
|
|
val value = Ok(230).and(Ok(500)).get()
|
2017-10-21 15:52:29 +00:00
|
|
|
assertThat(value, equalTo(500))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `and should return the result value if not ok`() {
|
2017-10-22 14:05:02 +00:00
|
|
|
val error = Ok(300).and(Error("hello world")).getError()
|
2017-10-21 15:52:29 +00:00
|
|
|
assertThat(error, equalTo("hello world"))
|
|
|
|
}
|
|
|
|
|
2017-10-21 02:51:30 +00:00
|
|
|
@Test
|
|
|
|
internal fun `andThen should return the transformed result value if ok`() {
|
2017-10-22 14:05:02 +00:00
|
|
|
val value = Ok(5).andThen { Ok(it + 7) }.get()
|
2017-10-21 02:51:30 +00:00
|
|
|
assertThat(value, equalTo(12))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `andThen should return the result error if not ok`() {
|
2017-10-22 14:05:02 +00:00
|
|
|
val error = Ok(20).andThen { Ok(it + 43) }.andThen { Error(AndError) }.getError()!!
|
2017-10-21 15:52:29 +00:00
|
|
|
assertThat(error, sameInstance(AndError))
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
}
|