Created
June 3, 2020 19:03
-
-
Save Nilzor/b6d20b7b5d77efe4a0668400c4baf7b6 to your computer and use it in GitHub Desktop.
Toothpick overriding and stuff
This file contains hidden or 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
package no.ruter.sales.utils | |
import org.hamcrest.MatcherAssert.assertThat | |
import org.hamcrest.core.IsInstanceOf | |
import org.junit.Test | |
import toothpick.Scope | |
import toothpick.Toothpick | |
import toothpick.config.Module | |
import toothpick.ktp.binding.bind | |
import toothpick.ktp.binding.module | |
import toothpick.locators.NoFactoryFoundException | |
import javax.inject.Inject | |
interface IAlpha | |
class AlphaOne : IAlpha | |
class AlphaTwo: IAlpha | |
class Injectable { | |
@Inject | |
lateinit var alpha: IAlpha | |
} | |
class AlphaOneModule : Module() { | |
init { | |
bind<IAlpha>().toInstance(AlphaOne()) | |
} | |
} | |
class AlphaTwoModule : Module() { | |
init { | |
bind<IAlpha>().toInstance(AlphaTwo()) | |
} | |
} | |
class TpTest { | |
@Test | |
fun testPlainResolution() { | |
val scopeInstance1 = Toothpick | |
.openScope("Roger1") | |
.installModules( | |
AlphaOneModule() | |
) | |
val injectable = Injectable() | |
scopeInstance1.inject(injectable) | |
assertThat(injectable.alpha, IsInstanceOf(AlphaOne::class.java)) | |
} | |
@Test | |
fun testResolutionWithSameNamedScope() { | |
Toothpick | |
.openScope("Roger2") | |
.installModules( | |
AlphaOneModule() | |
) | |
val scopeInstance2 = Toothpick | |
.openScope("Roger2") | |
val injectable = Injectable() | |
scopeInstance2.inject(injectable) | |
assertThat(injectable.alpha, IsInstanceOf(AlphaOne::class.java)) | |
} | |
@Test | |
fun testReplacingBindingInSameInstance() { | |
val scope = Toothpick | |
.openScope("Roger3") | |
.installModules( | |
AlphaOneModule(), | |
AlphaTwoModule() | |
) | |
// AlphaTwo does not override AlphaOne | |
val injectable = Injectable() | |
scope.inject(injectable) | |
assertThat(injectable.alpha, IsInstanceOf(AlphaOne::class.java)) | |
} | |
@Test | |
fun testReplacingBindingInSubScope() { | |
val scope = Toothpick | |
.openScope("Roger4") | |
.installModules( | |
AlphaOneModule() | |
) | |
.openSubScope("Hans4") | |
.installModules( | |
AlphaTwoModule() | |
) | |
// SubScope overrides parent scope | |
val injectable = Injectable() | |
scope.inject(injectable) | |
assertThat(injectable.alpha, IsInstanceOf(AlphaTwo::class.java)) | |
} | |
@Test | |
fun testInhertianceInSubScope() { | |
val scope = Toothpick | |
.openScope("Roger5") | |
.installModules( | |
AlphaOneModule() | |
) | |
.openSubScope("Hans5") | |
val injectable = Injectable() | |
scope.inject(injectable) | |
assertThat(injectable.alpha, IsInstanceOf(AlphaOne::class.java)) | |
} | |
/** Simplified syntax to install one module to scope. */ | |
inline fun Scope.installModule(crossinline bindings: Module.() -> Unit): Scope { | |
return installModules(module { bindings() }) | |
} | |
@Test(expected = NoFactoryFoundException::class) | |
fun testResolutionWithDifferentlyNamedScope() { | |
Toothpick | |
.openScope("Roger6") | |
.installModules( | |
AlphaOneModule() | |
) | |
val scopeInstance2 = Toothpick | |
.openScope("Hans6") | |
val injectable = Injectable() | |
scopeInstance2.inject(injectable) | |
} | |
@Test | |
fun testOverrideWithTestScope() { | |
val scopeInstance = Toothpick | |
.openScope("Roger7") | |
.installModules( | |
AlphaOneModule() | |
) | |
.installTestModules( | |
AlphaTwoModule() | |
) | |
val injectable = Injectable() | |
scopeInstance.inject(injectable) | |
assertThat(injectable.alpha, IsInstanceOf(AlphaTwo::class.java)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment