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
|
|
|
|
2018-11-01 19:02:57 +00:00
|
|
|
class GetTest {
|
|
|
|
class Get {
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsValueIfOk() {
|
2017-12-16 19:30:54 +00:00
|
|
|
assertEquals(
|
|
|
|
expected = 12,
|
|
|
|
actual = Ok(12).get()
|
|
|
|
)
|
|
|
|
}
|
2017-10-21 02:51:30 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsNullIfErr() {
|
2017-12-16 19:30:54 +00:00
|
|
|
assertNull(Err("error").get())
|
|
|
|
}
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 19:02:57 +00:00
|
|
|
class GetError {
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsNullIfOk() {
|
2017-12-16 19:30:54 +00:00
|
|
|
assertNull(Ok("example").getError())
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsErrorIfErr() {
|
2017-12-16 19:30:54 +00:00
|
|
|
assertEquals(
|
|
|
|
expected = "example",
|
|
|
|
actual = Err("example").getError()
|
|
|
|
)
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 19:02:57 +00:00
|
|
|
class GetOr {
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsValueIfOk() {
|
2017-12-16 19:30:54 +00:00
|
|
|
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
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsDefaultValueIfErr() {
|
2017-12-16 19:30:54 +00:00
|
|
|
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
|
|
|
|
2018-11-01 19:02:57 +00:00
|
|
|
class GetErrorOr {
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsDefaultValueIfOk() {
|
2017-12-16 19:30:54 +00:00
|
|
|
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
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsErrorIfErr() {
|
2017-12-16 19:30:54 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-01 19:02:57 +00:00
|
|
|
class GetOrElse {
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsValueIfOk() {
|
2017-12-16 19:30:54 +00:00
|
|
|
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
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsTransformedErrorIfErr() {
|
2017-12-16 19:30:54 +00:00
|
|
|
assertEquals(
|
|
|
|
expected = "world",
|
|
|
|
actual = Err("hello").getOrElse { "world" }
|
|
|
|
)
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
2017-11-22 23:25:11 +00:00
|
|
|
|
2018-11-01 19:02:57 +00:00
|
|
|
class GetErrorOrElse {
|
2017-12-16 19:30:54 +00:00
|
|
|
@Test
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsTransformedValueIfOk() {
|
2017-12-16 19:30:54 +00:00
|
|
|
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
|
2018-11-01 19:02:57 +00:00
|
|
|
fun returnsErrorIfErr() {
|
2017-12-16 19:30:54 +00:00
|
|
|
assertEquals(
|
|
|
|
expected = "hello",
|
|
|
|
actual = Err("hello").getErrorOrElse { "world" }
|
|
|
|
)
|
|
|
|
}
|
2017-11-22 23:25:11 +00:00
|
|
|
}
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|