Improve consistency across unit tests

This commit is contained in:
Michael Bull 2024-03-05 17:36:19 +00:00
parent 4e5cdeede7
commit f05ce6e0c7
14 changed files with 289 additions and 366 deletions

View File

@ -36,7 +36,7 @@ class SuspendableBindingTest {
assertEquals( assertEquals(
expected = Ok(3), expected = Ok(3),
actual = result actual = result,
) )
} }
@ -60,7 +60,7 @@ class SuspendableBindingTest {
assertEquals( assertEquals(
expected = Ok(3), expected = Ok(3),
actual = result actual = result,
) )
} }
@ -90,7 +90,7 @@ class SuspendableBindingTest {
assertEquals( assertEquals(
expected = Err(BindingError), expected = Err(BindingError),
actual = result actual = result,
) )
} }
@ -127,7 +127,7 @@ class SuspendableBindingTest {
assertEquals( assertEquals(
expected = Err(BindingError), expected = Err(BindingError),
actual = result actual = result,
) )
assertTrue(xStateChange) assertTrue(xStateChange)
@ -161,7 +161,7 @@ class SuspendableBindingTest {
assertEquals( assertEquals(
expected = Err(BindingError), expected = Err(BindingError),
actual = result actual = result,
) )
} }
} }

View File

@ -15,9 +15,9 @@ import kotlin.test.assertTrue
@ExperimentalCoroutinesApi @ExperimentalCoroutinesApi
class AsyncSuspendableBindingTest { class AsyncSuspendableBindingTest {
private sealed class BindingError { private sealed interface BindingError {
object BindingErrorA : BindingError() data object BindingErrorA : BindingError
object BindingErrorB : BindingError() data object BindingErrorB : BindingError
} }
@Test @Test
@ -32,7 +32,7 @@ class AsyncSuspendableBindingTest {
return Ok(2) return Ok(2)
} }
val result = binding<Int, BindingError> { val result = binding {
val x = async { provideX().bind() } val x = async { provideX().bind() }
val y = async { provideY().bind() } val y = async { provideY().bind() }
x.await() + y.await() x.await() + y.await()
@ -40,7 +40,7 @@ class AsyncSuspendableBindingTest {
assertEquals( assertEquals(
expected = Ok(3), expected = Ok(3),
actual = result actual = result,
) )
} }
@ -61,7 +61,7 @@ class AsyncSuspendableBindingTest {
return Err(BindingError.BindingErrorB) return Err(BindingError.BindingErrorB)
} }
val result = binding<Int, BindingError> { val result = binding {
val x = async { provideX().bind() } val x = async { provideX().bind() }
val y = async { provideY().bind() } val y = async { provideY().bind() }
val z = async { provideZ().bind() } val z = async { provideZ().bind() }
@ -98,7 +98,7 @@ class AsyncSuspendableBindingTest {
return Err(BindingError.BindingErrorB) return Err(BindingError.BindingErrorB)
} }
val result = binding<Int, BindingError> { val result = binding {
val x = async { provideX().bind() } val x = async { provideX().bind() }
val y = async { provideY().bind() } val y = async { provideY().bind() }
val z = async { provideZ().bind() } val z = async { provideZ().bind() }
@ -108,7 +108,7 @@ class AsyncSuspendableBindingTest {
assertEquals( assertEquals(
expected = Err(BindingError.BindingErrorB), expected = Err(BindingError.BindingErrorB),
actual = result actual = result,
) )
assertFalse(xStateChange) assertFalse(xStateChange)

View File

@ -49,7 +49,7 @@ class RunSuspendCatchingTest {
assertEquals( assertEquals(
expected = Ok("example"), expected = Ok("example"),
actual = result actual = result,
) )
} }
@ -61,7 +61,7 @@ class RunSuspendCatchingTest {
assertEquals( assertEquals(
expected = Err(exception), expected = Err(exception),
actual = result actual = result,
) )
} }
} }

View File

@ -2,43 +2,44 @@ package com.github.michaelbull.result
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertSame
class AndTest { class AndTest {
private object AndError private object AndError
class And { class And {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
expected = 500, expected = Ok(500),
actual = Ok(230).and(Ok(500)).get() actual = Ok(230).and(Ok(500)),
) )
} }
@Test @Test
fun returnsValueIfErr() { fun returnsValueIfErr() {
assertEquals( assertEquals(
expected = "hello world", expected = Err("hello world"),
actual = Ok(300).and(Err("hello world")).getError() actual = Ok(300).and(Err("hello world")),
) )
} }
} }
class AndThen { class AndThen {
@Test @Test
fun returnsTransformedValueIfOk() { fun returnsTransformedValueIfOk() {
assertEquals( assertEquals(
expected = 12, expected = Ok(12),
actual = Ok(5).andThen { Ok(it + 7) }.get() actual = Ok(5).andThen { Ok(it + 7) },
) )
} }
@Test @Test
fun returnsErrorIfErr() { fun returnsErrorIfErr() {
assertSame( assertEquals(
expected = AndError, expected = Err(AndError),
actual = Ok(20).andThen { Ok(it + 43) }.andThen { Err(AndError) }.getError()!! actual = Ok(20).andThen { Ok(it + 43) }.andThen { Err(AndError) },
) )
} }
} }

View File

@ -2,7 +2,6 @@ package com.github.michaelbull.result
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertTrue
class BindingTest { class BindingTest {
@ -13,14 +12,16 @@ class BindingTest {
fun provideX(): Result<Int, BindingError> = Ok(1) fun provideX(): Result<Int, BindingError> = Ok(1)
fun provideY(): Result<Int, BindingError> = Ok(2) fun provideY(): Result<Int, BindingError> = Ok(2)
val result = binding<Int, BindingError> { val result = binding {
val x = provideX().bind() val x = provideX().bind()
val y = provideY().bind() val y = provideY().bind()
x + y x + y
} }
assertTrue(result is Ok) assertEquals(
assertEquals(3, result.value) expected = Ok(3),
actual = result,
)
} }
@Test @Test
@ -28,14 +29,16 @@ class BindingTest {
fun provideX(): Result<String, BindingError> = Ok("1") fun provideX(): Result<String, BindingError> = Ok("1")
fun provideY(x: Int): Result<Int, BindingError> = Ok(x + 2) fun provideY(x: Int): Result<Int, BindingError> = Ok(x + 2)
val result = binding<Int, BindingError> { val result = binding {
val x = provideX().bind() val x = provideX().bind()
val y = provideY(x.toInt()).bind() val y = provideY(x.toInt()).bind()
y y
} }
assertTrue(result is Ok) assertEquals(
assertEquals(3, result.value) expected = Ok(3),
actual = result,
)
} }
@Test @Test
@ -44,15 +47,17 @@ class BindingTest {
fun provideY(): Result<Int, BindingError> = Err(BindingError) fun provideY(): Result<Int, BindingError> = Err(BindingError)
fun provideZ(): Result<Int, BindingError> = Ok(2) fun provideZ(): Result<Int, BindingError> = Ok(2)
val result = binding<Int, BindingError> { val result = binding {
val x = provideX().bind() val x = provideX().bind()
val y = provideY().bind() val y = provideY().bind()
val z = provideZ().bind() val z = provideZ().bind()
x + y + z x + y + z
} }
assertTrue(result is Err) assertEquals(
assertEquals(BindingError, result.error) expected = Err(BindingError),
actual = result,
)
} }
@Test @Test
@ -68,7 +73,9 @@ class BindingTest {
x + y.toInt() + z x + y.toInt() + z
} }
assertTrue(result is Err) assertEquals(
assertEquals(BindingError, result.error) expected = Err(BindingError),
actual = result,
)
} }
} }

View File

@ -2,52 +2,45 @@ package com.github.michaelbull.result
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertSame
class FactoryTest { class FactoryTest {
class RunCatching { class RunCatching {
@Test @Test
fun returnsOkIfInvocationSuccessful() { fun returnsOkIfInvocationSuccessful() {
val callback = { "example" }
val result = runCatching(callback)
assertEquals( assertEquals(
expected = "example", expected = Ok("example"),
actual = result.get() actual = runCatching { "example" },
) )
} }
@Test @Test
@Suppress("UNREACHABLE_CODE")
fun returnsErrIfInvocationFails() { fun returnsErrIfInvocationFails() {
val exception = IllegalArgumentException("throw me") val exception = IllegalArgumentException("throw me")
val callback = { throw exception }
val result = runCatching(callback)
assertSame( assertEquals(
expected = exception, expected = Err(exception),
actual = result.getError() actual = runCatching { throw exception },
) )
} }
} }
class ToResultOr { class ToResultOr {
@Test @Test
fun returnsOkfIfNonNull() { fun returnsOkfIfNonNull() {
val result = "ok".toResultOr { "err" }
assertEquals( assertEquals(
expected = "ok", expected = "ok",
actual = result.get() actual = "ok".toResultOr { "err" }.get()
) )
} }
@Test @Test
fun returnsErrIfNull() { fun returnsErrIfNull() {
val result = "ok".toLongOrNull().toResultOr { "err" }
assertEquals( assertEquals(
expected = "err", expected = Err("err"),
actual = result.getError() actual = "ok".toLongOrNull().toResultOr { "err" }
) )
} }
} }

View File

@ -86,7 +86,7 @@ class GetTest {
@Test @Test
fun throwsTransformedErrorIfErr() { fun throwsTransformedErrorIfErr() {
assertFailsWith<CustomException> { assertFailsWith<CustomException> {
Err("error").getOrThrow { error -> CustomException(error) } Err("error").getOrThrow(::CustomException)
} }
} }
@ -148,19 +148,43 @@ class GetTest {
} }
class Merge { class Merge {
interface Direction
object Left : Direction
object Right : Direction
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
val left: Result<Left, Left> = Ok(Left)
val right: Result<Right, Right> = Ok(Right)
val result: Result<Direction, Direction> = left.flatMapEither(
success = { left },
failure = { right },
)
val direction: Direction = result.merge()
assertEquals( assertEquals(
expected = setOf(4, 5, 6), expected = Left,
actual = Ok(listOf(1, 2, 3)).and(Ok(setOf(4, 5, 6))).merge() actual = direction
) )
} }
@Test @Test
fun returnsErrorIfErr() { fun returnsErrorIfErr() {
val left: Result<Left, Left> = Err(Left)
val right: Result<Right, Right> = Err(Right)
val result: Result<Direction, Direction> = left.flatMapEither(
success = { left },
failure = { right },
)
val direction: Direction = result.merge()
assertEquals( assertEquals(
expected = listOf(1, 2, 3), expected = Right,
actual = Err(listOf(1, 2, 3)).and(Err(setOf(4, 5, 6))).merge() actual = direction
) )
} }
} }

View File

@ -2,27 +2,25 @@ package com.github.michaelbull.result
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertSame
class IterableTest { class IterableTest {
private sealed class IterableError { private sealed interface IterableError {
object IterableError1 : IterableError() data object IterableError1 : IterableError
object IterableError2 : IterableError() data object IterableError2 : IterableError
} }
class Fold { class Fold {
@Test @Test
fun returnAccumulatedValueIfOk() { fun returnAccumulatedValueIfOk() {
val result = listOf(20, 30, 40, 50).fold( val result = listOf(20, 30, 40, 50).fold(
initial = 10, initial = 10,
operation = { a, b -> Ok(a + b) } operation = { a, b -> Ok(a + b) },
) )
result as Ok
assertEquals( assertEquals(
expected = 150, expected = Ok(150),
actual = result.value actual = result,
) )
} }
@ -36,31 +34,28 @@ class IterableTest {
(5 + 10 + 15 + 20) -> Err(IterableError.IterableError2) (5 + 10 + 15 + 20) -> Err(IterableError.IterableError2)
else -> Ok(a * b) else -> Ok(a * b)
} }
} },
) )
result as Err assertEquals(
expected = Err(IterableError.IterableError1),
assertSame( actual = result,
expected = IterableError.IterableError1,
actual = result.error
) )
} }
} }
class FoldRight { class FoldRight {
@Test @Test
fun returnsAccumulatedValueIfOk() { fun returnsAccumulatedValueIfOk() {
val result = listOf(2, 5, 10, 20).foldRight( val result = listOf(2, 5, 10, 20).foldRight(
initial = 100, initial = 100,
operation = { a, b -> Ok(b - a) } operation = { a, b -> Ok(b - a) },
) )
result as Ok
assertEquals( assertEquals(
expected = 63, expected = Ok(63),
actual = result.value actual = result,
) )
} }
@ -74,45 +69,29 @@ class IterableTest {
((38500 / 40) / 20) -> Err(IterableError.IterableError2) ((38500 / 40) / 20) -> Err(IterableError.IterableError2)
else -> Ok(b / a) else -> Ok(b / a)
} }
} },
) )
result as Err assertEquals(
expected = Err(IterableError.IterableError2),
assertSame( actual = result,
expected = IterableError.IterableError2,
actual = result.error
) )
} }
} }
class Combine { class Combine {
@Test @Test
fun returnsValuesIfAllOk() { fun returnsValuesIfAllOk() {
val values = combine( val result = combine(
Ok(10), Ok(10),
Ok(20), Ok(20),
Ok(30) Ok(30),
).get()!!
assertEquals(
expected = 3,
actual = values.size
) )
assertEquals( assertEquals(
expected = 10, expected = Ok(listOf(10, 20, 30)),
actual = values[0] actual = result,
)
assertEquals(
expected = 20,
actual = values[1]
)
assertEquals(
expected = 30,
actual = values[2]
) )
} }
@ -124,61 +103,41 @@ class IterableTest {
Err(IterableError.IterableError1), Err(IterableError.IterableError1),
Ok(60), Ok(60),
Err(IterableError.IterableError2), Err(IterableError.IterableError2),
Ok(80) Ok(80),
) )
result as Err assertEquals(
expected = Err(IterableError.IterableError1),
assertSame( actual = result,
expected = IterableError.IterableError1,
actual = result.error
) )
} }
} }
class GetAll { class GetAll {
@Test @Test
fun returnsAllValues() { fun returnsAllValues() {
val values = getAll( val result = getAll(
Ok("hello"), Ok("hello"),
Ok("big"), Ok("big"),
Err(IterableError.IterableError2), Err(IterableError.IterableError2),
Ok("wide"), Ok("wide"),
Err(IterableError.IterableError1), Err(IterableError.IterableError1),
Ok("world") Ok("world"),
) )
assertEquals( assertEquals(
expected = 4, expected = listOf("hello", "big", "wide", "world"),
actual = values.size actual = result
)
assertEquals(
expected = "hello",
actual = values[0]
)
assertEquals(
expected = "big",
actual = values[1]
)
assertEquals(
expected = "wide",
actual = values[2]
)
assertEquals(
expected = "world",
actual = values[3]
) )
} }
} }
class GetAllErrors { class GetAllErrors {
@Test @Test
fun returnsAllErrors() { fun returnsAllErrors() {
val errors = getAllErrors( val result = getAllErrors(
Err(IterableError.IterableError2), Err(IterableError.IterableError2),
Ok("haskell"), Ok("haskell"),
Err(IterableError.IterableError2), Err(IterableError.IterableError2),
@ -187,45 +146,42 @@ class IterableTest {
Ok("elm"), Ok("elm"),
Err(IterableError.IterableError1), Err(IterableError.IterableError1),
Ok("clojure"), Ok("clojure"),
Err(IterableError.IterableError2) Err(IterableError.IterableError2),
) )
assertEquals( assertEquals(
expected = 5, expected = listOf(
actual = errors.size IterableError.IterableError2,
) IterableError.IterableError2,
IterableError.IterableError1,
assertSame( IterableError.IterableError1,
expected = IterableError.IterableError2, IterableError.IterableError2,
actual = errors[0] ),
) actual = result
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]
) )
} }
} }
class Partition { class Partition {
@Test @Test
fun returnsPairOfValuesAndErrors() { 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), Err(IterableError.IterableError2),
Ok("haskell"), Ok("haskell"),
Err(IterableError.IterableError2), Err(IterableError.IterableError2),
@ -237,63 +193,9 @@ class IterableTest {
Err(IterableError.IterableError2) Err(IterableError.IterableError2)
) )
val values = pairs.first
assertEquals( assertEquals(
expected = 4, expected = Pair(strings, errors),
actual = values.size actual = result,
)
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]
) )
} }
} }

View File

@ -8,6 +8,7 @@ class OnTest {
class Counter(var count: Int) class Counter(var count: Int)
class OnSuccess { class OnSuccess {
@Test @Test
fun invokesActionIfOk() { fun invokesActionIfOk() {
val counter = Counter(50) val counter = Counter(50)
@ -34,6 +35,7 @@ class OnTest {
} }
class OnFailure { class OnFailure {
@Test @Test
fun invokesActionIfErr() { fun invokesActionIfErr() {
val counter = Counter(555) val counter = Counter(555)

View File

@ -8,19 +8,20 @@ class OrTest {
private object OrError private object OrError
class Or { class Or {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
expected = 500, expected = Ok(500),
actual = Ok(500).or(Ok(1000)).get() actual = Ok(500).or(Ok(1000))
) )
} }
@Test @Test
fun returnsDefaultValueIfErr() { fun returnsDefaultValueIfErr() {
assertEquals( assertEquals(
expected = 5000, expected = Ok(5000),
actual = Err(OrError).or(Ok(5000)).get() actual = Err(OrError).or(Ok(5000))
) )
} }
} }
@ -29,21 +30,22 @@ class OrTest {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
expected = 3000, expected = Ok(3000),
actual = Ok(3000).orElse { Ok(4000) }.get() actual = Ok(3000).orElse { Ok(4000) }
) )
} }
@Test @Test
fun returnsTransformedValueIfErr() { fun returnsTransformedValueIfErr() {
assertEquals( assertEquals(
expected = 2000, expected = Ok(2000),
actual = Err(4000).orElse { Ok(2000) }.get() actual = Err(4000).orElse { Ok(2000) }
) )
} }
} }
class OrElseThrow { class OrElseThrow {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
@ -53,18 +55,14 @@ class OrTest {
} }
@Test @Test
fun returnsTransformedValueIfErr() { fun throwsErrorIfErr() {
val error = RuntimeException("or else throw") val result = Err(RuntimeException("or else throw"))
assertFailsWith<RuntimeException>(block = result::orElseThrow)
fun provideError(): Result<String, Throwable> {
return Err(error)
}
assertFailsWith<RuntimeException>(error.message, provideError()::orElseThrow)
} }
} }
class ThrowIf { class ThrowIf {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
@ -75,25 +73,26 @@ class OrTest {
@Test @Test
fun returnsErrIfPredicateDoesNotMatch() { fun returnsErrIfPredicateDoesNotMatch() {
val error = RuntimeException("throw if") val result: Result<Int, Exception> = Err(RuntimeException("throw if"))
assertEquals( assertEquals(
expected = Err(error), expected = result,
actual = Err(error).throwIf { false } actual = result.throwIf { false }
) )
} }
@Test @Test
fun throwsErrIfPredicateMatches() { fun throwsErrIfPredicateMatches() {
val error = RuntimeException("throw if") val result = Err(RuntimeException("throw if"))
assertFailsWith<RuntimeException>(error.message) { assertFailsWith<RuntimeException> {
Err(error).throwIf { true } result.throwIf { true }
} }
} }
} }
class ThrowUnless { class ThrowUnless {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
@ -104,20 +103,20 @@ class OrTest {
@Test @Test
fun returnsErrIfPredicateMatches() { fun returnsErrIfPredicateMatches() {
val error = RuntimeException("example") val result: Result<Int, Exception> = Err(RuntimeException("throw unless"))
assertEquals( assertEquals(
expected = Err(error), expected = result,
actual = Err(error).throwUnless { true } actual = result.throwUnless { true }
) )
} }
@Test @Test
fun throwsErrIfPredicateDoesNotMatch() { fun throwsErrIfPredicateDoesNotMatch() {
val error = RuntimeException("throw unless") val result = Err(RuntimeException("throw unless"))
assertFailsWith<RuntimeException>(error.message) { assertFailsWith<RuntimeException> {
Err(error).throwUnless { false } result.throwUnless { false }
} }
} }
} }

View File

@ -7,11 +7,12 @@ class RecoverTest {
private object RecoverError private object RecoverError
class Recover { class Recover {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
expected = Ok(3000), expected = Ok(3000),
actual = Ok(3000).recover { 4000 } actual = Ok(3000).recover { 4000 },
) )
} }
@ -19,17 +20,18 @@ class RecoverTest {
fun returnsTransformedValueIfErr() { fun returnsTransformedValueIfErr() {
assertEquals( assertEquals(
expected = Ok(2000), expected = Ok(2000),
actual = Err(4000).recover { 2000 } actual = Err(4000).recover { 2000 },
) )
} }
} }
class RecoverCatching { class RecoverCatching {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
expected = Ok(3000), expected = Ok(3000),
actual = Ok(3000).recoverCatching { 4000 } actual = Ok(3000).recoverCatching { 4000 },
) )
} }
@ -37,7 +39,7 @@ class RecoverTest {
fun returnsTransformedValueIfErr() { fun returnsTransformedValueIfErr() {
assertEquals( assertEquals(
expected = Ok(2000), expected = Ok(2000),
actual = Err(4000).recoverCatching { 2000 } actual = Err(4000).recoverCatching { 2000 },
) )
} }
@ -46,15 +48,14 @@ class RecoverTest {
val exception = IllegalArgumentException("throw me") val exception = IllegalArgumentException("throw me")
assertEquals( assertEquals(
expected = exception, expected = Err(exception),
actual = Err(4000) actual = Err(4000).recoverCatching { throw exception },
.recoverCatching { throw exception }
.getError()
) )
} }
} }
class RecoverIf { class RecoverIf {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
fun predicate(int: Int): Boolean { fun predicate(int: Int): Boolean {
@ -63,7 +64,7 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Ok(3000), expected = Ok(3000),
actual = Ok(3000).recoverIf(::predicate) { 2000 } actual = Ok(3000).recoverIf(::predicate) { 2000 },
) )
} }
@ -75,7 +76,7 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Ok(2000), expected = Ok(2000),
actual = Err(4000).recoverIf(::predicate) { 2000 } actual = Err(4000).recoverIf(::predicate) { 2000 },
) )
} }
@ -87,7 +88,7 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Err(4000), expected = Err(4000),
actual = Err(4000).recoverIf(::predicate) { 2000 } actual = Err(4000).recoverIf(::predicate) { 2000 },
) )
} }
@ -99,12 +100,13 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Err(4000), expected = Err(4000),
actual = Err(4000).recoverIf(::predicate) { 2000 } actual = Err(4000).recoverIf(::predicate) { 2000 },
) )
} }
} }
class RecoverUnless { class RecoverUnless {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
fun predicate(int: Int): Boolean { fun predicate(int: Int): Boolean {
@ -113,7 +115,7 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Ok(3000), expected = Ok(3000),
actual = Ok(3000).recoverUnless(::predicate) { 2000 } actual = Ok(3000).recoverUnless(::predicate) { 2000 },
) )
} }
@ -125,7 +127,7 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Ok(2000), expected = Ok(2000),
actual = Err(4000).recoverUnless(::predicate) { 2000 } actual = Err(4000).recoverUnless(::predicate) { 2000 },
) )
} }
@ -137,7 +139,7 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Err(4000), expected = Err(4000),
actual = Err(4000).recoverUnless(::predicate) { 2000 } actual = Err(4000).recoverUnless(::predicate) { 2000 },
) )
} }
@ -149,25 +151,26 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Err(4000), expected = Err(4000),
actual = Err(4000).recoverUnless(::predicate) { 2000 } actual = Err(4000).recoverUnless(::predicate) { 2000 },
) )
} }
} }
class AndThenRecover { class AndThenRecover {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
expected = 5, expected = Ok(5),
actual = Ok(5).andThenRecover { Ok(7) }.get() actual = Ok(5).andThenRecover { Ok(7) },
) )
} }
@Test @Test
fun returnsTransformValueIfErr() { fun returnsTransformValueIfErr() {
assertEquals( assertEquals(
expected = 20, expected = Ok(20),
actual = Err(RecoverError).andThenRecover { Ok(20) }.get() actual = Err(RecoverError).andThenRecover { Ok(20) },
) )
} }
} }
@ -181,7 +184,7 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Ok(3000), expected = Ok(3000),
actual = Ok(3000).andThenRecoverIf(::predicate) { Ok(2000) } actual = Ok(3000).andThenRecoverIf(::predicate) { Ok(2000) },
) )
} }
@ -193,7 +196,7 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Ok(2000), expected = Ok(2000),
actual = Err(4000).andThenRecoverIf(::predicate) { Ok(2000) } actual = Err(4000).andThenRecoverIf(::predicate) { Ok(2000) },
) )
} }
@ -205,7 +208,7 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Err(2000), expected = Err(2000),
actual = Err(4000).andThenRecoverIf(::predicate) { Err(2000) } actual = Err(4000).andThenRecoverIf(::predicate) { Err(2000) },
) )
} }
@ -217,7 +220,7 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Err(4000), expected = Err(4000),
actual = Err(4000).andThenRecoverIf(::predicate) { Ok(2000) } actual = Err(4000).andThenRecoverIf(::predicate) { Ok(2000) },
) )
} }
} }
@ -231,7 +234,7 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Ok(3000), expected = Ok(3000),
actual = Ok(3000).andThenRecoverUnless(::predicate) { Ok(2000) } actual = Ok(3000).andThenRecoverUnless(::predicate) { Ok(2000) },
) )
} }
@ -243,7 +246,7 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Ok(2000), expected = Ok(2000),
actual = Err(4000).andThenRecoverUnless(::predicate) { Ok(2000) } actual = Err(4000).andThenRecoverUnless(::predicate) { Ok(2000) },
) )
} }
@ -255,7 +258,7 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Err(2000), expected = Err(2000),
actual = Err(4000).andThenRecoverUnless(::predicate) { Err(2000) } actual = Err(4000).andThenRecoverUnless(::predicate) { Err(2000) },
) )
} }
@ -267,7 +270,7 @@ class RecoverTest {
assertEquals( assertEquals(
expected = Err(4000), expected = Err(4000),
actual = Err(4000).andThenRecoverUnless(::predicate) { Ok(2000) } actual = Err(4000).andThenRecoverUnless(::predicate) { Ok(2000) },
) )
} }
} }

View File

@ -7,28 +7,35 @@ import kotlin.test.assertFalse
import kotlin.test.assertTrue import kotlin.test.assertTrue
class ResultIteratorTest { class ResultIteratorTest {
class HasNext { class HasNext {
@Test @Test
fun returnsTrueIfUnyieldedAndOk() { fun returnsTrueIfUnyieldedAndOk() {
val iterator = Ok("hello").iterator() val iterator = Ok("hello").iterator()
assertTrue { iterator.hasNext() }
assertTrue(iterator.hasNext())
} }
@Test @Test
fun returnsFalseIfErr() { fun returnsFalseIfErr() {
val iterator = Err("hello").iterator() val iterator = Err("hello").iterator()
assertFalse { iterator.hasNext() }
assertFalse(iterator.hasNext())
} }
@Test @Test
fun returnsFalseIfYielded() { fun returnsFalseIfYielded() {
val iterator = Ok("hello").iterator() val iterator = Ok("hello").iterator()
iterator.next() iterator.next()
assertFalse { iterator.hasNext() }
assertFalse(iterator.hasNext())
} }
} }
class Next { class Next {
@Test @Test
fun returnsValueIfUnyieldedAndOk() { fun returnsValueIfUnyieldedAndOk() {
assertEquals( assertEquals(
@ -42,7 +49,6 @@ class ResultIteratorTest {
val iterator = Err("hello").iterator() val iterator = Err("hello").iterator()
assertFailsWith<NoSuchElementException> { assertFailsWith<NoSuchElementException> {
@Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION")
iterator.next() iterator.next()
} }
} }
@ -50,24 +56,35 @@ class ResultIteratorTest {
@Test @Test
fun throwsExceptionIfYieldedAndOk() { fun throwsExceptionIfYieldedAndOk() {
val iterator = Ok("hello").iterator() val iterator = Ok("hello").iterator()
iterator.next() iterator.next()
assertFailsWith<NoSuchElementException> { iterator.next() }
assertFailsWith<NoSuchElementException> {
iterator.next()
}
} }
} }
class Remove { class Remove {
@Test @Test
fun makesHasNextReturnFalse() { fun makesHasNextReturnFalse() {
val iterator = Ok("hello").mutableIterator() val iterator = Ok("hello").mutableIterator()
iterator.remove() iterator.remove()
assertFalse { iterator.hasNext() }
assertFalse(iterator.hasNext())
} }
@Test @Test
fun makesNextThrowException() { fun makesNextThrowException() {
val iterator = Ok("hello").mutableIterator() val iterator = Ok("hello").mutableIterator()
iterator.remove() iterator.remove()
assertFailsWith<NoSuchElementException> { iterator.next() }
assertFailsWith<NoSuchElementException> {
iterator.next()
}
} }
} }
} }

View File

@ -10,25 +10,25 @@ class UnwrapTest {
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
expected = 5000, expected = 5000,
actual = Ok(5000).unwrap() actual = Ok(5000).unwrap(),
) )
} }
@Test @Test
fun throwsExceptionIfErr() { fun throwsExceptionIfErr() {
assertFailsWith<UnwrapException>("called Result.unwrap on an Err value 5000") { assertFailsWith<UnwrapException> {
@Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION")
Err(5000).unwrap() Err(5000).unwrap()
} }
} }
} }
class Expect { class Expect {
@Test @Test
fun returnsValueIfOk() { fun returnsValueIfOk() {
assertEquals( assertEquals(
expected = 1994, 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" override fun toString() = "the year should be"
} }
assertFailsWith<UnwrapException>("the year should be 1994") { assertFailsWith<UnwrapException> {
@Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION")
Err(1994).expect { message } Err(1994).expect { message }
} }
} }
} }
class UnwrapError { class UnwrapError {
@Test @Test
fun throwsExceptionIfOk() { fun throwsExceptionIfOk() {
assertFailsWith<UnwrapException>("called Result.unwrapError on an Ok value example") { assertFailsWith<UnwrapException> {
@Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION")
Ok("example").unwrapError() Ok("example").unwrapError()
} }
} }
@ -58,20 +57,20 @@ class UnwrapTest {
fun returnsErrorIfErr() { fun returnsErrorIfErr() {
assertEquals( assertEquals(
expected = "example", expected = "example",
actual = Err("example").unwrapError() actual = Err("example").unwrapError(),
) )
} }
} }
class ExpectError { class ExpectError {
@Test @Test
fun throwsExceptionIfOk() { fun throwsExceptionIfOk() {
val message = object { val message = object {
override fun toString() = "the year should be" override fun toString() = "the year should be"
} }
assertFailsWith<UnwrapException>("the year should be 2010") { assertFailsWith<UnwrapException> {
@Suppress("IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION")
Ok(2010).expectError { message } Ok(2010).expectError { message }
} }
} }
@ -80,7 +79,7 @@ class UnwrapTest {
fun returnsErrorIfErr() { fun returnsErrorIfErr() {
assertEquals( assertEquals(
expected = 2010, expected = 2010,
actual = Err(2010).expectError { "the year should be" } actual = Err(2010).expectError { "the year should be" },
) )
} }
} }

View File

@ -20,14 +20,12 @@ class ZipTest {
val result = zip( val result = zip(
{ Ok(10) }, { Ok(10) },
{ Ok(20) }, { Ok(20) },
Int::plus Int::plus,
) )
result as Ok
assertEquals( assertEquals(
expected = 30, expected = Ok(30),
actual = result.value actual = result,
) )
} }
@ -36,14 +34,12 @@ class ZipTest {
val result = zip( val result = zip(
{ Ok(10) }, { Ok(10) },
{ produce(20, "hello") }, { produce(20, "hello") },
Int::plus Int::plus,
) )
result as Err
assertEquals( assertEquals(
expected = "hello", expected = Err("hello"),
actual = result.error actual = result,
) )
} }
@ -52,14 +48,12 @@ class ZipTest {
val result = zip( val result = zip(
{ produce(10, "foo") }, { produce(10, "foo") },
{ produce(20, "bar") }, { produce(20, "bar") },
Int::plus Int::plus,
) )
result as Err
assertEquals( assertEquals(
expected = "foo", expected = Err("foo"),
actual = result.error actual = result,
) )
} }
@ -69,14 +63,12 @@ class ZipTest {
{ Ok("hello") }, { Ok("hello") },
{ Ok(2) }, { Ok(2) },
{ Ok(false) }, { Ok(false) },
::ZipData3 ::ZipData3,
) )
result as Ok
assertEquals( assertEquals(
expected = ZipData3("hello", 2, false), expected = Ok(ZipData3("hello", 2, false)),
actual = result.value actual = result,
) )
} }
@ -86,14 +78,12 @@ class ZipTest {
{ Ok("foo") }, { Ok("foo") },
{ Ok(1).and(Err("bar")) }, { Ok(1).and(Err("bar")) },
{ Ok(false) }, { Ok(false) },
::ZipData3 ::ZipData3,
) )
result as Err
assertEquals( assertEquals(
expected = "bar", expected = Err("bar"),
actual = result.error actual = result,
) )
} }
@ -103,12 +93,12 @@ class ZipTest {
{ Ok("foo") }, { Ok("foo") },
{ Ok(1).and(Err("bar")) }, { Ok(1).and(Err("bar")) },
{ Ok(false).and(Err("baz")) }, { Ok(false).and(Err("baz")) },
::ZipData3 ::ZipData3,
) )
assertEquals( assertEquals(
expected = "bar", expected = Err("bar"),
actual = result.getError() actual = result,
) )
} }
@ -118,14 +108,12 @@ class ZipTest {
{ Ok("foo").and(Err(1)) }, { Ok("foo").and(Err(1)) },
{ Ok(1).and(Err(2)) }, { Ok(1).and(Err(2)) },
{ Ok(false).and(Err(3)) }, { Ok(false).and(Err(3)) },
::ZipData3 ::ZipData3,
) )
result as Err
assertEquals( assertEquals(
expected = 1, expected = Err(1),
actual = result.error actual = result,
) )
} }
@ -136,14 +124,12 @@ class ZipTest {
{ Ok(2) }, { Ok(2) },
{ Ok(false) }, { Ok(false) },
{ Ok(1.5) }, { Ok(1.5) },
::ZipData4 ::ZipData4,
) )
result as Ok
assertEquals( assertEquals(
expected = ZipData4("hello", 2, false, 1.5), expected = Ok(ZipData4("hello", 2, false, 1.5)),
actual = result.value actual = result
) )
} }
@ -154,14 +140,12 @@ class ZipTest {
{ Ok(2).and(Err(1)) }, { Ok(2).and(Err(1)) },
{ Ok(false) }, { Ok(false) },
{ Ok(1.5).and(Err(2)) }, { Ok(1.5).and(Err(2)) },
::ZipData4 ::ZipData4,
) )
result as Err
assertEquals( assertEquals(
expected = 1, expected = Err(1),
actual = result.error actual = result,
) )
} }
@ -173,14 +157,12 @@ class ZipTest {
{ Ok(false) }, { Ok(false) },
{ Ok(1.5) }, { Ok(1.5) },
{ Ok('a') }, { Ok('a') },
::ZipData5 ::ZipData5,
) )
result as Ok
assertEquals( assertEquals(
expected = ZipData5("hello", 2, false, 1.5, 'a'), expected = Ok(ZipData5("hello", 2, false, 1.5, 'a')),
actual = result.value actual = result,
) )
} }
@ -192,14 +174,12 @@ class ZipTest {
{ Ok(false) }, { Ok(false) },
{ Ok(1.5) }, { Ok(1.5) },
{ Ok('a').and(Err(2)) }, { Ok('a').and(Err(2)) },
::ZipData5 ::ZipData5,
) )
result as Err
assertEquals( assertEquals(
expected = 1, expected = Err(1),
actual = result.error actual = result,
) )
} }
} }
@ -218,11 +198,9 @@ class ZipTest {
a + b + c + d + e a + b + c + d + e
} }
result as Ok
assertEquals( assertEquals(
expected = 150, expected = Ok(150),
actual = result.value, actual = result,
) )
} }
@ -238,17 +216,17 @@ class ZipTest {
a + b + c + d + e a + b + c + d + e
} }
result as Err val errors = listOf(
assertEquals(
expected = listOf(
"error one", "error one",
"error two", "error two",
"error three", "error three",
"error four", "error four",
"error five", "error five",
), )
actual = result.error,
assertEquals(
expected = Err(errors),
actual = result,
) )
} }
@ -264,11 +242,9 @@ class ZipTest {
a + b + c + d + e a + b + c + d + e
} }
result as Err
assertEquals( assertEquals(
expected = listOf("only error"), expected = Err(listOf("only error")),
actual = result.error, actual = result,
) )
} }
} }