From bfcb0d7c911aef02935d4575f74beb719bf3e276 Mon Sep 17 00:00:00 2001 From: Michael Bull Date: Tue, 5 Mar 2024 17:36:19 +0000 Subject: [PATCH] Improve consistency across unit tests --- .../binding/SuspendableBindingTest.kt | 10 +- .../binding/AsyncSuspendableBindingTest.kt | 16 +- .../coroutines/RunSuspendCatchingTest.kt | 4 +- .../com/github/michaelbull/result/AndTest.kt | 21 +- .../github/michaelbull/result/BindingTest.kt | 31 ++- .../github/michaelbull/result/FactoryTest.kt | 29 +-- .../com/github/michaelbull/result/GetTest.kt | 34 ++- .../github/michaelbull/result/IterableTest.kt | 224 +++++------------- .../com/github/michaelbull/result/OnTest.kt | 2 + .../com/github/michaelbull/result/OrTest.kt | 55 +++-- .../github/michaelbull/result/RecoverTest.kt | 59 ++--- .../michaelbull/result/ResultIteratorTest.kt | 31 ++- .../github/michaelbull/result/UnwrapTest.kt | 23 +- .../com/github/michaelbull/result/ZipTest.kt | 116 ++++----- 14 files changed, 289 insertions(+), 366 deletions(-) diff --git a/kotlin-result-coroutines/src/commonTest/kotlin/com.github.michaelbull.result.coroutines/binding/SuspendableBindingTest.kt b/kotlin-result-coroutines/src/commonTest/kotlin/com.github.michaelbull.result.coroutines/binding/SuspendableBindingTest.kt index 9671538..f94f78f 100644 --- a/kotlin-result-coroutines/src/commonTest/kotlin/com.github.michaelbull.result.coroutines/binding/SuspendableBindingTest.kt +++ b/kotlin-result-coroutines/src/commonTest/kotlin/com.github.michaelbull.result.coroutines/binding/SuspendableBindingTest.kt @@ -36,7 +36,7 @@ class SuspendableBindingTest { assertEquals( expected = Ok(3), - actual = result + actual = result, ) } @@ -60,7 +60,7 @@ class SuspendableBindingTest { assertEquals( expected = Ok(3), - actual = result + actual = result, ) } @@ -90,7 +90,7 @@ class SuspendableBindingTest { assertEquals( expected = Err(BindingError), - actual = result + actual = result, ) } @@ -127,7 +127,7 @@ class SuspendableBindingTest { assertEquals( expected = Err(BindingError), - actual = result + actual = result, ) assertTrue(xStateChange) @@ -161,7 +161,7 @@ class SuspendableBindingTest { assertEquals( expected = Err(BindingError), - actual = result + actual = result, ) } } diff --git a/kotlin-result-coroutines/src/jsTest/kotlin/com.github.michaelbull.result.coroutines/binding/AsyncSuspendableBindingTest.kt b/kotlin-result-coroutines/src/jsTest/kotlin/com.github.michaelbull.result.coroutines/binding/AsyncSuspendableBindingTest.kt index 4fcd315..1b77609 100644 --- a/kotlin-result-coroutines/src/jsTest/kotlin/com.github.michaelbull.result.coroutines/binding/AsyncSuspendableBindingTest.kt +++ b/kotlin-result-coroutines/src/jsTest/kotlin/com.github.michaelbull.result.coroutines/binding/AsyncSuspendableBindingTest.kt @@ -15,9 +15,9 @@ import kotlin.test.assertTrue @ExperimentalCoroutinesApi class AsyncSuspendableBindingTest { - private sealed class BindingError { - object BindingErrorA : BindingError() - object BindingErrorB : BindingError() + private sealed interface BindingError { + data object BindingErrorA : BindingError + data object BindingErrorB : BindingError } @Test @@ -32,7 +32,7 @@ class AsyncSuspendableBindingTest { return Ok(2) } - val result = binding { + val result = binding { val x = async { provideX().bind() } val y = async { provideY().bind() } x.await() + y.await() @@ -40,7 +40,7 @@ class AsyncSuspendableBindingTest { assertEquals( expected = Ok(3), - actual = result + actual = result, ) } @@ -61,7 +61,7 @@ class AsyncSuspendableBindingTest { return Err(BindingError.BindingErrorB) } - val result = binding { + val result = binding { val x = async { provideX().bind() } val y = async { provideY().bind() } val z = async { provideZ().bind() } @@ -98,7 +98,7 @@ class AsyncSuspendableBindingTest { return Err(BindingError.BindingErrorB) } - val result = binding { + val result = binding { val x = async { provideX().bind() } val y = async { provideY().bind() } val z = async { provideZ().bind() } @@ -108,7 +108,7 @@ class AsyncSuspendableBindingTest { assertEquals( expected = Err(BindingError.BindingErrorB), - actual = result + actual = result, ) assertFalse(xStateChange) diff --git a/kotlin-result-coroutines/src/jvmTest/kotlin/com/github/michaelbull/result/coroutines/RunSuspendCatchingTest.kt b/kotlin-result-coroutines/src/jvmTest/kotlin/com/github/michaelbull/result/coroutines/RunSuspendCatchingTest.kt index 3fb9d4c..d7cb7e4 100644 --- a/kotlin-result-coroutines/src/jvmTest/kotlin/com/github/michaelbull/result/coroutines/RunSuspendCatchingTest.kt +++ b/kotlin-result-coroutines/src/jvmTest/kotlin/com/github/michaelbull/result/coroutines/RunSuspendCatchingTest.kt @@ -49,7 +49,7 @@ class RunSuspendCatchingTest { assertEquals( expected = Ok("example"), - actual = result + actual = result, ) } @@ -61,7 +61,7 @@ class RunSuspendCatchingTest { assertEquals( expected = Err(exception), - actual = result + actual = result, ) } } diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/AndTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/AndTest.kt index 4f80d54..a48a8f7 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/AndTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/AndTest.kt @@ -2,43 +2,44 @@ package com.github.michaelbull.result import kotlin.test.Test import kotlin.test.assertEquals -import kotlin.test.assertSame class AndTest { private object AndError class And { + @Test fun returnsValueIfOk() { assertEquals( - expected = 500, - actual = Ok(230).and(Ok(500)).get() + expected = Ok(500), + actual = Ok(230).and(Ok(500)), ) } @Test fun returnsValueIfErr() { assertEquals( - expected = "hello world", - actual = Ok(300).and(Err("hello world")).getError() + expected = Err("hello world"), + actual = Ok(300).and(Err("hello world")), ) } } class AndThen { + @Test fun returnsTransformedValueIfOk() { assertEquals( - expected = 12, - actual = Ok(5).andThen { Ok(it + 7) }.get() + expected = Ok(12), + actual = Ok(5).andThen { Ok(it + 7) }, ) } @Test fun returnsErrorIfErr() { - assertSame( - expected = AndError, - actual = Ok(20).andThen { Ok(it + 43) }.andThen { Err(AndError) }.getError()!! + assertEquals( + expected = Err(AndError), + actual = Ok(20).andThen { Ok(it + 43) }.andThen { Err(AndError) }, ) } } diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/BindingTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/BindingTest.kt index 5fc7009..4fa9787 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/BindingTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/BindingTest.kt @@ -2,7 +2,6 @@ package com.github.michaelbull.result import kotlin.test.Test import kotlin.test.assertEquals -import kotlin.test.assertTrue class BindingTest { @@ -13,14 +12,16 @@ class BindingTest { fun provideX(): Result = Ok(1) fun provideY(): Result = Ok(2) - val result = binding { + val result = binding { val x = provideX().bind() val y = provideY().bind() x + y } - assertTrue(result is Ok) - assertEquals(3, result.value) + assertEquals( + expected = Ok(3), + actual = result, + ) } @Test @@ -28,14 +29,16 @@ class BindingTest { fun provideX(): Result = Ok("1") fun provideY(x: Int): Result = Ok(x + 2) - val result = binding { + val result = binding { val x = provideX().bind() val y = provideY(x.toInt()).bind() y } - assertTrue(result is Ok) - assertEquals(3, result.value) + assertEquals( + expected = Ok(3), + actual = result, + ) } @Test @@ -44,15 +47,17 @@ class BindingTest { fun provideY(): Result = Err(BindingError) fun provideZ(): Result = Ok(2) - val result = binding { + val result = binding { val x = provideX().bind() val y = provideY().bind() val z = provideZ().bind() x + y + z } - assertTrue(result is Err) - assertEquals(BindingError, result.error) + assertEquals( + expected = Err(BindingError), + actual = result, + ) } @Test @@ -68,7 +73,9 @@ class BindingTest { x + y.toInt() + z } - assertTrue(result is Err) - assertEquals(BindingError, result.error) + assertEquals( + expected = Err(BindingError), + actual = result, + ) } } diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/FactoryTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/FactoryTest.kt index c23ede9..0fa3666 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/FactoryTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/FactoryTest.kt @@ -2,52 +2,45 @@ package com.github.michaelbull.result import kotlin.test.Test import kotlin.test.assertEquals -import kotlin.test.assertSame class FactoryTest { class RunCatching { + @Test fun returnsOkIfInvocationSuccessful() { - val callback = { "example" } - val result = runCatching(callback) - assertEquals( - expected = "example", - actual = result.get() + expected = Ok("example"), + actual = runCatching { "example" }, ) } @Test + @Suppress("UNREACHABLE_CODE") fun returnsErrIfInvocationFails() { val exception = IllegalArgumentException("throw me") - val callback = { throw exception } - val result = runCatching(callback) - assertSame( - expected = exception, - actual = result.getError() + assertEquals( + expected = Err(exception), + actual = runCatching { throw exception }, ) } } class ToResultOr { + @Test fun returnsOkfIfNonNull() { - val result = "ok".toResultOr { "err" } - assertEquals( expected = "ok", - actual = result.get() + actual = "ok".toResultOr { "err" }.get() ) } @Test fun returnsErrIfNull() { - val result = "ok".toLongOrNull().toResultOr { "err" } - assertEquals( - expected = "err", - actual = result.getError() + expected = Err("err"), + actual = "ok".toLongOrNull().toResultOr { "err" } ) } } diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/GetTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/GetTest.kt index f187843..e6d20b4 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/GetTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/GetTest.kt @@ -86,7 +86,7 @@ class GetTest { @Test fun throwsTransformedErrorIfErr() { assertFailsWith { - Err("error").getOrThrow { error -> CustomException(error) } + Err("error").getOrThrow(::CustomException) } } @@ -148,19 +148,43 @@ class GetTest { } class Merge { + interface Direction + object Left : Direction + object Right : Direction + @Test fun returnsValueIfOk() { + val left: Result = Ok(Left) + val right: Result = Ok(Right) + + val result: Result = left.flatMapEither( + success = { left }, + failure = { right }, + ) + + val direction: Direction = result.merge() + assertEquals( - expected = setOf(4, 5, 6), - actual = Ok(listOf(1, 2, 3)).and(Ok(setOf(4, 5, 6))).merge() + expected = Left, + actual = direction ) } @Test fun returnsErrorIfErr() { + val left: Result = Err(Left) + val right: Result = Err(Right) + + val result: Result = left.flatMapEither( + success = { left }, + failure = { right }, + ) + + val direction: Direction = result.merge() + assertEquals( - expected = listOf(1, 2, 3), - actual = Err(listOf(1, 2, 3)).and(Err(setOf(4, 5, 6))).merge() + expected = Right, + actual = direction ) } } diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/IterableTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/IterableTest.kt index 690aaeb..1ffe99c 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/IterableTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/IterableTest.kt @@ -2,27 +2,25 @@ package com.github.michaelbull.result import kotlin.test.Test import kotlin.test.assertEquals -import kotlin.test.assertSame class IterableTest { - private sealed class IterableError { - object IterableError1 : IterableError() - object IterableError2 : IterableError() + private sealed interface IterableError { + data object IterableError1 : IterableError + data object IterableError2 : IterableError } class Fold { + @Test fun returnAccumulatedValueIfOk() { val result = listOf(20, 30, 40, 50).fold( initial = 10, - operation = { a, b -> Ok(a + b) } + operation = { a, b -> Ok(a + b) }, ) - result as Ok - assertEquals( - expected = 150, - actual = result.value + expected = Ok(150), + actual = result, ) } @@ -36,31 +34,28 @@ class IterableTest { (5 + 10 + 15 + 20) -> Err(IterableError.IterableError2) else -> Ok(a * b) } - } + }, ) - result as Err - - assertSame( - expected = IterableError.IterableError1, - actual = result.error + assertEquals( + expected = Err(IterableError.IterableError1), + actual = result, ) } } class FoldRight { + @Test fun returnsAccumulatedValueIfOk() { val result = listOf(2, 5, 10, 20).foldRight( initial = 100, - operation = { a, b -> Ok(b - a) } + operation = { a, b -> Ok(b - a) }, ) - result as Ok - assertEquals( - expected = 63, - actual = result.value + expected = Ok(63), + actual = result, ) } @@ -74,45 +69,29 @@ class IterableTest { ((38500 / 40) / 20) -> Err(IterableError.IterableError2) else -> Ok(b / a) } - } + }, ) - result as Err - - assertSame( - expected = IterableError.IterableError2, - actual = result.error + assertEquals( + expected = Err(IterableError.IterableError2), + actual = result, ) } } class Combine { + @Test fun returnsValuesIfAllOk() { - val values = combine( + val result = combine( Ok(10), Ok(20), - Ok(30) - ).get()!! - - assertEquals( - expected = 3, - actual = values.size - ) - - assertEquals( - expected = 10, - actual = values[0] + Ok(30), ) assertEquals( - expected = 20, - actual = values[1] - ) - - assertEquals( - expected = 30, - actual = values[2] + expected = Ok(listOf(10, 20, 30)), + actual = result, ) } @@ -124,61 +103,41 @@ class IterableTest { Err(IterableError.IterableError1), Ok(60), Err(IterableError.IterableError2), - Ok(80) + Ok(80), ) - result as Err - - assertSame( - expected = IterableError.IterableError1, - actual = result.error + assertEquals( + expected = Err(IterableError.IterableError1), + actual = result, ) } } class GetAll { + @Test fun returnsAllValues() { - val values = getAll( + val result = getAll( Ok("hello"), Ok("big"), Err(IterableError.IterableError2), Ok("wide"), Err(IterableError.IterableError1), - Ok("world") - ) - - assertEquals( - expected = 4, - actual = values.size - ) - - assertEquals( - expected = "hello", - actual = values[0] - ) - - assertEquals( - expected = "big", - actual = values[1] + Ok("world"), ) assertEquals( - expected = "wide", - actual = values[2] - ) - - assertEquals( - expected = "world", - actual = values[3] + expected = listOf("hello", "big", "wide", "world"), + actual = result ) } } class GetAllErrors { + @Test fun returnsAllErrors() { - val errors = getAllErrors( + val result = getAllErrors( Err(IterableError.IterableError2), Ok("haskell"), Err(IterableError.IterableError2), @@ -187,45 +146,42 @@ class IterableTest { Ok("elm"), Err(IterableError.IterableError1), Ok("clojure"), - Err(IterableError.IterableError2) + Err(IterableError.IterableError2), ) assertEquals( - expected = 5, - actual = errors.size - ) - - assertSame( - expected = IterableError.IterableError2, - actual = errors[0] - ) - - assertSame( - expected = IterableError.IterableError2, - actual = errors[1] - ) - - assertSame( - expected = IterableError.IterableError1, - actual = errors[2] - ) - - assertSame( - expected = IterableError.IterableError1, - actual = errors[3] - ) - - assertSame( - expected = IterableError.IterableError2, - actual = errors[4] + expected = listOf( + IterableError.IterableError2, + IterableError.IterableError2, + IterableError.IterableError1, + IterableError.IterableError1, + IterableError.IterableError2, + ), + actual = result ) } } class Partition { + @Test fun returnsPairOfValuesAndErrors() { - val pairs = partition( + val strings = listOf( + "haskell", + "f#", + "elm", + "clojure", + ) + + val errors = listOf( + IterableError.IterableError2, + IterableError.IterableError2, + IterableError.IterableError1, + IterableError.IterableError1, + IterableError.IterableError2, + ) + + val result = partition( Err(IterableError.IterableError2), Ok("haskell"), Err(IterableError.IterableError2), @@ -237,63 +193,9 @@ class IterableTest { Err(IterableError.IterableError2) ) - val values = pairs.first - - assertEquals( - expected = 4, - actual = values.size - ) - assertEquals( - expected = "haskell", - actual = values[0] - ) - - assertEquals( - expected = "f#", - actual = values[1] - ) - - assertEquals( - expected = "elm", - actual = values[2] - ) - - assertEquals( - expected = "clojure", - actual = values[3] - ) - - val errors = pairs.second - - assertEquals( - expected = 5, - actual = errors.size - ) - - assertSame( - expected = IterableError.IterableError2, - actual = errors[0] - ) - - assertSame( - expected = IterableError.IterableError2, - actual = errors[1] - ) - - assertSame( - expected = IterableError.IterableError1, - actual = errors[2] - ) - - assertSame( - expected = IterableError.IterableError1, - actual = errors[3] - ) - - assertSame( - expected = IterableError.IterableError2, - actual = errors[4] + expected = Pair(strings, errors), + actual = result, ) } } diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OnTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OnTest.kt index 54ba791..51c0b2f 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OnTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OnTest.kt @@ -8,6 +8,7 @@ class OnTest { class Counter(var count: Int) class OnSuccess { + @Test fun invokesActionIfOk() { val counter = Counter(50) @@ -34,6 +35,7 @@ class OnTest { } class OnFailure { + @Test fun invokesActionIfErr() { val counter = Counter(555) diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OrTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OrTest.kt index 2e7c27f..0dc6cda 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OrTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/OrTest.kt @@ -8,19 +8,20 @@ class OrTest { private object OrError class Or { + @Test fun returnsValueIfOk() { assertEquals( - expected = 500, - actual = Ok(500).or(Ok(1000)).get() + expected = Ok(500), + actual = Ok(500).or(Ok(1000)) ) } @Test fun returnsDefaultValueIfErr() { assertEquals( - expected = 5000, - actual = Err(OrError).or(Ok(5000)).get() + expected = Ok(5000), + actual = Err(OrError).or(Ok(5000)) ) } } @@ -29,21 +30,22 @@ class OrTest { @Test fun returnsValueIfOk() { assertEquals( - expected = 3000, - actual = Ok(3000).orElse { Ok(4000) }.get() + expected = Ok(3000), + actual = Ok(3000).orElse { Ok(4000) } ) } @Test fun returnsTransformedValueIfErr() { assertEquals( - expected = 2000, - actual = Err(4000).orElse { Ok(2000) }.get() + expected = Ok(2000), + actual = Err(4000).orElse { Ok(2000) } ) } } class OrElseThrow { + @Test fun returnsValueIfOk() { assertEquals( @@ -53,18 +55,14 @@ class OrTest { } @Test - fun returnsTransformedValueIfErr() { - val error = RuntimeException("or else throw") - - fun provideError(): Result { - return Err(error) - } - - assertFailsWith(error.message, provideError()::orElseThrow) + fun throwsErrorIfErr() { + val result = Err(RuntimeException("or else throw")) + assertFailsWith(block = result::orElseThrow) } } class ThrowIf { + @Test fun returnsValueIfOk() { assertEquals( @@ -75,25 +73,26 @@ class OrTest { @Test fun returnsErrIfPredicateDoesNotMatch() { - val error = RuntimeException("throw if") + val result: Result = Err(RuntimeException("throw if")) assertEquals( - expected = Err(error), - actual = Err(error).throwIf { false } + expected = result, + actual = result.throwIf { false } ) } @Test fun throwsErrIfPredicateMatches() { - val error = RuntimeException("throw if") + val result = Err(RuntimeException("throw if")) - assertFailsWith(error.message) { - Err(error).throwIf { true } + assertFailsWith { + result.throwIf { true } } } } class ThrowUnless { + @Test fun returnsValueIfOk() { assertEquals( @@ -104,20 +103,20 @@ class OrTest { @Test fun returnsErrIfPredicateMatches() { - val error = RuntimeException("example") + val result: Result = Err(RuntimeException("throw unless")) assertEquals( - expected = Err(error), - actual = Err(error).throwUnless { true } + expected = result, + actual = result.throwUnless { true } ) } @Test fun throwsErrIfPredicateDoesNotMatch() { - val error = RuntimeException("throw unless") + val result = Err(RuntimeException("throw unless")) - assertFailsWith(error.message) { - Err(error).throwUnless { false } + assertFailsWith { + result.throwUnless { false } } } } diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/RecoverTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/RecoverTest.kt index badd725..e11e5d4 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/RecoverTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/RecoverTest.kt @@ -7,11 +7,12 @@ class RecoverTest { private object RecoverError class Recover { + @Test fun returnsValueIfOk() { assertEquals( expected = Ok(3000), - actual = Ok(3000).recover { 4000 } + actual = Ok(3000).recover { 4000 }, ) } @@ -19,17 +20,18 @@ class RecoverTest { fun returnsTransformedValueIfErr() { assertEquals( expected = Ok(2000), - actual = Err(4000).recover { 2000 } + actual = Err(4000).recover { 2000 }, ) } } class RecoverCatching { + @Test fun returnsValueIfOk() { assertEquals( expected = Ok(3000), - actual = Ok(3000).recoverCatching { 4000 } + actual = Ok(3000).recoverCatching { 4000 }, ) } @@ -37,7 +39,7 @@ class RecoverTest { fun returnsTransformedValueIfErr() { assertEquals( expected = Ok(2000), - actual = Err(4000).recoverCatching { 2000 } + actual = Err(4000).recoverCatching { 2000 }, ) } @@ -46,15 +48,14 @@ class RecoverTest { val exception = IllegalArgumentException("throw me") assertEquals( - expected = exception, - actual = Err(4000) - .recoverCatching { throw exception } - .getError() + expected = Err(exception), + actual = Err(4000).recoverCatching { throw exception }, ) } } class RecoverIf { + @Test fun returnsValueIfOk() { fun predicate(int: Int): Boolean { @@ -63,7 +64,7 @@ class RecoverTest { assertEquals( expected = Ok(3000), - actual = Ok(3000).recoverIf(::predicate) { 2000 } + actual = Ok(3000).recoverIf(::predicate) { 2000 }, ) } @@ -75,7 +76,7 @@ class RecoverTest { assertEquals( expected = Ok(2000), - actual = Err(4000).recoverIf(::predicate) { 2000 } + actual = Err(4000).recoverIf(::predicate) { 2000 }, ) } @@ -87,7 +88,7 @@ class RecoverTest { assertEquals( expected = Err(4000), - actual = Err(4000).recoverIf(::predicate) { 2000 } + actual = Err(4000).recoverIf(::predicate) { 2000 }, ) } @@ -99,12 +100,13 @@ class RecoverTest { assertEquals( expected = Err(4000), - actual = Err(4000).recoverIf(::predicate) { 2000 } + actual = Err(4000).recoverIf(::predicate) { 2000 }, ) } } class RecoverUnless { + @Test fun returnsValueIfOk() { fun predicate(int: Int): Boolean { @@ -113,7 +115,7 @@ class RecoverTest { assertEquals( expected = Ok(3000), - actual = Ok(3000).recoverUnless(::predicate) { 2000 } + actual = Ok(3000).recoverUnless(::predicate) { 2000 }, ) } @@ -125,7 +127,7 @@ class RecoverTest { assertEquals( expected = Ok(2000), - actual = Err(4000).recoverUnless(::predicate) { 2000 } + actual = Err(4000).recoverUnless(::predicate) { 2000 }, ) } @@ -137,7 +139,7 @@ class RecoverTest { assertEquals( expected = Err(4000), - actual = Err(4000).recoverUnless(::predicate) { 2000 } + actual = Err(4000).recoverUnless(::predicate) { 2000 }, ) } @@ -149,25 +151,26 @@ class RecoverTest { assertEquals( expected = Err(4000), - actual = Err(4000).recoverUnless(::predicate) { 2000 } + actual = Err(4000).recoverUnless(::predicate) { 2000 }, ) } } class AndThenRecover { + @Test fun returnsValueIfOk() { assertEquals( - expected = 5, - actual = Ok(5).andThenRecover { Ok(7) }.get() + expected = Ok(5), + actual = Ok(5).andThenRecover { Ok(7) }, ) } @Test fun returnsTransformValueIfErr() { assertEquals( - expected = 20, - actual = Err(RecoverError).andThenRecover { Ok(20) }.get() + expected = Ok(20), + actual = Err(RecoverError).andThenRecover { Ok(20) }, ) } } @@ -181,7 +184,7 @@ class RecoverTest { assertEquals( expected = Ok(3000), - actual = Ok(3000).andThenRecoverIf(::predicate) { Ok(2000) } + actual = Ok(3000).andThenRecoverIf(::predicate) { Ok(2000) }, ) } @@ -193,7 +196,7 @@ class RecoverTest { assertEquals( expected = Ok(2000), - actual = Err(4000).andThenRecoverIf(::predicate) { Ok(2000) } + actual = Err(4000).andThenRecoverIf(::predicate) { Ok(2000) }, ) } @@ -205,7 +208,7 @@ class RecoverTest { assertEquals( expected = Err(2000), - actual = Err(4000).andThenRecoverIf(::predicate) { Err(2000) } + actual = Err(4000).andThenRecoverIf(::predicate) { Err(2000) }, ) } @@ -217,7 +220,7 @@ class RecoverTest { assertEquals( expected = Err(4000), - actual = Err(4000).andThenRecoverIf(::predicate) { Ok(2000) } + actual = Err(4000).andThenRecoverIf(::predicate) { Ok(2000) }, ) } } @@ -231,7 +234,7 @@ class RecoverTest { assertEquals( expected = Ok(3000), - actual = Ok(3000).andThenRecoverUnless(::predicate) { Ok(2000) } + actual = Ok(3000).andThenRecoverUnless(::predicate) { Ok(2000) }, ) } @@ -243,7 +246,7 @@ class RecoverTest { assertEquals( expected = Ok(2000), - actual = Err(4000).andThenRecoverUnless(::predicate) { Ok(2000) } + actual = Err(4000).andThenRecoverUnless(::predicate) { Ok(2000) }, ) } @@ -255,7 +258,7 @@ class RecoverTest { assertEquals( expected = Err(2000), - actual = Err(4000).andThenRecoverUnless(::predicate) { Err(2000) } + actual = Err(4000).andThenRecoverUnless(::predicate) { Err(2000) }, ) } @@ -267,7 +270,7 @@ class RecoverTest { assertEquals( expected = Err(4000), - actual = Err(4000).andThenRecoverUnless(::predicate) { Ok(2000) } + actual = Err(4000).andThenRecoverUnless(::predicate) { Ok(2000) }, ) } } diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/ResultIteratorTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/ResultIteratorTest.kt index f4d51d2..a345c4d 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/ResultIteratorTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/ResultIteratorTest.kt @@ -7,28 +7,35 @@ import kotlin.test.assertFalse import kotlin.test.assertTrue class ResultIteratorTest { + class HasNext { + @Test fun returnsTrueIfUnyieldedAndOk() { val iterator = Ok("hello").iterator() - assertTrue { iterator.hasNext() } + + assertTrue(iterator.hasNext()) } @Test fun returnsFalseIfErr() { val iterator = Err("hello").iterator() - assertFalse { iterator.hasNext() } + + assertFalse(iterator.hasNext()) } @Test fun returnsFalseIfYielded() { val iterator = Ok("hello").iterator() + iterator.next() - assertFalse { iterator.hasNext() } + + assertFalse(iterator.hasNext()) } } class Next { + @Test fun returnsValueIfUnyieldedAndOk() { assertEquals( @@ -42,7 +49,6 @@ class ResultIteratorTest { val iterator = Err("hello").iterator() assertFailsWith { - @Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION") iterator.next() } } @@ -50,24 +56,35 @@ class ResultIteratorTest { @Test fun throwsExceptionIfYieldedAndOk() { val iterator = Ok("hello").iterator() + iterator.next() - assertFailsWith { iterator.next() } + + assertFailsWith { + iterator.next() + } } } class Remove { + @Test fun makesHasNextReturnFalse() { val iterator = Ok("hello").mutableIterator() + iterator.remove() - assertFalse { iterator.hasNext() } + + assertFalse(iterator.hasNext()) } @Test fun makesNextThrowException() { val iterator = Ok("hello").mutableIterator() + iterator.remove() - assertFailsWith { iterator.next() } + + assertFailsWith { + iterator.next() + } } } } diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/UnwrapTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/UnwrapTest.kt index cb6e06f..1db67e8 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/UnwrapTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/UnwrapTest.kt @@ -10,25 +10,25 @@ class UnwrapTest { fun returnsValueIfOk() { assertEquals( expected = 5000, - actual = Ok(5000).unwrap() + actual = Ok(5000).unwrap(), ) } @Test fun throwsExceptionIfErr() { - assertFailsWith("called Result.unwrap on an Err value 5000") { - @Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION") + assertFailsWith { Err(5000).unwrap() } } } class Expect { + @Test fun returnsValueIfOk() { assertEquals( expected = 1994, - actual = Ok(1994).expect { "the year should be" } + actual = Ok(1994).expect { "the year should be" }, ) } @@ -38,18 +38,17 @@ class UnwrapTest { override fun toString() = "the year should be" } - assertFailsWith("the year should be 1994") { - @Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION") + assertFailsWith { Err(1994).expect { message } } } } class UnwrapError { + @Test fun throwsExceptionIfOk() { - assertFailsWith("called Result.unwrapError on an Ok value example") { - @Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION") + assertFailsWith { Ok("example").unwrapError() } } @@ -58,20 +57,20 @@ class UnwrapTest { fun returnsErrorIfErr() { assertEquals( expected = "example", - actual = Err("example").unwrapError() + actual = Err("example").unwrapError(), ) } } class ExpectError { + @Test fun throwsExceptionIfOk() { val message = object { override fun toString() = "the year should be" } - assertFailsWith("the year should be 2010") { - @Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION") + assertFailsWith { Ok(2010).expectError { message } } } @@ -80,7 +79,7 @@ class UnwrapTest { fun returnsErrorIfErr() { assertEquals( expected = 2010, - actual = Err(2010).expectError { "the year should be" } + actual = Err(2010).expectError { "the year should be" }, ) } } diff --git a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/ZipTest.kt b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/ZipTest.kt index d522310..b8e1403 100644 --- a/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/ZipTest.kt +++ b/kotlin-result/src/commonTest/kotlin/com/github/michaelbull/result/ZipTest.kt @@ -20,14 +20,12 @@ class ZipTest { val result = zip( { Ok(10) }, { Ok(20) }, - Int::plus + Int::plus, ) - result as Ok - assertEquals( - expected = 30, - actual = result.value + expected = Ok(30), + actual = result, ) } @@ -36,14 +34,12 @@ class ZipTest { val result = zip( { Ok(10) }, { produce(20, "hello") }, - Int::plus + Int::plus, ) - result as Err - assertEquals( - expected = "hello", - actual = result.error + expected = Err("hello"), + actual = result, ) } @@ -52,14 +48,12 @@ class ZipTest { val result = zip( { produce(10, "foo") }, { produce(20, "bar") }, - Int::plus + Int::plus, ) - result as Err - assertEquals( - expected = "foo", - actual = result.error + expected = Err("foo"), + actual = result, ) } @@ -69,14 +63,12 @@ class ZipTest { { Ok("hello") }, { Ok(2) }, { Ok(false) }, - ::ZipData3 + ::ZipData3, ) - result as Ok - assertEquals( - expected = ZipData3("hello", 2, false), - actual = result.value + expected = Ok(ZipData3("hello", 2, false)), + actual = result, ) } @@ -86,14 +78,12 @@ class ZipTest { { Ok("foo") }, { Ok(1).and(Err("bar")) }, { Ok(false) }, - ::ZipData3 + ::ZipData3, ) - result as Err - assertEquals( - expected = "bar", - actual = result.error + expected = Err("bar"), + actual = result, ) } @@ -103,12 +93,12 @@ class ZipTest { { Ok("foo") }, { Ok(1).and(Err("bar")) }, { Ok(false).and(Err("baz")) }, - ::ZipData3 + ::ZipData3, ) assertEquals( - expected = "bar", - actual = result.getError() + expected = Err("bar"), + actual = result, ) } @@ -118,14 +108,12 @@ class ZipTest { { Ok("foo").and(Err(1)) }, { Ok(1).and(Err(2)) }, { Ok(false).and(Err(3)) }, - ::ZipData3 + ::ZipData3, ) - result as Err - assertEquals( - expected = 1, - actual = result.error + expected = Err(1), + actual = result, ) } @@ -136,14 +124,12 @@ class ZipTest { { Ok(2) }, { Ok(false) }, { Ok(1.5) }, - ::ZipData4 + ::ZipData4, ) - result as Ok - assertEquals( - expected = ZipData4("hello", 2, false, 1.5), - actual = result.value + expected = Ok(ZipData4("hello", 2, false, 1.5)), + actual = result ) } @@ -154,14 +140,12 @@ class ZipTest { { Ok(2).and(Err(1)) }, { Ok(false) }, { Ok(1.5).and(Err(2)) }, - ::ZipData4 + ::ZipData4, ) - result as Err - assertEquals( - expected = 1, - actual = result.error + expected = Err(1), + actual = result, ) } @@ -173,14 +157,12 @@ class ZipTest { { Ok(false) }, { Ok(1.5) }, { Ok('a') }, - ::ZipData5 + ::ZipData5, ) - result as Ok - assertEquals( - expected = ZipData5("hello", 2, false, 1.5, 'a'), - actual = result.value + expected = Ok(ZipData5("hello", 2, false, 1.5, 'a')), + actual = result, ) } @@ -192,14 +174,12 @@ class ZipTest { { Ok(false) }, { Ok(1.5) }, { Ok('a').and(Err(2)) }, - ::ZipData5 + ::ZipData5, ) - result as Err - assertEquals( - expected = 1, - actual = result.error + expected = Err(1), + actual = result, ) } } @@ -218,11 +198,9 @@ class ZipTest { a + b + c + d + e } - result as Ok - assertEquals( - expected = 150, - actual = result.value, + expected = Ok(150), + actual = result, ) } @@ -238,17 +216,17 @@ class ZipTest { a + b + c + d + e } - result as Err + val errors = listOf( + "error one", + "error two", + "error three", + "error four", + "error five", + ) assertEquals( - expected = listOf( - "error one", - "error two", - "error three", - "error four", - "error five", - ), - actual = result.error, + expected = Err(errors), + actual = result, ) } @@ -264,11 +242,9 @@ class ZipTest { a + b + c + d + e } - result as Err - assertEquals( - expected = listOf("only error"), - actual = result.error, + expected = Err(listOf("only error")), + actual = result, ) } }