From 560bb333bdaf95bdf6cc557f2cf01494555f8631 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 10 Mar 2019 11:50:19 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Add=20test=20for=20parser=20to=20be?= =?utf8?q?=20injectable?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../kotlin/net/pterodactylus/sone/text/SoneTextParser.kt | 3 ++- .../java/net/pterodactylus/sone/text/SoneTextParserTest.java | 12 ++++++++++++ src/test/kotlin/net/pterodactylus/sone/test/Guice.kt | 3 +++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt b/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt index 690ce6d..3cd6587 100644 --- a/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt +++ b/src/main/kotlin/net/pterodactylus/sone/text/SoneTextParser.kt @@ -17,11 +17,12 @@ import net.pterodactylus.sone.text.LinkType.SSK import net.pterodactylus.sone.text.LinkType.USK import org.bitpedia.util.Base32 import java.net.MalformedURLException +import javax.inject.* /** * [Parser] implementation that can recognize Freenet URIs. */ -class SoneTextParser(private val soneProvider: SoneProvider?, private val postProvider: PostProvider?) { +class SoneTextParser @Inject constructor(private val soneProvider: SoneProvider?, private val postProvider: PostProvider?) { fun parse(source: String, context: SoneTextParserContext?) = source.split("\n") diff --git a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java index e763a20..cf8cdd2 100644 --- a/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java +++ b/src/test/java/net/pterodactylus/sone/text/SoneTextParserTest.java @@ -17,7 +17,9 @@ package net.pterodactylus.sone.text; +import static com.google.inject.Guice.createInjector; import static java.lang.String.format; +import static net.pterodactylus.sone.test.GuiceKt.supply; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.isIn; @@ -36,6 +38,7 @@ import net.pterodactylus.sone.database.PostProvider; import net.pterodactylus.sone.database.SoneProvider; import com.google.common.base.Optional; +import com.google.inject.Injector; import kotlin.jvm.functions.Function1; import org.junit.Test; @@ -415,6 +418,15 @@ public class SoneTextParserTest { return text.toString(); } + @Test + public void parserCanBeCreatedByGuice() { + Injector injector = createInjector( + supply(SoneProvider.class).byMock(), + supply(PostProvider.class).byMock() + ); + assertThat(injector.getInstance(SoneTextParser.class), notNullValue()); + } + /** * Mock Sone provider. */ diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt b/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt index 360b0cd..e27ad07 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Guice.kt @@ -2,6 +2,7 @@ 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 @@ -17,6 +18,7 @@ fun supply(javaClass: Class): Source = object : Source { 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 { @@ -24,4 +26,5 @@ interface Source { fun byInstance(instance: T): Module fun byProvider(provider: com.google.inject.Provider): Module fun byProvider(provider: Class>): Module + fun byMock(): Module } -- 2.7.4