TemplateParser { public TNode parse(Path path) { try { final TNode root = parse(Files.readAllBytes(path), TemplateType.fromPath(path)); root.setUnparsedSize(Files.size(path)); return root; } catch (IOException e) { throw new TemplateException(e); } catch (TemplateException e) { if (e.getCause() != null) { throw new TemplateException("Error when parsing " + path + ":" + e.getCause().getMessage(), e.getCause()); } else { throw new TemplateException("Error when parsing " + path + ":" + e.getMessage(), e); } } }  TNode parse(Path path); TNode parse(byte[] in, TemplateType type);  }
@Test(expected = TemplateException.class) public void testRepeatNotMapWhenMapRequired() throws Exception { final TemplateParser parser = createTestParser(SystemInfo.STAGE_TESTING); final URL url = TemplateParserTest.class.getResource("repeat-template.html"); final TNode parseTree = parser.parse(IO.toPath(url.toURI())); Map<String, Object> model = Maps.newHashMap(); model.put("maps", Lists.newArrayList(Lists.newArrayList("elem1"))); model.put("type", "x"); String expected = "This doesn't matter because the render should fail"; assertCorrectRender(parseTree, model, expected); }
@Test(expected = TemplateException.class) public void testMultipleDirectivesPerElem() throws Exception { final TemplateParser parser = createTestParser(SystemInfo.STAGE_TESTING); final URL url = TemplateParserTest.class.getResource("multiple-directives-per-el-template.html"); parser.parse(IO.toPath(url.toURI())); }
@Test public void testSpacing() throws Exception { final TemplateParser parser = createTestParser(SystemInfo.STAGE_PRODUCTION); final URL url = AbstractTemplateParserTest.class.getResource("template-spacing.html"); final Path path = IO.toPath(url.toURI()); final TNode parseTree = parser.parse(path); Map<String, Object> model = Maps.newHashMap(); model.put("title", "Title"); String expected = "<html>\n" + "<head>\n" + " <title>Title</title>\n" + "</head>\n" + "</html>"; final ByteArrayOutputStream result = new ByteArrayOutputStream(); TRenderContext context = new TRenderContext(result, model); parseTree.render(context); assertEquals(expected + "\n" + result, expected, result.toString()); }