1 package net.pterodactylus.sone.test
3 import com.google.inject.Injector
4 import com.google.inject.Module
6 import javax.inject.Provider
7 import kotlin.reflect.KClass
9 fun <T : Any> KClass<T>.isProvidedBy(instance: T) = Module { it.bind(this.java).toProvider(Provider<T> { instance }) }
10 fun <T : Any> KClass<T>.isProvidedBy(provider: com.google.inject.Provider<T>) = Module { it.bind(this.java).toProvider(provider) }
11 fun <T : Any> KClass<T>.isProvidedBy(provider: KClass<out Provider<T>>) = Module { it.bind(this.java).toProvider(provider.java) }
12 inline fun <reified T : Any> KClass<T>.isProvidedByMock() = Module { it.bind(this.java).toProvider(Provider<T> { mock() }) }
14 inline fun <reified T : Any> Injector.getInstance() = getInstance(T::class.java)!!
16 fun <T : Any> supply(javaClass: Class<T>): Source<T> = object : Source<T> {
17 override fun fromInstance(instance: T) = Module { it.bind(javaClass).toInstance(instance) }
18 override fun byInstance(instance: T) = Module { it.bind(javaClass).toProvider(Provider<T> { instance }) }
19 override fun byProvider(provider: com.google.inject.Provider<T>) = Module { it.bind(javaClass).toProvider(provider) }
20 override fun byProvider(provider: Class<Provider<T>>) = Module { it.bind(javaClass).toProvider(provider) }
21 override fun byMock() = Module { it.bind(javaClass).toInstance(Mockito.mock(javaClass)) }
24 interface Source<T : Any> {
25 fun fromInstance(instance: T): Module
26 fun byInstance(instance: T): Module
27 fun byProvider(provider: com.google.inject.Provider<T>): Module
28 fun byProvider(provider: Class<Provider<T>>): Module