Search in sources :

Example 11 with ParseResult

use of com.google.template.soy.SoyFileSetParser.ParseResult 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 12 with ParseResult

use of com.google.template.soy.SoyFileSetParser.ParseResult 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 13 with ParseResult

use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.

the class PrerenderVisitorTest method prerender.

// -----------------------------------------------------------------------------------------------
// Helpers.
/**
 * Renders the given input string (should be a template body) and returns the result.
 *
 * @param input The input string to prerender.
 * @return The rendered result.
 * @throws Exception If there's an error.
 */
private String prerender(String input) throws Exception {
    ParseResult result = SoyFileSetParserBuilder.forTemplateContents(input).parse();
    StringBuilder outputSb = new StringBuilder();
    PrerenderVisitor prerenderVisitor = new PrerenderVisitor(new PreevalVisitorFactory(), outputSb, result.registry());
    prerenderVisitor.exec(result.fileSet().getChild(0).getChild(0));
    return outputSb.toString();
}
Also used : ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult)

Example 14 with ParseResult

use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.

the class SimplifyVisitorTest method simplifySoyCode.

private List<StandaloneNode> simplifySoyCode(String soyCode) throws Exception {
    ParseResult parse = SoyFileSetParserBuilder.forTemplateContents(soyCode).parse();
    SimplifyVisitor simplifyVisitor = SimplifyVisitor.create();
    simplifyVisitor.simplify(parse.fileSet(), parse.registry());
    return parse.fileSet().getChild(0).getChild(0).getChildren();
}
Also used : ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult)

Example 15 with ParseResult

use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.

the class RenderVisitorTest method testRenderDelegateVariantCall.

@Test
public void testRenderDelegateVariantCall() throws Exception {
    String soyFileContent1 = "" + "{namespace ns1}\n" + "\n" + "/** @param greekB */\n" + "{template .callerTemplate}\n" + "  {delcall myApp.myDelegate variant=\"'alpha'\"}\n" + // variant is string
    "    {param boo: 'zzz' /}\n" + "  {/delcall}\n" + "  {delcall myApp.myDelegate variant=\"$greekB\"}\n" + // variant is expression
    "    {param boo: 'zzz' /}\n" + "  {/delcall}\n" + "  {delcall myApp.myDelegate variant=\"'gamma'\"}\n" + // variant "gamma" not implemented
    "    {param boo: 'zzz' /}\n" + "  {/delcall}\n" + "  {delcall myApp.myDelegate variant=\"test.GLOBAL\"}\n" + // variant is a global expression
    "    {param boo: 'zzz' /}\n" + "  {/delcall}\n" + "{/template}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate}\n" + // variant "" default
    "  000empty\n" + "{/deltemplate}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"'alpha'\"}\n" + // variant "alpha" default
    "  000alpha\n" + "{/deltemplate}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"'beta'\"}\n" + // variant "beta" default
    "  000beta\n" + "{/deltemplate}\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"test.GLOBAL\"}\n" + // variant using global
    "  000global\n" + "{/deltemplate}\n";
    String soyFileContent2 = "" + "{delpackage SecretFeature}\n" + "{namespace ns2}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate}\n" + // variant "" in SecretFeature
    "  111empty\n" + "{/deltemplate}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"'alpha'\"}\n" + // "alpha" in SecretFeature
    "  111alpha\n" + "{/deltemplate}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"'beta'\"}\n" + // "beta" in SecretFeature
    "  111beta\n" + "{/deltemplate}\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"test.GLOBAL\"}\n" + // variant using global
    "  111global\n" + "{/deltemplate}\n";
    String soyFileContent3 = "" + "{delpackage AlternateSecretFeature}\n" + "{namespace ns3}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate}\n" + // variant "" in AlternateSecretFeature
    "  222empty\n" + "{/deltemplate}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"'alpha'\"}\n" + // variant "alpha" in Alternate
    "  222alpha\n" + "{/deltemplate}\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"test.GLOBAL\"}\n" + // variant using global
    "  222global\n" + // Note: No variant "beta" in AlternateSecretFeature.
    "{/deltemplate}\n";
    SoyGeneralOptions options = new SoyGeneralOptions();
    options.setCompileTimeGlobals(ImmutableMap.<String, Object>of("test.GLOBAL", 1));
    ParseResult result = SoyFileSetParserBuilder.forFileContents(soyFileContent1, soyFileContent2, soyFileContent3).options(options).errorReporter(FAIL).parse();
    Predicate<String> activeDelPackageNames = Predicates.alwaysFalse();
    assertThat(renderTemplateInFile(result, "ns1.callerTemplate", TEST_DATA, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("000alpha000beta000empty000global");
    activeDelPackageNames = Predicates.equalTo("SecretFeature");
    assertThat(renderTemplateInFile(result, "ns1.callerTemplate", TEST_DATA, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("111alpha111beta111empty111global");
    activeDelPackageNames = Predicates.equalTo("AlternateSecretFeature");
    assertThat(renderTemplateInFile(result, "ns1.callerTemplate", TEST_DATA, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("222alpha000beta222empty222global");
    activeDelPackageNames = Predicates.equalTo("NonexistentFeature");
    assertThat(renderTemplateInFile(result, "ns1.callerTemplate", TEST_DATA, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("000alpha000beta000empty000global");
    activeDelPackageNames = Predicates.in(ImmutableSet.of("NonexistentFeature", "AlternateSecretFeature"));
    assertThat(renderTemplateInFile(result, "ns1.callerTemplate", TEST_DATA, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("222alpha000beta222empty222global");
    try {
        renderTemplateInFile(result, "ns1.callerTemplate", TEST_DATA, TEST_IJ_DATA, Predicates.in(ImmutableSet.of("SecretFeature", "AlternateSecretFeature")));
        fail("expected RenderException");
    } catch (RenderException e) {
        assertThat(e).hasMessageThat().contains("For delegate template 'myApp.myDelegate:alpha', found two active implementations " + "with equal priority");
    }
}
Also used : ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) SoyGeneralOptions(com.google.template.soy.shared.SoyGeneralOptions) Test(org.junit.Test)

Aggregations

ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)38 Test (org.junit.Test)21 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)13 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)13 TemplateNode (com.google.template.soy.soytree.TemplateNode)10 TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)8 SoyRecord (com.google.template.soy.data.SoyRecord)2 IncrementalDomSrcMain (com.google.template.soy.incrementaldomsrc.IncrementalDomSrcMain)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 SoyTypeRegistry (com.google.template.soy.types.SoyTypeRegistry)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Injector (com.google.inject.Injector)1 SoyFileSetParserBuilder (com.google.template.soy.SoyFileSetParserBuilder)1 SoyModule (com.google.template.soy.SoyModule)1 UniqueNameGenerator (com.google.template.soy.base.internal.UniqueNameGenerator)1 CopyState (com.google.template.soy.basetree.CopyState)1 SyntaxVersion (com.google.template.soy.basetree.SyntaxVersion)1