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
|
2017-10-21 02:51:30 +00:00
|
|
|
|
|
|
|
internal class OrTest {
|
|
|
|
private object OrError
|
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
internal class `or` {
|
|
|
|
@Test
|
|
|
|
internal fun returnsValueIfOk() {
|
|
|
|
assertEquals(
|
|
|
|
expected = 500,
|
2018-01-09 20:33:00 +00:00
|
|
|
actual = Ok(500).or { Ok(1000) }.get()
|
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 = 5000,
|
2018-01-09 20:33:00 +00:00
|
|
|
actual = Err(OrError).or { Ok(5000) }.get()
|
2017-12-16 19:30:54 +00:00
|
|
|
)
|
|
|
|
}
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
internal class `orElse` {
|
|
|
|
@Test
|
|
|
|
internal fun returnsValueIfOk() {
|
|
|
|
assertEquals(
|
|
|
|
expected = 3000,
|
|
|
|
actual = Ok(3000).orElse { Ok(4000) }.get()
|
|
|
|
)
|
|
|
|
}
|
2017-10-21 02:51:30 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
|
|
|
internal fun returnsTransformedValueIfErr() {
|
|
|
|
assertEquals(
|
|
|
|
expected = 2000,
|
|
|
|
actual = Err(4000).orElse { Ok(2000) }.get()
|
|
|
|
)
|
|
|
|
}
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
2018-11-01 11:42:09 +00:00
|
|
|
|
|
|
|
internal class `recover` {
|
|
|
|
@Test
|
|
|
|
internal fun returnsValueIfOk() {
|
|
|
|
assertEquals(
|
|
|
|
expected = 3000,
|
|
|
|
actual = Ok(3000).recover { 4000 }.get()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun returnsTransformedValueIfErr() {
|
|
|
|
assertEquals(
|
|
|
|
expected = 2000,
|
|
|
|
actual = Err(4000).recover { 2000 }.get()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|