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.assertSame
|
2017-10-21 02:51:30 +00:00
|
|
|
|
2018-11-01 19:02:57 +00:00
|
|
|
class AndTest {
|
2017-10-21 02:51:30 +00:00
|
|
|
private object AndError
|
|
|
|
|
2018-11-01 19:02:57 +00:00
|
|
|
class And {
|
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 = 500,
|
2018-01-09 20:33:00 +00:00
|
|
|
actual = Ok(230).and { Ok(500) }.get()
|
2017-12-16 19:30:54 +00:00
|
|
|
)
|
|
|
|
}
|
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 returnsValueIfErr() {
|
2017-12-16 19:30:54 +00:00
|
|
|
assertEquals(
|
|
|
|
expected = "hello world",
|
2018-01-09 20:33:00 +00:00
|
|
|
actual = Ok(300).and { Err("hello world") }.getError()
|
2017-12-16 19:30:54 +00:00
|
|
|
)
|
|
|
|
}
|
2017-10-21 15:52:29 +00:00
|
|
|
}
|
|
|
|
|
2018-11-01 19:02:57 +00:00
|
|
|
class AndThen {
|
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 = 12,
|
|
|
|
actual = Ok(5).andThen { Ok(it + 7) }.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 returnsErrorIfErr() {
|
2017-12-16 19:30:54 +00:00
|
|
|
assertSame(
|
|
|
|
expected = AndError,
|
|
|
|
actual = Ok(20).andThen { Ok(it + 43) }.andThen { Err(AndError) }.getError()!!
|
|
|
|
)
|
|
|
|
}
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
}
|