Skip to content

Commit

Permalink
refactor: migrate IorSpec to kotlin-test (#3249)
Browse files Browse the repository at this point in the history
Co-authored-by: Simon Vergauwen <nomisRev@users.noreply.github.com>
  • Loading branch information
milgner and nomisRev authored Nov 8, 2023
1 parent e35d935 commit aa3402b
Showing 1 changed file with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,46 @@ import arrow.core.Either
import arrow.core.Ior
import arrow.core.test.nonEmptyList
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe
import io.kotest.property.Arb
import io.kotest.property.arbitrary.int
import io.kotest.property.checkAll
import kotlin.test.Test
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.test.runTest

@Suppress(
"UNREACHABLE_CODE",
"IMPLICIT_NOTHING_TYPE_ARGUMENT_IN_RETURN_POSITION",
"UNUSED_VARIABLE"
)
class IorSpec : StringSpec({
"Accumulates" {
class IorSpec {
@Test fun accumulates() = runTest {
ior(String::plus) {
val one = Ior.Both("Hello", 1).bind()
val two = Ior.Both(", World!", 2).bind()
one + two
} shouldBe Ior.Both("Hello, World!", 3)
}

"Accumulates and short-circuits with Left" {
@Test fun accumulatesAndShortCircuitsWithLeft() = runTest {
ior(String::plus) {
val one = Ior.Both("Hello", 1).bind()
val two: Int = Ior.Left(", World!").bind()
one + two
} shouldBe Ior.Left("Hello, World!")
}

"Accumulates with Either" {
@Test fun accumulatesWithEither() = runTest {
ior(String::plus) {
val one = Ior.Both("Hello", 1).bind()
val two: Int = Either.Left(", World!").bind<Int>()
one + two
} shouldBe Ior.Left("Hello, World!")
}

"Concurrent - arrow.ior bind" {
@Test fun concurrentArrowIorBind() = runTest {
checkAll(Arb.nonEmptyList(Arb.int())) { xs ->
ior(List<Int>::plus) {
xs.mapIndexed { index, s -> async { Ior.Both(listOf(s), index).bind() } }.awaitAll()
Expand All @@ -51,23 +52,23 @@ class IorSpec : StringSpec({
}
}

"Accumulates eagerly" {
@Test fun accumulatesEagerly() = runTest {
ior(String::plus) {
val one = Ior.Both("Hello", 1).bind()
val two = Ior.Both(", World!", 2).bind()
one + two
} shouldBe Ior.Both("Hello, World!", 3)
}

"Accumulates with Either eagerly" {
@Test fun accumulatesWithEitherEagerly() = runTest {
ior(String::plus) {
val one = Ior.Both("Hello", 1).bind()
val two: Int = Either.Left(", World!").bind<Int>()
one + two
} shouldBe Ior.Left("Hello, World!")
}

"Ior rethrows exception" {
@Test fun iorRethrowsException() = runTest {
val boom = RuntimeException("Boom!")
shouldThrow<RuntimeException> {
ior(String::plus) {
Expand All @@ -76,12 +77,12 @@ class IorSpec : StringSpec({
}.message shouldBe "Boom!"
}

"Recover works as expected" {
@Test fun recoverWorksAsExpected() = runTest {
ior(String::plus) {
val one = recover({ Ior.Left("Hello").bind() }) { 1 }
val two = Ior.Right(2).bind()
val three = Ior.Both(", World", 3).bind()
one + two + three
} shouldBe Ior.Both(", World", 6)
}
})
}

0 comments on commit aa3402b

Please sign in to comment.