1 package net.pterodactylus.sone.web.page
3 import com.google.inject.*
4 import net.pterodactylus.sone.test.*
5 import net.pterodactylus.sone.utils.*
6 import net.pterodactylus.util.template.*
7 import net.pterodactylus.util.web.*
8 import org.hamcrest.MatcherAssert.*
9 import org.hamcrest.Matchers.*
11 import org.junit.rules.*
13 class TemplateRendererTest {
17 val expectedException: ExpectedException = ExpectedException.none()
18 private val templateContextFactory = TemplateContextFactory()
19 private val templateRenderer = TemplateRenderer(templateContextFactory)
22 fun `renderer can render template`() {
23 val template = "foo".asTemplate()
24 val rendered = templateRenderer.render(template)
25 assertThat(rendered, equalTo("foo"))
29 fun `renderer merges template contexts from template and context factory`() {
30 templateContextFactory.addTemplateObject("a", 1)
31 val template = "<%a><%b>".asTemplate()
32 template.initialContext.set("b", 2)
33 val rendered = templateRenderer.render(template)
34 assertThat(rendered, equalTo("12"))
38 fun `template context can be processed`() {
39 templateContextFactory.addTemplateObject("a", 1)
40 val template = "<%a><%b><%c>".asTemplate()
41 template.initialContext.set("b", 2)
42 val rendered = templateRenderer.render(template) { templateContext -> templateContext.set("c", 3) }
43 assertThat(rendered, equalTo("123"))
47 fun `redirect exceptions are thrown`() {
48 expectedException.expect(RedirectException::class.java)
49 templateRenderer.render(Template()) { _ -> throw RedirectException("foo") }
53 fun `template renderer can be created by guice`() {
54 val injector = Guice.createInjector(
55 TemplateContextFactory::class.isProvidedByMock()
57 assertThat(injector.getInstance<TemplateRenderer>(), notNullValue())