From eb5babc07bc218b993d23c521f1d92d1c5014154 Mon Sep 17 00:00:00 2001 From: iliyangermanov Date: Wed, 21 Aug 2024 00:24:47 +0300 Subject: [PATCH] Update tests --- di/src/commonMain/kotlin/DiContainer.kt | 10 ++-- di/src/commonTest/kotlin/DiContainerTest.kt | 61 ++++++++++----------- gradle/libs.versions.toml | 6 -- 3 files changed, 35 insertions(+), 42 deletions(-) diff --git a/di/src/commonMain/kotlin/DiContainer.kt b/di/src/commonMain/kotlin/DiContainer.kt index 086aab7..6beb49c 100644 --- a/di/src/commonMain/kotlin/DiContainer.kt +++ b/di/src/commonMain/kotlin/DiContainer.kt @@ -15,12 +15,12 @@ object Di { modules.forEach(DiModule::init) } - fun appScope(block: Scope.() -> Unit) { - AppScope.block() - } + fun appScope(block: Scope.() -> Unit) = scope(AppScope, block) + + fun featureScope(block: Scope.() -> Unit) = scope(FeatureScope, block) - fun featureScope(block: Scope.() -> Unit) { - FeatureScope.block() + fun scope(scope: Scope, block: Scope.() -> Unit) { + scope.block() } inline fun Scope.register(noinline factory: () -> T) { diff --git a/di/src/commonTest/kotlin/DiContainerTest.kt b/di/src/commonTest/kotlin/DiContainerTest.kt index 1cba96d..e63c5dd 100644 --- a/di/src/commonTest/kotlin/DiContainerTest.kt +++ b/di/src/commonTest/kotlin/DiContainerTest.kt @@ -1,108 +1,106 @@ +import Di.register +import Di.singleton import io.kotest.assertions.throwables.shouldThrow import io.kotest.matchers.nulls.shouldNotBeNull import io.kotest.matchers.shouldBe -import io.ktor.client.* -import io.mockk.mockk -import ivy.di.Di.register -import ivy.di.Di.singleton -import org.junit.Before -import org.junit.Test +import kotlin.test.BeforeTest +import kotlin.test.Test class DiContainerTest { - @Before + @BeforeTest fun setup() { Di.reset() } @Test fun `creates an instance in app scope`() { - // given + // Given Di.appScope { register { FakeStateHolder() } } Di.get().number = 42 - // when + // When val stateHolder = Di.get() - // then + // Then stateHolder.number shouldBe 0 } @Test fun `creates a singleton in app scope`() { - // given + // Given Di.appScope { singleton { FakeStateHolder() } } Di.get().number = 42 - // when + // When val stateHolder = Di.get() - // then + // Then stateHolder.number shouldBe 42 } @Test fun `constructs a more complex DI graph`() { - // given + // Given Di.appScope { singleton { FakeStateHolder() } - singleton { mockk() } + singleton { HttpClient() } register { FakeDataSource(Di.get()) } register { FakeRepository(Di.get()) } register { FakeViewModel(Di.get(), Di.get()) } } - // when + // When val viewModel: FakeViewModel = Di.get() - // then + // Then viewModel.shouldNotBeNull() } @Test fun `throws an exception for not registered classes`() { - // when - val thrownException = shouldThrow { + // When + val thrownException = shouldThrow { Di.get() } - // then + // Then thrownException.message.shouldNotBeNull() } @Test fun `binds an interface`() { - // given + // Given Di.appScope { register { FakeImplOne() } } - // when + // When val instance = Di.get() - // then + // Then instance.shouldNotBeNull() } @Test - fun `creates an instance in screen scope`() { - // given - Di.screenScope { + fun `creates an instance in feature scope`() { + // Given + Di.featureScope { register { FakeStateHolder() } } Di.get().number = 42 - // when + // When val stateHolder = Di.get() - // then + // Then stateHolder.number shouldBe 0 } @Test - fun `creates a singleton in screen scope`() { + fun `creates a singleton in feature scope`() { // given - Di.screenScope { + Di.featureScope { singleton { FakeStateHolder() } } Di.get().number = 42 @@ -114,7 +112,7 @@ class DiContainerTest { stateHolder.number shouldBe 42 // when the scope is reset - Di.clearInstances(Di.ScreenScope) + Di.clearInstances(FeatureScope) // then after the reset Di.get().number shouldBe 0 @@ -143,6 +141,7 @@ class FakeStateHolder { } class FakeDataSource(@Suppress("unused") val httpClient: HttpClient) +class HttpClient class FakeRepository(@Suppress("unused") val dataSource: FakeDataSource) class FakeViewModel( @Suppress("unused") diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6d72c81..3a96361 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -10,16 +10,10 @@ kotest = "5.9.1" kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } nexus-publish = { module = "io.github.gradle-nexus.publish-plugin:io.github.gradle-nexus.publish-plugin.gradle.plugin", version.ref = "nexus-publish" } kotest-assertions = { module = "io.kotest:kotest-assertions-core", version.ref = "kotest" } -kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "kotlin" } -google-testparameterinjector = { module = "com.google.testparameterinjector:test-parameter-injector", version = "1.16" } -mockk = { module = "io.mockk:mockk", version = "1.13.12" } [bundles] test = [ "kotlin-test", - "kotlin-test-junit", - "google-testparameterinjector", - "mockk", "kotest-assertions", ]