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

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