1 package net.pterodactylus.sone.template
3 import com.google.common.base.Optional.of
4 import net.pterodactylus.sone.core.Core
5 import net.pterodactylus.sone.data.Sone
6 import net.pterodactylus.sone.test.mock
7 import net.pterodactylus.sone.text.SoneTextParser
8 import net.pterodactylus.sone.text.SoneTextParserContext
9 import net.pterodactylus.util.template.TemplateContext
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.`is`
12 import org.hamcrest.Matchers.emptyIterable
14 import org.mockito.ArgumentCaptor.forClass
15 import org.mockito.Mockito.`when`
16 import org.mockito.Mockito.eq
17 import org.mockito.Mockito.verify
20 * Unit test for [ParserFilter].
22 class ParserFilterTest {
25 private const val SONE_IDENTITY = "nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI"
28 private val core = mock<Core>()
29 private val sone = setupSone(SONE_IDENTITY)
30 private val soneTextParser = mock<SoneTextParser>()
31 private val templateContext = TemplateContext()
32 private val parameters = mutableMapOf<String, Any?>()
33 private val filter = ParserFilter(core, soneTextParser)
35 private fun setupSone(identity: String): Sone {
36 val sone = mock<Sone>()
37 `when`(sone.id).thenReturn(identity)
38 `when`(core.getSone(identity)).thenReturn(of(sone))
43 fun `parsing null returns an empty iterable`() {
44 assertThat(filter.format(templateContext, null, mutableMapOf()) as Iterable<*>, emptyIterable())
48 fun `given sone is used to create parser context`() {
49 setupSoneAndVerifyItIsUsedInContext(sone, sone)
53 fun `sone with given sone ID is used to create parser context`() {
54 setupSoneAndVerifyItIsUsedInContext(SONE_IDENTITY, sone)
57 private fun setupSoneAndVerifyItIsUsedInContext(soneOrSoneId: Any, sone: Sone) {
58 parameters.put("sone", soneOrSoneId)
59 filter.format(templateContext, "text", parameters)
60 val context = forClass(SoneTextParserContext::class.java)
61 verify(soneTextParser).parse(eq<String>("text"), context.capture())
62 assertThat(context.value.postingSone, `is`(sone))