🔥 Remove “create” flag from session provider interface
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / page / TemplateRendererTest.kt
1 package net.pterodactylus.sone.web.page
2
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.*
10 import org.junit.*
11 import org.junit.rules.*
12
13 class TemplateRendererTest {
14
15         @Rule
16         @JvmField
17         val expectedException: ExpectedException = ExpectedException.none()
18         private val templateContextFactory = TemplateContextFactory()
19         private val templateRenderer = TemplateRenderer(templateContextFactory)
20
21         @Test
22         fun `renderer can render template`() {
23                 val template = "foo".asTemplate()
24                 val rendered = templateRenderer.render(template)
25                 assertThat(rendered, equalTo("foo"))
26         }
27
28         @Test
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"))
35         }
36
37         @Test
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"))
44         }
45
46         @Test
47         fun `redirect exceptions are thrown`() {
48                 expectedException.expect(RedirectException::class.java)
49                 templateRenderer.render(Template()) { _ -> throw RedirectException("foo") }
50         }
51
52         @Test
53         fun `template renderer can be created by guice`() {
54                 val injector = Guice.createInjector(
55                                 TemplateContextFactory::class.isProvidedByMock()
56                 )
57                 assertThat(injector.getInstance<TemplateRenderer>(), notNullValue())
58         }
59
60 }