Use named arguments in unit tests

This commit is contained in:
Michael Bull 2017-10-21 04:21:56 +01:00
parent 3602e8dce1
commit faf315d395
3 changed files with 20 additions and 14 deletions

View File

@ -20,7 +20,10 @@ inline fun <T, R, E> Iterable<T>.fold(
return ok(accumulator)
}
inline fun <T, R, E> List<T>.foldRight(initial: R, operation: (T, acc: R) -> Result<R, E>): Result<R, E> {
inline fun <T, R, E> List<T>.foldRight(
initial: R,
operation: (T, acc: R) -> Result<R, E>
): Result<R, E> {
var accumulator = initial
if (!isEmpty()) {

View File

@ -26,7 +26,10 @@ inline fun <V, E, U> Result<V, E>.mapError(transform: (E) -> U): Result<V, U> {
* - Elm: [Result.Extra.mapBoth](http://package.elm-lang.org/packages/circuithub/elm-result-extra/1.4.0/Result-Extra#mapBoth)
* - Haskell: [Data.Either.either](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:either)
*/
inline fun <V, E, U> Result<V, E>.mapBoth(success: (V) -> U, failure: (E) -> U): U {
inline fun <V, E, U> Result<V, E>.mapBoth(
success: (V) -> U,
failure: (E) -> U
): U {
return when (this) {
is Ok -> success(value)
is Error -> failure(error)
@ -38,11 +41,11 @@ inline fun <V, E, U> Result<V, E>.mapBoth(success: (V) -> U, failure: (E) -> U):
* - Haskell: [Data.Bifunctor.Bimap](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:bimap)
*/
inline fun <V1, V2, E1, E2> Result<V1, E1>.mapEither(
okTransform: (V1) -> V2,
errorTransform: (E1) -> E2
success: (V1) -> V2,
failure: (E1) -> E2
): Result<V2, E2> {
return when (this) {
is Ok -> ok(okTransform(value))
is Error -> error(errorTransform(error))
is Ok -> ok(success(value))
is Error -> error(failure(error))
}
}

View File

@ -58,8 +58,8 @@ internal class MapTest {
@Test
internal fun `mapBoth should return the transformed result value if ok`() {
val value = ok("there is").mapBoth(
{ "$it a light" },
{ MapError.CustomError("$it that never") }
success = { "$it a light" },
failure = { MapError.CustomError("$it that never") }
) as String
assertThat(value, equalTo("there is a light"))
@ -68,8 +68,8 @@ internal class MapTest {
@Test
internal fun `mapBoth should return the transformed result error if not ok`() {
val error = error(MapError.CustomError("this")).mapBoth(
{ "$it charming" },
{ MapError.CustomError("${it.reason} man") }
success = { "$it charming" },
failure = { MapError.CustomError("${it.reason} man") }
) as MapError.CustomError
assertThat(error.reason, equalTo("this man"))
@ -78,8 +78,8 @@ internal class MapTest {
@Test
internal fun `mapEither should return the transformed result value if ok`() {
val result = ok(500).mapEither(
{ it + 500 },
{ MapError.CustomError(it) }
success = { it + 500 },
failure = { MapError.CustomError(it) }
)
result as Ok
@ -90,8 +90,8 @@ internal class MapTest {
@Test
internal fun `mapEither should return the transformed result error if not ok`() {
val result = error("the reckless").mapEither(
{ "the wild youth" },
{ MapError.CustomError("the truth") }
success = { "the wild youth" },
failure = { MapError.CustomError("the truth") }
)
result as Error