🔀 Merge “release/v81” into “master”
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / test / Guice.kt
index c8f2417..b0bed5c 100644 (file)
@@ -2,6 +2,8 @@ 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.*
@@ -17,6 +19,13 @@ inline fun <reified T : Any> Injector.getInstance(annotation: Annotation? = null
                ?.let { getInstance(Key.get(object : TypeLiteral<T>() {}, it)) }
                ?: getInstance(Key.get(object : TypeLiteral<T>() {}))
 
+
+inline fun <reified T : Any> Injector.verifySingletonInstance(annotation: Annotation? = null) {
+       val firstInstance = getInstance<T>(annotation)
+       val secondInstance = getInstance<T>(annotation)
+       assertThat(firstInstance, sameInstance(secondInstance))
+}
+
 fun <T : Any> supply(javaClass: Class<T>): Source<T> = object : Source<T> {
        override fun fromInstance(instance: T) = Module { it.bind(javaClass).toInstance(instance) }
        override fun byInstance(instance: T) = Module { it.bind(javaClass).toProvider(Provider<T> { instance }) }