use of com.google.template.soy.soytree.TemplateRegistry in project closure-templates by google.
the class FindTransitiveDepTemplatesVisitorTest method testExecOnAllTemplates.
@Test
public void testExecOnAllTemplates() {
// aaa -> {bbb, ccc}, bbb -> ddd.
String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + " {$ij.boo} {$ij.goo} {call .ddd /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + " {call .bbb /} {$ij.boo} {call .ccc /} {$ij.foo}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + " {$ij.boo} {$ij.moo + $ij.woo}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ddd}\n" + " {$ij.boo} {$ij.moo} {round($ij.zoo)}\n" + "{/template}\n";
ParseResult result = SoyFileSetParserBuilder.forFileContents(fileContent).errorReporter(FAIL).parse();
TemplateRegistry templateRegistry = result.registry();
SoyFileSetNode soyTree = result.fileSet();
TemplateNode bbb = soyTree.getChild(0).getChild(0);
TemplateNode aaa = soyTree.getChild(0).getChild(1);
TemplateNode ccc = soyTree.getChild(0).getChild(2);
TemplateNode ddd = soyTree.getChild(0).getChild(3);
ImmutableMap<TemplateNode, TransitiveDepTemplatesInfo> resultMap = new FindTransitiveDepTemplatesVisitor(templateRegistry).execOnAllTemplates(soyTree);
assertThat(resultMap).hasSize(4);
assertThat(resultMap.get(ddd).depTemplateSet).isEqualTo(ImmutableSet.of(ddd));
assertThat(resultMap.get(ccc).depTemplateSet).isEqualTo(ImmutableSet.of(ccc));
assertThat(resultMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb, ddd));
assertThat(resultMap.get(aaa).depTemplateSet).isEqualTo(ImmutableSet.of(aaa, bbb, ccc, ddd));
}
use of com.google.template.soy.soytree.TemplateRegistry in project closure-templates by google.
the class FindTransitiveDepTemplatesVisitorTest method testLargerRecursiveCycle.
@Test
public void testLargerRecursiveCycle() {
// Tests indirect recursion with a cycle of 3.
// aaa -> bbb, bbb -> ccc, ccc -> aaa.
String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + " {$ij.foo} {$ij.boo} {call .bbb /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + " {$ij.goo} {call .ccc /} {$ij.boo}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + " {call .aaa /} {$ij.moo} {$ij.boo}\n" + "{/template}\n";
ParseResult result = SoyFileSetParserBuilder.forFileContents(fileContent).errorReporter(FAIL).parse();
TemplateRegistry templateRegistry = result.registry();
SoyFileSetNode soyTree = result.fileSet();
TemplateNode aaa = soyTree.getChild(0).getChild(0);
TemplateNode bbb = soyTree.getChild(0).getChild(1);
TemplateNode ccc = soyTree.getChild(0).getChild(2);
// Test with exec(aaa).
// Exercises: processCalleeHelper case 3 (ccc-> aaa).
// Exercises: processCalleeHelper case 5 with incorporateCalleeVisitInfo case 3 (bbb -> ccc).
// Exercises: processCalleeHelper case 5 with incorporateCalleeVisitInfo case 2 (aaa -> bbb).
FindTransitiveDepTemplatesVisitor visitor = new FindTransitiveDepTemplatesVisitor(templateRegistry);
Map<TemplateNode, TransitiveDepTemplatesInfo> memoizedInfoMap = visitor.templateToFinishedInfoMap;
visitor.exec(aaa);
assertThat(memoizedInfoMap).hasSize(3);
assertThat(memoizedInfoMap.get(ccc).depTemplateSet).isEqualTo(ImmutableSet.of(ccc, aaa, bbb));
assertThat(memoizedInfoMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb, ccc, aaa));
assertThat(memoizedInfoMap.get(aaa).depTemplateSet).isEqualTo(ImmutableSet.of(aaa, bbb, ccc));
}
use of com.google.template.soy.soytree.TemplateRegistry in project closure-templates by google.
the class VeLogInstrumentationVisitorTest method runPass.
/**
* Parses the given input as a template content.
*/
private static SoyFileSetNode runPass(String input) {
String soyFile = Joiner.on('\n').join("{namespace ns}", "", "{template .t}", input, "{/template}");
ParseResult result = SoyFileSetParserBuilder.forFileContents(soyFile).desugarHtmlNodes(false).typeRegistry(new SoyTypeRegistry.Builder().addDescriptors(ImmutableList.of(com.google.template.soy.testing.Foo.getDescriptor())).build()).setLoggingConfig(LOGGING_CONFIG).addSoyFunction(new TestLoggingFunction()).errorReporter(ErrorReporter.exploding()).parse();
TemplateRegistry templateRegistry = result.registry();
SoyFileSetNode soyTree = result.fileSet();
new VeLogInstrumentationVisitor(templateRegistry).exec(soyTree);
return soyTree;
}
use of com.google.template.soy.soytree.TemplateRegistry in project closure-templates by google.
the class VeLoggingTest method renderTemplate.
private void renderTemplate(Map<String, ?> params, OutputAppendable output, String... templateBodyLines) throws IOException {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents("{namespace ns}\n" + "{template .foo}\n" + Joiner.on("\n").join(templateBodyLines) + "\n{/template}").typeRegistry(new SoyTypeRegistry.Builder().addDescriptors(ImmutableList.of(com.google.template.soy.testing.Foo.getDescriptor())).build()).setLoggingConfig(config).addSoyFunction(new DepthFunction()).runAutoescaper(true).parse().fileSet();
TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
RenderContext ctx = TemplateTester.getDefaultContext(templates).toBuilder().hasLogger(true).build();
RenderResult result = templates.getTemplateFactory("ns.foo").create(TemplateTester.asRecord(params), EMPTY_DICT).render(output, ctx);
assertThat(result).isEqualTo(RenderResult.done());
}
use of com.google.template.soy.soytree.TemplateRegistry in project closure-templates by google.
the class BytecodeCompilerTest method testDebugSoyTemplateInfo.
@Test
public void testDebugSoyTemplateInfo() throws IOException {
String soyFileContent = Joiner.on("\n").join("{namespace ns}", "", "{template .html}", " <div>foo</div>", "{/template}", "", "{template .text kind=\"text\"}", " foo", "{/template}", "", "{template .htmlNoTag}", " foo", "{/template}");
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(soyFileContent).addHtmlAttributesForDebugging(true).parse().fileSet();
TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
// HTML templates
CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.html");
assertThat(renderWithContext(factory, getDefaultContext(templates))).isEqualTo("<div>foo</div>");
// If debugSoyTemplateInfo is enabled, we should render additional HTML comments.
assertThat(renderWithContext(factory, getDefaultContextWithDebugInfo(templates))).isEqualTo("<div data-debug-soy=\"ns.html no-path:4\">foo</div>");
// We should never render these comments for templates with kind="text".
factory = templates.getTemplateFactory("ns.text");
assertThat(renderWithContext(factory, getDefaultContext(templates))).isEqualTo("foo");
assertThat(renderWithContext(factory, getDefaultContextWithDebugInfo(templates))).isEqualTo("foo");
factory = templates.getTemplateFactory("ns.htmlNoTag");
assertThat(renderWithContext(factory, getDefaultContext(templates))).isEqualTo("foo");
assertThat(renderWithContext(factory, getDefaultContextWithDebugInfo(templates))).isEqualTo("foo");
}
Aggregations