2017-10-21 23:59:16 +00:00
|
|
|
package com.github.michaelbull.result
|
2017-10-21 02:51:30 +00:00
|
|
|
|
2017-12-16 19:30:54 +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 {
|
2017-12-16 19:30:54 +00:00
|
|
|
internal class `get` {
|
|
|
|
@Test
|
|
|
|
internal fun returnsValueIfOk() {
|
|
|
|
assertEquals(
|
|
|
|
expected = 12,
|
|
|
|
actual = Ok(12).get()
|
|
|
|
)
|
|
|
|
}
|
2017-10-21 02:51:30 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
|
|
|
internal fun returnsNullIfErr() {
|
|
|
|
assertNull(Err("error").get())
|
|
|
|
}
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
internal class `getError` {
|
|
|
|
@Test
|
|
|
|
internal fun returnsNullIfOk() {
|
|
|
|
assertNull(Ok("example").getError())
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
|
|
|
internal fun returnsErrorIfErr() {
|
|
|
|
assertEquals(
|
|
|
|
expected = "example",
|
|
|
|
actual = Err("example").getError()
|
|
|
|
)
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
internal class `getOr` {
|
|
|
|
@Test
|
|
|
|
internal fun returnsValueIfOk() {
|
|
|
|
assertEquals(
|
|
|
|
expected = "hello",
|
2018-01-09 20:33:00 +00:00
|
|
|
actual = Ok("hello").getOr { "world" }
|
2017-12-16 19:30:54 +00:00
|
|
|
)
|
|
|
|
}
|
2017-10-21 02:51:30 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
|
|
|
internal fun returnsDefaultValueIfErr() {
|
|
|
|
assertEquals(
|
|
|
|
expected = "default",
|
2018-01-09 20:33:00 +00:00
|
|
|
actual = Err("error").getOr { "default" }
|
2017-12-16 19:30:54 +00:00
|
|
|
)
|
|
|
|
}
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
internal class `getErrorOr` {
|
|
|
|
@Test
|
|
|
|
internal fun returnsDefaultValueIfOk() {
|
|
|
|
assertEquals(
|
|
|
|
expected = "world",
|
2018-01-09 20:33:00 +00:00
|
|
|
actual = Ok("hello").getErrorOr { "world" }
|
2017-12-16 19:30:54 +00:00
|
|
|
)
|
|
|
|
}
|
2017-10-21 17:10:08 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
|
|
|
internal fun returnsErrorIfErr() {
|
|
|
|
assertEquals(
|
|
|
|
expected = "hello",
|
2018-01-09 20:33:00 +00:00
|
|
|
actual = Err("hello").getErrorOr { "world" }
|
2017-12-16 19:30:54 +00:00
|
|
|
)
|
|
|
|
}
|
2017-10-21 17:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
internal class `getOrElse` {
|
|
|
|
@Test
|
|
|
|
internal fun returnsValueIfOk() {
|
|
|
|
assertEquals(
|
|
|
|
expected = "hello",
|
|
|
|
actual = Ok("hello").getOrElse { "world" }
|
|
|
|
)
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
|
|
|
internal fun returnsTransformedErrorIfErr() {
|
|
|
|
assertEquals(
|
|
|
|
expected = "world",
|
|
|
|
actual = Err("hello").getOrElse { "world" }
|
|
|
|
)
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
2017-11-22 23:25:11 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
internal class `getErrorOrElse` {
|
|
|
|
@Test
|
|
|
|
internal fun returnsTransformedValueIfOk() {
|
|
|
|
assertEquals(
|
|
|
|
expected = "world",
|
|
|
|
actual = Ok("hello").getErrorOrElse { "world" }
|
|
|
|
)
|
|
|
|
}
|
2017-11-22 23:25:11 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
|
|
|
internal fun returnsErrorIfErr() {
|
|
|
|
assertEquals(
|
|
|
|
expected = "hello",
|
|
|
|
actual = Err("hello").getErrorOrElse { "world" }
|
|
|
|
)
|
|
|
|
}
|
2017-11-22 23:25:11 +00:00
|
|
|
}
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|