Skip to content

Instantly share code, notes, and snippets.

@dimka11
Created November 27, 2018 13:42
Show Gist options
  • Save dimka11/49f58427d04f0c639697f4624eb53d90 to your computer and use it in GitHub Desktop.
Save dimka11/49f58427d04f0c639697f4624eb53d90 to your computer and use it in GitHub Desktop.
package funWithIoC
import com.nhaarman.mockitokotlin2.anyVararg
import hwdtech.ioc.*
import org.junit.Test
import org.mockito.Mockito
import java.lang.AssertionError
import kotlin.test.*
class HelloTest {
class MyObject(val str: String) {
operator fun invoke() = println(str)
}
class ConcreteStrategy : IIoCResolverStrategy {
override fun invoke(args: Array<out Any>): Any {
return args[0]
}
}
@Test
fun `my test always create new outside`() { // always new object
register("dep") { MyObject("Hello") }
val mo1 = resolve<MyObject>("dep") //need resolve type as Interface type not concrete like in my examples !!!
val mo2 = resolve<MyObject>("dep")
assertNotEquals(mo1, mo2)
assertNotSame(mo1, mo2)
}
@Test
fun `my test always return same object`() { // aka Singleton
val mo__ = MyObject("World")
val rs =
register("dep") { return@register mo__; }
val mo1 = resolve<MyObject>("dep")
val mo2 = resolve<MyObject>("dep")
assertSame(mo1, mo2)
}
class ConcreteStrategyWithParameters : IIoCResolverStrategy {
override fun invoke(args: Array<out Any>): Any {
return MyObject(args[0] as String)
}
}
@Test
fun `my test ctor object inside`() { // aka create object with new inside class
register("dep", ConcreteStrategyWithParameters())
val mo1 = resolve<MyObject>("dep", "Hello")
val mo2 = resolve<MyObject>("dep", "World")
mo1()
mo2()
}
@Test
fun `smth with scope`() {
Scopes.startNew().use {
val obj: Any = 1
register("dep") { return@register obj; }
assertEquals(1, resolve<Int>("dep"))
}
Scopes.startNew()
val obj2: Any = 2
register("dep") { return@register obj2; }
assertEquals(2, resolve<Int>("dep"))
}
fun createScope() : IScope {
val obj2: Any = 123
val sc = Scopes.startNew()
sc.register("dep123") { return@register obj2 }
return sc
}
@Test
fun `scopes again`() {
`smth with scope`()
assertEquals(2, resolve<Int>("dep"))
val aaa = createScope().resolve("dep123")
val s = aaa.invoke(arrayOf())
print(s.toString()) // нууу
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment