Skip to content

Commit

Permalink
feat(#396): allow chaining on ConfigurableDI configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Romain Boisselle committed Feb 19, 2022
1 parent c1d5f99 commit ec822b7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public class ConfigurableDI : DI {
* @param config The lambda to be applied when the DI instance is constructed.
* @exception IllegalStateException When calling this function after [getOrConstruct] or any `DI` retrieval function.
*/
public fun addConfig(config: DI.MainBuilder.() -> Unit) {
public fun addConfig(config: DI.MainBuilder.() -> Unit) : ConfigurableDI {
maySynchronized(_lock) {
val configs = _configs
if (configs == null) {
Expand All @@ -140,6 +140,7 @@ public class ConfigurableDI : DI {
}
_configs!!.add(config)
}
return this
}

/**
Expand All @@ -149,7 +150,9 @@ public class ConfigurableDI : DI {
* @param allowOverride Whether this module is allowed to override existing bindings.
* @exception IllegalStateException When calling this function after [getOrConstruct] or any `DI` retrieval function.
*/
public fun addImport(module: DI.Module, allowOverride: Boolean = false): Unit = addConfig { import(module, allowOverride) }
public fun addImport(module: DI.Module, allowOverride: Boolean = false): ConfigurableDI = addConfig {
import(module, allowOverride)
}

/**
* Adds the bindings of an existing DI instance to the bindings that will be applied when the DI is constructed.
Expand All @@ -161,7 +164,7 @@ public class ConfigurableDI : DI {
* By default, all bindings that do not hold references (e.g. not singleton or multiton) are copied.
* @exception IllegalStateException When calling this function after [getOrConstruct] or any `DI` retrieval function.
*/
public fun addExtend(di: DI, allowOverride: Boolean = false, copy: Copy = Copy.NonCached): Unit = addConfig {
public fun addExtend(di: DI, allowOverride: Boolean = false, copy: Copy = Copy.NonCached): ConfigurableDI = addConfig {
extend(di, allowOverride, copy)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

package org.kodein.di.conf

import org.kodein.di.*
Expand All @@ -10,7 +9,8 @@ import kotlin.test.*
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
class ConfTests {

@Test fun test_00_Configurable() {
@Test
fun test_00_Configurable() {
val di = ConfigurableDI()

di.addConfig {
Expand All @@ -26,7 +26,8 @@ class ConfTests {
assertFalse(di.canConfigure)
}

@Test fun test_01_Clear() {
@Test
fun test_01_Clear() {
val di = ConfigurableDI(true)

di.addImport(DI.Module("myModule") {
Expand All @@ -44,7 +45,8 @@ class ConfTests {
assertEquals(42, di.direct.instance(tag = "answer"))
}

@Test fun test_02_Mutate() {
@Test
fun test_02_Mutate() {
val di = ConfigurableDI(true)

di.addExtend(DI {
Expand All @@ -61,7 +63,8 @@ class ConfTests {
assertEquals(42, di.direct.instance(tag = "full"))
}

@Test fun test_03_NonMutableClear() {
@Test
fun test_03_NonMutableClear() {
val di = ConfigurableDI()

di.addConfig {
Expand All @@ -75,7 +78,8 @@ class ConfTests {
}
}

@Test fun test_04_NonMutableMutate() {
@Test
fun test_04_NonMutableMutate() {
val di = ConfigurableDI()

di.addConfig {
Expand All @@ -89,7 +93,8 @@ class ConfTests {
}
}

@Test fun test_05_mutateConfig() {
@Test
fun test_05_mutateConfig() {
val di = ConfigurableDI(true)

di.addConfig {
Expand All @@ -106,7 +111,8 @@ class ConfTests {
assertEquals(42, di.direct.instance(tag = "full"))
}

@Test fun test_06_nonMutableMutateConfig() {
@Test
fun test_06_nonMutableMutateConfig() {
val di = ConfigurableDI()

di.addConfig {
Expand Down Expand Up @@ -137,11 +143,12 @@ class ConfTests {
assertEquals("Salomon BRYS", di.direct.factory<FullName, String>().invoke(FullName("Salomon", "BRYS")))
}

class T08: DIGlobalAware {
class T08 : DIGlobalAware {
val answer: Int by instance(tag = "full")
}

@Test fun test_08_Global() {
@Test
fun test_08_Global() {
DI.global.mutable = true

DI.global.addConfig {
Expand All @@ -158,7 +165,8 @@ class ConfTests {
assertEquals(42, T08().answer)
}

@Test fun test_09_Callback() {
@Test
fun test_09_Callback() {
val di = ConfigurableDI()

var ready = false
Expand All @@ -184,5 +192,18 @@ class ConfTests {
assertTrue(ready)
}

@Test
fun test_10_config_chaining() {
val di = ConfigurableDI(true)

di
.addConfig {
constant(tag = "half") with 21
}.addConfig {
constant(tag = "full") with 42
}

assertEquals(21, di.direct.instance(tag = "half"))
assertEquals(42, di.direct.instance(tag = "full"))
}
}

0 comments on commit ec822b7

Please sign in to comment.