public void testImplicitIteratorWithScope() throws IOException { Mustache test = new DefaultMustacheFactory().compile(new StringReader("{{#test}}_{{.}}_{{/test}}"), "test"); StringWriter sw = new StringWriter(); test.execute(sw, new Object() { List<String> test = Arrays.asList("a", "b", "c"); }).close(); assertEquals("_a__b__c_", sw.toString()); }
@SuppressWarnings("StatementWithEmptyBody") @Override public synchronized void init() { filterText(); Map<String, ExtendNameCode> replaceMap = new HashMap<>(); for (Code code : mustache.getCodes()) { if (code instanceof ExtendNameCode) { // put name codes in the map ExtendNameCode erc = (ExtendNameCode) code; replaceMap.put(erc.getName(), erc); erc.init(); } else if ((code instanceof WriteCode) || (code instanceof CommentCode)) { // ignore text and comments } else { // fail on everything else throw new IllegalArgumentException( "Illegal code in extend section: " + code.getClass().getName()); } } Mustache original = mf.compilePartial(partialName()); partial = (Mustache) original.clone(); Code[] supercodes = partial.getCodes(); // recursively replace named sections with replacements Set<Code> seen = new HashSet<>(); seen.add(partial); partial.setCodes(replaceCodes(supercodes, replaceMap, seen)); }
@Override public void displaySummary(long numFiles, long numSkipped, long numErrors, long numWarnings) throws IOException { Map<String, Object> output = new HashMap<>(); output.put(Messages.SUMMARY_KEY, Formatter.formatSummary(numFiles, numSkipped, numErrors, numWarnings).replace(NEWLINE_PATTERN, "")); // Sort files by descending order of the number of violations FILES.sort((o1, o2) -> ((List) o2.get(Messages.VIOLATIONS_KEY)).size() - ((List) o1.get(Messages.VIOLATIONS_KEY)).size()); output.put(Messages.FILES_KEY, FILES); output.put(Messages.VERSION_LONG_OPT, new ConfigProperties().getVersion()); InputStreamReader inputStreamReader = new InputStreamReader( HTMLFormatter.class.getResourceAsStream(TEMPLATE_PATH), Charset.defaultCharset()); Mustache mustache = new DefaultMustacheFactory().compile(inputStreamReader, TEMPLATE_PATH); mustache.execute(new OutputStreamWriter(System.out, Charset.defaultCharset()), output).flush(); inputStreamReader.close(); }
@Test public void testDiskstats() throws IOException { DefaultMustacheFactory dmf = new DefaultMustacheFactory(); Mustache m = dmf.compile(new StringReader("{{#disk}}\n" + "\\s+[0-9]+\\s+[0-9]+\\s+{{tag_device}} {{reads}} {{reads_merged}} {{sectors_read}} {{read_time}} {{writes}} {{writes_merged}} {{sectors_written}} {{write_time}} {{ios}} {{io_time}} {{weighted_io_time}}\n" + "{{/disk}}"), "diskstats"); String txt = " 220 100 xvdb 3140 43 23896 216 57698654 45893891 1261011016 12232816 0 10994276 12222124\n" + " 220 100 xvdk 2417241 93 19338786 1287328 284969078 116717514 10144866416 1520589288 0 329180460 1521686240\n"; Node invert = m.invert(txt); output(invert); }
@Test public void should_load_teamplates_with_absolute_references_using_filepath() throws Exception { File file = new File("compiler/src/test/resources/templates_filepath"); File root = new File(file, TEMPLATE_FILE).exists() ? file : new File("src/test/resources/templates_filepath"); MustacheFactory factory = new DefaultMustacheFactory(root); Mustache maven = factory.compile(TEMPLATE_FILE); StringWriter sw = new StringWriter(); maven.execute(sw, new Object() { List<String> messages = Arrays.asList("w00pw00p", "mustache rocks"); }).close(); assertEquals("w00pw00p mustache rocks ", sw.toString()); }
public static void main(String[] args) throws MustacheException, IOException { Writer writer = new OutputStreamWriter(System.out); MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile(new StringReader("{{hello}}, {{world}}!"), "helloworld"); mustache.execute(writer, new HelloWorld()); writer.flush(); } }
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(options); Writer yamlWriter = new StringWriter(); yamlWriter.write("---\n"); yaml.dump(frontmatterData, yamlWriter); yamlWriter.write("---\n"); templateData.put("frontmatter", yamlWriter.toString()); MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile("com/google/errorprone/resources/bugpatterns_external.mustache"); mustache.execute(w, templateData); } else { MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile("com/google/errorprone/resources/bugpatterns_internal.mustache"); mustache.execute(w, templateData);
@Before public void setUp() { MustacheFactory factory = new DefaultMustacheFactory(); Reader reader = new StringReader(TEMPLATE); mustache = factory.compile(reader, "template"); }
@Test public void testCollectPoints() throws Exception { MustacheFactory dmf = new DefaultMustacheFactory(); Mustache compile = dmf.compile(new StringReader("{{#page}}This is a {{test}}{{/page}}"), UUID.randomUUID().toString()); Node node = compile.invert("This is a good day"); assertNotNull(node); }
@Test public void testConcurrentExecutionWithConcurrentTemplate() throws InterruptedException { if (skip()) return; String template = "{{calla}}:{{callb}}:{{callc}}"; ExecutorService es = Executors.newCachedThreadPool(); DefaultMustacheFactory dmf = new DefaultMustacheFactory(); dmf.setExecutorService(es); final Mustache test = dmf.compile(new StringReader(template), "test"); final AtomicInteger total = render(test, es); assertEquals(0, total.intValue()); } }
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()); } }
@Test public void testParser() throws IOException { DefaultMustacheFactory dmf = new DefaultMustacheFactory(); Mustache compile = dmf.compile("fdbcli.mustache"); Path file = getPath("src/test/resources/fdbcli.txt"); String txt = new String(Files.readAllBytes(file), "UTF-8"); Node invert = compile.invert(txt); System.out.println(invert); }
private MustacheFactory createNewMustacheFactory(Class<? extends View> key) { return new DefaultMustacheFactory( fileRoot.isPresent() ? new FileSystemResolver(fileRoot.get()) : new PerClassMustacheResolver(key)); }
@Override public synchronized void init() { filterText(); partial = df.compilePartial(partialName()); if (partial instanceof DefaultMustache && ((DefaultMustache)partial).isRecursive()) { isRecursive = true; } if (partial == null) { throw new MustacheException("Failed to compile partial: " + name, tc); } }
@Test public void should_handle_more_than_one_level_of_partial_nesting() throws Exception { MustacheFactory factory = new DefaultMustacheFactory(root); Mustache maven = factory.compile(TEMPLATE_FILE); StringWriter sw = new StringWriter(); maven.execute(sw, new Object() { List<String> messages = Arrays.asList("w00pw00p", "mustache rocks"); }).close(); assertEquals("w00pw00p mustache rocks ", sw.toString()); }
@Test public void testMap() throws IOException { ArrayList<Map<String, String>> fn = new ArrayList<>(); Map<String, String> map1 = new HashMap<>(); map1.put("name", "firstName"); map1.put("last", "lastName"); fn.add(map1); Map<String, String> map2 = new HashMap<>(); map2.put("name", "firstName 1"); map2.put("last", "lastName 1"); fn.add(map2); Map<String, ArrayList<Map<String, String>>> map = new HashMap<>(); map.put("names", fn); Writer writer = new OutputStreamWriter(System.out); MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile(new StringReader("{{#names}}<h2>First Name : {{name}}</h2> <h2>Last Name : {{last}}</h2>{{/names}}"), "example"); mustache.execute(writer, map); writer.flush(); } }
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(options); Writer yamlWriter = new StringWriter(); yamlWriter.write("---\n"); yaml.dump(frontmatterData, yamlWriter); yamlWriter.write("---\n"); templateData.put("frontmatter", yamlWriter.toString()); MustacheFactory mf = new DefaultMustacheFactory(); Mustache mustache = mf.compile("com/google/errorprone/resources/bugpattern.mustache"); mustache.execute(writer, templateData.build()); writer.write("\n----------\n\n");
@Before public void setUp() { MustacheFactory factory = new DefaultMustacheFactory(); template = factory.compile(new StringReader(TEMPLATE), "template"); scopesTemplate = factory.compile(new StringReader(SCOPES_TEMPLATE), "template"); }
@Test public void testNoNode() throws Exception { MustacheFactory dmf = new DefaultMustacheFactory(); Mustache compile = dmf.compile(new StringReader("Using cluster file [^\\n]+\nHello World"), UUID.randomUUID().toString()); Node node = compile.invert("Using cluster file `/etc/foundationdb/fdb.cluster'.\nHello World"); assertNotNull(node); } }
@Test public void testSimple() throws IOException { DefaultMustacheFactory dmf = new DefaultMustacheFactory(); Mustache test = dmf.compile(new StringReader("test {{value}} test"), "test"); Node invert = test.invert("test value test"); Node node = new Node(); node.put("value", value("value")); assertEquals(node, invert); }