Search in sources :

Example 96 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.

the class FindIjParamsVisitorTest method testExecForAllTemplates.

@Test
public void testExecForAllTemplates() {
    // 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";
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(fileContent).parse().fileSet();
    TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, FAIL);
    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, IjParamsInfo> templateToIjParamsInfoMap = new FindIjParamsVisitor(templateRegistry).execOnAllTemplates(soyTree);
    assertThat(templateToIjParamsInfoMap).hasSize(4);
    assertThat(templateToIjParamsInfoMap.get(ddd).ijParamToCalleesMultimap).hasSize(3);
    assertThat(templateToIjParamsInfoMap.get(ccc).ijParamToCalleesMultimap).hasSize(3);
    assertThat(templateToIjParamsInfoMap.get(bbb).ijParamToCalleesMultimap).hasSize(5);
    assertThat(templateToIjParamsInfoMap.get(bbb).ijParamToCalleesMultimap.keySet()).hasSize(4);
    assertThat(templateToIjParamsInfoMap.get(aaa).ijParamToCalleesMultimap).hasSize(10);
    assertThat(templateToIjParamsInfoMap.get(aaa).ijParamToCalleesMultimap.keySet()).hasSize(6);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) IjParamsInfo(com.google.template.soy.passes.FindIjParamsVisitor.IjParamsInfo) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Test(org.junit.Test)

Example 97 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.

the class FindIjParamsVisitorTest method testSimple.

@Test
public void testSimple() {
    // aaa -> {bbb, ccc}, bbb -> ddd.
    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} {call .ddd /}\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";
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(fileContent).parse().fileSet();
    TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, FAIL);
    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 5 with incorporateCalleeVisitInfo case 1 (aaa -> bbb).
    FindIjParamsVisitor visitor = new FindIjParamsVisitor(templateRegistry);
    visitor.exec(aaa);
    assertThat(visitor.exec(ddd).ijParamToCalleesMultimap).hasSize(3);
    assertThat(visitor.exec(ccc).ijParamToCalleesMultimap).hasSize(3);
    assertThat(visitor.exec(bbb).ijParamToCalleesMultimap).hasSize(5);
    assertThat(visitor.exec(bbb).ijParamToCalleesMultimap.keySet()).hasSize(4);
    assertThat(visitor.exec(aaa).ijParamToCalleesMultimap).hasSize(10);
    assertThat(visitor.exec(aaa).ijParamToCalleesMultimap.keySet()).hasSize(6);
    // Test with exec(bbb) then exec(aaa).
    // Exercises: processCalleeHelper case 1 (aaa -> bbb).
    visitor = new FindIjParamsVisitor(templateRegistry);
    visitor.exec(bbb);
    assertThat(visitor.exec(ddd).ijParamToCalleesMultimap).hasSize(3);
    assertThat(visitor.exec(bbb).ijParamToCalleesMultimap).hasSize(5);
    assertThat(visitor.exec(bbb).ijParamToCalleesMultimap.keySet()).hasSize(4);
    visitor.exec(aaa);
    assertThat(visitor.exec(ddd).ijParamToCalleesMultimap).hasSize(3);
    assertThat(visitor.exec(ccc).ijParamToCalleesMultimap).hasSize(3);
    assertThat(visitor.exec(bbb).ijParamToCalleesMultimap).hasSize(5);
    assertThat(visitor.exec(bbb).ijParamToCalleesMultimap.keySet()).hasSize(4);
    assertThat(visitor.exec(aaa).ijParamToCalleesMultimap).hasSize(10);
    assertThat(visitor.exec(aaa).ijParamToCalleesMultimap.keySet()).hasSize(6);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Test(org.junit.Test)

Example 98 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.

the class FindTransitiveDepTemplatesVisitorTest method testSmallerRecursiveCycleInLargerRecursiveCycle.

@Test
public void testSmallerRecursiveCycleInLargerRecursiveCycle() {
    // aaa -> {bbb, ccc}, bbb -> aaa, ccc -> bbb.
    String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + "  {$ij.foo} {$ij.boo} {call .bbb /} {call .ccc /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + "  {$ij.goo} {$ij.boo} {call .aaa /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + "  {$ij.moo} {$ij.boo} {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 3 (ccc -> 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, bbb, aaa));
    assertThat(memoizedInfoMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb, aaa, 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 99 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.

the class FindTransitiveDepTemplatesVisitorTest method testSimple.

@Test
public void testSimple() {
    // aaa -> {bbb, ccc}, bbb -> ddd.
    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} {call .ddd /}\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 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 5 with incorporateCalleeVisitInfo case 1 (aaa -> bbb).
    FindTransitiveDepTemplatesVisitor visitor = new FindTransitiveDepTemplatesVisitor(templateRegistry);
    Map<TemplateNode, TransitiveDepTemplatesInfo> memoizedInfoMap = visitor.templateToFinishedInfoMap;
    visitor.exec(aaa);
    assertThat(memoizedInfoMap).hasSize(4);
    assertThat(memoizedInfoMap.get(ddd).depTemplateSet).isEqualTo(ImmutableSet.of(ddd));
    assertThat(memoizedInfoMap.get(ccc).depTemplateSet).isEqualTo(ImmutableSet.of(ccc));
    assertThat(memoizedInfoMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb, ddd));
    assertThat(memoizedInfoMap.get(aaa).depTemplateSet).isEqualTo(ImmutableSet.of(aaa, bbb, ccc, ddd));
    // Test with exec(bbb) then exec(aaa).
    // Exercises: processCalleeHelper case 1 (aaa -> bbb).
    visitor = new FindTransitiveDepTemplatesVisitor(templateRegistry);
    memoizedInfoMap = visitor.templateToFinishedInfoMap;
    visitor.exec(bbb);
    assertThat(memoizedInfoMap).hasSize(2);
    assertThat(memoizedInfoMap.get(ddd).depTemplateSet).isEqualTo(ImmutableSet.of(ddd));
    assertThat(memoizedInfoMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb, ddd));
    visitor.exec(aaa);
    assertThat(memoizedInfoMap).hasSize(4);
    assertThat(memoizedInfoMap.get(ddd).depTemplateSet).isEqualTo(ImmutableSet.of(ddd));
    assertThat(memoizedInfoMap.get(ccc).depTemplateSet).isEqualTo(ImmutableSet.of(ccc));
    assertThat(memoizedInfoMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb, ddd));
    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 100 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.

the class FindCalleesNotInFileVisitorTest method testFindCalleesNotInFile.

@Test
public void testFindCalleesNotInFile() {
    String testFileContent = "" + "{namespace boo.foo}\n" + "\n" + "/** Test template 1. */\n" + "{template .goo autoescape=\"deprecated-noncontextual\"}\n" + "  {call .goo data=\"all\" /}\n" + "  {call .moo data=\"all\" /}\n" + "  {call boo.woo.hoo data=\"all\" /}\n" + // not defined in this file
    "{/template}\n" + "\n" + "/** Test template 2. */\n" + "{template .moo autoescape=\"deprecated-noncontextual\"}\n" + "  {for $i in range(8)}\n" + "    {call boo.foo.goo data=\"all\" /}\n" + "    {call .too data=\"all\" /}\n" + // not defined in this file
    "    {call .goo}" + "      {param a}{call .moo /}{/param}" + "      {param b}{call .zoo /}{/param}" + // not defined in this file
    "    {/call}" + "  {/for}\n" + "{/template}\n" + "\n" + "/** Test template 3. */\n" + "{deltemplate booHoo autoescape=\"deprecated-noncontextual\"}\n" + "  {call .goo data=\"all\" /}\n" + "  {call .moo data=\"all\" /}\n" + "  {call boo.hoo.roo data=\"all\" /}\n" + // not defined in this file
    "{/deltemplate}\n";
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(testFileContent).errorReporter(boom).parse().fileSet();
    SoyFileNode soyFile = soyTree.getChild(0);
    Iterable<String> calleesNotInFile = Iterables.transform(new FindCalleesNotInFileVisitor().exec(soyFile), CallBasicNode::getCalleeName);
    assertThat(calleesNotInFile).containsExactly("boo.woo.hoo", "boo.foo.too", "boo.foo.zoo", "boo.hoo.roo");
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) CallBasicNode(com.google.template.soy.soytree.CallBasicNode) SoyFileNode(com.google.template.soy.soytree.SoyFileNode) Test(org.junit.Test)

Aggregations

SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)106 Test (org.junit.Test)81 TemplateNode (com.google.template.soy.soytree.TemplateNode)35 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)29 ErrorReporter (com.google.template.soy.error.ErrorReporter)19 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)13 TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)8 RawTextNode (com.google.template.soy.soytree.RawTextNode)8 MsgNode (com.google.template.soy.soytree.MsgNode)7 SoyNode (com.google.template.soy.soytree.SoyNode)6 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)5 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)5 PrintNode (com.google.template.soy.soytree.PrintNode)5 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)4 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)4 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)3 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)3 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)3 PluginResolver (com.google.template.soy.soyparse.PluginResolver)3 ImmutableList (com.google.common.collect.ImmutableList)2