Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate IorSpec to kotlin-test #3249

Merged
merged 2 commits into from
Nov 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
}