From: David ‘Bombe’ Roden Date: Fri, 17 May 2019 19:03:36 +0000 (+0200) Subject: 🚧 Add template renderer X-Git-Tag: v79^2~26 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=1e6ed6655b808480d7dfed38afed70a84872f938;p=Sone.git 🚧 Add template renderer --- 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()) + } + +}