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

110 lines
2.6 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 kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
2017-10-21 02:51:30 +00:00
internal class GetTest {
internal class `get` {
@Test
internal fun returnsValueIfOk() {
assertEquals(
expected = 12,
actual = Ok(12).get()
)
}
2017-10-21 02:51:30 +00:00
@Test
internal fun returnsNullIfErr() {
assertNull(Err("error").get())
}
2017-10-21 02:51:30 +00:00
}
internal class `getError` {
@Test
internal fun returnsNullIfOk() {
assertNull(Ok("example").getError())
}
@Test
internal fun returnsErrorIfErr() {
assertEquals(
expected = "example",
actual = Err("example").getError()
)
}
}
internal class `getOr` {
@Test
internal fun returnsValueIfOk() {
assertEquals(
expected = "hello",
actual = Ok("hello").getOr("world")
)
}
2017-10-21 02:51:30 +00:00
@Test
internal fun returnsDefaultValueIfErr() {
assertEquals(
expected = "default",
actual = Err("error").getOr("default")
)
}
2017-10-21 02:51:30 +00:00
}
internal class `getErrorOr` {
@Test
internal fun returnsDefaultValueIfOk() {
assertEquals(
expected = "world",
actual = Ok("hello").getErrorOr("world")
)
}
@Test
internal fun returnsErrorIfErr() {
assertEquals(
expected = "hello",
actual = Err("hello").getErrorOr("world")
)
}
}
internal class `getOrElse` {
@Test
internal fun returnsValueIfOk() {
assertEquals(
expected = "hello",
actual = Ok("hello").getOrElse { "world" }
)
}
@Test
internal fun returnsTransformedErrorIfErr() {
assertEquals(
expected = "world",
actual = Err("hello").getOrElse { "world" }
)
}
}
internal class `getErrorOrElse` {
@Test
internal fun returnsTransformedValueIfOk() {
assertEquals(
expected = "world",
actual = Ok("hello").getErrorOrElse { "world" }
)
}
@Test
internal fun returnsErrorIfErr() {
assertEquals(
expected = "hello",
actual = Err("hello").getErrorOrElse { "world" }
)
}
}
2017-10-21 02:51:30 +00:00
}