From 1e6ed6655b808480d7dfed38afed70a84872f938 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 17 May 2019 21:03:36 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=9A=A7=20Add=20template=20renderer?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/web/page/TemplateRenderer.kt | 16 ++++++ .../sone/web/page/TemplateRendererTest.kt | 60 ++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/page/TemplateRenderer.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/page/TemplateRendererTest.kt diff --git a/src/main/kotlin/net/pterodactylus/sone/web/page/TemplateRenderer.kt b/src/main/kotlin/net/pterodactylus/sone/web/page/TemplateRenderer.kt new file mode 100644 index 0000000..21eeb2a --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/page/TemplateRenderer.kt @@ -0,0 +1,16 @@ +package net.pterodactylus.sone.web.page + +import net.pterodactylus.util.template.* +import java.io.* +import javax.inject.* + +class TemplateRenderer @Inject constructor(private val templateContextFactory: TemplateContextFactory) { + + fun render(template: Template, processor: (TemplateContext) -> Unit = {}): String = + templateContextFactory.createTemplateContext().let { templateContext -> + templateContext.mergeContext(template.initialContext) + processor(templateContext) + StringWriter().also { template.render(templateContext, it) }.toString() + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/page/TemplateRendererTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/page/TemplateRendererTest.kt new file mode 100644 index 0000000..cb03c61 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/page/TemplateRendererTest.kt @@ -0,0 +1,60 @@ +package net.pterodactylus.sone.web.page + +import com.google.inject.* +import net.pterodactylus.sone.test.* +import net.pterodactylus.sone.utils.* +import net.pterodactylus.util.template.* +import net.pterodactylus.util.web.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* +import org.junit.rules.* + +class TemplateRendererTest { + + @Rule + @JvmField + val expectedException: ExpectedException = ExpectedException.none() + private val templateContextFactory = TemplateContextFactory() + private val templateRenderer = TemplateRenderer(templateContextFactory) + + @Test + fun `renderer can render template`() { + val template = "foo".asTemplate() + val rendered = templateRenderer.render(template) + assertThat(rendered, equalTo("foo")) + } + + @Test + fun `renderer merges template contexts from template and context factory`() { + templateContextFactory.addTemplateObject("a", 1) + val template = "<%a><%b>".asTemplate() + template.initialContext.set("b", 2) + val rendered = templateRenderer.render(template) + assertThat(rendered, equalTo("12")) + } + + @Test + fun `template context can be processed`() { + templateContextFactory.addTemplateObject("a", 1) + val template = "<%a><%b><%c>".asTemplate() + template.initialContext.set("b", 2) + val rendered = templateRenderer.render(template) { templateContext -> templateContext.set("c", 3) } + assertThat(rendered, equalTo("123")) + } + + @Test + fun `redirect exceptions are thrown`() { + expectedException.expect(RedirectException::class.java) + templateRenderer.render(Template()) { _ -> throw RedirectException("foo") } + } + + @Test + fun `template renderer can be created by guice`() { + val injector = Guice.createInjector( + TemplateContextFactory::class.isProvidedByMock() + ) + assertThat(injector.getInstance(), notNullValue()) + } + +} -- 2.7.4