Search in sources :

Example 86 with SoyFileSetNode

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

the class ResolveNamesVisitorTest method testLetContentSlotLifetime.

@Test
public void testLetContentSlotLifetime() {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(constructTemplateSource("{let $a kind=\"text\"}", // introduce an extra scope
    "  {if true}", "    {let $b: 2 /}", "    {$b}", "  {/if}", "{/let}", "{$a}")).parse().fileSet();
    new ResolveNamesVisitor(ErrorReporter.exploding()).exec(soyTree);
    TemplateNode n = soyTree.getChild(0).getChild(0);
    // 1 because each new $la binding overwrites the prior one
    assertThat(n.getMaxLocalVariableTableSize()).isEqualTo(2);
    LetContentNode aLetNode = (LetContentNode) n.getChild(0);
    assertThat(aLetNode.getVar().localVariableIndex()).isEqualTo(1);
    LetValueNode bLetNode = (LetValueNode) ((IfCondNode) ((IfNode) aLetNode.getChild(0)).getChild(0)).getChild(0);
    assertThat(bLetNode.getVar().localVariableIndex()).isEqualTo(0);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) LetValueNode(com.google.template.soy.soytree.LetValueNode) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) IfNode(com.google.template.soy.soytree.IfNode) LetContentNode(com.google.template.soy.soytree.LetContentNode) Test(org.junit.Test)

Example 87 with SoyFileSetNode

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

the class ResolveNamesVisitorTest method testLetNameLookupSuccess.

@Test
public void testLetNameLookupSuccess() {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(constructTemplateSource("{let $pa: 1 /}", "{$pa}")).parse().fileSet();
    new ResolveNamesVisitor(ErrorReporter.exploding()).exec(soyTree);
    TemplateNode n = soyTree.getChild(0).getChild(0);
    assertThat(n.getMaxLocalVariableTableSize()).isEqualTo(1);
    assertThat(((LetValueNode) n.getChild(0)).getVar().localVariableIndex()).isEqualTo(0);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Test(org.junit.Test)

Example 88 with SoyFileSetNode

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

the class ContextualAutoescaperTest method rewrite.

public SoyFileNode rewrite(String... inputs) {
    ErrorReporter reporter = ErrorReporter.createForTest();
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(inputs).errorReporter(reporter).allowUnboundGlobals(true).addPrintDirectives(SOY_PRINT_DIRECTIVES).runAutoescaper(true).parse().fileSet();
    if (!reporter.getErrors().isEmpty()) {
        SoyError soyError = reporter.getErrors().get(0);
        String message = soyError.message();
        if (message.startsWith(ContextualAutoescaper.AUTOESCAPE_ERROR_PREFIX)) {
            // Grab the part after the prefix (and the "- " used for indentation).
            message = message.substring(ContextualAutoescaper.AUTOESCAPE_ERROR_PREFIX.length() + 2);
            // reporter, we can stop throwing and simply add explicit checks in the cases.
            throw new RewriteError(soyError, message);
        } else {
            throw new IllegalStateException("Unexpected error: " + message);
        }
    }
    return soyTree.getChild(0);
}
Also used : ErrorReporter(com.google.template.soy.error.ErrorReporter) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) SoyError(com.google.template.soy.error.SoyError)

Example 89 with SoyFileSetNode

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

the class PerformDeprecatedNonContextualAutoescapeVisitorTest method testAutoescapeOnWithEscapeHtml.

@Test
public void testAutoescapeOnWithEscapeHtml() throws Exception {
    String testPrintTags = "{'<br>' |escapeHtml}{'<br>' |noAutoescape |escapeHtml}{'<br>' |escapeHtml |noAutoescape}";
    SoyFileSetNode soyTree = parseTestPrintTagsHelper(testPrintTags);
    List<PrintNode> printNodes = SoyTreeUtils.getAllNodesOfType(soyTree, PrintNode.class);
    // Before.
    assertThat(printNodes.get(0).getChildren()).hasSize(1);
    assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo(EscapeHtmlDirective.NAME);
    assertThat(printNodes.get(1).getChildren()).hasSize(2);
    assertThat(printNodes.get(1).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    assertThat(printNodes.get(1).getChild(1).getName()).isEqualTo(EscapeHtmlDirective.NAME);
    assertThat(printNodes.get(2).getChildren()).hasSize(2);
    assertThat(printNodes.get(2).getChild(0).getName()).isEqualTo(EscapeHtmlDirective.NAME);
    assertThat(printNodes.get(2).getChild(1).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    performAutoescape(soyTree);
    // After. Note that noAutoescape remains to filter against ContentKind.TEXT.
    assertThat(printNodes.get(0).getChildren()).hasSize(1);
    assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo(EscapeHtmlDirective.NAME);
    assertThat(printNodes.get(1).getChildren()).hasSize(2);
    assertThat(printNodes.get(1).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    assertThat(printNodes.get(1).getChild(1).getName()).isEqualTo(EscapeHtmlDirective.NAME);
    assertThat(printNodes.get(2).getChildren()).hasSize(2);
    assertThat(printNodes.get(2).getChild(0).getName()).isEqualTo(EscapeHtmlDirective.NAME);
    assertThat(printNodes.get(2).getChild(1).getName()).isEqualTo(NoAutoescapeDirective.NAME);
}
Also used : SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Example 90 with SoyFileSetNode

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

the class PerformDeprecatedNonContextualAutoescapeVisitorTest method testAutoescapeOnWithNoAutoescape.

@Test
public void testAutoescapeOnWithNoAutoescape() throws Exception {
    String testPrintTags = "{'<br>' |noAutoescape}{'<br>' |noAutoescape |noAutoescape}";
    SoyFileSetNode soyTree = parseTestPrintTagsHelper(testPrintTags);
    List<PrintNode> printNodes = SoyTreeUtils.getAllNodesOfType(soyTree, PrintNode.class);
    // Before.
    assertThat(printNodes.get(0).getChildren()).hasSize(1);
    assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    assertThat(printNodes.get(1).getChildren()).hasSize(2);
    assertThat(printNodes.get(1).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    assertThat(printNodes.get(1).getChild(1).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    performAutoescape(soyTree);
    // After. Note that noAutoescape remains to filter against ContentKind.TEXT.
    assertThat(printNodes.get(0).getChildren()).hasSize(1);
    assertThat(printNodes.get(0).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    assertThat(printNodes.get(1).getChildren()).hasSize(2);
    assertThat(printNodes.get(1).getChild(0).getName()).isEqualTo(NoAutoescapeDirective.NAME);
    assertThat(printNodes.get(1).getChild(1).getName()).isEqualTo(NoAutoescapeDirective.NAME);
}
Also used : SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) PrintNode(com.google.template.soy.soytree.PrintNode) 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