Skip to content

Commit

Permalink
Move arrow-functions tests to kotlin.test (#3243)
Browse files Browse the repository at this point in the history
  • Loading branch information
serras authored Oct 28, 2023
1 parent 24d7abc commit 1fd3cbf
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 198 deletions.
10 changes: 0 additions & 10 deletions arrow-libs/core/arrow-functions/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
@file:Suppress("DSL_SCOPE_VIOLATION")

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id(libs.plugins.kotlin.multiplatform.get().pluginId)
alias(libs.plugins.arrowGradleConfig.kotlin)
alias(libs.plugins.arrowGradleConfig.publish)
alias(libs.plugins.kotlinx.kover)
alias(libs.plugins.kotest.multiplatform)
alias(libs.plugins.spotless)
}

Expand All @@ -34,17 +31,10 @@ kotlin {
implementation(projects.arrowFxCoroutines)
implementation(libs.kotlin.test)
implementation(libs.coroutines.test)
implementation(libs.kotest.frameworkEngine)
implementation(libs.kotest.assertionsCore)
implementation(libs.kotest.property)
}
}

jvmTest {
dependencies {
runtimeOnly(libs.kotest.runnerJUnit5)
}
}
}

jvm {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@ package arrow.core

import io.kotest.common.Platform
import io.kotest.common.platform
import io.kotest.core.spec.style.StringSpec
import io.kotest.property.Arb
import io.kotest.matchers.shouldBe
import io.kotest.property.arbitrary.arbitrary
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.list
import io.kotest.property.arbitrary.next
import io.kotest.property.checkAll
import kotlinx.coroutines.test.runTest
import kotlin.test.Test

class AndThenTests : StringSpec({
val count = when (platform) {
class AndThenTests {
private val count = when (platform) {
Platform.JVM -> 200_000
else -> 1000
}

fun <A, B> Arb.Companion.functionAToB(arb: Arb<B>): Arb<(A) -> B> = arbitrary { random ->
private fun <A, B> Arb.Companion.functionAToB(arb: Arb<B>): Arb<(A) -> B> = arbitrary { random ->
{ _: A -> arb.next(random) }.memoize()
}

"AndThen0 - compose a chain of functions with andThen should be same with AndThen" {
@Test fun andThen0ComposeChainWithAndThen() = runTest {
checkAll(Arb.int(), Arb.list(Arb.functionAToB<Int, Int>(Arb.int()))) { i, fs ->
val result = fs.fold({ i }) { acc, f ->
{ f(acc()) }
Expand All @@ -35,15 +36,15 @@ class AndThenTests : StringSpec({
}
}

"AndThen0 - andThen is stack safe" {
@Test fun andThen0AndThenStackSafe() = runTest {
val result = (0 until count).fold({ 0 }) { acc, _ ->
acc.andThen { it + 1 }
}.invoke()

result shouldBe count
}

"AndThen1 - compose a chain of functions with andThen should be same with AndThen" {
@Test fun andThen1ComposeChainWithAndThen() = runTest {
checkAll(Arb.int(), Arb.list(Arb.functionAToB<Int, Int>(Arb.int()))) { i, fs ->
val result = fs.fold({ x: Int -> x }) { acc, f ->
{ x: Int -> f(acc(x)) }
Expand All @@ -57,7 +58,7 @@ class AndThenTests : StringSpec({
}
}

"AndThen1 - compose a chain of function with compose should be same with AndThen" {
@Test fun andThen1ComposeChainWithCompose() = runTest {
checkAll(Arb.int(), Arb.list(Arb.functionAToB<Int, Int>(Arb.int()))) { i, fs ->
val result = fs.fold({ x: Int -> x }) { acc, f ->
{ x: Int -> acc(f(x)) }
Expand All @@ -71,23 +72,23 @@ class AndThenTests : StringSpec({
}
}

"AndThen1 - andThen is stack safe" {
@Test fun andThen1AndThenStackSafe() = runTest {
val result = (0 until count).fold({ x: Int -> x }) { acc, _ ->
acc.andThen { it + 1 }
}.invoke(0)

result shouldBe count
}

"AndThen1 - compose is stack safe" {
@Test fun andThen1ComposeStackSafe() = runTest {
val result = (0 until count).fold({ x: Int -> x }) { acc, _ ->
acc.compose { it + 1 }
}.invoke(0)

result shouldBe count
}

"AndThen2 - compose a chain of functions with andThen should be same with AndThen" {
@Test fun andThen2ComposeChainWithAndThen() = runTest {
checkAll(Arb.int(), Arb.int(), Arb.list(Arb.functionAToB<Int, Int>(Arb.int()))) { i, j, fs ->
val result = fs.fold({ x: Int, y: Int -> x + y }) { acc, f ->
{ x: Int, y: Int -> f(acc(x, y)) }
Expand All @@ -101,11 +102,11 @@ class AndThenTests : StringSpec({
}
}

"AndThen2 - andThen is stack safe" {
@Test fun andThen2AndThenStackSafe() = runTest {
val result = (0 until count).fold({ x: Int, y: Int -> x + y }) { acc, _ ->
acc.andThen { it + 1 }
}.invoke(0, 0)

result shouldBe count
}
})
}
Loading

0 comments on commit 1fd3cbf

Please sign in to comment.