From a8b58a357bcd4b12a0f2bd676458e96227986d24 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 26 Jan 2017 22:13:29 +0100 Subject: [PATCH] Make parser filter injectable --- .../pterodactylus/sone/template/ParserFilter.kt | 5 ++++- .../sone/template/ParserFilterTest.kt | 26 ++++++++++++++++++++++ .../kotlin/net/pterodactylus/sone/test/Guice.kt | 4 ++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/template/ParserFilter.kt b/src/main/kotlin/net/pterodactylus/sone/template/ParserFilter.kt index e389652..cec5d56 100644 --- a/src/main/kotlin/net/pterodactylus/sone/template/ParserFilter.kt +++ b/src/main/kotlin/net/pterodactylus/sone/template/ParserFilter.kt @@ -7,11 +7,14 @@ import net.pterodactylus.sone.text.SoneTextParser 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?): Any? { val text = data?.toString() ?: return listOf() diff --git a/src/test/kotlin/net/pterodactylus/sone/template/ParserFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/ParserFilterTest.kt index 3c735c2..ac2886a 100644 --- a/src/test/kotlin/net/pterodactylus/sone/template/ParserFilterTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/template/ParserFilterTest.kt @@ -1,8 +1,11 @@ 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 @@ -10,6 +13,8 @@ import net.pterodactylus.util.template.TemplateContext 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` @@ -62,4 +67,25 @@ class ParserFilterTest { 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(), notNullValue()) + } + + @Test + fun `parser filter is created as singleton`() { + val injector = Guice.createInjector( + Core::class.isProvidedByMock(), + SoneTextParser::class.isProvidedByMock() + ) + val firstInstance = injector.getInstance() + val secondInstance = injector.getInstance() + assertThat(firstInstance, sameInstance(secondInstance)) + + } + } diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt b/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt index a5df14e..d8a2d32 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt @@ -1,5 +1,6 @@ package net.pterodactylus.sone.test +import com.google.inject.Injector import com.google.inject.Module import javax.inject.Provider import kotlin.reflect.KClass @@ -8,6 +9,9 @@ fun KClass.isInstance(instance: T) = Module { it.bind(this.java).to fun KClass.isProvidedBy(instance: T) = Module { it.bind(this.java).toProvider { 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 { 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) } -- 2.7.4