Skip to content

Commit

Permalink
refactor: migrate NumberInstancesTest to kotlin-test (#3232)
Browse files Browse the repository at this point in the history
  • Loading branch information
milgner authored Oct 31, 2023
1 parent 6b794a9 commit bd5baa2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package arrow.core.extensions

import arrow.core.test.laws.MonoidLaws
import arrow.core.test.laws.SemiringLaws
import arrow.core.test.testLaws
import io.kotest.core.spec.style.StringSpec
import arrow.core.test.testLawsCommon
import io.kotest.property.Arb
import io.kotest.property.arbitrary.byte
import io.kotest.property.arbitrary.int
import io.kotest.property.arbitrary.long
import io.kotest.property.arbitrary.short
import kotlin.test.Test

class NumberInstancesTest : StringSpec({
class NumberInstancesTest {

fun <F> testAllLaws(
name: String,
Expand All @@ -21,13 +21,19 @@ class NumberInstancesTest : StringSpec({
GEN: Arb<F>,
eq: (F, F) -> Boolean = { a, b -> a == b }
) {
testLaws(SemiringLaws(name, zero, combine, one, combineMultiplicate, GEN, eq))
testLaws(MonoidLaws(name, zero, combine, GEN, eq))
testLawsCommon(SemiringLaws(name, zero, combine, one, combineMultiplicate, GEN, eq))
testLawsCommon(MonoidLaws(name, zero, combine, GEN, eq))
}

@Test fun testByteLaws() =
testAllLaws("Byte", 0, { x, y -> (x + y).toByte() }, 1, { x, y -> (x * y).toByte() }, Arb.byte())

@Test fun testShortLaws() =
testAllLaws("Short", 0, { x, y -> (x + y).toShort() }, 1, { x, y -> (x * y).toShort() }, Arb.short())

@Test fun testIntLaws() =
testAllLaws("Int", 0, Int::plus, 1, Int::times, Arb.int())
testAllLaws("Long", 0, Long::plus, 1, Long::times, Arb.long())

})
@Test fun testLongLaws() =
testAllLaws("Long", 0, Long::plus, 1, Long::times, Arb.long())
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package arrow.core.test

import io.kotest.assertions.fail
import io.kotest.assertions.withClue
import io.kotest.core.names.TestName
import io.kotest.core.spec.style.StringSpec
import io.kotest.core.spec.style.scopes.StringSpecScope
import io.kotest.core.spec.style.scopes.addTest
import io.kotest.core.test.TestContext
import kotlinx.coroutines.test.runTest

interface LawSet {
val laws: List<Law>
}

data class Law(val name: String, val test: suspend TestContext.() -> Unit)
data class Law(val name: String, val test: suspend () -> Unit)

fun <A> A.equalUnderTheLaw(b: A, f: (A, A) -> Boolean = { x, y -> x == y }): Boolean =
if (f(this, b)) true else fail("Found $this but expected: $b")
Expand All @@ -23,15 +23,21 @@ fun StringSpec.testLaws(vararg laws: List<Law>): Unit = laws
.distinctBy { law: Law -> law.name }
.forEach { law: Law ->
addTest(TestName(null, law.name, false), false, null) {
law.test(StringSpecScope(this.coroutineContext, testCase))
runTest { law.test() }
}
}

fun StringSpec.testLaws(prefix: String, vararg laws: List<Law>): Unit = laws
.flatMap { list: List<Law> -> list.asIterable() }
.distinctBy { law: Law -> law.name }
.forEach { law: Law ->
addTest(TestName(prefix, law.name, false), false, null) {
law.test(StringSpecScope(this.coroutineContext, testCase))
fun testLawsCommon(lawSet: LawSet) = withClue("In $lawSet") {
testLawsCommon(lawSet.laws)
}

fun testLawsCommon(vararg laws: List<Law>) = runTest {
laws
.flatMap(List<Law>::asIterable)
.distinctBy(Law::name)
.forEach { law: Law ->
withClue("Testing ${law.name}") {
law.test()
}
}
}
}

0 comments on commit bd5baa2

Please sign in to comment.