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] 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 {