Search in sources :

Example 1 with TransitiveDepTemplatesInfo

use of com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo in project closure-templates by google.

the class FindIjParamsVisitor method exec.

/**
 * Computes injected params info for a template.
 *
 * <p>Note: This method is not thread-safe. If you need to get injected params info in a
 * thread-safe manner, then please use {@link #execOnAllTemplates}() in a thread-safe manner.
 */
public IjParamsInfo exec(TemplateNode rootTemplate) {
    TransitiveDepTemplatesInfo depsInfo = findTransitiveDepTemplatesVisitor.exec(rootTemplate);
    if (!depsInfoToIjParamsInfoMap.containsKey(depsInfo)) {
        ImmutableMultimap.Builder<String, TemplateNode> ijParamToCalleesMultimapBuilder = ImmutableMultimap.builder();
        for (TemplateNode template : depsInfo.depTemplateSet) {
            if (!templateToLocalIjParamsMap.containsKey(template)) {
                templateToLocalIjParamsMap.put(template, getAllIjs(template));
            }
            for (String localIjParam : templateToLocalIjParamsMap.get(template)) {
                ijParamToCalleesMultimapBuilder.put(localIjParam, template);
            }
            for (TemplateParam injectedParam : template.getInjectedParams()) {
                ijParamToCalleesMultimapBuilder.put(injectedParam.name(), template);
            }
        }
        IjParamsInfo ijParamsInfo = new IjParamsInfo(ijParamToCalleesMultimapBuilder.build());
        depsInfoToIjParamsInfoMap.put(depsInfo, ijParamsInfo);
    }
    return depsInfoToIjParamsInfoMap.get(depsInfo);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TransitiveDepTemplatesInfo(com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) TemplateParam(com.google.template.soy.soytree.defn.TemplateParam)

Example 2 with TransitiveDepTemplatesInfo

use of com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo in project closure-templates by google.

the class FindTransitiveDepTemplatesVisitorTest method testTwoPathsToSameRecursiveCycle.

@Test
public void testTwoPathsToSameRecursiveCycle() {
    // aaa -> {bbb, ccc}, bbb -> ddd, ccc -> ddd, ddd -> bbb.
    String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + "  {$ij.boo} {$ij.foo} {call .bbb /} {call .ccc /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + "  {$ij.boo} {$ij.goo} {call .ddd /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + "  {$ij.boo} {$ij.moo} {call .ddd /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ddd}\n" + "  {$ij.boo} {$ij.too} {call .bbb /}\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);
    TemplateNode ddd = soyTree.getChild(0).getChild(3);
    // Test with exec(aaa).
    // Exercises: processCalleeHelper case 4 with incorporateCalleeVisitInfo case 4 (ccc -> ddd).
    FindTransitiveDepTemplatesVisitor visitor = new FindTransitiveDepTemplatesVisitor(templateRegistry);
    Map<TemplateNode, TransitiveDepTemplatesInfo> memoizedInfoMap = visitor.templateToFinishedInfoMap;
    visitor.exec(aaa);
    assertThat(memoizedInfoMap).hasSize(4);
    assertThat(memoizedInfoMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb, ddd));
    assertThat(memoizedInfoMap.get(ddd).depTemplateSet).isEqualTo(ImmutableSet.of(ddd, bbb));
    assertThat(memoizedInfoMap.get(ccc).depTemplateSet).isEqualTo(ImmutableSet.of(ccc, ddd, bbb));
    assertThat(memoizedInfoMap.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 3 with TransitiveDepTemplatesInfo

use of com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo in project closure-templates by google.

the class FindTransitiveDepTemplatesVisitorTest method testSimpleRecursion.

@Test
public void testSimpleRecursion() {
    // Tests direct recursion (cycle of 1) and indirect recursion with a cycle of 2.
    // aaa -> bbb, bbb -> {bbb, ccc}, ccc -> bbb.
    String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + "  {call .bbb /} {$ij.boo} {$ij.foo}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + "  {$ij.boo} {$ij.goo} {call .bbb /} {call .ccc /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + "  {$ij.boo} {call .bbb /} {$ij.moo}\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 2 (bbb -> bbb).
    // Exercises: processCalleeHelper case 3 (ccc -> bbb).
    // Exercises: processCalleeHelper case 5 with incorporateCalleeVisitInfo case 2 (bbb -> ccc).
    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, bbb));
    assertThat(memoizedInfoMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb, ccc));
    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 4 with TransitiveDepTemplatesInfo

use of com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo in project closure-templates by google.

the class FindTransitiveDepTemplatesVisitorTest method testTwoPathsToSameTemplate.

@Test
public void testTwoPathsToSameTemplate() {
    // aaa -> {bbb, ccc}, ccc -> bbb.
    String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + "  {call .bbb /} {$ij.boo} {call .ccc /} {$ij.foo}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + "  {$ij.boo} {$ij.goo}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + "  {$ij.boo} {$ij.moo + $ij.woo} {call .bbb /}\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 4 with incorporateCalleeVisitInfo case 1 (ccc -> bbb).
    FindTransitiveDepTemplatesVisitor visitor = new FindTransitiveDepTemplatesVisitor(templateRegistry);
    Map<TemplateNode, TransitiveDepTemplatesInfo> memoizedInfoMap = visitor.templateToFinishedInfoMap;
    visitor.exec(aaa);
    assertThat(memoizedInfoMap).hasSize(3);
    assertThat(memoizedInfoMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb));
    assertThat(memoizedInfoMap.get(ccc).depTemplateSet).isEqualTo(ImmutableSet.of(ccc, bbb));
    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 5 with TransitiveDepTemplatesInfo

use of com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo 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)

Aggregations

TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)9 TemplateNode (com.google.template.soy.soytree.TemplateNode)9 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)8 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)8 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)8 Test (org.junit.Test)7 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)1 ExtractMsgsVisitor (com.google.template.soy.msgs.internal.ExtractMsgsVisitor)1 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)1 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)1 FindTransitiveDepTemplatesVisitor (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor)1 PluginResolver (com.google.template.soy.soyparse.PluginResolver)1 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)1 TemplateParam (com.google.template.soy.soytree.defn.TemplateParam)1