X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FParserFilterTest.kt;fp=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftemplate%2FParserFilterTest.kt;h=3c735c2fd002d1f22bc993150db7bcf4133d7ed2;hb=f6bb52d378645bf821febd6696bc69f250a92280;hp=0000000000000000000000000000000000000000;hpb=8e2fb40c556d340c5d6a8d78f8c38c9dc14d8365;p=Sone.git diff --git a/src/test/kotlin/net/pterodactylus/sone/template/ParserFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/ParserFilterTest.kt new file mode 100644 index 0000000..3c735c2 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/template/ParserFilterTest.kt @@ -0,0 +1,65 @@ +package net.pterodactylus.sone.template + +import com.google.common.base.Optional.of +import net.pterodactylus.sone.core.Core +import net.pterodactylus.sone.data.Sone +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.text.SoneTextParser +import net.pterodactylus.sone.text.SoneTextParserContext +import net.pterodactylus.util.template.TemplateContext +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.`is` +import org.hamcrest.Matchers.emptyIterable +import org.junit.Test +import org.mockito.ArgumentCaptor.forClass +import org.mockito.Mockito.`when` +import org.mockito.Mockito.eq +import org.mockito.Mockito.verify + +/** + * Unit test for [ParserFilter]. + */ +class ParserFilterTest { + + companion object { + private const val SONE_IDENTITY = "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI" + } + + private val core = mock() + private val sone = setupSone(SONE_IDENTITY) + private val soneTextParser = mock() + private val templateContext = TemplateContext() + private val parameters = mutableMapOf() + private val filter = ParserFilter(core, soneTextParser) + + private fun setupSone(identity: String): Sone { + val sone = mock() + `when`(sone.id).thenReturn(identity) + `when`(core.getSone(identity)).thenReturn(of(sone)) + return sone + } + + @Test + fun `parsing null returns an empty iterable`() { + assertThat(filter.format(templateContext, null, mutableMapOf()) as Iterable<*>, emptyIterable()) + } + + @Test + fun `given sone is used to create parser context`() { + setupSoneAndVerifyItIsUsedInContext(sone, sone) + } + + @Test + fun `sone with given sone ID is used to create parser context`() { + setupSoneAndVerifyItIsUsedInContext(SONE_IDENTITY, sone) + } + + private fun setupSoneAndVerifyItIsUsedInContext(soneOrSoneId: Any, sone: Sone) { + parameters.put("sone", soneOrSoneId) + filter.format(templateContext, "text", parameters) + val context = forClass(SoneTextParserContext::class.java) + verify(soneTextParser).parse(eq("text"), context.capture()) + assertThat(context.value.postingSone, `is`(sone)) + } + +}