diff --git a/README.md b/README.md index 4cbb707..0382c6a 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![Release](https://jitpack.io/v/michaelbull/kotlin-result.svg)](https://jitpack.io/#michaelbull/kotlin-result) [![Build Status](https://travis-ci.org/michaelbull/kotlin-result.svg?branch=master)](https://travis-ci.org/michaelbull/kotlin-result) [![License](https://img.shields.io/github/license/michaelbull/kotlin-result.svg)](https://github.com/michaelbull/kotlin-result/blob/master/LICENSE) [`Result`][result] is a monad for modelling success ([`Ok`][result-ok]) or -failure ([`Error`][result-error]) operations. +failure ([`Err`][result-err]) operations. ## Inspiration @@ -71,7 +71,7 @@ This project is available under the terms of the ISC license. See the [result]: https://github.com/michaelbull/kotlin-result/blob/master/src/main/kotlin/com/github/michaelbull/result/Result.kt#L10 [result-ok]: https://github.com/michaelbull/kotlin-result/blob/master/src/main/kotlin/com/github/michaelbull/result/Result.kt#L27 -[result-error]: https://github.com/michaelbull/kotlin-result/blob/master/src/main/kotlin/com/github/michaelbull/result/Result.kt#L28 +[result-err]: https://github.com/michaelbull/kotlin-result/blob/master/src/main/kotlin/com/github/michaelbull/result/Result.kt#L28 [unit-tests]: https://github.com/michaelbull/kotlin-result/tree/master/src/test/kotlin/com/github/michaelbull/result [wiki]: https://github.com/michaelbull/kotlin-result/wiki [wiki-elm]: https://github.com/michaelbull/kotlin-result/wiki/Elm diff --git a/gradle.properties b/gradle.properties index 2806514..00a9df5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group=com.github.michaelbull.kotlin-result -version=1.1.1-SNAPSHOT +version=1.0.0-SNAPSHOT dokkaVersion=0.9.15 gradleReleaseVersion=2.6.0 diff --git a/src/main/kotlin/com/github/michaelbull/result/And.kt b/src/main/kotlin/com/github/michaelbull/result/And.kt index ab26a83..310592d 100644 --- a/src/main/kotlin/com/github/michaelbull/result/And.kt +++ b/src/main/kotlin/com/github/michaelbull/result/And.kt @@ -4,12 +4,12 @@ package com.github.michaelbull.result * - Rust: [Result.and](https://doc.rust-lang.org/std/result/enum.Result.html#method.and) * * @param result The [Result] to return if [Ok]. - * @return The [result] if [Ok], otherwise [Error]. + * @return The [result] if [Ok], otherwise [Err]. */ infix fun Result.and(result: Result): Result { return when (this) { is Ok -> result - is Error -> this + is Err -> this } } @@ -18,11 +18,11 @@ infix fun Result.and(result: Result): Result { * - Rust: [Result.and_then](https://doc.rust-lang.org/std/result/enum.Result.html#method.and_then) * * @param transform The transformation to apply to the [value][Ok.value]. - * @return The [transformed][transform] [Result] if [Ok], otherwise [Error]. + * @return The [transformed][transform] [Result] if [Ok], otherwise [Err]. */ infix inline fun Result.andThen(transform: (V) -> Result): Result { return when (this) { is Ok -> transform(value) - is Error -> this + is Err -> this } } diff --git a/src/main/kotlin/com/github/michaelbull/result/Get.kt b/src/main/kotlin/com/github/michaelbull/result/Get.kt index ab200f4..d4e13ee 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Get.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Get.kt @@ -9,19 +9,19 @@ package com.github.michaelbull.result fun Result.get(): V? { return when (this) { is Ok -> value - is Error -> null + is Err -> null } } /** * - Rust: [Result.err](https://doc.rust-lang.org/std/result/enum.Result.html#method.err) * - * @return The [error][Error.error] if [Error], otherwise `null`. + * @return The [error][Err.error] if [Err], otherwise `null`. */ fun Result.getError(): E? { return when (this) { is Ok -> null - is Error -> error + is Err -> error } } @@ -30,13 +30,13 @@ fun Result.getError(): E? { * - Haskell: [Result.fromLeft](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:fromLeft) * - Rust: [Result.unwrap_or](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or) * - * @param default The value to return if [Error]. + * @param default The value to return if [Err]. * @return The [value][Ok.value] if [Ok], otherwise [default]. */ infix fun Result.getOr(default: V): V { return when (this) { is Ok -> value - is Error -> default + is Err -> default } } @@ -44,12 +44,12 @@ infix fun Result.getOr(default: V): V { * - Haskell: [Result.fromRight](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:fromRight) * * @param default The error to return if [Ok]. - * @return The [error][Error.error] if [Error], otherwise [default]. + * @return The [error][Err.error] if [Err], otherwise [default]. */ infix fun Result.getErrorOr(default: E): E { return when (this) { is Ok -> default - is Error -> error + is Err -> error } } @@ -57,12 +57,12 @@ infix fun Result.getErrorOr(default: E): E { * - Elm: [Result.extract](http://package.elm-lang.org/packages/circuithub/elm-result-extra/1.4.0/Result-Extra#extract) * - Rust: [Result.unwrap_or_else](https://doc.rust-lang.org/src/core/result.rs.html#735-740) * - * @param transform The transformation to apply to the [error][Error.error]. - * @return The [value][Ok.value] if [Ok], otherwise the [transformed][transform] [error][Error.error]. + * @param transform The transformation to apply to the [error][Err.error]. + * @return The [value][Ok.value] if [Ok], otherwise the [transformed][transform] [error][Err.error]. */ infix inline fun Result.getOrElse(transform: (E) -> V): V { return when (this) { is Ok -> value - is Error -> transform(error) + is Err -> transform(error) } } diff --git a/src/main/kotlin/com/github/michaelbull/result/Iterable.kt b/src/main/kotlin/com/github/michaelbull/result/Iterable.kt index 7805c30..3554cda 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Iterable.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Iterable.kt @@ -20,7 +20,7 @@ inline fun Iterable.fold( is Ok -> { accumulator = operationResult.value } - is Error -> return Error(operationResult.error) + is Err -> return Err(operationResult.error) } } @@ -49,7 +49,7 @@ inline fun List.foldRight( is Ok -> { accumulator = operationResult.value } - is Error -> return Error(operationResult.error) + is Err -> return Err(operationResult.error) } } } @@ -78,7 +78,7 @@ fun Iterable>.combine(): Result, E> { return Ok(map { when (it) { is Ok -> it.value - is Error -> return it + is Err -> return it } }) } @@ -107,50 +107,50 @@ fun Iterable>.getAll(): List { } /** - * Extracts from a vararg of [Results][Result] all the [Error] elements. All the [Error] elements + * Extracts from a vararg of [Results][Result] all the [Err] elements. All the [Err] elements * are extracted in order. * * - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights) * - * @param results The [Results][Result] from which to extract [Error] elements. - * @return The extracted [Error] elements. + * @param results The [Results][Result] from which to extract [Err] elements. + * @return The extracted [Err] elements. */ fun getAllErrors(vararg results: Result) = results.asIterable().getAllErrors() /** - * Extracts from an [Iterable] of [Results][Result] all the [Error] elements. All the [Error] + * Extracts from an [Iterable] of [Results][Result] all the [Err] elements. All the [Err] * elements are extracted in order. * * - Haskell: [Data.Either.rights](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:rights) * - * @return The extracted [Error] elements. + * @return The extracted [Err] elements. */ fun Iterable>.getAllErrors(): List { - return filterIsInstance>().map { it.error } + return filterIsInstance>().map { it.error } } /** * Partitions a vararg of [Results][Result] into a [Pair] of [Lists][List]. All the [Ok] elements - * are extracted, in order, to the [first][Pair.first] value. Similarly the [Error] elements are + * are extracted, in order, to the [first][Pair.first] value. Similarly the [Err] elements are * extracted to the [Pair.second] value. * * - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers) * * @param results The [Results][Result] to partition. * @return A [Pair] of [Lists][List] where the [first][Pair.first] value - * contains the [Ok] elements and the [second][Pair.second] value contains the [Error] elements. + * contains the [Ok] elements and the [second][Pair.second] value contains the [Err] elements. */ fun partition(vararg results: Result) = results.asIterable().partition() /** * Partitions an [Iterable] of [Results][Result] into a [Pair] of [Lists][List]. All the [Ok] - * elements are extracted, in order, to the [first][Pair.first] value. Similarly the [Error] + * elements are extracted, in order, to the [first][Pair.first] value. Similarly the [Err] * elements are extracted to the [Pair.second] value. * * - Haskell: [Data.Either.partitionEithers](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html#v:partitionEithers) * * @return A [Pair] of [Lists][List] where the [first][Pair.first] value - * contains the [Ok] elements and the [second][Pair.second] value contains the [Error] elements. + * contains the [Ok] elements and the [second][Pair.second] value contains the [Err] elements. */ fun Iterable>.partition(): Pair, List> { val values = mutableListOf() @@ -159,7 +159,7 @@ fun Iterable>.partition(): Pair, List> { forEach { result -> when (result) { is Ok -> values.add(result.value) - is Error -> errors.add(result.error) + is Err -> errors.add(result.error) } } diff --git a/src/main/kotlin/com/github/michaelbull/result/Map.kt b/src/main/kotlin/com/github/michaelbull/result/Map.kt index 3a4d902..fd41253 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Map.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Map.kt @@ -2,50 +2,50 @@ package com.github.michaelbull.result /** * Maps a [Result][Result] to [Result][Result] by applying a function to a contained - * [Ok] value, leaving an [Error] value untouched. + * [Ok] value, leaving an [Err] value untouched. * * - Elm: [Result.map](http://package.elm-lang.org/packages/elm-lang/core/latest/Result#map) * - Haskell: [Data.Bifunctor.first](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:first) * - Rust: [Result.map](https://doc.rust-lang.org/std/result/enum.Result.html#method.map) * * @param transform The transformation to apply to the [value][Ok.value] - * @return The [transformed][transform] [Result] if [Ok], otherwise [Error]. + * @return The [transformed][transform] [Result] if [Ok], otherwise [Err]. */ infix inline fun Result.map(transform: (V) -> U): Result { return when (this) { is Ok -> Ok(transform(value)) - is Error -> this + is Err -> this } } /** * Maps a [Result][Result] to [Result][Result] by applying a function to a contained - * [Error] value, leaving an [Ok] value untouched. + * [Err] value, leaving an [Ok] value untouched. * * - Elm: [Result.mapError](http://package.elm-lang.org/packages/elm-lang/core/latest/Result#mapError) * - Haskell: [Data.Bifunctor.right](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:second) * - Rust: [Result.map_err](https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err) * - * @param transform The transformation to apply to the [error][Error.error]. - * @return The [value][Ok.value] if [Ok], otherwise the [transformed][transform] [Error]. + * @param transform The transformation to apply to the [error][Err.error]. + * @return The [value][Ok.value] if [Ok], otherwise the [transformed][transform] [Err]. */ infix inline fun Result.mapError(transform: (E) -> F): Result { return when (this) { is Ok -> this - is Error -> Error(transform(error)) + is Err -> Err(transform(error)) } } /** * Map a [Result][Result] to `U` by applying either the [success] function if the [Result] - * is [Ok] or the [failure] function if the [Result] is an [Error]. Both of these functions must + * is [Ok] or the [failure] function if the [Result] is an [Err]. Both of these functions must * return the same type (`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) * * @param success The function to apply to `V` if [Ok]. - * @param failure The function to apply to `E` if [Error]. + * @param failure The function to apply to `E` if [Err]. * @return The mapped value. */ inline fun Result.mapBoth( @@ -54,19 +54,19 @@ inline fun Result.mapBoth( ): U { return when (this) { is Ok -> success(value) - is Error -> failure(error) + is Err -> failure(error) } } // TODO: better name? /** * Map a [Result][Result] to [Result][Result] by applying either the [success] function - * if the [Result] is [Ok] or the [failure] function if the [Result] is an [Error]. + * if the [Result] is [Ok] or the [failure] function if the [Result] is an [Err]. * * - Haskell: [Data.Bifunctor.Bimap](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Bifunctor.html#v:bimap) * * @param success The function to apply to `V` if [Ok]. - * @param failure The function to apply to `E` if [Error]. + * @param failure The function to apply to `E` if [Err]. * @return The mapped [Result]. */ inline fun Result.mapEither( @@ -75,6 +75,6 @@ inline fun Result.mapEither( ): Result { return when (this) { is Ok -> Ok(success(value)) - is Error -> Error(failure(error)) + is Err -> Err(failure(error)) } } diff --git a/src/main/kotlin/com/github/michaelbull/result/On.kt b/src/main/kotlin/com/github/michaelbull/result/On.kt index 9e5c437..09f0fec 100644 --- a/src/main/kotlin/com/github/michaelbull/result/On.kt +++ b/src/main/kotlin/com/github/michaelbull/result/On.kt @@ -7,7 +7,7 @@ package com.github.michaelbull.result fun Result.onSuccess(callback: (V) -> Unit) = mapBoth(callback, {}) /** - * Calls a [callback] if the [Result] is [Error]. + * Calls a [callback] if the [Result] is [Err]. * @param callback The function to call. */ fun Result.onFailure(callback: (E) -> Unit) = mapBoth({}, callback) diff --git a/src/main/kotlin/com/github/michaelbull/result/Or.kt b/src/main/kotlin/com/github/michaelbull/result/Or.kt index 0ee7d53..3fbd0bb 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Or.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Or.kt @@ -3,25 +3,25 @@ package com.github.michaelbull.result /** * - Rust: [Result.or](https://doc.rust-lang.org/std/result/enum.Result.html#method.or) * - * @param result The [Result] to return if [Error]. - * @return The [result] if [Error], otherwise [Ok]. + * @param result The [Result] to return if [Err]. + * @return The [result] if [Err], otherwise [Ok]. */ infix fun Result.or(result: Result): Result { return when (this) { is Ok -> this - is Error -> result + is Err -> result } } /** * - Rust: [Result.or_else](https://doc.rust-lang.org/std/result/enum.Result.html#method.or_else) * - * @param transform The transformation to apply to the [error][Error.error]. - * @return The [transformed][transform] [Result] if [Error], otherwise [Ok]. + * @param transform The transformation to apply to the [error][Err.error]. + * @return The [transformed][transform] [Result] if [Err], otherwise [Ok]. */ infix inline fun Result.orElse(transform: (E) -> Result): Result { return when (this) { is Ok -> this - is Error -> transform(error) + is Err -> transform(error) } } diff --git a/src/main/kotlin/com/github/michaelbull/result/Result.kt b/src/main/kotlin/com/github/michaelbull/result/Result.kt index 9f3b1f9..9e8a9c5 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Result.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Result.kt @@ -1,7 +1,7 @@ package com.github.michaelbull.result /** - * [Result] is a type that represents either success ([Ok]) or failure ([Error]). + * [Result] is a type that represents either success ([Ok]) or failure ([Err]). * * - Elm: [Result](http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Result) * - Haskell: [Data.Either](https://hackage.haskell.org/package/base-4.10.0.0/docs/Data-Either.html) @@ -11,18 +11,18 @@ sealed class Result { companion object { /** - * Invokes a [function] and wraps it in a [Result], returning an [Error] if a [Throwable] + * Invokes a [function] and wraps it in a [Result], returning an [Err] if a [Throwable] * was thrown, otherwise [Ok]. */ inline fun of(function: () -> T): Result { return try { Ok(function.invoke()) } catch (t: Throwable) { - Error(t) + Err(t) } } } } data class Ok constructor(val value: V) : Result() -data class Error constructor(val error: E) : Result() +data class Err constructor(val error: E) : Result() diff --git a/src/main/kotlin/com/github/michaelbull/result/ResultIterator.kt b/src/main/kotlin/com/github/michaelbull/result/ResultIterator.kt index af11cdf..396b232 100644 --- a/src/main/kotlin/com/github/michaelbull/result/ResultIterator.kt +++ b/src/main/kotlin/com/github/michaelbull/result/ResultIterator.kt @@ -41,7 +41,7 @@ private class ResultIterator(private val result: Result) : M return when (result) { is Ok -> true - is Error -> false + is Err -> false } } diff --git a/src/main/kotlin/com/github/michaelbull/result/Unwrap.kt b/src/main/kotlin/com/github/michaelbull/result/Unwrap.kt index 58cb8de..005dd09 100644 --- a/src/main/kotlin/com/github/michaelbull/result/Unwrap.kt +++ b/src/main/kotlin/com/github/michaelbull/result/Unwrap.kt @@ -7,12 +7,12 @@ class UnwrapException(message: String) : Exception(message) * * - Rust: [Result.unwrap](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap) * - * @throws UnwrapException if the [Result] is an [Error], with a message containing the [error][Error.error]. + * @throws UnwrapException if the [Result] is an [Err], with a message containing the [error][Err.error]. */ fun Result.unwrap(): V { return when (this) { is Ok -> value - is Error -> throw UnwrapException("called Result.wrap on an Error value $error") + is Err -> throw UnwrapException("called Result.wrap on an Err value $error") } } @@ -21,18 +21,18 @@ fun Result.unwrap(): V { * * - Rust: [Result.expect](https://doc.rust-lang.org/std/result/enum.Result.html#method.expect) * - * @param message The message to include in the [UnwrapException] if the [Result] is an [Error]. - * @throws UnwrapException if the [Result] is an [Error], with the specified [message]. + * @param message The message to include in the [UnwrapException] if the [Result] is an [Err]. + * @throws UnwrapException if the [Result] is an [Err], with the specified [message]. */ infix fun Result.expect(message: String): V { return when (this) { is Ok -> value - is Error -> throw UnwrapException("$message $error") + is Err -> throw UnwrapException("$message $error") } } /** - * Unwraps a [Result], yielding the [error][Error.error]. + * Unwraps a [Result], yielding the [error][Err.error]. * * - Rust: [Result.unwrap_err](https://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_err) * @@ -41,12 +41,12 @@ infix fun Result.expect(message: String): V { fun Result.unwrapError(): E { return when (this) { is Ok -> throw UnwrapException("called Result.unwrapError on an Ok value $value") - is Error -> error + is Err -> error } } /** - * Unwraps a [Result], yielding the [error][Error.error]. + * Unwraps a [Result], yielding the [error][Err.error]. * * - Rust: [Result.expect_err](https://doc.rust-lang.org/std/result/enum.Result.html#method.expect_err) * @@ -56,6 +56,6 @@ fun Result.unwrapError(): E { infix fun Result.expectError(message: String): E { return when (this) { is Ok -> throw UnwrapException("$message $value") - is Error -> error + is Err -> error } } diff --git a/src/test/kotlin/com/github/michaelbull/result/AndTest.kt b/src/test/kotlin/com/github/michaelbull/result/AndTest.kt index e9b36ca..b556335 100644 --- a/src/test/kotlin/com/github/michaelbull/result/AndTest.kt +++ b/src/test/kotlin/com/github/michaelbull/result/AndTest.kt @@ -16,7 +16,7 @@ internal class AndTest { @Test internal fun `and should return the result value if not ok`() { - val error = Ok(300).and(Error("hello world")).getError() + val error = Ok(300).and(Err("hello world")).getError() assertThat(error, equalTo("hello world")) } @@ -28,7 +28,7 @@ internal class AndTest { @Test internal fun `andThen should return the result error if not ok`() { - val error = Ok(20).andThen { Ok(it + 43) }.andThen { Error(AndError) }.getError()!! + val error = Ok(20).andThen { Ok(it + 43) }.andThen { Err(AndError) }.getError()!! assertThat(error, sameInstance(AndError)) } } diff --git a/src/test/kotlin/com/github/michaelbull/result/GetTest.kt b/src/test/kotlin/com/github/michaelbull/result/GetTest.kt index d4eb213..2bf509e 100644 --- a/src/test/kotlin/com/github/michaelbull/result/GetTest.kt +++ b/src/test/kotlin/com/github/michaelbull/result/GetTest.kt @@ -13,7 +13,7 @@ internal class GetTest { @Test internal fun `get should return null if not ok`() { - val value = Error("error").get() + val value = Err("error").get() assertThat(value, equalTo(null)) } @@ -25,7 +25,7 @@ internal class GetTest { @Test internal fun `getError should return the result error if not ok`() { - val error = Error("example").getError() + val error = Err("example").getError() assertThat(error, equalTo("example")) } @@ -37,7 +37,7 @@ internal class GetTest { @Test internal fun `getOr should return default value if not ok`() { - val value = Error("error").getOr("default") + val value = Err("error").getOr("default") assertThat(value, equalTo("default")) } @@ -49,7 +49,7 @@ internal class GetTest { @Test internal fun `getErrorOr should return the result error if not ok`() { - val error = Error("hello").getErrorOr("world") + val error = Err("hello").getErrorOr("world") assertThat(error, equalTo("hello")) } @@ -61,7 +61,7 @@ internal class GetTest { @Test internal fun `getOrElse should return the transformed result error if ok`() { - val value = Error("hello").getOrElse { "world" } + val value = Err("hello").getOrElse { "world" } assertThat(value, equalTo("world")) } } diff --git a/src/test/kotlin/com/github/michaelbull/result/IterableTest.kt b/src/test/kotlin/com/github/michaelbull/result/IterableTest.kt index ebb4319..804cb93 100644 --- a/src/test/kotlin/com/github/michaelbull/result/IterableTest.kt +++ b/src/test/kotlin/com/github/michaelbull/result/IterableTest.kt @@ -34,14 +34,14 @@ internal class IterableTest { initial = 1, operation = { a, b -> when (b) { - (5 + 10) -> Error(IterableError.IterableError1) - (5 + 10 + 15 + 20) -> Error(IterableError.IterableError2) + (5 + 10) -> Err(IterableError.IterableError1) + (5 + 10 + 15 + 20) -> Err(IterableError.IterableError2) else -> Ok(a * b) } } ) - result as Error + result as Err val matcher: Matcher = sameInstance(IterableError.IterableError1) assertThat(result.error, matcher) @@ -65,14 +65,14 @@ internal class IterableTest { initial = 38500, operation = { a, b -> when (b) { - (((38500 / 40) / 20) / 10) -> Error(IterableError.IterableError1) - ((38500 / 40) / 20) -> Error(IterableError.IterableError2) + (((38500 / 40) / 20) / 10) -> Err(IterableError.IterableError1) + ((38500 / 40) / 20) -> Err(IterableError.IterableError2) else -> Ok(b / a) } } ) - result as Error + result as Err assertThat(result.error, sameError(IterableError.IterableError2)) } @@ -96,13 +96,13 @@ internal class IterableTest { val result = combine( Ok(20), Ok(40), - Error(IterableError.IterableError1), + Err(IterableError.IterableError1), Ok(60), - Error(IterableError.IterableError2), + Err(IterableError.IterableError2), Ok(80) ) - result as Error + result as Err assertThat(result.error, sameError(IterableError.IterableError1)) } @@ -112,9 +112,9 @@ internal class IterableTest { val values = getAll( Ok("hello"), Ok("big"), - Error(IterableError.IterableError2), + Err(IterableError.IterableError2), Ok("wide"), - Error(IterableError.IterableError1), + Err(IterableError.IterableError1), Ok("world") ) @@ -128,15 +128,15 @@ internal class IterableTest { @Test internal fun `getAllErrors should return all of the result errors`() { val errors = getAllErrors( - Error(IterableError.IterableError2), + Err(IterableError.IterableError2), Ok("haskell"), - Error(IterableError.IterableError2), + Err(IterableError.IterableError2), Ok("f#"), - Error(IterableError.IterableError1), + Err(IterableError.IterableError1), Ok("elm"), - Error(IterableError.IterableError1), + Err(IterableError.IterableError1), Ok("clojure"), - Error(IterableError.IterableError2) + Err(IterableError.IterableError2) ) assertThat(errors.size, equalTo(5)) @@ -150,15 +150,15 @@ internal class IterableTest { @Test internal fun `partition should return a pair of all the result values and errors`() { val pairs = partition( - Error(IterableError.IterableError2), + Err(IterableError.IterableError2), Ok("haskell"), - Error(IterableError.IterableError2), + Err(IterableError.IterableError2), Ok("f#"), - Error(IterableError.IterableError1), + Err(IterableError.IterableError1), Ok("elm"), - Error(IterableError.IterableError1), + Err(IterableError.IterableError1), Ok("clojure"), - Error(IterableError.IterableError2) + Err(IterableError.IterableError2) ) val values = pairs.first diff --git a/src/test/kotlin/com/github/michaelbull/result/MapTest.kt b/src/test/kotlin/com/github/michaelbull/result/MapTest.kt index e543149..362f3ab 100644 --- a/src/test/kotlin/com/github/michaelbull/result/MapTest.kt +++ b/src/test/kotlin/com/github/michaelbull/result/MapTest.kt @@ -27,9 +27,9 @@ internal class MapTest { @Test internal fun `map should return the result error if not ok`() { - val result = Error(MapError.HelloError).map { "hello $it" } + val result = Err(MapError.HelloError).map { "hello $it" } - result as Error + result as Err assertThat(result.error, sameError(MapError.HelloError)) } @@ -46,13 +46,13 @@ internal class MapTest { .map { "$it me" } .andThen { when (it) { - "let me" -> Error(MapError.CustomError("$it $it")) + "let me" -> Err(MapError.CustomError("$it $it")) else -> Ok("$it get") } } .mapError { MapError.CustomError("${it.reason} get what i want") } - result as Error + result as Err assertThat(result.error.reason, equalTo("let me let me get what i want")) } @@ -69,7 +69,7 @@ internal class MapTest { @Test internal fun `mapBoth should return the transformed result error if not ok`() { - val error = Error(MapError.CustomError("this")).mapBoth( + val error = Err(MapError.CustomError("this")).mapBoth( success = { "$it charming" }, failure = { "${it.reason} man" } ) @@ -91,12 +91,12 @@ internal class MapTest { @Test internal fun `mapEither should return the transformed result error if not ok`() { - val result = Error("the reckless").mapEither( + val result = Err("the reckless").mapEither( success = { "the wild youth" }, failure = { MapError.CustomError("the truth") } ) - result as Error + result as Err assertThat(result.error.reason, equalTo("the truth")) } diff --git a/src/test/kotlin/com/github/michaelbull/result/OnTest.kt b/src/test/kotlin/com/github/michaelbull/result/OnTest.kt index 92e618a..086eedd 100644 --- a/src/test/kotlin/com/github/michaelbull/result/OnTest.kt +++ b/src/test/kotlin/com/github/michaelbull/result/OnTest.kt @@ -18,14 +18,14 @@ internal class OnTest { @Test internal fun `onSuccess should not invoke the callback when result is not ok`() { val counter = Counter(200) - Error(CounterError).onSuccess { counter.count -= 50 } + Err(CounterError).onSuccess { counter.count -= 50 } assertThat(counter.count, equalTo(200)) } @Test internal fun `onFailure should invoke the callback when result is not ok`() { val counter = Counter(555) - Error(CounterError).onFailure { counter.count += 100 } + Err(CounterError).onFailure { counter.count += 100 } assertThat(counter.count, equalTo(655)) } diff --git a/src/test/kotlin/com/github/michaelbull/result/OrTest.kt b/src/test/kotlin/com/github/michaelbull/result/OrTest.kt index bca63c4..d8ce59e 100644 --- a/src/test/kotlin/com/github/michaelbull/result/OrTest.kt +++ b/src/test/kotlin/com/github/michaelbull/result/OrTest.kt @@ -15,7 +15,7 @@ internal class OrTest { @Test internal fun `or should return the default value if not ok`() { - val value = Error(OrError).or(Ok(5000)).get() + val value = Err(OrError).or(Ok(5000)).get() assertThat(value, equalTo(5000)) } @@ -27,7 +27,7 @@ internal class OrTest { @Test internal fun `orElse should return the transformed value if not ok`() { - val value = Error(4000).orElse { Ok(2000) }.get() + val value = Err(4000).orElse { Ok(2000) }.get() assertThat(value, equalTo(2000)) } } diff --git a/src/test/kotlin/com/github/michaelbull/result/ResultIteratorTest.kt b/src/test/kotlin/com/github/michaelbull/result/ResultIteratorTest.kt index 634af97..bdf23f8 100644 --- a/src/test/kotlin/com/github/michaelbull/result/ResultIteratorTest.kt +++ b/src/test/kotlin/com/github/michaelbull/result/ResultIteratorTest.kt @@ -14,7 +14,7 @@ internal class ResultIteratorTest { @Test internal fun `hasNext should return false if result is not ok`() { - val iterator = Error("hello").iterator() + val iterator = Err("hello").iterator() assertThat(iterator.hasNext(), equalTo(false)) } @@ -33,7 +33,7 @@ internal class ResultIteratorTest { @Test internal fun `next should throw NoSuchElementException if unyielded and result is not ok`() { - val iterator = Error("hello").iterator() + val iterator = Err("hello").iterator() assertThrows(NoSuchElementException::class.java) { iterator.next() diff --git a/src/test/kotlin/com/github/michaelbull/result/UnwrapTest.kt b/src/test/kotlin/com/github/michaelbull/result/UnwrapTest.kt index f7dfaa0..044bc99 100644 --- a/src/test/kotlin/com/github/michaelbull/result/UnwrapTest.kt +++ b/src/test/kotlin/com/github/michaelbull/result/UnwrapTest.kt @@ -15,10 +15,10 @@ internal class UnwrapTest { @Test internal fun `unwrap should throw an UnwrapException if not ok`() { val throwable = assertThrows(UnwrapException::class.java, { - Error(5000).unwrap() + Err(5000).unwrap() }) - assertThat(throwable.message, equalTo("called Result.wrap on an Error value 5000")) + assertThat(throwable.message, equalTo("called Result.wrap on an Err value 5000")) } @Test @@ -30,7 +30,7 @@ internal class UnwrapTest { @Test internal fun `expect should throw an UnwrapException with a specified message if not ok`() { val throwable = assertThrows(UnwrapException::class.java, { - Error(1994).expect("the year should be") + Err(1994).expect("the year should be") }) assertThat(throwable.message, equalTo("the year should be 1994")) @@ -47,7 +47,7 @@ internal class UnwrapTest { @Test internal fun `unwrapError should return the result error if not ok`() { - val error = Error("example").unwrapError() + val error = Err("example").unwrapError() assertThat(error, equalTo("example")) } @@ -62,7 +62,7 @@ internal class UnwrapTest { @Test internal fun `expectError should return the result error if not ok`() { - val error = Error(2010).expectError("the year should be") + val error = Err(2010).expectError("the year should be") assertThat(error, equalTo(2010)) } }