Search in sources :

Example 16 with PrintNode

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

use of com.google.template.soy.soytree.PrintNode 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 18 with PrintNode

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

the class MsgCompiler method putPlaceholderIntoMap.

private void putPlaceholderIntoMap(Expression mapExpression, MsgNode originalMsg, Map<String, Statement> placeholderNameToPutStatement, SoyMsgPlaceholderPart placeholder) throws AssertionError {
    String placeholderName = placeholder.getPlaceholderName();
    if (!placeholderNameToPutStatement.containsKey(placeholderName)) {
        MsgPlaceholderNode repPlaceholderNode = originalMsg.getRepPlaceholderNode(placeholder.getPlaceholderName());
        if (repPlaceholderNode.numChildren() == 0) {
            throw new IllegalStateException("empty rep node for: " + placeholderName);
        }
        StandaloneNode initialNode = repPlaceholderNode.getChild(0);
        Statement putEntyInMap;
        if (initialNode instanceof MsgHtmlTagNode) {
            putEntyInMap = addHtmlTagNodeToPlaceholderMap(mapExpression, placeholderName, (MsgHtmlTagNode) initialNode);
        } else if (initialNode instanceof CallNode) {
            putEntyInMap = addCallNodeToPlaceholderMap(mapExpression, placeholderName, (CallNode) initialNode);
        } else if (initialNode instanceof PrintNode) {
            putEntyInMap = addPrintNodeToPlaceholderMap(mapExpression, placeholderName, (PrintNode) initialNode);
        } else if (initialNode instanceof RawTextNode) {
            putEntyInMap = addRawTextNodeToPlaceholderMap(mapExpression, placeholderName, (RawTextNode) initialNode);
        } else {
            // the AST for MsgNodes guarantee that these are the only options
            throw new AssertionError("Unexpected child: " + initialNode.getClass());
        }
        placeholderNameToPutStatement.put(placeholder.getPlaceholderName(), putEntyInMap.withSourceLocation(repPlaceholderNode.getSourceLocation()));
    }
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) Statement(com.google.template.soy.jbcsrc.restricted.Statement) MsgHtmlTagNode(com.google.template.soy.soytree.MsgHtmlTagNode) PrintNode(com.google.template.soy.soytree.PrintNode) CallNode(com.google.template.soy.soytree.CallNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) MsgPlaceholderNode(com.google.template.soy.soytree.MsgPlaceholderNode)

Example 19 with PrintNode

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

the class ResolvePackageRelativeCssNamesVisitorTest method testBaseCssOnNamespace.

@Test
public void testBaseCssOnNamespace() {
    TemplateNode template = compileTemplate("{namespace boo cssbase=\"some.test.package\"}\n\n" + "/** Test template.*/\n" + "{template .foo}\n" + "  <p class=\"{css('%AAA')}\">\n" + "{/template}\n");
    PrintNode printNode = Iterables.getOnlyElement(SoyTreeUtils.getAllNodesOfType(template, PrintNode.class));
    FunctionNode cssFn = (FunctionNode) printNode.getExpr().getRoot();
    assertThat(((StringNode) cssFn.getChild(0)).getValue()).isEqualTo("someTestPackageAAA");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) StringNode(com.google.template.soy.exprtree.StringNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Example 20 with PrintNode

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

the class ResolvePackageRelativeCssNamesVisitorTest method testBaseCssOnTemplate.

@Test
public void testBaseCssOnTemplate() {
    TemplateNode template = compileTemplate("{namespace boo}\n\n" + "/** Test template.  */\n" + "{template .foo cssbase=\"some.test.package\"}\n" + "  <p class=\"{css('%AAA')}\">\n" + "{/template}\n");
    PrintNode printNode = Iterables.getOnlyElement(SoyTreeUtils.getAllNodesOfType(template, PrintNode.class));
    FunctionNode cssFn = (FunctionNode) printNode.getExpr().getRoot();
    assertThat(((StringNode) cssFn.getChild(0)).getValue()).isEqualTo("someTestPackageAAA");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) StringNode(com.google.template.soy.exprtree.StringNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Aggregations

PrintNode (com.google.template.soy.soytree.PrintNode)25 Test (org.junit.Test)15 FunctionNode (com.google.template.soy.exprtree.FunctionNode)8 StandaloneNode (com.google.template.soy.soytree.SoyNode.StandaloneNode)8 StringNode (com.google.template.soy.exprtree.StringNode)6 TemplateNode (com.google.template.soy.soytree.TemplateNode)6 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)5 RawTextNode (com.google.template.soy.soytree.RawTextNode)4 FieldAccessNode (com.google.template.soy.exprtree.FieldAccessNode)3 VarRefNode (com.google.template.soy.exprtree.VarRefNode)3 IfCondNode (com.google.template.soy.soytree.IfCondNode)3 IfNode (com.google.template.soy.soytree.IfNode)3 MsgHtmlTagNode (com.google.template.soy.soytree.MsgHtmlTagNode)3 MsgPlaceholderNode (com.google.template.soy.soytree.MsgPlaceholderNode)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 SourceLocation (com.google.template.soy.base.SourceLocation)2 ExprNode (com.google.template.soy.exprtree.ExprNode)2 IntegerNode (com.google.template.soy.exprtree.IntegerNode)2 PyExpr (com.google.template.soy.pysrc.restricted.PyExpr)2 SoyFunction (com.google.template.soy.shared.restricted.SoyFunction)2