public Mustache compile(String file) { Reader reader = mf.getReader(file); if (reader == null) { throw new MustacheNotFoundException(file); } return compile(reader, file); }
/** * Execute the mustache object with a given writer and a single scope context. * * @param writer write the output of the executed template here * @param scope the root object to use * @return the new writer */ default Writer execute(Writer writer, Object scope) { return execute(writer, makeList(scope)); }
protected void filterText() { if (df != null && appended != null) { appended = df.filterText(appended, tc.startOfLine()); } }
public void testPartialWithTF() throws MustacheException, IOException { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile("partialintemplatefunction.html"); StringWriter sw = new StringWriter(); m.execute(sw, new Object() { public TemplateFunction i() { return s -> s; } }); assertEquals("This is not interesting.", sw.toString()); }
private StringWriter execute(String name, Object object) { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile(name); StringWriter sw = new StringWriter(); m.execute(sw, object); return sw; }
public void testComplexParallel() throws MustacheException, IOException { MustacheFactory c = initParallel(); Mustache m = c.compile("complex.html"); StringWriter sw = new StringWriter(); m.execute(sw, new ParallelComplexObject()).close(); assertEquals(getContents(root, "complex.txt"), sw.toString()); }
protected Writer writeTemplate(Writer writer, String templateText, List<Object> scopes) { return df.getFragment(new FragmentKey(tc, templateText)).execute(writer, scopes); }
public void testRelativePathFail() throws IOException { MustacheFactory mf = createMustacheFactory(); try { Mustache compile = mf.compile("relative/pathfail.html"); fail("Should have failed to compile"); } catch (MustacheException e) { // Success } }
@Override protected Mustache resolve(final String templatePath, final Reader reader) { return factory.compile(reader, templatePath); }
public void testImproperlyClosedVariable() throws IOException { try { new DefaultMustacheFactory().compile(new StringReader("{{{#containers}} {{/containers}}"), "example"); fail("Should have throw MustacheException"); } catch (MustacheException actual) { assertEquals("Improperly closed variable in example:1 @[example:1]", actual.getMessage()); } }
public void testMustacheNotFoundException() { String nonExistingMustache = "404"; try { new DefaultMustacheFactory().compile(nonExistingMustache); fail("Didn't throw an exception"); } catch (MustacheNotFoundException e) { assertEquals(nonExistingMustache, e.getName()); } }
public void testLimitedDepthRecursion() { try { StringWriter sw = execute("infiniteparent.html", new Context()); fail("Should have failed"); } catch (StackOverflowError soe) { fail("Should not have overflowed the stack"); } catch (MustacheException e) { assertEquals("Maximum partial recursion limit reached: 100 @[infiniteparent.html:1]", e.getMessage()); } }
@Override public Reader getReader(String resourceName) { Reader reader = mustacheResolver.getReader(resourceName); if (reader == null) { throw new MustacheNotFoundException(resourceName); } return reader; }
public void testSimpleWithMap() throws MustacheException, IOException, ExecutionException, InterruptedException { StringWriter sw = execute("simple.html", new HashMap<String, Object>() {{ put("name", "Chris"); put("value", 10000); put("taxed_value", 6000); put("in_ca", true); }}); assertEquals(getContents(root, "simple.txt"), sw.toString()); }
public void testReadmeSerial() throws MustacheException, IOException { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile("items2.html"); StringWriter sw = new StringWriter(); long start = System.currentTimeMillis(); m.execute(sw, new Context()); long diff = System.currentTimeMillis() - start; assertEquals(getContents(root, "items.txt"), sw.toString()); assertTrue("Should be a little bit more than 4 seconds: " + diff, diff > 3999 && diff < 6000); }
private StringWriter execute(String name, List<Object> objects) { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile(name); StringWriter sw = new StringWriter(); m.execute(sw, objects); return sw; }
public void testRootCheck() throws MustacheException, IOException, ExecutionException, InterruptedException { MustacheFactory c = createMustacheFactory(); try { Mustache m = c.compile("../../../pom.xml"); fail("Should have failed to compile"); } catch (MustacheException e) { // Success } }
public void testRelativePathsDotDotDir() throws IOException { MustacheFactory mf = createMustacheFactory(); Mustache compile = mf.compile("relative/dotdot.html"); StringWriter sw = new StringWriter(); compile.execute(sw, "").close(); assertEquals(getContents(root, "uninterestingpartial.html"), sw.toString()); }
public void testReadme() throws MustacheException, IOException { MustacheFactory c = createMustacheFactory(); Mustache m = c.compile("items.html"); StringWriter sw = new StringWriter(); long start = System.currentTimeMillis(); m.execute(sw, new Context()); long diff = System.currentTimeMillis() - start; assertEquals(getContents(root, "items.txt"), sw.toString()); }