Search in sources :

Example 1 with SwitchNode

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

the class TemplateParserTest method testParseSwitchStmt.

@Test
public void testParseSwitchStmt() throws Exception {
    String templateBody = "{@param boo: ?}{@param foo : ?}{@param moo : ?}\n" + "  {switch $boo} {case 0}Blah\n" + "    {case $foo.goo}\n" + "      Bleh\n" + "    {case -1, 1, $moo}\n" + "      Bluh\n" + "    {default}\n" + "      Bloh\n" + "  {/switch}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(1, nodes.size());
    SwitchNode sn = (SwitchNode) nodes.get(0);
    assertEquals("$boo", sn.getExpr().toSourceString());
    assertTrue(sn.getExpr().getRoot() instanceof VarRefNode);
    assertEquals(4, sn.numChildren());
    SwitchCaseNode scn0 = (SwitchCaseNode) sn.getChild(0);
    assertEquals(1, scn0.getExprList().size());
    assertTrue(scn0.getExprList().get(0).getRoot() instanceof IntegerNode);
    assertEquals(0, ((IntegerNode) scn0.getExprList().get(0).getRoot()).getValue());
    SwitchCaseNode scn1 = (SwitchCaseNode) sn.getChild(1);
    assertEquals(1, scn1.getExprList().size());
    assertTrue(scn1.getExprList().get(0).getRoot() instanceof FieldAccessNode);
    assertEquals("$foo.goo", scn1.getExprList().get(0).getRoot().toSourceString());
    SwitchCaseNode scn2 = (SwitchCaseNode) sn.getChild(2);
    assertEquals(3, scn2.getExprList().size());
    assertTrue(scn2.getExprList().get(0).getRoot() instanceof IntegerNode);
    assertTrue(scn2.getExprList().get(1).getRoot() instanceof IntegerNode);
    assertTrue(scn2.getExprList().get(2).getRoot() instanceof VarRefNode);
    assertEquals("-1", scn2.getExprList().get(0).getRoot().toSourceString());
    assertEquals("1", scn2.getExprList().get(1).getRoot().toSourceString());
    assertEquals("$moo", scn2.getExprList().get(2).getRoot().toSourceString());
    assertEquals("Bluh", ((RawTextNode) scn2.getChild(0)).getRawText());
    assertEquals("Bloh", ((RawTextNode) ((SwitchDefaultNode) sn.getChild(3)).getChild(0)).getRawText());
    assertEquals("{switch $boo}{case 0}Blah{case $foo.goo}Bleh{case -1, 1, $moo}Bluh{default}Bloh{/switch}", sn.toSourceString());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) IntegerNode(com.google.template.soy.exprtree.IntegerNode) SwitchCaseNode(com.google.template.soy.soytree.SwitchCaseNode) SwitchNode(com.google.template.soy.soytree.SwitchNode) FieldAccessNode(com.google.template.soy.exprtree.FieldAccessNode) SwitchDefaultNode(com.google.template.soy.soytree.SwitchDefaultNode) Test(org.junit.Test)

Aggregations

FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)1 IntegerNode (com.google.template.soy.exprtree.IntegerNode)1 VarRefNode (com.google.template.soy.exprtree.VarRefNode)1 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)1 SwitchCaseNode (com.google.template.soy.soytree.SwitchCaseNode)1 SwitchDefaultNode (com.google.template.soy.soytree.SwitchDefaultNode)1 SwitchNode (com.google.template.soy.soytree.SwitchNode)1 Test (org.junit.Test)1