From cbb576afc5cf589a0143de80913a1f30dcff2316 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Sun, 8 Dec 2024 14:22:42 +0100 Subject: [PATCH] breaking: Move Random control to its own package --- .../commonTest/kotlin/ParamaterizeTests.kt | 2 ++ .../src/commonTest/kotlin/RandomTest.kt | 2 +- suite/README.md | 8 +++--- suite/src/commonMain/kotlin/Prepared.kt | 2 +- suite/src/commonMain/kotlin/Shared.kt | 2 +- suite/src/commonMain/kotlin/SuiteDsl.kt | 1 + .../commonMain/kotlin/{ => random}/Random.kt | 26 ++++++++++--------- suite/src/commonTest/kotlin/RandomTest.kt | 7 +++++ 8 files changed, 31 insertions(+), 19 deletions(-) rename suite/src/commonMain/kotlin/{ => random}/Random.kt (91%) diff --git a/compat/compat-parameterize/src/commonTest/kotlin/ParamaterizeTests.kt b/compat/compat-parameterize/src/commonTest/kotlin/ParamaterizeTests.kt index edd1693..019a5db 100644 --- a/compat/compat-parameterize/src/commonTest/kotlin/ParamaterizeTests.kt +++ b/compat/compat-parameterize/src/commonTest/kotlin/ParamaterizeTests.kt @@ -10,6 +10,8 @@ import opensavvy.prepared.compat.kotlinx.datetime.now import opensavvy.prepared.compat.kotlinx.datetime.set import opensavvy.prepared.runner.kotest.preparedSuite import opensavvy.prepared.suite.* +import opensavvy.prepared.suite.random.nextInt +import opensavvy.prepared.suite.random.random import kotlin.time.Duration.Companion.minutes import kotlin.time.ExperimentalTime diff --git a/runners/runner-kotlin-test/src/commonTest/kotlin/RandomTest.kt b/runners/runner-kotlin-test/src/commonTest/kotlin/RandomTest.kt index c48f709..a073251 100644 --- a/runners/runner-kotlin-test/src/commonTest/kotlin/RandomTest.kt +++ b/runners/runner-kotlin-test/src/commonTest/kotlin/RandomTest.kt @@ -1,7 +1,7 @@ package opensavvy.prepared.runner.kotlin import opensavvy.prepared.suite.SuiteDsl -import opensavvy.prepared.suite.randomInt +import opensavvy.prepared.suite.random.randomInt import kotlin.test.assertNotEquals class RandomTest : TestExecutor() { diff --git a/suite/README.md b/suite/README.md index 3a4569e..6e1393b 100644 --- a/suite/README.md +++ b/suite/README.md @@ -105,8 +105,8 @@ Randomness is useful to generate test data, but it makes reproducing test failur Prepared exposes utilities to generate reproducible random tests: -- [random.setSeed][opensavvy.prepared.suite.Random.setSeed] to set the seed for an entire test. -- [random.nextInt][opensavvy.prepared.suite.nextInt] (and similar functions) to generate random values. -- [randomInt][opensavvy.prepared.suite.randomInt] (and similar functions) to generate random prepared values. +- [random.setSeed][opensavvy.prepared.suite.random.Random.setSeed] to set the seed for an entire test. +- [random.nextInt][opensavvy.prepared.suite.random.nextInt] (and similar functions) to generate random values. +- [randomInt][opensavvy.prepared.suite.random.randomInt] (and similar functions) to generate random prepared values. -To learn more, see [random][opensavvy.prepared.suite.random]. +To learn more, see [random][opensavvy.prepared.suite.random.random]. diff --git a/suite/src/commonMain/kotlin/Prepared.kt b/suite/src/commonMain/kotlin/Prepared.kt index 4c4cef6..89403b4 100644 --- a/suite/src/commonMain/kotlin/Prepared.kt +++ b/suite/src/commonMain/kotlin/Prepared.kt @@ -32,7 +32,7 @@ import kotlin.reflect.KProperty * Prepared values are constructed lazily when they are accessed within a test. * Because of this, they have access to the test's [TestDsl] and can `suspend`. * - * For the specific use-case of generating random values, see [random][TestDsl.random]. + * For the specific use-case of generating random values, see [random][opensavvy.prepared.suite.random.random]. * * ### Comparison with other frameworks * diff --git a/suite/src/commonMain/kotlin/Shared.kt b/suite/src/commonMain/kotlin/Shared.kt index d80eb97..4eb4641 100644 --- a/suite/src/commonMain/kotlin/Shared.kt +++ b/suite/src/commonMain/kotlin/Shared.kt @@ -50,7 +50,7 @@ import kotlin.reflect.KProperty * accesses it. Side effects, if any, will only be observable in the first test. * * Additionally, **shared values cannot access most features of this library**, including [time control][TestDsl.time], - * [randomness control][TestDsl.random], [finalizers][TestDsl.cleanUp], etc. + * [randomness control][opensavvy.prepared.suite.random.random], [finalizers][TestDsl.cleanUp], etc. * This is because these features are based on side effects, which this class swallows silently. * * To summarize: diff --git a/suite/src/commonMain/kotlin/SuiteDsl.kt b/suite/src/commonMain/kotlin/SuiteDsl.kt index a7a03e9..78a52ca 100644 --- a/suite/src/commonMain/kotlin/SuiteDsl.kt +++ b/suite/src/commonMain/kotlin/SuiteDsl.kt @@ -1,6 +1,7 @@ package opensavvy.prepared.suite import opensavvy.prepared.suite.config.TestConfig +import opensavvy.prepared.suite.random.randomInt import kotlin.coroutines.CoroutineContext import kotlin.coroutines.EmptyCoroutineContext diff --git a/suite/src/commonMain/kotlin/Random.kt b/suite/src/commonMain/kotlin/random/Random.kt similarity index 91% rename from suite/src/commonMain/kotlin/Random.kt rename to suite/src/commonMain/kotlin/random/Random.kt index a86369e..7b5b451 100644 --- a/suite/src/commonMain/kotlin/Random.kt +++ b/suite/src/commonMain/kotlin/random/Random.kt @@ -1,7 +1,9 @@ -package opensavvy.prepared.suite +package opensavvy.prepared.suite.random import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock +import opensavvy.prepared.suite.TestDsl +import opensavvy.prepared.suite.prepared import kotlin.jvm.JvmName import kotlin.random.Random.Default.nextBits import kotlin.random.Random.Default.nextBoolean @@ -40,7 +42,7 @@ private val randomSource by prepared { } /** - * Random control helper. See [random][TestDsl.random]. + * Random control helper. See [random][random]. */ class Random internal constructor(private val dsl: TestDsl) { @@ -138,7 +140,7 @@ class Random internal constructor(private val dsl: TestDsl) { * itself, using [prepared] values. However, the functions mentioned above are only available on this helper, which * only exists in tests. * - * To simplify this pattern, we also expose [randomInt] and similar functions which expose [prepared providers][PreparedProvider]. + * To simplify this pattern, we also expose [randomInt] and similar functions which expose [prepared providers][opensavvy.prepared.suite.PreparedProvider]. * * ```kotlin * val int by randomInt() @@ -232,7 +234,7 @@ suspend fun Random.nextFloat() = /** * Provider for random bits. - * @see TestDsl.random Learn more about random values and reproducibility. + * @see random Learn more about random values and reproducibility. * @see KotlinRandom.nextBits Standard library. * @see nextBits Direct value equivalent. */ @@ -240,7 +242,7 @@ fun randomBits(bitCount: Int) = prepared { random.nextBits(bitCount) } /** * Provider for a random integer. - * @see TestDsl.random Learn more about random values and reproducibility. + * @see random Learn more about random values and reproducibility. * @see KotlinRandom.nextInt Standard library. * @see nextInt Direct value equivalent. */ @@ -248,7 +250,7 @@ fun randomInt() = prepared { random.nextInt() } /** * Provider for a random integer. - * @see TestDsl.random Learn more about random values and reproducibility. + * @see random Learn more about random values and reproducibility. * @see KotlinRandom.nextInt Standard library. * @see nextInt Direct value equivalent. */ @@ -256,7 +258,7 @@ fun randomInt(from: Int, until: Int) = prepared { random.nextInt(from, until) } /** * Provider for a random integer. - * @see TestDsl.random Learn more about random values and reproducibility. + * @see random Learn more about random values and reproducibility. * @see KotlinRandom.nextLong Standard library. * @see nextLong Direct value equivalent. */ @@ -264,7 +266,7 @@ fun randomLong() = prepared { random.nextLong() } /** * Provider for a random integer. - * @see TestDsl.random Learn more about random values and reproducibility. + * @see random Learn more about random values and reproducibility. * @see KotlinRandom.nextLong Standard library. * @see nextLong Direct value equivalent. */ @@ -272,7 +274,7 @@ fun randomLong(from: Long, until: Long) = prepared { random.nextLong(from, until /** * Provider for a random boolean. - * @see TestDsl.random Learn more about random values and reproducibility. + * @see random Learn more about random values and reproducibility. * @see KotlinRandom.nextBoolean Standard library. * @see nextBoolean Direct value equivalent. */ @@ -280,7 +282,7 @@ fun randomBoolean() = prepared { random.nextBoolean() } /** * Provider for a random double. - * @see TestDsl.random Learn more about random values and reproducibility. + * @see random Learn more about random values and reproducibility. * @see KotlinRandom.nextDouble Standard library. * @see nextDouble Direct value equivalent. */ @@ -288,7 +290,7 @@ fun randomDouble() = prepared { random.nextDouble() } /** * Provider for a random double. - * @see TestDsl.random Learn more about random values and reproducibility. + * @see random Learn more about random values and reproducibility. * @see KotlinRandom.nextDouble Standard library. * @see nextDouble Direct value equivalent. */ @@ -296,7 +298,7 @@ fun randomDouble(from: Double, until: Double) = prepared { random.nextDouble(fro /** * Provider for a random float. - * @see TestDsl.random Learn more about random values and reproducibility. + * @see random Learn more about random values and reproducibility. * @see KotlinRandom.nextFloat Standard library. * @see nextFloat Direct value equivalent. */ diff --git a/suite/src/commonTest/kotlin/RandomTest.kt b/suite/src/commonTest/kotlin/RandomTest.kt index 1da357a..1d3b55a 100644 --- a/suite/src/commonTest/kotlin/RandomTest.kt +++ b/suite/src/commonTest/kotlin/RandomTest.kt @@ -4,6 +4,13 @@ import io.kotest.assertions.assertSoftly import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldNotBe import opensavvy.prepared.runner.kotest.PreparedSpec +import opensavvy.prepared.suite.random.nextBoolean +import opensavvy.prepared.suite.random.nextDouble +import opensavvy.prepared.suite.random.nextInt +import opensavvy.prepared.suite.random.random +import opensavvy.prepared.suite.random.randomBoolean +import opensavvy.prepared.suite.random.randomDouble +import opensavvy.prepared.suite.random.randomInt class RandomTest : PreparedSpec({ -- 2.51.2