@@ -13,17 +13,21 @@ internal class DITest {
13
13
val testDI = DIImpl ()
14
14
15
15
init {
16
- testDI.register { FooDeps (get(someConfigFlag), barDeps) }
16
+ testDI.register { FooDeps (get(someConfigFlag), barDeps, defaultDeps ) }
17
17
testDI.register { BarDeps (fooDeps) }
18
18
}
19
19
20
+ private val defaultFlow = testDI.derived { get(defaultDeps) }
21
+ private val default = defaultFlow.value
20
22
private val fooFlow = testDI.derived { get(fooDeps) }
21
23
private val foo = fooFlow.value
22
24
private val barFlow = testDI.derived { get(barDeps) }
23
25
private val bar = barFlow.value
24
26
25
27
@Test
26
28
fun stability () {
29
+ assertSame(default, testDI.derived { get(defaultDeps) }.value)
30
+ assertSame(default, foo.defaultDeps)
27
31
assertSame(foo, testDI.derived { get(fooDeps) }.value)
28
32
assertSame(bar, testDI.derived { get(barDeps) }.value)
29
33
}
@@ -37,16 +41,17 @@ internal class DITest {
37
41
testDI.register {
38
42
// This causes a too early access
39
43
get(barDeps)
40
- FooDeps (get(someConfigFlag), barDeps)
44
+ FooDeps (get(someConfigFlag), barDeps, defaultDeps )
41
45
}
42
46
assertFailsWith<IllegalStateException > { barFlow.value.configFlag }
43
47
}
44
48
45
49
@Test
46
50
fun updateDIGraphOnRegister () {
47
51
// Replacing FooDeps invalidates the whole subgraph depending on FooDeps. So, BarDeps gets re-created.
48
- testDI.register { FooDeps (get(someConfigFlag), barDeps) }
52
+ testDI.register { FooDeps (get(someConfigFlag), barDeps, defaultDeps ) }
49
53
assertNotSame(foo, testDI.derived { get(fooDeps) }.value)
54
+ assertSame(default, testDI.derived { get(fooDeps) }.value.defaultDeps)
50
55
val newBar = testDI.derived { get(barDeps) }.value
51
56
assertNotSame(bar, newBar)
52
57
assertTrue(foo.circularConfigFlag)
@@ -72,6 +77,16 @@ internal class DITest {
72
77
}
73
78
}
74
79
80
+ // -------------
81
+ // module DefaultDeps
82
+ // -------------
83
+
84
+ // Convenience accessor for FooDeps
85
+ private val DIResolver .defaultDeps: LazyProperty <DefaultDeps >
86
+ get() = DI .run { get { DefaultDeps () } }
87
+
88
+ private class DefaultDeps (val value : Boolean = true )
89
+
75
90
// -------------
76
91
// module foo
77
92
// -------------
@@ -83,9 +98,11 @@ private val DIResolver.fooDeps: LazyProperty<FooDeps> get() = DI.run { get() }
83
98
private class FooDeps (
84
99
val configFlag : Boolean ,
85
100
lazyBarDeps : LazyProperty <BarDeps >,
101
+ lazyDefaultDeps : LazyProperty <DefaultDeps >,
86
102
) {
87
103
// All deps have to be resolved lazily
88
104
val barDeps by lazyBarDeps
105
+ val defaultDeps by lazyDefaultDeps
89
106
90
107
// All deps have to be resolved lazily
91
108
val circularConfigFlag by lazy { barDeps.configFlag }
0 commit comments