From 437757c6bdbbc79ecd0ef0eb2afd2c050d7ac5ce Mon Sep 17 00:00:00 2001 From: Vitaly Pestryakov <34812712+dsvag@users.noreply.github.com> Date: Fri, 24 Nov 2023 18:45:11 +0200 Subject: [PATCH 1/2] update test dependencies --- .../src/main/kotlin/elmslie.tests-convention.gradle.kts | 2 +- gradle/libs.versions.toml | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/build-logic/src/main/kotlin/elmslie.tests-convention.gradle.kts b/build-logic/src/main/kotlin/elmslie.tests-convention.gradle.kts index 73a36d74..4b4594e9 100644 --- a/build-logic/src/main/kotlin/elmslie.tests-convention.gradle.kts +++ b/build-logic/src/main/kotlin/elmslie.tests-convention.gradle.kts @@ -4,5 +4,5 @@ val catalog = extensions.getByType().named("libs") dependencies { val testImplementation by configurations - catalog.findLibrary("junit").ifPresent { testImplementation(it) } + catalog.findLibrary("kotlin-test").ifPresent { testImplementation(it) } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 324a4edb..b428400d 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,7 +2,6 @@ agp = "8.0.2" coroutines = "1.7.1" dokka = "1.8.20" -junit = "5.9.3" kotlin = "1.8.21" lifecycle = "2.6.1" @@ -19,8 +18,8 @@ detekt-gradlePlugin = "io.gitlab.arturbosch.detekt:detekt-gradle-plugin:1.23.0" dokka-gradlePlugin = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "dokka" } dokka-kotlinAsJavaPlugin = { module = "org.jetbrains.dokka:kotlin-as-java-plugin", version.ref = "dokka" } google-material = "com.google.android.material:material:1.9.0" -junit = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" } kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } +kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } kotlinx-coroutinesCore = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "coroutines" } kotlinx-coroutinesTest = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" } From 9aeb51d094af4bf25aa031f7b3794b3e7cfbc531 Mon Sep 17 00:00:00 2001 From: Vitaly Pestryakov <34812712+dsvag@users.noreply.github.com> Date: Fri, 24 Nov 2023 18:47:23 +0200 Subject: [PATCH 2/2] migrate tests --- elmslie-core/build.gradle.kts | 4 +-- .../core/store/EffectCachingElmStoreTest.kt | 26 ++++++++++++++---- .../money/elmslie/core/store/ElmStoreTest.kt | 26 ++++++++++++++---- .../elmslie/core/store/dsl/DslReducerTest.kt | 6 ++--- .../core/store/dsl/ScreenDslReducerTest.kt | 6 ++--- .../elmslie/test/TestDispatcherExtension.kt | 27 ------------------- samples/kotlin-calculator/build.gradle.kts | 2 +- .../elmslie/samples/calculator/StoreTest.kt | 26 ++++++++++++++---- 8 files changed, 71 insertions(+), 52 deletions(-) delete mode 100644 elmslie-core/src/testFixtures/kotlin/vivid/money/elmslie/test/TestDispatcherExtension.kt diff --git a/elmslie-core/build.gradle.kts b/elmslie-core/build.gradle.kts index aef9c419..cd9b76a3 100644 --- a/elmslie-core/build.gradle.kts +++ b/elmslie-core/build.gradle.kts @@ -1,11 +1,9 @@ plugins { id("elmslie.kotlin-lib") - `java-test-fixtures` + id("elmslie.tests-convention") } dependencies { implementation(libs.kotlinx.coroutinesCore) - testFixturesImplementation(libs.kotlinx.coroutinesTest) - testFixturesImplementation(libs.junit) testImplementation(libs.kotlinx.coroutinesTest) } diff --git a/elmslie-core/src/test/java/vivid/money/elmslie/core/store/EffectCachingElmStoreTest.kt b/elmslie-core/src/test/java/vivid/money/elmslie/core/store/EffectCachingElmStoreTest.kt index 1bde81d8..3975fea5 100644 --- a/elmslie-core/src/test/java/vivid/money/elmslie/core/store/EffectCachingElmStoreTest.kt +++ b/elmslie-core/src/test/java/vivid/money/elmslie/core/store/EffectCachingElmStoreTest.kt @@ -1,24 +1,40 @@ package vivid.money.elmslie.core.store +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.toList import kotlinx.coroutines.launch +import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.extension.RegisterExtension +import kotlinx.coroutines.test.setMain +import vivid.money.elmslie.core.config.ElmslieConfig import vivid.money.elmslie.core.testutil.model.Command import vivid.money.elmslie.core.testutil.model.Effect import vivid.money.elmslie.core.testutil.model.Event import vivid.money.elmslie.core.testutil.model.State -import vivid.money.elmslie.test.TestDispatcherExtension @OptIn(ExperimentalCoroutinesApi::class) class EffectCachingElmStoreTest { - @JvmField @RegisterExtension val testDispatcherExtension = TestDispatcherExtension() + @BeforeTest + fun beforeEach() { + val testDispatcher = StandardTestDispatcher() + ElmslieConfig.ioDispatchers { testDispatcher } + Dispatchers.setMain(testDispatcher) + } + + + @AfterTest + fun afterEach() { + Dispatchers.resetMain() + } @Test fun `Should collect effects which are emitted before collecting flow`() = runTest { diff --git a/elmslie-core/src/test/java/vivid/money/elmslie/core/store/ElmStoreTest.kt b/elmslie-core/src/test/java/vivid/money/elmslie/core/store/ElmStoreTest.kt index 5b540dd2..7d9ac77f 100644 --- a/elmslie-core/src/test/java/vivid/money/elmslie/core/store/ElmStoreTest.kt +++ b/elmslie-core/src/test/java/vivid/money/elmslie/core/store/ElmStoreTest.kt @@ -1,5 +1,10 @@ package vivid.money.elmslie.core.store +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow @@ -8,22 +13,33 @@ import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.toList import kotlinx.coroutines.launch +import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.extension.RegisterExtension +import kotlinx.coroutines.test.setMain +import vivid.money.elmslie.core.config.ElmslieConfig import vivid.money.elmslie.core.testutil.model.Command import vivid.money.elmslie.core.testutil.model.Effect import vivid.money.elmslie.core.testutil.model.Event import vivid.money.elmslie.core.testutil.model.State -import vivid.money.elmslie.test.TestDispatcherExtension @OptIn(ExperimentalCoroutinesApi::class) class ElmStoreTest { - @JvmField @RegisterExtension val testDispatcherExtension = TestDispatcherExtension() + @BeforeTest + fun beforeEach() { + val testDispatcher = StandardTestDispatcher() + ElmslieConfig.ioDispatchers { testDispatcher } + Dispatchers.setMain(testDispatcher) + } + + + @AfterTest + fun afterEach() { + Dispatchers.resetMain() + } @Test fun `Should stop the store properly`() = runTest { diff --git a/elmslie-core/src/test/java/vivid/money/elmslie/core/store/dsl/DslReducerTest.kt b/elmslie-core/src/test/java/vivid/money/elmslie/core/store/dsl/DslReducerTest.kt index c6c64a6b..171c0769 100644 --- a/elmslie-core/src/test/java/vivid/money/elmslie/core/store/dsl/DslReducerTest.kt +++ b/elmslie-core/src/test/java/vivid/money/elmslie/core/store/dsl/DslReducerTest.kt @@ -1,8 +1,8 @@ package vivid.money.elmslie.core.store.dsl -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.Test +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue private object BasicDslReducer : DslReducer() { diff --git a/elmslie-core/src/test/java/vivid/money/elmslie/core/store/dsl/ScreenDslReducerTest.kt b/elmslie-core/src/test/java/vivid/money/elmslie/core/store/dsl/ScreenDslReducerTest.kt index 63784c39..ba6f5a87 100644 --- a/elmslie-core/src/test/java/vivid/money/elmslie/core/store/dsl/ScreenDslReducerTest.kt +++ b/elmslie-core/src/test/java/vivid/money/elmslie/core/store/dsl/ScreenDslReducerTest.kt @@ -1,8 +1,8 @@ package vivid.money.elmslie.core.store.dsl -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Assertions.assertTrue -import org.junit.jupiter.api.Test +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue object BasicScreenDslReducer : ScreenDslReducer< diff --git a/elmslie-core/src/testFixtures/kotlin/vivid/money/elmslie/test/TestDispatcherExtension.kt b/elmslie-core/src/testFixtures/kotlin/vivid/money/elmslie/test/TestDispatcherExtension.kt deleted file mode 100644 index 9b133b16..00000000 --- a/elmslie-core/src/testFixtures/kotlin/vivid/money/elmslie/test/TestDispatcherExtension.kt +++ /dev/null @@ -1,27 +0,0 @@ -package vivid.money.elmslie.test - -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.StandardTestDispatcher -import kotlinx.coroutines.test.TestDispatcher -import kotlinx.coroutines.test.resetMain -import kotlinx.coroutines.test.setMain -import org.junit.jupiter.api.extension.AfterEachCallback -import org.junit.jupiter.api.extension.BeforeEachCallback -import org.junit.jupiter.api.extension.ExtensionContext -import vivid.money.elmslie.core.config.ElmslieConfig - -@OptIn(ExperimentalCoroutinesApi::class) -class TestDispatcherExtension( - private val testDispatcher: TestDispatcher = StandardTestDispatcher(), -) : BeforeEachCallback, AfterEachCallback { - - override fun beforeEach(context: ExtensionContext?) { - ElmslieConfig.ioDispatchers { testDispatcher } - Dispatchers.setMain(testDispatcher) - } - - override fun afterEach(context: ExtensionContext?) { - Dispatchers.resetMain() - } -} diff --git a/samples/kotlin-calculator/build.gradle.kts b/samples/kotlin-calculator/build.gradle.kts index 17c357cb..2ca7a590 100644 --- a/samples/kotlin-calculator/build.gradle.kts +++ b/samples/kotlin-calculator/build.gradle.kts @@ -8,6 +8,6 @@ dependencies { implementation(projects.elmslieCore) implementation(libs.kotlinx.coroutinesCore) + testImplementation(projects.elmslieCore) testImplementation(libs.kotlinx.coroutinesTest) - testImplementation(testFixtures(projects.elmslieCore)) } diff --git a/samples/kotlin-calculator/src/test/java/vivid/money/elmslie/samples/calculator/StoreTest.kt b/samples/kotlin-calculator/src/test/java/vivid/money/elmslie/samples/calculator/StoreTest.kt index a2d917a9..f4d66de6 100644 --- a/samples/kotlin-calculator/src/test/java/vivid/money/elmslie/samples/calculator/StoreTest.kt +++ b/samples/kotlin-calculator/src/test/java/vivid/money/elmslie/samples/calculator/StoreTest.kt @@ -1,19 +1,35 @@ package vivid.money.elmslie.samples.calculator +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.toList import kotlinx.coroutines.launch +import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.runTest -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.extension.RegisterExtension -import vivid.money.elmslie.test.TestDispatcherExtension +import kotlinx.coroutines.test.setMain +import vivid.money.elmslie.core.config.ElmslieConfig @OptIn(ExperimentalCoroutinesApi::class) internal class StoreTest { - @JvmField @RegisterExtension val testDispatcherExtension = TestDispatcherExtension() + @BeforeTest + fun beforeEach() { + val testDispatcher = StandardTestDispatcher() + ElmslieConfig.ioDispatchers { testDispatcher } + Dispatchers.setMain(testDispatcher) + } + + + @AfterTest + fun afterEach() { + Dispatchers.resetMain() + } @Test fun `1 + 1 = 2`() = runTest {