From 745cba2a4e60c446f646affc772a5a47a78d3255 Mon Sep 17 00:00:00 2001 From: Ivan “CLOVIS” Canet Date: Sat, 24 Feb 2024 13:35:43 +0000 Subject: [PATCH] test(progress): Migrate all tests to Prepared --- progress-coroutines/build.gradle.kts | 24 +++++++----------------- progress/build.gradle.kts | 20 +++++--------------- progress/src/commonTest/kotlin/DoneTest.kt | 15 +++++++-------- progress/src/commonTest/kotlin/SimpleLoadingImplementationTest.kt | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------- progress/src/commonTest/kotlin/UnquantifiedLoadingTest.kt | 16 +++++++--------- progress/src/commonTest/kotlin/report/IntervalReduceProgressReporterTest.kt | 51 +++++++++++++++++++++------------------------------ progress/src/commonTest/kotlin/report/MapProgressReporterTest.kt | 21 ++++++++++----------- progress/src/commonTest/kotlin/report/NoOpProgressReporterTest.kt | 11 +++++------ 8 file(s) changed, 116 insertion(s)(+), 146 deletion(s)(-) diff --git a/progress-coroutines/build.gradle.kts b/progress-coroutines/build.gradle.kts --- a/progress-coroutines/build.gradle.kts +++ b/progress-coroutines/build.gradle.kts @@ -1,5 +1,3 @@ -@file:Suppress("UNUSED_VARIABLE") - plugins { alias(opensavvyConventions.plugins.base) alias(opensavvyConventions.plugins.kotlin.library) @@ -16,23 +14,15 @@ iosX64() linuxX64() - sourceSets { - val commonMain by getting { - dependencies { - api(projects.progress) - api(libs.kotlinx.coroutines.core) + sourceSets.commonMain.dependencies { + api(projects.progress) + api(libs.kotlinx.coroutines.core) - implementation(projects.logger) - } - } + implementation(projects.logger) + } - val commonTest by getting { - dependencies { - implementation(projects.tester) - - api(libs.kotlinx.coroutines.test) - } - } + sourceSets.commonTest.dependencies { + implementation(projects.tester) } } diff --git a/progress/build.gradle.kts b/progress/build.gradle.kts --- a/progress/build.gradle.kts +++ b/progress/build.gradle.kts @@ -1,5 +1,3 @@ -@file:Suppress("UNUSED_VARIABLE") - plugins { alias(opensavvyConventions.plugins.base) alias(opensavvyConventions.plugins.kotlin.library) @@ -16,20 +14,12 @@ iosX64() linuxX64() - sourceSets { - val commonMain by getting { - dependencies { - implementation(projects.logger) - } - } + sourceSets.commonMain.dependencies { + implementation(projects.logger) + } - val commonTest by getting { - dependencies { - implementation(projects.tester) - - api(libs.kotlinx.coroutines.test) - } - } + sourceSets.commonTest.dependencies { + implementation(projects.tester) } } diff --git a/progress/src/commonTest/kotlin/DoneTest.kt b/progress/src/commonTest/kotlin/DoneTest.kt --- a/progress/src/commonTest/kotlin/DoneTest.kt +++ b/progress/src/commonTest/kotlin/DoneTest.kt @@ -1,12 +1,11 @@ package opensavvy.progress -import kotlin.test.Test -import kotlin.test.assertEquals +import io.kotest.matchers.shouldBe +import opensavvy.prepared.runner.kotest.PreparedSpec -class DoneTest { - - @Test - fun string() { - assertEquals("Done", done().toString()) +@Suppress("unused") +class DoneTest : PreparedSpec({ + test("String representation") { + done().toString() shouldBe "Done" } -} +}) diff --git a/progress/src/commonTest/kotlin/SimpleLoadingImplementationTest.kt b/progress/src/commonTest/kotlin/SimpleLoadingImplementationTest.kt --- a/progress/src/commonTest/kotlin/SimpleLoadingImplementationTest.kt +++ b/progress/src/commonTest/kotlin/SimpleLoadingImplementationTest.kt @@ -1,63 +1,67 @@ package opensavvy.progress -import kotlin.test.Test +import com.benwoodworth.parameterize.parameterOf +import com.benwoodworth.parameterize.parameterize +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.matchers.shouldBe +import opensavvy.prepared.runner.kotest.PreparedSpec import kotlin.test.assertContains -import kotlin.test.assertEquals -import kotlin.test.assertFails -class SimpleLoadingImplementationTest { +@Suppress("unused") +class SimpleLoadingImplementationTest : PreparedSpec({ + suite("Constructor range validation") { + parameterize { + val legal by parameterOf(0.0, 0.1, 0.00001, 0.33, 1.0) - @Test - fun normalized0() { - assertEquals(0.0, loading(0.0).normalized) + test("The loading constructor should accept the value $legal") { + loading(legal).normalized shouldBe legal + } + } + + parameterize { + val illegal by parameterOf(-1.0, 1.01, 1.000001, -0.000001, Double.MAX_VALUE, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY) + + test("The loading constructor should not accept the value $illegal") { + shouldThrow { loading(illegal) } + } + } } - @Test - fun normalizedThird() { - assertEquals(0.33, loading(0.33).normalized) + suite("Conversion to integer percent") { + parameterize { + val value by parameterOf( + 0.0 to 0, + 0.33 to 33, + 1.0 to 100, + 0.01 to 1, + 0.001 to 0, + 0.4597 to 45, + ) + val (input, expected) = value + + test("Converting $input should give $expected") { + loading(input).percent shouldBe expected + } + } } - @Test - fun normalized1() { - assertEquals(1.0, loading(1.0).normalized) + suite("String conversion") { + parameterize { + val value by parameterOf( + 0.0 to "Loading(0%)", + 0.2 to "Loading(20%)", + 0.99 to "Loading(99%)", + 1.0 to "Loading(100%)", + ) + val (input, expected) = value + + test("loading($input) should be represented by the string $expected") { + loading(input).toString() shouldBe expected + } + } } - @Test - fun normalizedIllegalValues() { - assertFails { loading(-1.0) } - assertFails { loading(1.01) } - assertFails { loading(1.0000001) } - assertFails { loading(-0.000001) } - assertFails { loading(Double.MAX_VALUE) } - assertFails { loading(Double.NEGATIVE_INFINITY) } - assertFails { loading(Double.POSITIVE_INFINITY) } - } - - @Test - fun percent0() { - assertEquals(0, loading(0.0).percent) - } - - @Test - fun percentThird() { - assertEquals(33, loading(0.33).percent) - } - - @Test - fun percent100() { - assertEquals(100, loading(1.0).percent) - } - - @Test - fun string() { - assertEquals("Loading(0%)", loading(0.0).toString()) - assertEquals("Loading(20%)", loading(0.2).toString()) - assertEquals("Loading(99%)", loading(0.99).toString()) - assertEquals("Loading(100%)", loading(1.0).toString()) - } - - @Test - fun hash() { + test("The hashCode implementation is correct") { val set = hashSetOf( done(), loading(0.0), @@ -72,4 +76,4 @@ assertContains(set, loading(0.9)) assertContains(set, loading(1.0)) } -} +}) diff --git a/progress/src/commonTest/kotlin/UnquantifiedLoadingTest.kt b/progress/src/commonTest/kotlin/UnquantifiedLoadingTest.kt --- a/progress/src/commonTest/kotlin/UnquantifiedLoadingTest.kt +++ b/progress/src/commonTest/kotlin/UnquantifiedLoadingTest.kt @@ -1,13 +1,11 @@ package opensavvy.progress -import kotlin.test.Test -import kotlin.test.assertEquals +import io.kotest.matchers.shouldBe +import opensavvy.prepared.runner.kotest.PreparedSpec -class UnquantifiedLoadingTest { - - @Test - fun string() { - assertEquals("Loading", loading().toString()) +@Suppress("unused") +class UnquantifiedLoadingTest : PreparedSpec({ + test("String representation") { + loading().toString() shouldBe "Loading" } - -} +}) diff --git a/progress/src/commonTest/kotlin/report/IntervalReduceProgressReporterTest.kt b/progress/src/commonTest/kotlin/report/IntervalReduceProgressReporterTest.kt --- a/progress/src/commonTest/kotlin/report/IntervalReduceProgressReporterTest.kt +++ b/progress/src/commonTest/kotlin/report/IntervalReduceProgressReporterTest.kt @@ -1,86 +1,77 @@ package opensavvy.progress.report +import io.kotest.assertions.throwables.shouldThrow +import io.kotest.matchers.shouldBe +import opensavvy.prepared.runner.kotest.PreparedSpec import opensavvy.progress.Progress import opensavvy.progress.done import opensavvy.progress.loading -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFails -class IntervalReduceProgressReporterTest { - - @Test - fun cannotCreateInvalidRange() { - assertFails { - ProgressReporter { } - .reduceToInterval(0.7, 0.2) +@Suppress("unused") +class IntervalReduceProgressReporterTest : PreparedSpec({ + test("Cannot create an invalid range") { + shouldThrow { + emptyProgressReporter().reduceToInterval(0.7, 0.2) } } - @Test - fun reduceInterval() { + test("Reduce with the range syntax") { var value: Progress? = null val reporter = ProgressReporter { value = it } .reduceToInterval(0.2..0.4) reporter.report(loading(0.1)) - assertEquals(loading(0.22), value) + value shouldBe loading(0.22) } - @Test - fun reduceMinMax() { + test("Reduce with the min-max syntax") { var value: Progress? = null val reporter = ProgressReporter { value = it } .reduceToInterval(0.2, 0.4) reporter.report(loading(0.1)) - assertEquals(loading(0.22), value) + value shouldBe loading(0.22) } - @Test - fun reduceDone() { + test("Reducing the 'done' event should return the range maximum") { var value: Progress? = null val reporter = ProgressReporter { value = it } .reduceToInterval(0.2..0.4) reporter.report(done()) - assertEquals(loading(0.4), value) + value shouldBe loading(0.4) } - @Test - fun reduceUnquantified() { + test("Reducing an unquantified loading event should return the range middle") { var value: Progress? = null val reporter = ProgressReporter { value = it } .reduceToInterval(0.2..0.4) reporter.report(loading()) - assertEquals(loading(0.3), value) + value shouldBe loading(0.3) } - @Test - fun reduceZero() { + test("Reducing 0 should give the range minimum") { var value: Progress? = null val reporter = ProgressReporter { value = it } .reduceToInterval(0.2..0.4) reporter.report(loading(0.0)) - assertEquals(loading(0.2), value) + value shouldBe loading(0.2) } - @Test - fun reduceOne() { + test("Reducing 1 should give the range maximum") { var value: Progress? = null val reporter = ProgressReporter { value = it } .reduceToInterval(0.2..0.4) reporter.report(loading(1.0)) - assertEquals(loading(0.4), value) + value shouldBe loading(0.4) } - -} +}) diff --git a/progress/src/commonTest/kotlin/report/MapProgressReporterTest.kt b/progress/src/commonTest/kotlin/report/MapProgressReporterTest.kt --- a/progress/src/commonTest/kotlin/report/MapProgressReporterTest.kt +++ b/progress/src/commonTest/kotlin/report/MapProgressReporterTest.kt @@ -1,41 +1,40 @@ package opensavvy.progress.report +import io.kotest.matchers.shouldBe +import opensavvy.prepared.runner.kotest.PreparedSpec import opensavvy.progress.Progress import opensavvy.progress.done import opensavvy.progress.loading -import kotlin.test.Test import kotlin.test.assertEquals -class MapProgressReporterTest { +@Suppress("unused") +class MapProgressReporterTest : PreparedSpec({ - @Test - fun replaceByDone() { + test("Should intercept reported values and transform them (replacing by done)") { var value: Progress? = null val reporter = ProgressReporter { value = it } .map { done() } reporter.report(loading(0.5)) - assertEquals(done(), value) + value shouldBe done() } - @Test - fun addOne() { + test("Should intercept reported values and transform them (increasing the value)") { var value: Progress? = null val reporter = ProgressReporter { value = it } .map { it as Progress.Loading.Quantified; loading(it.normalized + 0.2) } reporter.report(loading(0.2)) - assertEquals(loading(0.4), value) + value shouldBe loading(0.4) } - @Test - fun string() { + test("String representation") { val reporter = emptyProgressReporter() .reduceToInterval(0.1, 0.2) .map { it } assertEquals("NoOpProgressReporter.reduceToInterval(0.1..0.2).map()", reporter.toString()) } -} +}) diff --git a/progress/src/commonTest/kotlin/report/NoOpProgressReporterTest.kt b/progress/src/commonTest/kotlin/report/NoOpProgressReporterTest.kt --- a/progress/src/commonTest/kotlin/report/NoOpProgressReporterTest.kt +++ b/progress/src/commonTest/kotlin/report/NoOpProgressReporterTest.kt @@ -1,13 +1,12 @@ package opensavvy.progress.report +import opensavvy.prepared.runner.kotest.PreparedSpec import opensavvy.progress.loading -import kotlin.test.Test -class NoOpProgressReporterTest { - - @Test - fun coverage() { +@Suppress("unused") +class NoOpProgressReporterTest : PreparedSpec({ + test("No-op") { // it does nothing anyway… emptyProgressReporter().report(loading(0.5)) } -} +}) -- tangled.sh