Search in sources :

Example 26 with TemplateRegistry

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

the class RenderVisitorTest method testRenderFuture.

@Test
public void testRenderFuture() throws Exception {
    final StringBuilder progress = new StringBuilder();
    Flushable flushable = new Flushable() {

        @Override
        public void flush() {
            progress.append("flush;");
        }
    };
    String soyFileContent = "{namespace ns}\n" + "\n" + "/** @param boo @param foo @param goo */\n" + "{template .callerTemplate autoescape=\"deprecated-noncontextual\"}\n" + "  {call .calleeTemplate data=\"all\" /}\n" + "  {call .calleeTemplate data=\"$foo\" /}\n" + "  {call .calleeTemplate data=\"all\"}\n" + "    {param boo: $foo.boo /}\n" + "  {/call}\n" + "  {call .calleeTemplate data=\"all\"}\n" + "    {param boo: 'moo' /}\n" + "  {/call}\n" + "  {call .calleeTemplate data=\"$foo\"}\n" + "    {param boo}moo{/param}\n" + "  {/call}\n" + "  {call .calleeTemplate}\n" + "    {param boo}zoo{/param}\n" + "    {param goo: $foo.goo /}\n" + "  {/call}\n" + "{/template}\n" + "\n" + "/**\n" + " * @param boo\n" + " * @param goo\n" + " */\n" + "{template .calleeTemplate autoescape=\"deprecated-noncontextual\"}\n" + "  {$boo}{$ij.future}\n" + "  {for $n in $goo} {$n}{/for}{\\n}\n" + "{/template}\n";
    TemplateRegistry templateRegistry = SoyFileSetParserBuilder.forFileContents(soyFileContent).errorReporter(FAIL).parse().registry();
    SoyDict foo = SoyValueConverterUtility.newDict("boo", new TestFuture("foo", progress), "goo", SoyValueConverterUtility.newList(3, 2, 1));
    SoyDict data = SoyValueConverterUtility.newDict("boo", new TestFuture("boo", progress), "foo", foo, "goo", SoyValueConverterUtility.newList(1, 2, 3));
    SoyRecord testIj = SoyValueConverterUtility.newDict("future", new TestFuture("ij", progress));
    StringBuilder outputSb = new StringBuilder();
    CountingFlushableAppendable output = new CountingFlushableAppendable(outputSb, flushable);
    RenderVisitor rv = new RenderVisitor(new EvalVisitorFactoryImpl(), output, templateRegistry, data, testIj, Predicates.<String>alwaysFalse(), null, xidRenamingMap, cssRenamingMap, false);
    rv.exec(templateRegistry.getBasicTemplate("ns.callerTemplate"));
    String expectedOutput = "booij 1 2 3\n" + "fooij 3 2 1\n" + "fooij 1 2 3\n" + "mooij 1 2 3\n" + "mooij 3 2 1\n" + "zooij 3 2 1\n";
    assertThat(outputSb.toString()).isEqualTo(expectedOutput);
    assertThat(progress.toString()).isEqualTo("booflush;ijflush;foo");
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyRecord(com.google.template.soy.data.SoyRecord) SoyDict(com.google.template.soy.data.SoyDict) Flushable(java.io.Flushable) Test(org.junit.Test)

Example 27 with TemplateRegistry

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

the class AddHtmlCommentsForDebugPassTest method runPass.

/**
 * Parses the given input as a Soy file content, runs the AddHtmlCommentsForDebug pass and returns
 * a map that contains pairs of template names and rewritten template body.
 */
private static ImmutableMap<String, String> runPass(String input, String fileName) {
    String soyFile = "{namespace ns}" + input;
    IncrementingIdGenerator nodeIdGen = new IncrementingIdGenerator();
    ErrorReporter boom = ErrorReporter.exploding();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forSuppliers(SoyFileSupplier.Factory.create(soyFile, SoyFileKind.SRC, fileName)).errorReporter(boom).parse().fileSet();
    TemplateRegistry registry = new TemplateRegistry(soyTree, boom);
    // We need to run HtmlRewritePass to produce HTML nodes. Otherwise we will not add any comments.
    new HtmlRewritePass(boom).run(soyTree.getChild(0), nodeIdGen);
    new AddHtmlCommentsForDebugPass().run(soyTree, registry);
    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    for (TemplateNode node : soyTree.getChild(0).getChildren()) {
        StringBuilder sb = new StringBuilder();
        node.appendSourceStringForChildren(sb);
        String templateName = (node instanceof TemplateDelegateNode) ? ((TemplateDelegateNode) node).getDelTemplateName() : node.getTemplateName();
        builder.put(templateName, sb.toString());
    }
    return builder.build();
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateDelegateNode(com.google.template.soy.soytree.TemplateDelegateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) IncrementingIdGenerator(com.google.template.soy.base.internal.IncrementingIdGenerator) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 28 with TemplateRegistry

use of com.google.template.soy.soytree.TemplateRegistry 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 29 with TemplateRegistry

use of com.google.template.soy.soytree.TemplateRegistry 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 30 with TemplateRegistry

use of com.google.template.soy.soytree.TemplateRegistry 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)

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