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 NumberInstancesTest to kotlin-test #3232

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
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 @@ -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()
}
}
}
}