Search in sources :

Example 36 with ParseResult

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

the class RenderVisitorTest method testRenderDelegateCall.

@Test
public void testRenderDelegateCall() throws Exception {
    String soyFileContent1 = "{namespace ns1}\n" + "\n" + "/***/\n" + "{template .callerTemplate}\n" + "  {delcall myApp.myDelegate}\n" + "    {param boo: 'aaaaaah' /}\n" + "  {/delcall}\n" + "{/template}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate}\n" + // default implementation (doesn't use $boo)
    "  000\n" + "{/deltemplate}\n";
    String soyFileContent2 = "{delpackage SecretFeature}\n" + "{namespace ns2}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate}\n" + // implementation in SecretFeature
    "  111 {$boo}\n" + "{/deltemplate}\n";
    String soyFileContent3 = "{delpackage AlternateSecretFeature}\n" + "{namespace ns3}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate}\n" + // implementation in AlternateSecretFeature
    "  222 {call .helper data=\"all\" /}\n" + "{/deltemplate}\n";
    String soyFileContent4 = "{delpackage AlternateSecretFeature}\n" + "{namespace ns3}\n" + "\n" + "/** @param boo */\n" + "{template .helper}\n" + "  {$boo} {$ij.ijStr}\n" + "{/template}\n";
    final ParseResult parseResult = SoyFileSetParserBuilder.forFileContents(soyFileContent1, soyFileContent2, soyFileContent3, soyFileContent4).errorReporter(FAIL).parse();
    final SoyRecord data = SoyValueConverterUtility.newDict();
    Predicate<String> activeDelPackageNames = Predicates.alwaysFalse();
    assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("000");
    activeDelPackageNames = Predicates.equalTo("SecretFeature");
    assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("111 aaaaaah");
    activeDelPackageNames = Predicates.equalTo("AlternateSecretFeature");
    assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("222 aaaaaah injected");
    activeDelPackageNames = Predicates.equalTo("NonexistentFeature");
    assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("000");
    activeDelPackageNames = Predicates.in(ImmutableSet.of("NonexistentFeature", "AlternateSecretFeature"));
    assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("222 aaaaaah injected");
    try {
        renderTemplateInFile(parseResult, "ns1.callerTemplate", 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', found two active implementations with" + " equal priority");
    }
}
Also used : SoyRecord(com.google.template.soy.data.SoyRecord) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) Test(org.junit.Test)

Example 37 with ParseResult

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

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

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