Search in sources :

Example 16 with TemplateRegistry

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));
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) TransitiveDepTemplatesInfo(com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo) Test(org.junit.Test)

Example 17 with TemplateRegistry

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));
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) TransitiveDepTemplatesInfo(com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo) Test(org.junit.Test)

Example 18 with TemplateRegistry

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;
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyTypeRegistry(com.google.template.soy.types.SoyTypeRegistry) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode)

Example 19 with TemplateRegistry

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());
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) SoyFileSetParserBuilder(com.google.template.soy.SoyFileSetParserBuilder) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) RenderResult(com.google.template.soy.jbcsrc.api.RenderResult) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates)

Example 20 with TemplateRegistry

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");
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Aggregations

TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)31 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)29 Test (org.junit.Test)20 TemplateNode (com.google.template.soy.soytree.TemplateNode)19 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)13 TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)8 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)5 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)3 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)3 IncrementingIdGenerator (com.google.template.soy.base.internal.IncrementingIdGenerator)2 ErrorReporter (com.google.template.soy.error.ErrorReporter)2 JsSrcMain (com.google.template.soy.jssrc.internal.JsSrcMain)2 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)2 PluginResolver (com.google.template.soy.soyparse.PluginResolver)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 SoyFileSetParserBuilder (com.google.template.soy.SoyFileSetParserBuilder)1 IdGenerator (com.google.template.soy.base.internal.IdGenerator)1 SoyFileSupplier (com.google.template.soy.base.internal.SoyFileSupplier)1