Skip to content

Commit

Permalink
Improve consistency across unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbull committed Mar 5, 2024
1 parent 84b8ccb commit bfcb0d7
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 366 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SuspendableBindingTest {

assertEquals(
expected = Ok(3),
actual = result
actual = result,
)
}

Expand All @@ -60,7 +60,7 @@ class SuspendableBindingTest {

assertEquals(
expected = Ok(3),
actual = result
actual = result,
)
}

Expand Down Expand Up @@ -90,7 +90,7 @@ class SuspendableBindingTest {

assertEquals(
expected = Err(BindingError),
actual = result
actual = result,
)
}

Expand Down Expand Up @@ -127,7 +127,7 @@ class SuspendableBindingTest {

assertEquals(
expected = Err(BindingError),
actual = result
actual = result,
)

assertTrue(xStateChange)
Expand Down Expand Up @@ -161,7 +161,7 @@ class SuspendableBindingTest {

assertEquals(
expected = Err(BindingError),
actual = result
actual = result,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,15 +32,15 @@ class AsyncSuspendableBindingTest {
return Ok(2)
}

val result = binding<Int, BindingError> {
val result = binding {
val x = async { provideX().bind() }
val y = async { provideY().bind() }
x.await() + y.await()
}

assertEquals(
expected = Ok(3),
actual = result
actual = result,
)
}

Expand All @@ -61,7 +61,7 @@ class AsyncSuspendableBindingTest {
return Err(BindingError.BindingErrorB)
}

val result = binding<Int, BindingError> {
val result = binding {
val x = async { provideX().bind() }
val y = async { provideY().bind() }
val z = async { provideZ().bind() }
Expand Down Expand Up @@ -98,7 +98,7 @@ class AsyncSuspendableBindingTest {
return Err(BindingError.BindingErrorB)
}

val result = binding<Int, BindingError> {
val result = binding {
val x = async { provideX().bind() }
val y = async { provideY().bind() }
val z = async { provideZ().bind() }
Expand All @@ -108,7 +108,7 @@ class AsyncSuspendableBindingTest {

assertEquals(
expected = Err(BindingError.BindingErrorB),
actual = result
actual = result,
)

assertFalse(xStateChange)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class RunSuspendCatchingTest {

assertEquals(
expected = Ok("example"),
actual = result
actual = result,
)
}

Expand All @@ -61,7 +61,7 @@ class RunSuspendCatchingTest {

assertEquals(
expected = Err(exception),
actual = result
actual = result,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.github.michaelbull.result

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class BindingTest {

Expand All @@ -13,29 +12,33 @@ class BindingTest {
fun provideX(): Result<Int, BindingError> = Ok(1)
fun provideY(): Result<Int, BindingError> = Ok(2)

val result = binding<Int, BindingError> {
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
fun returnsOkIfAllBindsOfDifferentTypeAreSuccessful() {
fun provideX(): Result<String, BindingError> = Ok("1")
fun provideY(x: Int): Result<Int, BindingError> = Ok(x + 2)

val result = binding<Int, BindingError> {
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
Expand All @@ -44,15 +47,17 @@ class BindingTest {
fun provideY(): Result<Int, BindingError> = Err(BindingError)
fun provideZ(): Result<Int, BindingError> = Ok(2)

val result = binding<Int, BindingError> {
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
Expand All @@ -68,7 +73,9 @@ class BindingTest {
x + y.toInt() + z
}

assertTrue(result is Err)
assertEquals(BindingError, result.error)
assertEquals(
expected = Err(BindingError),
actual = result,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class GetTest {
@Test
fun throwsTransformedErrorIfErr() {
assertFailsWith<CustomException> {
Err("error").getOrThrow { error -> CustomException(error) }
Err("error").getOrThrow(::CustomException)
}
}

Expand Down Expand Up @@ -148,19 +148,43 @@ class GetTest {
}

class Merge {
interface Direction
object Left : Direction
object Right : Direction

@Test
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(
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<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(
expected = listOf(1, 2, 3),
actual = Err(listOf(1, 2, 3)).and(Err(setOf(4, 5, 6))).merge()
expected = Right,
actual = direction
)
}
}
Expand Down
Loading

0 comments on commit bfcb0d7

Please sign in to comment.