From bd5baa2de6165951a30543ff8f24ac116b3e97b8 Mon Sep 17 00:00:00 2001 From: Marcus Ilgner Date: Tue, 31 Oct 2023 08:32:35 +0100 Subject: [PATCH] refactor: migrate NumberInstancesTest to kotlin-test (#3232) --- .../core/extensions/NumberInstancesTest.kt | 20 ++++++++----- .../commonTest/kotlin/arrow/core/test/Laws.kt | 28 +++++++++++-------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/extensions/NumberInstancesTest.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/extensions/NumberInstancesTest.kt index ad826f6f1ad..59709b7e86a 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/extensions/NumberInstancesTest.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/extensions/NumberInstancesTest.kt @@ -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 testAllLaws( name: String, @@ -21,13 +21,19 @@ class NumberInstancesTest : StringSpec({ GEN: Arb, 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()) +} diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/test/Laws.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/test/Laws.kt index b2383454f66..c5221757595 100644 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/test/Laws.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/test/Laws.kt @@ -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 } -data class Law(val name: String, val test: suspend TestContext.() -> Unit) +data class Law(val name: String, val test: suspend () -> Unit) fun 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") @@ -23,15 +23,21 @@ fun StringSpec.testLaws(vararg laws: List): 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): Unit = laws - .flatMap { list: List -> 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) = runTest { + laws + .flatMap(List::asIterable) + .distinctBy(Law::name) + .forEach { law: Law -> + withClue("Testing ${law.name}") { + law.test() + } } - } +}