package net.pterodactylus.sone.test import com.google.inject.* import com.google.inject.name.* import org.hamcrest.MatcherAssert.* import org.hamcrest.Matchers.* import org.mockito.* import javax.inject.Provider import kotlin.reflect.* fun KClass.isProvidedBy(instance: T) = Module { it.bind(this.java).toProvider(Provider { instance }) } fun KClass.withNameIsProvidedBy(instance: T, name: String) = Module { it.bind(this.java).annotatedWith(Names.named(name)).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 KClass.isProvidedByDeepMock() = Module { it.bind(this.java).toProvider(Provider { deepMock() }) } inline fun Injector.getInstance(annotation: Annotation? = null): T = annotation ?.let { getInstance(Key.get(object : TypeLiteral() {}, it)) } ?: getInstance(Key.get(object : TypeLiteral() {})) inline fun Injector.verifySingletonInstance(annotation: Annotation? = null) { val firstInstance = getInstance(annotation) val secondInstance = getInstance(annotation) assertThat(firstInstance, sameInstance(secondInstance)) } 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 }