From 4273a6ebdde8c4f1092a6c5d4f62ad483f3eaa96 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 11 Nov 2016 08:21:28 +0100 Subject: [PATCH] Add filter for linked images --- .../sone/template/LinkedImagesFilter.kt | 23 ++++++++++++++ .../sone/template/LinkedImagesFilterTest.kt | 37 ++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/main/kotlin/net/pterodactylus/sone/template/LinkedImagesFilter.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/template/LinkedImagesFilterTest.kt diff --git a/src/main/kotlin/net/pterodactylus/sone/template/LinkedImagesFilter.kt b/src/main/kotlin/net/pterodactylus/sone/template/LinkedImagesFilter.kt new file mode 100644 index 0000000..584bec3 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/template/LinkedImagesFilter.kt @@ -0,0 +1,23 @@ +package net.pterodactylus.sone.template + +import net.pterodactylus.sone.core.ImageLoader +import net.pterodactylus.sone.core.LoadedImage +import net.pterodactylus.sone.text.FreenetLinkPart +import net.pterodactylus.sone.text.Part +import net.pterodactylus.util.template.Filter +import net.pterodactylus.util.template.TemplateContext + +/** + * Filter that takes a number of pre-rendered [Part]s and replaces all identified links to freenet images + * with [LoadedImage]s. + */ +class LinkedImagesFilter(private val imageLoader: ImageLoader) : Filter { + + @Suppress("UNCHECKED_CAST") + override fun format(templateContext: TemplateContext?, data: Any?, parameters: MutableMap?) = + (data as? Iterable) + ?.filterIsInstance() + ?.mapNotNull { imageLoader.toLoadedImage(it.link) } + ?: listOf() + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/template/LinkedImagesFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/LinkedImagesFilterTest.kt new file mode 100644 index 0000000..8e86bd4 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/template/LinkedImagesFilterTest.kt @@ -0,0 +1,37 @@ +package net.pterodactylus.sone.template + +import net.pterodactylus.sone.core.ImageLoader +import net.pterodactylus.sone.core.LoadedImage +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.text.FreenetLinkPart +import net.pterodactylus.sone.text.LinkPart +import net.pterodactylus.sone.text.PlainTextPart +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers +import org.junit.Test +import org.mockito.Mockito.`when` + +/** + * Unit test for [LinkedImagesFilter]. + */ +class LinkedImagesFilterTest { + + private val imageLoader = mock() + private val filter = LinkedImagesFilter(imageLoader) + + @Test + fun `filter finds all loaded freenet images`() { + val parts = listOf( + PlainTextPart("text"), + LinkPart("http://link", "link"), + FreenetLinkPart("KSK@link", "link", false), + FreenetLinkPart("KSK@link.png", "link", false) + ) + `when`(imageLoader.toLoadedImage("KSK@link.png")).thenReturn(LoadedImage("KSK@link.png", "image/png", 1440, 900)) + val loadedImages = filter.format(null, parts, null) + assertThat(loadedImages, Matchers.contains( + LoadedImage("KSK@link.png", "image/png", 1440, 900) + )) + } + +} -- 2.7.4