Search in sources :

Example 16 with ParseResult

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

the class RenderVisitorTest method testRenderDelegateCallWithoutDefault.

@Test
public void testRenderDelegateCallWithoutDefault() throws Exception {
    String soyFileContent1a = "{namespace ns1}\n" + "\n" + "/***/\n" + "{template .callerTemplate}\n" + "  {delcall myApp.myDelegate allowemptydefault=\"true\"}\n" + "    {param boo: 'aaaaaah' /}\n" + "  {/delcall}\n" + "{/template}\n";
    String soyFileContent1b = "{namespace ns1}\n" + "\n" + "/***/\n" + "{template .callerTemplate}\n" + "  {delcall myApp.myDelegate}\n" + "    {param boo: 'aaaaaah' /}\n" + "  {/delcall}\n" + "{/template}\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";
    SoyRecord data = SoyValueConverterUtility.newDict();
    ParseResult parseResult;
    // ------ Test with only file 1a in bundle. ------
    parseResult = SoyFileSetParserBuilder.forFileContents(soyFileContent1a).parse();
    Predicate<String> activeDelPackageNames = Predicates.alwaysFalse();
    assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEmpty();
    activeDelPackageNames = Predicates.equalTo("SecretFeature");
    assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEmpty();
    // ------ Test with both files 1a and 2 in bundle. ------
    parseResult = SoyFileSetParserBuilder.forFileContents(soyFileContent1a, soyFileContent2).parse();
    activeDelPackageNames = Predicates.alwaysFalse();
    assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEmpty();
    activeDelPackageNames = Predicates.equalTo("SecretFeature");
    assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEqualTo("111 aaaaaah");
    activeDelPackageNames = Predicates.equalTo("NonexistentFeature");
    assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEmpty();
    activeDelPackageNames = Predicates.in(ImmutableSet.of("NonexistentFeature", "SecretFeature"));
    assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEqualTo("111 aaaaaah");
    // ------ Test with only file 1b in bundle. ------
    activeDelPackageNames = Predicates.alwaysFalse();
    try {
        renderTemplateInFile(SoyFileSetParserBuilder.forFileContents(soyFileContent1b).parse(), "ns1.callerTemplate", data, null, activeDelPackageNames);
        fail("expected RenderException");
    } catch (RenderException e) {
        assertThat(e).hasMessageThat().contains("Found no active impl for delegate call");
    }
    try {
        renderTemplateInFile(SoyFileSetParserBuilder.forFileContents(soyFileContent1b, soyFileContent2).parse(), "ns1.callerTemplate", data, null, activeDelPackageNames);
        fail("expected RenderException");
    } catch (RenderException e) {
        assertThat(e).hasMessageThat().contains("Found no active impl for delegate call");
    }
    activeDelPackageNames = Predicates.equalTo("SecretFeature");
    assertThat(renderTemplateInFile(SoyFileSetParserBuilder.forFileContents(soyFileContent1b, soyFileContent2).parse(), "ns1.callerTemplate", data, null, activeDelPackageNames)).isEqualTo("111 aaaaaah");
}
Also used : SoyRecord(com.google.template.soy.data.SoyRecord) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) Test(org.junit.Test)

Example 17 with ParseResult

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

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

Example 19 with ParseResult

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

the class FindTransitiveDepTemplatesVisitorTest method testLargerRecursiveCycle.

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

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

the class VeLogInstrumentationVisitorTest method runPass.

/**
 * Parses the given input as a template content.
 */
private static SoyFileSetNode runPass(String input) {
    String soyFile = Joiner.on('\n').join("{namespace ns}", "", "{template .t}", input, "{/template}");
    ParseResult result = SoyFileSetParserBuilder.forFileContents(soyFile).desugarHtmlNodes(false).typeRegistry(new SoyTypeRegistry.Builder().addDescriptors(ImmutableList.of(com.google.template.soy.testing.Foo.getDescriptor())).build()).setLoggingConfig(LOGGING_CONFIG).addSoyFunction(new TestLoggingFunction()).errorReporter(ErrorReporter.exploding()).parse();
    TemplateRegistry templateRegistry = result.registry();
    SoyFileSetNode soyTree = result.fileSet();
    new VeLogInstrumentationVisitor(templateRegistry).exec(soyTree);
    return soyTree;
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyTypeRegistry(com.google.template.soy.types.SoyTypeRegistry) ParseResult(com.google.template.soy.SoyFileSetParser.ParseResult) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode)

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