Search in sources :

Example 1 with ForIfemptyNode

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

the class TemplateParserTest method testParseForeachStmt.

@Test
public void testParseForeachStmt() throws Exception {
    String templateBody = "{@param goose : ?}{@param foo: ?}\n" + "  {for $goo in $goose}\n" + "    {$goose.numKids} goslings.{\\n}\n" + "  {/for}\n" + "  {for $boo in $foo.booze}\n" + "    Scary drink {$boo.name}!\n" + "    {if not isLast($boo)}{\\n}{/if}\n" + "  {ifempty}\n" + "    Sorry, no booze.\n" + "  {/for}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(2, nodes.size());
    ForNode fn0 = (ForNode) nodes.get(0);
    assertEquals("$goose", fn0.getExpr().toSourceString());
    assertTrue(fn0.getExpr().getRoot() instanceof VarRefNode);
    assertEquals(1, fn0.numChildren());
    ForNonemptyNode fn0fnn0 = (ForNonemptyNode) fn0.getChild(0);
    assertEquals("goo", fn0fnn0.getVarName());
    assertEquals(2, fn0fnn0.numChildren());
    assertEquals("$goose.numKids", ((PrintNode) fn0fnn0.getChild(0)).getExpr().toSourceString());
    assertEquals(" goslings.\n", ((RawTextNode) fn0fnn0.getChild(1)).getRawText());
    ForNode fn1 = (ForNode) nodes.get(1);
    assertEquals("$foo.booze", fn1.getExpr().toSourceString());
    assertTrue(fn1.getExpr().getRoot() instanceof FieldAccessNode);
    assertEquals(2, fn1.numChildren());
    ForNonemptyNode fn1fnn0 = (ForNonemptyNode) fn1.getChild(0);
    assertEquals("boo", fn1fnn0.getVarName());
    assertEquals("$foo.booze", fn1fnn0.getExpr().toSourceString());
    assertEquals("boo", fn1fnn0.getVarName());
    assertEquals(4, fn1fnn0.numChildren());
    IfNode fn1fnn0in = (IfNode) fn1fnn0.getChild(3);
    assertEquals(1, fn1fnn0in.numChildren());
    assertEquals("not isLast($boo)", ((IfCondNode) fn1fnn0in.getChild(0)).getExpr().toSourceString());
    ForIfemptyNode fn1fin1 = (ForIfemptyNode) fn1.getChild(1);
    assertEquals(1, fn1fin1.numChildren());
    assertEquals("Sorry, no booze.", ((RawTextNode) fn1fin1.getChild(0)).getRawText());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) ForNode(com.google.template.soy.soytree.ForNode) IfCondNode(com.google.template.soy.soytree.IfCondNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) IfNode(com.google.template.soy.soytree.IfNode) PrintNode(com.google.template.soy.soytree.PrintNode) FieldAccessNode(com.google.template.soy.exprtree.FieldAccessNode) ForIfemptyNode(com.google.template.soy.soytree.ForIfemptyNode) ForNonemptyNode(com.google.template.soy.soytree.ForNonemptyNode) Test(org.junit.Test)

Aggregations

FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)1 VarRefNode (com.google.template.soy.exprtree.VarRefNode)1 ForIfemptyNode (com.google.template.soy.soytree.ForIfemptyNode)1 ForNode (com.google.template.soy.soytree.ForNode)1 ForNonemptyNode (com.google.template.soy.soytree.ForNonemptyNode)1 IfCondNode (com.google.template.soy.soytree.IfCondNode)1 IfNode (com.google.template.soy.soytree.IfNode)1 PrintNode (com.google.template.soy.soytree.PrintNode)1 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)1 Test (org.junit.Test)1