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

68 lines
1.9 KiB
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 GetTest {
@Test
internal fun `get should return the result value if ok`() {
val value = ok(12).get()
assertThat(value, equalTo(12))
}
@Test
internal fun `get should return null if not ok`() {
2017-10-21 17:12:25 +00:00
val value = err("error").get()
2017-10-21 02:51:30 +00:00
assertThat(value, equalTo(null))
}
@Test
internal fun `getError should return null if ok`() {
val error = ok("example").getError()
assertThat(error, equalTo(null))
}
@Test
internal fun `getError should return the result error if not ok`() {
val error = err("example").getError()
assertThat(error, equalTo("example"))
}
@Test
internal fun `getOr should return the result value if ok`() {
val value = ok("hello").getOr("world")
2017-10-21 02:51:30 +00:00
assertThat(value, equalTo("hello"))
}
@Test
internal fun `getOr should return default value if not ok`() {
2017-10-21 17:12:25 +00:00
val value = err("error").getOr("default")
2017-10-21 02:51:30 +00:00
assertThat(value, equalTo("default"))
}
@Test
internal fun `getErrorOr should return the default value if ok`() {
val error = ok("hello").getErrorOr("world")
assertThat(error, equalTo("world"))
}
@Test
internal fun `getErrorOr should return the result error if not ok`() {
val error = err("hello").getErrorOr("world")
assertThat(error, equalTo("hello"))
}
@Test
internal fun `getOrElse should return the result value if ok`() {
val value = ok("hello").getOrElse { "world" }
assertThat(value, equalTo("hello"))
}
@Test
internal fun `getOrElse should return the transformed result error if ok`() {
val value = err("hello").getOrElse { "world" }
assertThat(value, equalTo("world"))
}
2017-10-21 02:51:30 +00:00
}