Search in sources :

Example 16 with StandaloneNode

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

Example 17 with StandaloneNode

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

the class TemplateParserTest method testParseMsgStmtWithPlural.

// -----------------------------------------------------------------------------------------------
// Tests for plural/select messages.
@Test
public void testParseMsgStmtWithPlural() throws Exception {
    String templateBody = "{@param num_people : ?}{@param person : ?}{@param place : ?}\n" + "  {msg desc=\"A sample plural message\"}\n" + "    {plural $num_people offset=\"1\"}\n" + "      {case 0}I see no one in {$place}.\n" + "      {case 1}I see {$person} in {$place}.\n" + "      {case 2}I see {$person} and one other person in {$place}.\n" + "      {default}I see {$person} and {remainder($num_people)} " + "other people in {$place}.\n" + "    {/plural}" + "  {/msg}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(1, nodes.size());
    MsgNode mn = ((MsgFallbackGroupNode) nodes.get(0)).getChild(0);
    assertEquals(1, mn.numChildren());
    assertEquals("A sample plural message", mn.getDesc());
    MsgPluralNode pn = (MsgPluralNode) mn.getChild(0);
    assertEquals("$num_people", pn.getExpr().toSourceString());
    assertEquals(1, pn.getOffset());
    // 3 cases and default
    assertEquals(4, pn.numChildren());
    // Case 0
    MsgPluralCaseNode cn0 = (MsgPluralCaseNode) pn.getChild(0);
    assertEquals(3, cn0.numChildren());
    assertEquals(0, cn0.getCaseNumber());
    RawTextNode rtn01 = (RawTextNode) cn0.getChild(0);
    assertEquals("I see no one in ", rtn01.getRawText());
    MsgPlaceholderNode phn01 = (MsgPlaceholderNode) cn0.getChild(1);
    assertEquals("{$place}", phn01.toSourceString());
    RawTextNode rtn02 = (RawTextNode) cn0.getChild(2);
    assertEquals(".", rtn02.getRawText());
    // Case 1
    MsgPluralCaseNode cn1 = (MsgPluralCaseNode) pn.getChild(1);
    assertEquals(5, cn1.numChildren());
    assertEquals(1, cn1.getCaseNumber());
    RawTextNode rtn11 = (RawTextNode) cn1.getChild(0);
    assertEquals("I see ", rtn11.getRawText());
    MsgPlaceholderNode phn11 = (MsgPlaceholderNode) cn1.getChild(1);
    assertEquals("{$person}", phn11.toSourceString());
    RawTextNode rtn12 = (RawTextNode) cn1.getChild(2);
    assertEquals(" in ", rtn12.getRawText());
    MsgPlaceholderNode phn12 = (MsgPlaceholderNode) cn1.getChild(3);
    assertEquals("{$place}", phn12.toSourceString());
    RawTextNode rtn13 = (RawTextNode) cn1.getChild(4);
    assertEquals(".", rtn13.getRawText());
    // Case 2
    MsgPluralCaseNode cn2 = (MsgPluralCaseNode) pn.getChild(2);
    assertEquals(5, cn2.numChildren());
    assertEquals(2, cn2.getCaseNumber());
    RawTextNode rtn21 = (RawTextNode) cn2.getChild(0);
    assertEquals("I see ", rtn21.getRawText());
    MsgPlaceholderNode phn21 = (MsgPlaceholderNode) cn2.getChild(1);
    assertEquals("{$person}", phn21.toSourceString());
    RawTextNode rtn22 = (RawTextNode) cn2.getChild(2);
    assertEquals(" and one other person in ", rtn22.getRawText());
    MsgPlaceholderNode phn22 = (MsgPlaceholderNode) cn2.getChild(3);
    assertEquals("{$place}", phn22.toSourceString());
    RawTextNode rtn23 = (RawTextNode) cn2.getChild(4);
    assertEquals(".", rtn23.getRawText());
    // Default
    MsgPluralDefaultNode dn = (MsgPluralDefaultNode) pn.getChild(3);
    assertEquals(7, dn.numChildren());
    RawTextNode rtnd1 = (RawTextNode) dn.getChild(0);
    assertEquals("I see ", rtnd1.getRawText());
    MsgPlaceholderNode phnd1 = (MsgPlaceholderNode) dn.getChild(1);
    assertEquals("{$person}", phnd1.toSourceString());
    RawTextNode rtnd2 = (RawTextNode) dn.getChild(2);
    assertEquals(" and ", rtnd2.getRawText());
    MsgPlaceholderNode phnd2 = (MsgPlaceholderNode) dn.getChild(3);
    assertEquals("{$num_people - 1}", phnd2.toSourceString());
    RawTextNode rtnd3 = (RawTextNode) dn.getChild(4);
    assertEquals(" other people in ", rtnd3.getRawText());
    MsgPlaceholderNode phnd3 = (MsgPlaceholderNode) dn.getChild(5);
    assertEquals("{$place}", phnd3.toSourceString());
    RawTextNode rtnd4 = (RawTextNode) dn.getChild(6);
    assertEquals(".", rtnd4.getRawText());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) MsgPluralCaseNode(com.google.template.soy.soytree.MsgPluralCaseNode) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) MsgPluralDefaultNode(com.google.template.soy.soytree.MsgPluralDefaultNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgNode(com.google.template.soy.soytree.MsgNode) MsgPluralNode(com.google.template.soy.soytree.MsgPluralNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) Test(org.junit.Test)

Example 18 with StandaloneNode

use of com.google.template.soy.soytree.SoyNode.StandaloneNode 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 19 with StandaloneNode

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

the class TemplateParserTest method testParseMsgStmtWithSelect.

@Test
public void testParseMsgStmtWithSelect() throws Exception {
    String templateBody = "{@param gender : ?}{@param person : ?}\n" + "{msg desc=\"A sample gender message\"}\n" + "  {select $gender}\n" + "    {case 'female'}{$person} added you to her circle.\n" + "    {default}{$person} added you to his circle.\n" + "  {/select}\n" + "{/msg}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(1, nodes.size());
    MsgNode mn = ((MsgFallbackGroupNode) nodes.get(0)).getChild(0);
    assertEquals(1, mn.numChildren());
    assertEquals("A sample gender message", mn.getDesc());
    MsgSelectNode sn = (MsgSelectNode) mn.getChild(0);
    assertEquals("$gender", sn.getExpr().toSourceString());
    // female and default
    assertEquals(2, sn.numChildren());
    // Case 'female'
    MsgSelectCaseNode cnf = (MsgSelectCaseNode) sn.getChild(0);
    assertEquals("female", cnf.getCaseValue());
    assertEquals(2, cnf.numChildren());
    MsgPlaceholderNode phnf = (MsgPlaceholderNode) cnf.getChild(0);
    assertEquals("{$person}", phnf.toSourceString());
    RawTextNode rtnf = (RawTextNode) cnf.getChild(1);
    assertEquals(" added you to her circle.", rtnf.getRawText());
    // Default
    MsgSelectDefaultNode dn = (MsgSelectDefaultNode) sn.getChild(1);
    assertEquals(2, dn.numChildren());
    MsgPlaceholderNode phnd = (MsgPlaceholderNode) dn.getChild(0);
    assertEquals("{$person}", phnd.toSourceString());
    RawTextNode rtnd = (RawTextNode) dn.getChild(1);
    assertEquals(" added you to his circle.", rtnd.getRawText());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) MsgSelectDefaultNode(com.google.template.soy.soytree.MsgSelectDefaultNode) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) MsgSelectNode(com.google.template.soy.soytree.MsgSelectNode) MsgSelectCaseNode(com.google.template.soy.soytree.MsgSelectCaseNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgNode(com.google.template.soy.soytree.MsgNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) Test(org.junit.Test)

Example 20 with StandaloneNode

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

the class TemplateParserTest method testParseMsgStmtWithCall.

@Test
public void testParseMsgStmtWithCall() throws Exception {
    String templateBody = "  {msg desc=\"Blah.\"}\n" + "    Blah {call .helper_ data=\"all\" /} blah{sp}\n" + "    {call .helper_}\n" + "      {param foo : 'foo' /}\n" + "    {/call}{sp}\n" + "    blah.\n" + "  {/msg}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(1, nodes.size());
    MsgNode mn = ((MsgFallbackGroupNode) nodes.get(0)).getChild(0);
    assertEquals(5, mn.numChildren());
    assertEquals("Blah ", ((RawTextNode) mn.getChild(0)).getRawText());
    assertEquals(0, ((CallNode) ((MsgPlaceholderNode) mn.getChild(1)).getChild(0)).numChildren());
    assertEquals(" blah ", ((RawTextNode) mn.getChild(2)).getRawText());
    assertEquals(1, ((CallNode) ((MsgPlaceholderNode) mn.getChild(3)).getChild(0)).numChildren());
    assertEquals(" blah.", ((RawTextNode) mn.getChild(4)).getRawText());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) MsgNode(com.google.template.soy.soytree.MsgNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) Test(org.junit.Test)

Aggregations

StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)30 Test (org.junit.Test)19 RawTextNode (com.google.template.soy.soytree.RawTextNode)10 MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)9 MsgNode (com.google.template.soy.soytree.MsgNode)8 PrintNode (com.google.template.soy.soytree.PrintNode)8 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)7 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)5 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)4 VarRefNode (com.google.template.soy.exprtree.VarRefNode)4 MsgSelectNode (com.google.template.soy.soytree.MsgSelectNode)4 FunctionNode (com.google.template.soy.exprtree.FunctionNode)3 CallParamContentNode (com.google.template.soy.soytree.CallParamContentNode)3 MsgSelectCaseNode (com.google.template.soy.soytree.MsgSelectCaseNode)3 MsgSelectDefaultNode (com.google.template.soy.soytree.MsgSelectDefaultNode)3 ImmutableList (com.google.common.collect.ImmutableList)2 IntegerNode (com.google.template.soy.exprtree.IntegerNode)2 StringNode (com.google.template.soy.exprtree.StringNode)2 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)2 SoyMsgPlaceholderPart (com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart)2