Search in sources :

Example 1 with LetValueNode

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

the class TemplateParserTest method testParseLetStmt.

@Test
public void testParseLetStmt() throws Exception {
    String templateBody = "{@param boo : ?}\n" + "  {let $alpha: $boo.foo /}\n" + "  {let $beta kind=\"html\"}Boo!{/let}\n" + "  {let $gamma kind=\"html\"}\n" + "    {for $i in range($alpha)}\n" + "      {$i}{$beta}\n" + "    {/for}\n" + "  {/let}\n" + "  {let $delta kind=\"html\"}Boo!{/let}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(4, nodes.size());
    LetValueNode alphaNode = (LetValueNode) nodes.get(0);
    assertEquals("alpha", alphaNode.getVarName());
    assertEquals("$boo.foo", alphaNode.getExpr().toSourceString());
    LetContentNode betaNode = (LetContentNode) nodes.get(1);
    assertEquals("beta", betaNode.getVarName());
    assertEquals("Boo!", ((RawTextNode) betaNode.getChild(0)).getRawText());
    assertEquals(SanitizedContentKind.HTML, betaNode.getContentKind());
    LetContentNode gammaNode = (LetContentNode) nodes.get(2);
    assertEquals("gamma", gammaNode.getVarName());
    assertThat(gammaNode.getChild(0)).isInstanceOf(ForNode.class);
    assertEquals(SanitizedContentKind.HTML, gammaNode.getContentKind());
    LetContentNode deltaNode = (LetContentNode) nodes.get(3);
    assertEquals("delta", deltaNode.getVarName());
    assertEquals("Boo!", ((RawTextNode) betaNode.getChild(0)).getRawText());
    assertEquals(SanitizedContentKind.HTML, deltaNode.getContentKind());
    // Test error case.
    TemplateSubject.assertThatTemplateContent("{let $alpha /}{/let}").causesError("parse error at '/}': expected }, ':', or identifier").at(1, 13);
    // Test error case.
    TemplateSubject.assertThatTemplateContent("{let $alpha: $boo.foo}{/let}").causesError("parse error at '}': expected /}, ?, '?:', or, and, ==, !=, <, >, <=, >=, +, -, *, /, " + "%, ., ?., [, or ?[").at(1, 22);
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) LetValueNode(com.google.template.soy.soytree.LetValueNode) LetContentNode(com.google.template.soy.soytree.LetContentNode) Test(org.junit.Test)

Example 2 with LetValueNode

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

the class ResolveNamesVisitorTest method testMultipleLocals.

@Test
public void testMultipleLocals() {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(constructTemplateSource("{let $la: 1 /}", "{let $lb: $la /}", "{let $lc: $lb /}")).parse().fileSet();
    new ResolveNamesVisitor(ErrorReporter.exploding()).exec(soyTree);
    TemplateNode n = soyTree.getChild(0).getChild(0);
    // 3 because each new $la binding is a 'new variable'
    assertThat(n.getMaxLocalVariableTableSize()).isEqualTo(3);
    LetValueNode firstLet = (LetValueNode) n.getChild(0);
    LetValueNode secondLet = (LetValueNode) n.getChild(1);
    LetValueNode thirdLet = (LetValueNode) n.getChild(2);
    assertThat(firstLet.getVar().localVariableIndex()).isEqualTo(0);
    assertThat(secondLet.getVar().localVariableIndex()).isEqualTo(1);
    assertThat(thirdLet.getVar().localVariableIndex()).isEqualTo(2);
    assertThat(((VarRefNode) secondLet.getExpr().getRoot()).getDefnDecl()).isEqualTo(firstLet.getVar());
    assertThat(((VarRefNode) thirdLet.getExpr().getRoot()).getDefnDecl()).isEqualTo(secondLet.getVar());
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) LetValueNode(com.google.template.soy.soytree.LetValueNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Test(org.junit.Test)

Example 3 with LetValueNode

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

Aggregations

LetValueNode (com.google.template.soy.soytree.LetValueNode)3 Test (org.junit.Test)3 LetContentNode (com.google.template.soy.soytree.LetContentNode)2 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)2 TemplateNode (com.google.template.soy.soytree.TemplateNode)2 VarRefNode (com.google.template.soy.exprtree.VarRefNode)1 IfNode (com.google.template.soy.soytree.IfNode)1 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)1