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 GetTest {
|
|
|
|
@Test
|
|
|
|
internal fun `get should return the result value if ok`() {
|
2017-10-22 14:05:02 +00:00
|
|
|
val value = Ok(12).get()
|
2017-10-21 02:51:30 +00:00
|
|
|
assertThat(value, equalTo(12))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `get should return null if not ok`() {
|
2017-10-22 15:01:05 +00:00
|
|
|
val value = Err("error").get()
|
2017-10-21 02:51:30 +00:00
|
|
|
assertThat(value, equalTo(null))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-10-21 15:52:29 +00:00
|
|
|
internal fun `getError should return null if ok`() {
|
2017-10-22 14:05:02 +00:00
|
|
|
val error = Ok("example").getError()
|
2017-10-21 15:52:29 +00:00
|
|
|
assertThat(error, equalTo(null))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `getError should return the result error if not ok`() {
|
2017-10-22 15:01:05 +00:00
|
|
|
val error = Err("example").getError()
|
2017-10-21 15:52:29 +00:00
|
|
|
assertThat(error, equalTo("example"))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `getOr should return the result value if ok`() {
|
2017-10-22 14:05:02 +00:00
|
|
|
val value = Ok("hello").getOr("world")
|
2017-10-21 02:51:30 +00:00
|
|
|
assertThat(value, equalTo("hello"))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-10-21 15:52:29 +00:00
|
|
|
internal fun `getOr should return default value if not ok`() {
|
2017-10-22 15:01:05 +00:00
|
|
|
val value = Err("error").getOr("default")
|
2017-10-21 02:51:30 +00:00
|
|
|
assertThat(value, equalTo("default"))
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
|
2017-10-21 17:10:08 +00:00
|
|
|
@Test
|
|
|
|
internal fun `getErrorOr should return the default value if ok`() {
|
2017-10-22 14:05:02 +00:00
|
|
|
val error = Ok("hello").getErrorOr("world")
|
2017-10-21 17:10:08 +00:00
|
|
|
assertThat(error, equalTo("world"))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `getErrorOr should return the result error if not ok`() {
|
2017-10-22 15:01:05 +00:00
|
|
|
val error = Err("hello").getErrorOr("world")
|
2017-10-21 17:10:08 +00:00
|
|
|
assertThat(error, equalTo("hello"))
|
|
|
|
}
|
|
|
|
|
2017-10-21 15:52:29 +00:00
|
|
|
@Test
|
|
|
|
internal fun `getOrElse should return the result value if ok`() {
|
2017-10-22 14:05:02 +00:00
|
|
|
val value = Ok("hello").getOrElse { "world" }
|
2017-10-21 15:52:29 +00:00
|
|
|
assertThat(value, equalTo("hello"))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `getOrElse should return the transformed result error if ok`() {
|
2017-10-22 15:01:05 +00:00
|
|
|
val value = Err("hello").getOrElse { "world" }
|
2017-10-21 15:52:29 +00:00
|
|
|
assertThat(value, equalTo("world"))
|
|
|
|
}
|
2017-11-22 23:25:11 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `getErrorOrElse should return the transformed result value if ok`() {
|
|
|
|
val error = Ok("hello").getErrorOrElse { "world" }
|
|
|
|
assertThat(error, equalTo("world"))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `getErrorOrElse should return the result error if not ok`() {
|
|
|
|
val error = Err("hello").getErrorOrElse { "world" }
|
|
|
|
assertThat(error, equalTo("hello"))
|
|
|
|
}
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|