package net.pterodactylus.sone.test import com.google.inject.Injector import com.google.inject.Module import org.mockito.* import javax.inject.Provider import kotlin.reflect.KClass fun KClass.isProvidedBy(instance: T) = Module { it.bind(this.java).toProvider(Provider { instance }) } fun KClass.isProvidedBy(provider: com.google.inject.Provider) = Module { it.bind(this.java).toProvider(provider) } fun KClass.isProvidedBy(provider: KClass>) = Module { it.bind(this.java).toProvider(provider.java) } inline fun KClass.isProvidedByMock() = Module { it.bind(this.java).toProvider(Provider { mock() }) } inline fun Injector.getInstance() = getInstance(T::class.java)!! fun supply(javaClass: Class): Source = object : Source { override fun fromInstance(instance: T) = Module { it.bind(javaClass).toInstance(instance) } override fun byInstance(instance: T) = Module { it.bind(javaClass).toProvider(Provider { instance }) } override fun byProvider(provider: com.google.inject.Provider) = Module { it.bind(javaClass).toProvider(provider) } override fun byProvider(provider: Class>) = Module { it.bind(javaClass).toProvider(provider) } override fun byMock() = Module { it.bind(javaClass).toInstance(Mockito.mock(javaClass)) } } interface Source { fun fromInstance(instance: T): Module fun byInstance(instance: T): Module fun byProvider(provider: com.google.inject.Provider): Module fun byProvider(provider: Class>): Module fun byMock(): Module }