import net.pterodactylus.sone.text.SoneTextParserContext
import net.pterodactylus.util.template.Filter
import net.pterodactylus.util.template.TemplateContext
+import javax.inject.Inject
+import javax.inject.Singleton
/**
* Parses a [String] into a number of [Part]s.
*/
-class ParserFilter(private val core: Core, private val soneTextParser: SoneTextParser) : Filter {
+@Singleton
+class ParserFilter @Inject constructor(private val core: Core, private val soneTextParser: SoneTextParser) : Filter {
override fun format(templateContext: TemplateContext?, data: Any?, parameters: MutableMap<String, Any?>?): Any? {
val text = data?.toString() ?: return listOf<Part>()
package net.pterodactylus.sone.template
import com.google.common.base.Optional.of
+import com.google.inject.Guice
import net.pterodactylus.sone.core.Core
import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.test.getInstance
+import net.pterodactylus.sone.test.isProvidedByMock
import net.pterodactylus.sone.test.mock
import net.pterodactylus.sone.text.SoneTextParser
import net.pterodactylus.sone.text.SoneTextParserContext
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.`is`
import org.hamcrest.Matchers.emptyIterable
+import org.hamcrest.Matchers.notNullValue
+import org.hamcrest.Matchers.sameInstance
import org.junit.Test
import org.mockito.ArgumentCaptor.forClass
import org.mockito.Mockito.`when`
assertThat(context.value.postingSone, `is`(sone))
}
+ @Test
+ fun `parser filter can be created by guice`() {
+ val injector = Guice.createInjector(
+ Core::class.isProvidedByMock(),
+ SoneTextParser::class.isProvidedByMock()
+ )
+ assertThat(injector.getInstance<ParserFilter>(), notNullValue())
+ }
+
+ @Test
+ fun `parser filter is created as singleton`() {
+ val injector = Guice.createInjector(
+ Core::class.isProvidedByMock(),
+ SoneTextParser::class.isProvidedByMock()
+ )
+ val firstInstance = injector.getInstance<ParserFilter>()
+ val secondInstance = injector.getInstance<ParserFilter>()
+ assertThat(firstInstance, sameInstance(secondInstance))
+
+ }
+
}
package net.pterodactylus.sone.test
+import com.google.inject.Injector
import com.google.inject.Module
import javax.inject.Provider
import kotlin.reflect.KClass
fun <T : Any> KClass<T>.isProvidedBy(instance: T) = Module { it.bind(this.java).toProvider { instance } }
fun <T : Any> KClass<T>.isProvidedBy(provider: com.google.inject.Provider<T>) = Module { it.bind(this.java).toProvider(provider) }
fun <T : Any> KClass<T>.isProvidedBy(provider: KClass<out Provider<T>>) = Module { it.bind(this.java).toProvider(provider.java) }
+inline fun <reified T : Any> KClass<T>.isProvidedByMock() = Module { it.bind(this.java).toProvider { mock<T>() } }
+
+inline fun <reified T : Any> Injector.getInstance() = getInstance(T::class.java)!!
fun <T : Any> supply(javaClass: Class<T>): Source<T> = object : Source<T> {
override fun fromInstance(instance: T) = Module { it.bind(javaClass).toInstance(instance) }