@Test public void testTwoLevelHierarchy() throws Exception { JtwigResource oneResource = mock(JtwigResource.class); JtwigResource twoResource = mock(JtwigResource.class); when(resource.retrieve()) .thenReturn( new ByteArrayInputStream( ("{% extends 'level-1' %}" + "{% block two %}two{% endblock %}").getBytes())); when(resource.resolve("level-1")).thenReturn(oneResource); when(oneResource.retrieve()) .thenReturn( new ByteArrayInputStream( ("{% extends 'root' %}" + "{% block one %}one{% endblock %}").getBytes())); when(oneResource.resolve("root")).thenReturn(twoResource); when(twoResource.retrieve()) .thenReturn( new ByteArrayInputStream( "Block {% block one %}1{% endblock %} and {% block two %}2{% endblock %}" .getBytes())); underTest.output(toTheOutputStream(), context); assertThat(theOutput(), is("Block one and two")); }
@Test public void testExtendsWithArrayOfTemplates() throws Exception { JtwigResource twoResource = mock(JtwigResource.class); when(resource.resolve("num-two")).thenReturn(twoResource); when(twoResource.retrieve()).thenReturn(new ByteArrayInputStream("second".getBytes())); when(resource.retrieve()) .thenReturn( new ByteArrayInputStream("{% extends ['num-one','num-two','num-three'] %}".getBytes())); underTest.output(toTheOutputStream(), context); assertThat(theOutput(), is("second")); }
@Test public void testRootDocument() throws Exception { when(resource.retrieve()).thenReturn(new ByteArrayInputStream("joao".getBytes())); underTest.output(toTheOutputStream(), context); assertThat(theOutput(), is("joao")); }
@Test public void testExtendsVariableDefinedTemplate() throws Exception { JtwigResource oneResource = mock(JtwigResource.class); JtwigResource twoResource = mock(JtwigResource.class); when(resource.resolve("num-one")).thenReturn(oneResource); when(resource.resolve("num-two")).thenReturn(twoResource); when(oneResource.retrieve()).thenReturn(new ByteArrayInputStream("first".getBytes())); when(twoResource.retrieve()).thenReturn(new ByteArrayInputStream("second".getBytes())); when(resource.retrieve()).thenReturn(new ByteArrayInputStream("{% extends var %}".getBytes())); context.withModelAttribute("var", "num-two"); underTest.output(toTheOutputStream(), context); assertThat(theOutput(), is("second")); }
@Test public void testSingleHierarchy() throws Exception { JtwigResource joaoResource = mock(JtwigResource.class); when(resource.retrieve()) .thenReturn( new ByteArrayInputStream( ("{% extends 'test' %}" + "{% block joao %}joao{% endblock %}").getBytes())); when(resource.resolve("test")).thenReturn(joaoResource); when(joaoResource.retrieve()) .thenReturn( new ByteArrayInputStream("I am {% block joao %}no one{% endblock %}".getBytes())); underTest.output(toTheOutputStream(), context); assertThat(theOutput(), is("I am joao")); }