Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests + Fix koin-test jvm reflect import #1804

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package org.koin.test

import org.junit.Assert.fail
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.koin.core.logger.Level
import org.koin.core.module.dsl.createdAtStart
import org.koin.core.module.dsl.withOptions
import org.koin.core.parameter.parametersOf
import org.koin.core.qualifier.named
import org.koin.dsl.bind
import org.koin.dsl.binds
import org.koin.dsl.koinApplication
import org.koin.dsl.module
import org.koin.dsl.*
import org.koin.test.check.checkKoinModules
import org.koin.test.check.checkModules
import org.koin.test.mock.MockProviderRule
import org.mockito.Mockito
import java.util.*
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class CheckModulesTest {

Expand Down Expand Up @@ -87,6 +89,69 @@ class CheckModulesTest {
}
}

@Test
fun `check a module - created at start`() {
val modules = module {
single { Simple.ComponentB(get()) } withOptions { createdAtStart() }
}

koinApplication(createEagerInstances = false) {
modules(modules)
checkModules {
withInstance<Simple.ComponentA>()
}
}
}

@Test
fun `check module - dependency and param in one class`() {
val modules = module {
factory { p -> Simple.MyString(p.get()) }
}

koinApplication {
modules(modules)
checkModules {
withParameter<Simple.MyString> { "test" }
}
}
}

@Test
fun `check module - dependency and param in one class - 2`() {
val modules = module {
factory { Simple.ComponentA() }
factory { p -> Simple.MyComplexString2(get(), p.get()) }
}

koinApplication {
printLogger(Level.DEBUG)
modules(modules)
checkModules {
// withInstance<Simple.ComponentA>()
withParameter<Simple.MyComplexString2> { "test" }
}
}
}

// Not working
@Test
@Ignore
fun `check module - dependency and param in one class - 3`() {
val modules = module {
factory { p -> Simple.MyComplexBool(get(), p.get()) }
}

koinApplication {
printLogger(Level.DEBUG)
modules(modules)
checkModules {
withInstance<Simple.ComponentA>()
withParameter<Simple.MyComplexBool> { true }
}
}
}

@Test
fun `check a scoped module`() {
koinApplication {
Expand Down Expand Up @@ -306,8 +371,8 @@ class CheckModulesTest {
printLogger(Level.DEBUG)
modules(
module {
single { (s: String) -> Simple.MyString(s) }
single(UpperCase) { (s: String) -> Simple.MyString(s.uppercase(Locale.getDefault())) }
single { p -> Simple.MyString(p.get()) }
single(UpperCase) { p -> Simple.MyString(p.get<String>().uppercase(Locale.getDefault())) }
},
)
}.checkModules {
Expand Down Expand Up @@ -589,9 +654,6 @@ class CheckModulesTest {
}.checkModules()
}

assertEquals(
expected = "instance of class java.lang.String (Kotlin reflection is not available) is not inheritable from int (Kotlin reflection is not available)",
actual = exception.message,
)
assertNotNull(exception.message)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ class Simple {
class ComponentD()
class ComponentE(val d: ComponentD)
class MyString(val s: String)
class MyComplexString(val s: String, val a : ComponentA)
class MyComplexString2(val a : ComponentA, val s: String)
class MyComplexBool(val a : ComponentA, val b : Boolean)

class UUIDComponent {
fun getUUID() = UUID.randomUUID().toString()
Expand Down
3 changes: 3 additions & 0 deletions projects/core/koin-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ kotlin {
commonMain.dependencies {
api(project(":core:koin-core"))
}
jvmMain.dependencies {
implementation(kotlin("reflect"))
}
commonTest.dependencies {
implementation(libs.kotlin.test)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import org.koin.mp.KoinPlatformTools
import org.koin.test.mock.MockProvider
import org.koin.test.parameter.MockParameter

//TODO TO BE DEPRECATED in 3.6

/**
* Check all definition's dependencies - start all modules and check if definitions can run
*/
Expand Down Expand Up @@ -152,6 +154,7 @@ private fun Koin.checkDefinition(
]?.invoke(
definition.qualifier,
) ?: MockParameter(scope, allParameters.defaultValues)

logger.info("[Check] definition: $definition")
scope.get<Any>(definition.primaryType, definition.qualifier) { parameters }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import org.koin.mp.KoinPlatformTools
import org.koin.test.mock.MockProvider
import kotlin.reflect.KClass

//TODO TO BE DEPRECATED in 3.6

data class CheckedComponent(val qualifier: Qualifier? = null, val type: KClass<*>)

class ParametersBinding(val koin: Koin) {
Expand Down
Loading