Search in sources :

Example 6 with FieldAccessNode

use of com.google.template.soy.exprtree.FieldAccessNode in project closure-templates by google.

the class TemplateParserTest method testParsePrintStmtWithPhname.

@Test
public void testParsePrintStmtWithPhname() throws Exception {
    String templateBody = "{@param boo : ?}\n" + "{msg desc=\"...\"}\n" + "  {$boo.foo}\n" + "  {$boo.foo phname=\"booFoo\"}\n" + "  {$boo.foo    phname=\"booFoo\"    }\n" + "    {print $boo.foo phname=\"boo_foo\"}\n" + "{/msg}";
    List<StandaloneNode> nodes = ((MsgFallbackGroupNode) parseTemplateContent(templateBody, FAIL).getChild(0)).getChild(0).getChildren();
    assertEquals(4, nodes.size());
    PrintNode pn0 = (PrintNode) ((MsgPlaceholderNode) nodes.get(0)).getChild(0);
    assertEquals("$boo.foo", pn0.getExpr().toSourceString());
    assertEquals("FOO", pn0.genBasePhName());
    assertEquals("{$boo.foo}", pn0.toSourceString());
    PrintNode pn1 = (PrintNode) ((MsgPlaceholderNode) nodes.get(1)).getChild(0);
    assertEquals("$boo.foo", pn1.getExpr().toSourceString());
    assertEquals("BOO_FOO", pn1.genBasePhName());
    assertEquals("{$boo.foo phname=\"booFoo\"}", pn1.toSourceString());
    assertEquals(0, pn1.getChildren().size());
    assertTrue(pn1.getExpr().getRoot() instanceof FieldAccessNode);
    PrintNode pn2 = (PrintNode) ((MsgPlaceholderNode) nodes.get(2)).getChild(0);
    assertEquals("$boo.foo", pn2.getExpr().toSourceString());
    assertEquals("BOO_FOO", pn2.genBasePhName());
    assertEquals("{$boo.foo phname=\"booFoo\"}", pn2.toSourceString());
    PrintNode pn3 = (PrintNode) ((MsgPlaceholderNode) nodes.get(3)).getChild(0);
    assertEquals("$boo.foo", pn3.getExpr().toSourceString());
    assertEquals("BOO_FOO", pn3.genBasePhName());
    assertEquals("{print $boo.foo phname=\"boo_foo\"}", pn3.toSourceString());
    assertFalse(pn0.genSamenessKey().equals(pn1.genSamenessKey()));
    assertTrue(pn1.genSamenessKey().equals(pn2.genSamenessKey()));
    assertFalse(pn1.genSamenessKey().equals(pn3.genSamenessKey()));
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) PrintNode(com.google.template.soy.soytree.PrintNode) FieldAccessNode(com.google.template.soy.exprtree.FieldAccessNode) Test(org.junit.Test)

Example 7 with FieldAccessNode

use of com.google.template.soy.exprtree.FieldAccessNode 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)7 Test (org.junit.Test)5 VarRefNode (com.google.template.soy.exprtree.VarRefNode)4 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)4 IntegerNode (com.google.template.soy.exprtree.IntegerNode)3 PrintNode (com.google.template.soy.soytree.PrintNode)3 GlobalNode (com.google.template.soy.exprtree.GlobalNode)2 ItemAccessNode (com.google.template.soy.exprtree.ItemAccessNode)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 SourceLocation (com.google.template.soy.base.SourceLocation)1 IndentedLinesBuilder (com.google.template.soy.base.internal.IndentedLinesBuilder)1 DataAccessNode (com.google.template.soy.exprtree.DataAccessNode)1 ExprNode (com.google.template.soy.exprtree.ExprNode)1 PlusOpNode (com.google.template.soy.exprtree.OperatorNodes.PlusOpNode)1 ProtoInitNode (com.google.template.soy.exprtree.ProtoInitNode)1 StringNode (com.google.template.soy.exprtree.StringNode)1 CssTagsPrefixPresence (com.google.template.soy.parseinfo.SoyFileInfo.CssTagsPrefixPresence)1