From: David ‘Bombe’ Roden Date: Sun, 10 Mar 2019 10:50:19 +0000 (+0100) Subject: ✅ Add test for parser to be injectable X-Git-Tag: v79^2~73 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=560bb333bdaf95bdf6cc557f2cf01494555f8631;p=Sone.git ✅ Add test for parser to be injectable --- 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 }