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) 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 var accumulator = initial
if (!isEmpty()) { 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) * - 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) * - 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) { return when (this) {
is Ok -> success(value) is Ok -> success(value)
is Error -> failure(error) 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) * - 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( inline fun <V1, V2, E1, E2> Result<V1, E1>.mapEither(
okTransform: (V1) -> V2, success: (V1) -> V2,
errorTransform: (E1) -> E2 failure: (E1) -> E2
): Result<V2, E2> { ): Result<V2, E2> {
return when (this) { return when (this) {
is Ok -> ok(okTransform(value)) is Ok -> ok(success(value))
is Error -> error(errorTransform(error)) is Error -> error(failure(error))
} }
} }

View File

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