Search in sources :

Example 6 with PrintNode

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

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

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

the class TemplateParserTest method testParsePrintStmt.

@Test
public void testParsePrintStmt() throws Exception {
    String templateBody = "{@param boo : ?}{@param goo : ?}\n" + "  {$boo.foo}{$boo.foo}\n" + "  {$goo + 1 |noAutoescape}\n" + "  {print 'blah    blahblahblah' |escapeHtml|insertWordBreaks:8}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(4, nodes.size());
    PrintNode pn0 = (PrintNode) nodes.get(0);
    assertEquals("$boo.foo", pn0.getExpr().toSourceString());
    assertEquals(0, pn0.getChildren().size());
    assertEquals("FOO", pn0.genBasePhName());
    assertEquals("{$boo.foo}", pn0.toSourceString());
    assertTrue(pn0.getExpr().getRoot() instanceof FieldAccessNode);
    PrintNode pn1 = (PrintNode) nodes.get(1);
    assertTrue(pn0.genSamenessKey().equals(pn1.genSamenessKey()));
    assertTrue(pn1.getExpr().getRoot() instanceof FieldAccessNode);
    PrintNode pn2 = (PrintNode) nodes.get(2);
    assertEquals("$goo + 1", pn2.getExpr().toSourceString());
    assertEquals(1, pn2.getChildren().size());
    PrintDirectiveNode pn2d0 = pn2.getChild(0);
    assertEquals("|noAutoescape", pn2d0.getName());
    assertEquals("XXX", pn2.genBasePhName());
    assertTrue(pn2.getExpr().getRoot() instanceof PlusOpNode);
    PrintNode pn3 = (PrintNode) nodes.get(3);
    assertEquals("'blah    blahblahblah'", pn3.getExpr().toSourceString());
    assertEquals(2, pn3.getChildren().size());
    PrintDirectiveNode pn3d0 = pn3.getChild(0);
    assertEquals("|escapeHtml", pn3d0.getName());
    PrintDirectiveNode pn3d1 = pn3.getChild(1);
    assertEquals("|insertWordBreaks", pn3d1.getName());
    assertEquals(8, ((IntegerNode) pn3d1.getArgs().get(0).getRoot()).getValue());
    assertEquals("XXX", pn3.genBasePhName());
    assertTrue(pn3.getExpr().getRoot() instanceof StringNode);
    assertFalse(pn0.genSamenessKey().equals(pn2.genSamenessKey()));
    assertFalse(pn3.genSamenessKey().equals(pn0.genSamenessKey()));
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) PrintDirectiveNode(com.google.template.soy.soytree.PrintDirectiveNode) StringNode(com.google.template.soy.exprtree.StringNode) PlusOpNode(com.google.template.soy.exprtree.OperatorNodes.PlusOpNode) PrintNode(com.google.template.soy.soytree.PrintNode) FieldAccessNode(com.google.template.soy.exprtree.FieldAccessNode) Test(org.junit.Test)

Example 9 with PrintNode

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

the class ResolvePackageRelativeCssNamesVisitorTest method testRequireCssOnNamespace.

@Test
public void testRequireCssOnNamespace() {
    TemplateNode template = compileTemplate("{namespace boo requirecss=\"some.test.package,some.other.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 10 with PrintNode

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

the class EvalVisitorTest method eval.

/**
 * Evaluates the given expression and returns the result.
 *
 * @param expression The expression to evaluate.
 * @return The expression result.
 * @throws Exception If there's an error.
 */
private SoyValue eval(String expression) throws Exception {
    PrintNode code = (PrintNode) SoyFileSetParserBuilder.forTemplateContents(// wrap in a function so we don't run into the 'can't print bools' error message
    untypedTemplateBodyForExpression("fakeFunction(" + expression + ")")).addSoyFunction(new SoyFunction() {

        @Override
        public String getName() {
            return "fakeFunction";
        }

        @Override
        public Set<Integer> getValidArgsSizes() {
            return ImmutableSet.of(1);
        }
    }).parse().fileSet().getChild(0).getChild(0).getChild(0);
    ExprNode expr = ((FunctionNode) code.getExpr().getChild(0)).getChild(0);
    EvalVisitor evalVisitor = new EvalVisitorFactoryImpl().create(TestingEnvironment.createForTest(testData, LOCALS), TEST_IJ_DATA, cssRenamingMap, xidRenamingMap, null, /* debugSoyTemplateInfo= */
    false);
    return evalVisitor.exec(expr);
}
Also used : ExprNode(com.google.template.soy.exprtree.ExprNode) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) SoyFunction(com.google.template.soy.shared.restricted.SoyFunction) FunctionNode(com.google.template.soy.exprtree.FunctionNode) PrintNode(com.google.template.soy.soytree.PrintNode)

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