2017-10-21 02:51:30 +00:00
|
|
|
package com.mikebull94.result
|
|
|
|
|
|
|
|
import com.natpryce.hamkrest.Matcher
|
|
|
|
import com.natpryce.hamkrest.assertion.assertThat
|
|
|
|
import com.natpryce.hamkrest.equalTo
|
|
|
|
import com.natpryce.hamkrest.sameInstance
|
|
|
|
import org.junit.jupiter.api.Test
|
|
|
|
|
|
|
|
internal class MapTest {
|
|
|
|
private sealed class MapError(val reason: String) {
|
|
|
|
object HelloError : MapError("hello")
|
|
|
|
object WorldError : MapError("world")
|
|
|
|
class CustomError(reason: String) : MapError(reason)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun sameError(error: MapError): Matcher<MapError> {
|
|
|
|
return sameInstance(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `map should return the transformed result value if ok`() {
|
|
|
|
val value = ok(10).map { it + 20 }.get()
|
|
|
|
assertThat(value, equalTo(30))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `map should return the result error if not ok`() {
|
|
|
|
val result = error(MapError.HelloError).map { "hello $it" }
|
|
|
|
|
|
|
|
result as Error
|
|
|
|
|
|
|
|
assertThat(result.error, sameError(MapError.HelloError))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `mapError should return the result value if ok`() {
|
|
|
|
val value = ok(55).map { it + 15 }.mapError { MapError.WorldError }.get()
|
|
|
|
assertThat(value, equalTo(70))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `mapError should return the transformed result error if not ok`() {
|
|
|
|
val result: Result<String, MapError> = ok("let")
|
|
|
|
.map { "$it me" }
|
|
|
|
.andThen {
|
|
|
|
when (it) {
|
|
|
|
"let me" -> error(MapError.CustomError("$it $it"))
|
|
|
|
else -> ok("$it get")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.mapError { MapError.CustomError("${it.reason} get what i want") }
|
|
|
|
|
|
|
|
result as Error
|
|
|
|
|
|
|
|
assertThat(result.error.reason, equalTo("let me let me get what i want"))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `mapBoth should return the transformed result value if ok`() {
|
|
|
|
val value = ok("there is").mapBoth(
|
2017-10-21 03:21:56 +00:00
|
|
|
success = { "$it a light" },
|
2017-10-21 03:29:18 +00:00
|
|
|
failure = { "$it that never" }
|
|
|
|
)
|
2017-10-21 02:51:30 +00:00
|
|
|
|
|
|
|
assertThat(value, equalTo("there is a light"))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `mapBoth should return the transformed result error if not ok`() {
|
|
|
|
val error = error(MapError.CustomError("this")).mapBoth(
|
2017-10-21 03:21:56 +00:00
|
|
|
success = { "$it charming" },
|
2017-10-21 03:29:18 +00:00
|
|
|
failure = { "${it.reason} man" }
|
|
|
|
)
|
2017-10-21 02:51:30 +00:00
|
|
|
|
2017-10-21 03:29:18 +00:00
|
|
|
assertThat(error, equalTo("this man"))
|
2017-10-21 02:51:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `mapEither should return the transformed result value if ok`() {
|
|
|
|
val result = ok(500).mapEither(
|
2017-10-21 03:21:56 +00:00
|
|
|
success = { it + 500 },
|
2017-10-21 03:29:18 +00:00
|
|
|
failure = { MapError.CustomError("$it") }
|
2017-10-21 02:51:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
result as Ok
|
|
|
|
|
|
|
|
assertThat(result.value, equalTo(1000))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
internal fun `mapEither should return the transformed result error if not ok`() {
|
|
|
|
val result = error("the reckless").mapEither(
|
2017-10-21 03:21:56 +00:00
|
|
|
success = { "the wild youth" },
|
|
|
|
failure = { MapError.CustomError("the truth") }
|
2017-10-21 02:51:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
result as Error
|
|
|
|
|
|
|
|
assertThat(result.error.reason, equalTo("the truth"))
|
|
|
|
}
|
|
|
|
}
|