generated from Kotlin/multiplatform-library-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac083aa
commit e928184
Showing
2 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package ivy.di | ||
|
||
import io.kotest.assertions.throwables.shouldThrow | ||
import io.kotest.matchers.shouldBe | ||
import ivy.di.Di.register | ||
import ivy.di.Di.singleton | ||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
|
||
private var initialized = false | ||
|
||
class Counter(var x: Int) { | ||
init { | ||
initialized = true | ||
} | ||
} | ||
|
||
class CountManager(private val counter: Lazy<Counter>) { | ||
fun increment() { | ||
counter.value.x++ | ||
} | ||
} | ||
|
||
class LazyTest { | ||
|
||
@BeforeTest | ||
fun setup() { | ||
Di.reset() | ||
initialized = false | ||
} | ||
|
||
@Test | ||
fun lazyInitialization() { | ||
// Given | ||
Di.appScope { | ||
register { Counter(x = 42) } | ||
} | ||
|
||
// When | ||
val lazyCounter = Di.getLazy<Counter>() | ||
|
||
// Initially the lazy counter isn't initialized | ||
initialized shouldBe false | ||
|
||
// When the lazy value is accessed | ||
val counter = lazyCounter.value | ||
|
||
// Then | ||
initialized shouldBe true | ||
counter.x shouldBe 42 | ||
} | ||
|
||
@Test | ||
fun lazySingletonInitialization() { | ||
// Given | ||
Di.appScope { | ||
singleton { Counter(x = 0) } | ||
} | ||
|
||
// When | ||
val lazyCounter = Di.getLazy<Counter>() | ||
|
||
// Initially the lazy counter isn't initialized | ||
initialized shouldBe false | ||
|
||
// When the lazy value is accessed and modified | ||
lazyCounter.value.x shouldBe 0 | ||
lazyCounter.value.x = 42 | ||
|
||
// Then | ||
initialized shouldBe true | ||
Di.getLazy<Counter>().value.x shouldBe 42 | ||
} | ||
|
||
@Test | ||
fun missingFactory() { | ||
// When-Then | ||
shouldThrow<DependencyInjectionError> { | ||
Di.getLazy<Counter>() | ||
} | ||
} | ||
|
||
@Test | ||
fun realWorldScenario() { | ||
// Given | ||
Di.appScope { | ||
singleton { Counter(x = 0) } | ||
register { CountManager(Di.getLazy()) } | ||
} | ||
val manager = Di.get<CountManager>() | ||
|
||
// Initially the lazy counter isn't initialized | ||
initialized shouldBe false | ||
|
||
// When | ||
manager.increment() | ||
|
||
// Then | ||
initialized shouldBe true | ||
Di.get<Counter>().x shouldBe 1 | ||
} | ||
} |