Search in sources :

Example 6 with StandaloneNode

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

the class TemplateParserTest method testParseMsgStmtWithFallback.

@Test
public void testParseMsgStmtWithFallback() throws Exception {
    String templateBody = "" + "{msg meaning=\"verb\" desc=\"Used as a verb.\"}\n" + "  Archive\n" + "{fallbackmsg desc=\"\"}\n" + "  Archive\n" + "{/msg}";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(1, nodes.size());
    MsgFallbackGroupNode mfgn = (MsgFallbackGroupNode) nodes.get(0);
    assertEquals(2, mfgn.numChildren());
    MsgNode mn0 = mfgn.getChild(0);
    assertEquals("msg", mn0.getCommandName());
    assertEquals("verb", mn0.getMeaning());
    assertEquals("Used as a verb.", mn0.getDesc());
    assertEquals("Archive", ((RawTextNode) mn0.getChild(0)).getRawText());
    MsgNode mn1 = mfgn.getChild(1);
    assertEquals("fallbackmsg", mn1.getCommandName());
    assertEquals(null, mn1.getMeaning());
    assertEquals("", mn1.getDesc());
    assertEquals("Archive", ((RawTextNode) mn1.getChild(0)).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) Test(org.junit.Test)

Example 7 with StandaloneNode

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

the class TemplateParserTest method testParseLogStmt.

@Test
public void testParseLogStmt() throws Exception {
    String templateBody = "{@param foo : ?}\n" + "{log}Blah {$foo}.{/log}";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(1, nodes.size());
    LogNode logNode = (LogNode) nodes.get(0);
    assertEquals(3, logNode.numChildren());
    assertEquals("Blah ", ((RawTextNode) logNode.getChild(0)).getRawText());
    assertEquals("$foo", ((PrintNode) logNode.getChild(1)).getExpr().toSourceString());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) LogNode(com.google.template.soy.soytree.LogNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Example 8 with StandaloneNode

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

the class TemplateParserTest method testParseMsgStmt.

@Test
public void testParseMsgStmt() throws Exception {
    String templateBody = "{@param usedMb :?}{@param learnMoreUrl :?}\n" + "  {msg desc=\"Tells user's quota usage.\"}\n" + "    You're currently using {$usedMb} MB of your quota.{sp}\n" + "    <a href=\"{$learnMoreUrl}\">Learn more</A>\n" + "    <br /><br />\n" + "  {/msg}\n" + "  {msg meaning=\"noun\" desc=\"\" hidden=\"true\"}Archive{/msg}\n" + "  {msg meaning=\"noun\" desc=\"The archive (noun).\"}Archive{/msg}\n" + "  {msg meaning=\"verb\" desc=\"\"}Archive{/msg}\n" + "  {msg desc=\"\"}Archive{/msg}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(5, nodes.size());
    MsgNode mn0 = ((MsgFallbackGroupNode) nodes.get(0)).getMsg();
    assertEquals("Tells user's quota usage.", mn0.getDesc());
    assertEquals(null, mn0.getMeaning());
    assertEquals(false, mn0.isHidden());
    assertEquals(8, mn0.numChildren());
    assertEquals("You're currently using ", ((RawTextNode) mn0.getChild(0)).getRawText());
    MsgPlaceholderNode mpn1 = (MsgPlaceholderNode) mn0.getChild(1);
    assertEquals("$usedMb", ((PrintNode) mpn1.getChild(0)).getExpr().toSourceString());
    assertEquals(" MB of your quota. ", ((RawTextNode) mn0.getChild(2)).getRawText());
    MsgPlaceholderNode mpn3 = (MsgPlaceholderNode) mn0.getChild(3);
    MsgHtmlTagNode mhtn3 = (MsgHtmlTagNode) mpn3.getChild(0);
    assertEquals("a", mhtn3.getLcTagName());
    assertEquals("START_LINK", mhtn3.genBasePhName());
    assertEquals("<a href=\"{$learnMoreUrl}\">", mhtn3.toSourceString());
    assertEquals(3, mhtn3.numChildren());
    assertEquals("<a href=\"", ((RawTextNode) mhtn3.getChild(0)).getRawText());
    assertEquals("$learnMoreUrl", ((PrintNode) mhtn3.getChild(1)).getExpr().toSourceString());
    assertEquals("\">", ((RawTextNode) mhtn3.getChild(2)).getRawText());
    assertEquals("Learn more", ((RawTextNode) mn0.getChild(4)).getRawText());
    MsgPlaceholderNode mpn5 = (MsgPlaceholderNode) mn0.getChild(5);
    MsgHtmlTagNode mhtn5 = (MsgHtmlTagNode) mpn5.getChild(0);
    assertEquals("/a", mhtn5.getLcTagName());
    assertEquals("END_LINK", mhtn5.genBasePhName());
    assertEquals("</A>", mhtn5.toSourceString());
    MsgPlaceholderNode mpn6 = (MsgPlaceholderNode) mn0.getChild(6);
    MsgHtmlTagNode mhtn6 = (MsgHtmlTagNode) mpn6.getChild(0);
    assertEquals("BREAK", mhtn6.genBasePhName());
    assertTrue(mpn6.shouldUseSameVarNameAs((MsgPlaceholderNode) mn0.getChild(7)));
    assertFalse(mpn6.shouldUseSameVarNameAs(mpn5));
    assertFalse(mpn5.shouldUseSameVarNameAs(mpn3));
    MsgFallbackGroupNode mfgn1 = (MsgFallbackGroupNode) nodes.get(1);
    assertEquals("{msg meaning=\"noun\" desc=\"\" hidden=\"true\"}Archive{/msg}", mfgn1.toSourceString());
    MsgNode mn1 = mfgn1.getMsg();
    assertEquals("", mn1.getDesc());
    assertEquals("noun", mn1.getMeaning());
    assertEquals(true, mn1.isHidden());
    assertEquals(1, mn1.numChildren());
    assertEquals("Archive", ((RawTextNode) mn1.getChild(0)).getRawText());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) MsgFallbackGroupNode(com.google.template.soy.soytree.MsgFallbackGroupNode) MsgHtmlTagNode(com.google.template.soy.soytree.MsgHtmlTagNode) PrintNode(com.google.template.soy.soytree.PrintNode) MsgNode(com.google.template.soy.soytree.MsgNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode) Test(org.junit.Test)

Example 9 with StandaloneNode

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

the class TemplateParserTest method testParseIfStmt.

@Test
public void testParseIfStmt() throws Exception {
    String templateBody = "{@param zoo : ?}{@param boo: ?}{@param foo : ?}{@param moo : ?}\n" + "  {if $zoo}{$zoo}{/if}\n" + "  {if $boo}\n" + "    Blah\n" + "  {elseif $foo.goo > 2}\n" + "    {$moo}\n" + "  {else}\n" + "    Blah {$moo}\n" + "  {/if}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(2, nodes.size());
    IfNode in0 = (IfNode) nodes.get(0);
    assertEquals(1, in0.numChildren());
    IfCondNode in0icn0 = (IfCondNode) in0.getChild(0);
    assertEquals("$zoo", in0icn0.getExpr().toSourceString());
    assertEquals(1, in0icn0.numChildren());
    assertEquals("$zoo", ((PrintNode) in0icn0.getChild(0)).getExpr().toSourceString());
    assertTrue(in0icn0.getExpr().getRoot() instanceof VarRefNode);
    IfNode in1 = (IfNode) nodes.get(1);
    assertEquals(3, in1.numChildren());
    IfCondNode in1icn0 = (IfCondNode) in1.getChild(0);
    assertEquals("$boo", in1icn0.getExpr().toSourceString());
    assertTrue(in1icn0.getExpr().getRoot() instanceof VarRefNode);
    IfCondNode in1icn1 = (IfCondNode) in1.getChild(1);
    assertEquals("$foo.goo > 2", in1icn1.getExpr().toSourceString());
    assertTrue(in1icn1.getExpr().getRoot() instanceof GreaterThanOpNode);
    assertEquals("{if $boo}Blah{elseif $foo.goo > 2}{$moo}{else}Blah {$moo}{/if}", in1.toSourceString());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) IfCondNode(com.google.template.soy.soytree.IfCondNode) GreaterThanOpNode(com.google.template.soy.exprtree.OperatorNodes.GreaterThanOpNode) VarRefNode(com.google.template.soy.exprtree.VarRefNode) IfNode(com.google.template.soy.soytree.IfNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Example 10 with StandaloneNode

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

the class TemplateParserTest method testParseMsgStmtWithNestedSelects.

@Test
public void testParseMsgStmtWithNestedSelects() throws Exception {
    String templateBody = "{@param gender1 : ?}{@param gender2: ?}{@param person1 : ?}{@param person2: ?}\n" + "{msg desc=\"A sample nested message\"}\n" + "  {select $gender1}\n" + "    {case 'female'}\n" + "      {select $gender2}\n" + "        {case 'female'}{$person1} added {$person2} and her friends to her circle.\n" + "        {default}{$person1} added {$person2} and his friends to her circle.\n" + "      {/select}\n" + "    {default}\n" + "      {select $gender2}\n" + "        {case 'female'}{$person1} put {$person2} and her friends to his circle.\n" + "        {default}{$person1} put {$person2} and his friends to his circle.\n" + "      {/select}\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 nested message", mn.getDesc());
    // Outer select
    MsgSelectNode sn = (MsgSelectNode) mn.getChild(0);
    assertEquals("$gender1", sn.getExpr().toSourceString());
    // female and default
    assertEquals(2, sn.numChildren());
    // Outer select: Case 'female'
    MsgSelectCaseNode cnf = (MsgSelectCaseNode) sn.getChild(0);
    assertEquals("female", cnf.getCaseValue());
    // Another select
    assertEquals(1, cnf.numChildren());
    // Outer select: Case 'female': Inner select
    MsgSelectNode sn2 = (MsgSelectNode) cnf.getChild(0);
    assertEquals("$gender2", sn2.getExpr().toSourceString());
    // female and default
    assertEquals(2, sn2.numChildren());
    // Outer select: Case 'female': Inner select: Case 'female'
    MsgSelectCaseNode cnf2 = (MsgSelectCaseNode) sn2.getChild(0);
    assertEquals("female", cnf2.getCaseValue());
    assertEquals(4, cnf2.numChildren());
    // Outer select: Case 'female': Inner select: Case 'female': Placeholder $person1
    MsgPlaceholderNode phn1 = (MsgPlaceholderNode) cnf2.getChild(0);
    assertEquals("{$person1}", phn1.toSourceString());
    // Outer select: Case 'female': Inner select: Case 'female': RawText
    RawTextNode rtn1 = (RawTextNode) cnf2.getChild(1);
    assertEquals(" added ", rtn1.getRawText());
    // Outer select: Case 'female': Inner select: Case 'female': Placeholder $person2
    MsgPlaceholderNode phn2 = (MsgPlaceholderNode) cnf2.getChild(2);
    assertEquals("{$person2}", phn2.toSourceString());
    // Outer select: Case 'female': Inner select: Case 'female': RawText
    RawTextNode rtn2 = (RawTextNode) cnf2.getChild(3);
    assertEquals(" and her friends to her circle.", rtn2.getRawText());
    // Outer select: Case 'female': Inner select: Default
    MsgSelectDefaultNode dn2 = (MsgSelectDefaultNode) sn2.getChild(1);
    assertEquals(4, dn2.numChildren());
    // Outer select: Case 'female': Inner select: Default: Placeholder $person1
    MsgPlaceholderNode phn21 = (MsgPlaceholderNode) dn2.getChild(0);
    assertEquals("{$person1}", phn21.toSourceString());
    // Outer select: Case 'female': Inner select: Default: RawText
    RawTextNode rtn21 = (RawTextNode) dn2.getChild(1);
    assertEquals(" added ", rtn21.getRawText());
    // Outer select: Case 'female': Inner select: Default: Placeholder $person2
    MsgPlaceholderNode phn22 = (MsgPlaceholderNode) dn2.getChild(2);
    assertEquals("{$person2}", phn22.toSourceString());
    // Outer select: Case 'female': Inner select: Default: RawText
    RawTextNode rtn22 = (RawTextNode) dn2.getChild(3);
    assertEquals(" and his friends to her circle.", rtn22.getRawText());
    // Outer select: Default
    MsgSelectDefaultNode dn = (MsgSelectDefaultNode) sn.getChild(1);
    // Another select
    assertEquals(1, dn.numChildren());
    // Outer select: Default: Inner select
    MsgSelectNode sn3 = (MsgSelectNode) dn.getChild(0);
    assertEquals("$gender2", sn3.getExpr().toSourceString());
    // female and default
    assertEquals(2, sn3.numChildren());
    // Outer select: Default: Inner select: Case 'female'
    MsgSelectCaseNode cnf3 = (MsgSelectCaseNode) sn3.getChild(0);
    assertEquals("female", cnf3.getCaseValue());
    assertEquals(4, cnf3.numChildren());
    // Outer select: Default: Inner select: Case 'female': Placeholder $person1
    MsgPlaceholderNode phn3 = (MsgPlaceholderNode) cnf3.getChild(0);
    assertEquals("{$person1}", phn3.toSourceString());
    // Outer select: Default: Inner select: Case 'female': RawText
    RawTextNode rtn3 = (RawTextNode) cnf3.getChild(1);
    assertEquals(" put ", rtn3.getRawText());
    // Outer select: Default: Inner select: Case 'female': Placeholder $person2
    MsgPlaceholderNode phn4 = (MsgPlaceholderNode) cnf3.getChild(2);
    assertEquals("{$person2}", phn4.toSourceString());
    // Outer select: Default: Inner select: Case 'female': RawText
    RawTextNode rtn4 = (RawTextNode) cnf3.getChild(3);
    assertEquals(" and her friends to his circle.", rtn4.getRawText());
    // Outer select: Default: Inner select: Default
    MsgSelectDefaultNode dn3 = (MsgSelectDefaultNode) sn3.getChild(1);
    assertEquals(4, dn3.numChildren());
    // Outer select: Default: Inner select: Default: Placeholder $person1
    MsgPlaceholderNode phn5 = (MsgPlaceholderNode) dn3.getChild(0);
    assertEquals("{$person1}", phn5.toSourceString());
    // Outer select: Default: Inner select: Default: RawText
    RawTextNode rtn5 = (RawTextNode) dn3.getChild(1);
    assertEquals(" put ", rtn5.getRawText());
    // Outer select: Default: Inner select: Default: Placeholder $person2
    MsgPlaceholderNode phn6 = (MsgPlaceholderNode) dn3.getChild(2);
    assertEquals("{$person2}", phn6.toSourceString());
    // Outer select: Default: Inner select: Default: RawText
    RawTextNode rtn6 = (RawTextNode) dn3.getChild(3);
    assertEquals(" and his friends to his circle.", rtn6.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)

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