Search in sources :

Example 26 with StandaloneNode

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

the class TemplateParserTest method testParseLetStmt.

@Test
public void testParseLetStmt() throws Exception {
    String templateBody = "{@param boo : ?}\n" + "  {let $alpha: $boo.foo /}\n" + "  {let $beta kind=\"html\"}Boo!{/let}\n" + "  {let $gamma kind=\"html\"}\n" + "    {for $i in range($alpha)}\n" + "      {$i}{$beta}\n" + "    {/for}\n" + "  {/let}\n" + "  {let $delta kind=\"html\"}Boo!{/let}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(4, nodes.size());
    LetValueNode alphaNode = (LetValueNode) nodes.get(0);
    assertEquals("alpha", alphaNode.getVarName());
    assertEquals("$boo.foo", alphaNode.getExpr().toSourceString());
    LetContentNode betaNode = (LetContentNode) nodes.get(1);
    assertEquals("beta", betaNode.getVarName());
    assertEquals("Boo!", ((RawTextNode) betaNode.getChild(0)).getRawText());
    assertEquals(SanitizedContentKind.HTML, betaNode.getContentKind());
    LetContentNode gammaNode = (LetContentNode) nodes.get(2);
    assertEquals("gamma", gammaNode.getVarName());
    assertThat(gammaNode.getChild(0)).isInstanceOf(ForNode.class);
    assertEquals(SanitizedContentKind.HTML, gammaNode.getContentKind());
    LetContentNode deltaNode = (LetContentNode) nodes.get(3);
    assertEquals("delta", deltaNode.getVarName());
    assertEquals("Boo!", ((RawTextNode) betaNode.getChild(0)).getRawText());
    assertEquals(SanitizedContentKind.HTML, deltaNode.getContentKind());
    // Test error case.
    TemplateSubject.assertThatTemplateContent("{let $alpha /}{/let}").causesError("parse error at '/}': expected }, ':', or identifier").at(1, 13);
    // Test error case.
    TemplateSubject.assertThatTemplateContent("{let $alpha: $boo.foo}{/let}").causesError("parse error at '}': expected /}, ?, '?:', or, and, ==, !=, <, >, <=, >=, +, -, *, /, " + "%, ., ?., [, or ?[").at(1, 22);
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) LetValueNode(com.google.template.soy.soytree.LetValueNode) LetContentNode(com.google.template.soy.soytree.LetContentNode) Test(org.junit.Test)

Example 27 with StandaloneNode

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

the class TemplateParserTest method testParseBasicCallStmt.

@SuppressWarnings({ "ConstantConditions" })
@Test
public void testParseBasicCallStmt() throws Exception {
    String templateBody = "{@param too : ?}{@param animals: ?}\n" + "  {call .booTemplate_ /}\n" + "  {call foo.goo.mooTemplate data=\"all\" /}\n" + "  {call .booTemplate_ /}\n" + "  {call .zooTemplate data=\"$animals\"}\n" + "    {param yoo: round($too) /}\n" + "    {param woo kind=\"html\"}poo{/param}\n" + "    {param zoo: 0 /}\n" + "    {param doo kind=\"html\"}doopoo{/param}\n" + "  {/call}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertThat(nodes).hasSize(4);
    CallBasicNode cn0 = (CallBasicNode) nodes.get(0);
    assertEquals("brittle.test.ns.booTemplate_", cn0.getCalleeName());
    assertEquals(".booTemplate_", cn0.getSourceCalleeName());
    assertEquals(false, cn0.isPassingData());
    assertEquals(false, cn0.isPassingAllData());
    assertEquals(null, cn0.getDataExpr());
    assertEquals("XXX", cn0.genBasePhName());
    assertEquals(0, cn0.numChildren());
    CallBasicNode cn1 = (CallBasicNode) nodes.get(1);
    assertEquals("foo.goo.mooTemplate", cn1.getCalleeName());
    assertEquals("foo.goo.mooTemplate", cn1.getSourceCalleeName());
    assertEquals(true, cn1.isPassingData());
    assertEquals(true, cn1.isPassingAllData());
    assertEquals(null, cn1.getDataExpr());
    assertFalse(cn1.genSamenessKey().equals(cn0.genSamenessKey()));
    assertEquals(0, cn1.numChildren());
    CallBasicNode cn2 = (CallBasicNode) nodes.get(2);
    assertEquals("brittle.test.ns.booTemplate_", cn2.getCalleeName());
    assertEquals(".booTemplate_", cn2.getSourceCalleeName());
    assertFalse(cn2.isPassingData());
    assertEquals(false, cn2.isPassingAllData());
    assertEquals(null, cn2.getDataExpr());
    assertEquals("XXX", cn2.genBasePhName());
    assertEquals(0, cn2.numChildren());
    CallBasicNode cn3 = (CallBasicNode) nodes.get(3);
    assertEquals("brittle.test.ns.zooTemplate", cn3.getCalleeName());
    assertEquals(".zooTemplate", cn3.getSourceCalleeName());
    assertEquals(true, cn3.isPassingData());
    assertEquals(false, cn3.isPassingAllData());
    assertTrue(cn3.getDataExpr().getRoot() != null);
    assertEquals("$animals", cn3.getDataExpr().toSourceString());
    assertEquals(4, cn3.numChildren());
    {
        final CallParamValueNode cn4cpvn0 = (CallParamValueNode) cn3.getChild(0);
        assertEquals("yoo", cn4cpvn0.getKey().identifier());
        assertEquals("round($too)", cn4cpvn0.getExpr().toSourceString());
        assertTrue(cn4cpvn0.getExpr().getRoot() instanceof FunctionNode);
    }
    {
        final CallParamContentNode cn4cpcn1 = (CallParamContentNode) cn3.getChild(1);
        assertEquals("woo", cn4cpcn1.getKey().identifier());
        assertEquals(SanitizedContentKind.HTML, cn4cpcn1.getContentKind());
        assertEquals("poo", ((RawTextNode) cn4cpcn1.getChild(0)).getRawText());
    }
    {
        final CallParamValueNode cn4cpvn2 = (CallParamValueNode) cn3.getChild(2);
        assertEquals("zoo", cn4cpvn2.getKey().identifier());
        assertEquals("0", cn4cpvn2.getExpr().toSourceString());
    }
    {
        final CallParamContentNode cn4cpcn3 = (CallParamContentNode) cn3.getChild(3);
        assertEquals("doo", cn4cpcn3.getKey().identifier());
        assertNotNull(cn4cpcn3.getContentKind());
        assertEquals(SanitizedContentKind.HTML, cn4cpcn3.getContentKind());
        assertEquals("doopoo", ((RawTextNode) cn4cpcn3.getChild(0)).getRawText());
    }
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) CallParamContentNode(com.google.template.soy.soytree.CallParamContentNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) CallParamValueNode(com.google.template.soy.soytree.CallParamValueNode) CallBasicNode(com.google.template.soy.soytree.CallBasicNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) Test(org.junit.Test)

Example 28 with StandaloneNode

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

the class TemplateParserTest method testParseDelegateCallStmt.

@SuppressWarnings({ "ConstantConditions" })
@Test
public void testParseDelegateCallStmt() throws Exception {
    String templateBody = "{@param animals : ?}{@param too : ?}\n" + "  {delcall booTemplate /}\n" + "  {delcall foo.goo.mooTemplate data=\"all\" /}\n" + "  {delcall MySecretFeature.zooTemplate data=\"$animals\"}\n" + "    {param yoo: round($too) /}\n" + "    {param woo kind=\"html\"}poo{/param}\n" + "  {/delcall}\n";
    List<StandaloneNode> nodes = parseTemplateContent(templateBody, FAIL).getChildren();
    assertEquals(3, nodes.size());
    CallDelegateNode cn0 = (CallDelegateNode) nodes.get(0);
    assertEquals("booTemplate", cn0.getDelCalleeName());
    assertEquals(false, cn0.isPassingData());
    assertEquals(false, cn0.isPassingAllData());
    assertEquals(null, cn0.getDataExpr());
    assertEquals("XXX", cn0.genBasePhName());
    assertEquals(0, cn0.numChildren());
    CallDelegateNode cn1 = (CallDelegateNode) nodes.get(1);
    assertEquals("foo.goo.mooTemplate", cn1.getDelCalleeName());
    assertEquals(true, cn1.isPassingData());
    assertEquals(true, cn1.isPassingAllData());
    assertEquals(null, cn1.getDataExpr());
    assertFalse(cn1.genSamenessKey().equals(cn0.genSamenessKey()));
    assertEquals(0, cn1.numChildren());
    CallDelegateNode cn2 = (CallDelegateNode) nodes.get(2);
    assertEquals("MySecretFeature.zooTemplate", cn2.getDelCalleeName());
    assertEquals(true, cn2.isPassingData());
    assertEquals(false, cn2.isPassingAllData());
    assertTrue(cn2.getDataExpr().getRoot() != null);
    assertEquals("$animals", cn2.getDataExpr().toSourceString());
    assertEquals(2, cn2.numChildren());
    CallParamValueNode cn2cpvn0 = (CallParamValueNode) cn2.getChild(0);
    assertEquals("yoo", cn2cpvn0.getKey().identifier());
    assertEquals("round($too)", cn2cpvn0.getExpr().toSourceString());
    assertTrue(cn2cpvn0.getExpr().getRoot() instanceof FunctionNode);
    CallParamContentNode cn2cpcn1 = (CallParamContentNode) cn2.getChild(1);
    assertEquals("woo", cn2cpcn1.getKey().identifier());
    assertEquals("poo", ((RawTextNode) cn2cpcn1.getChild(0)).getRawText());
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) CallParamContentNode(com.google.template.soy.soytree.CallParamContentNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) CallParamValueNode(com.google.template.soy.soytree.CallParamValueNode) CallDelegateNode(com.google.template.soy.soytree.CallDelegateNode) Test(org.junit.Test)

Example 29 with StandaloneNode

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

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

the class LazyClosureCompiler method asRawTextOnly.

private Optional<Expression> asRawTextOnly(RenderUnitNode renderUnit) {
    StringBuilder builder = null;
    for (StandaloneNode child : renderUnit.getChildren()) {
        if (child instanceof RawTextNode) {
            if (builder == null) {
                builder = new StringBuilder();
            }
            builder.append(((RawTextNode) child).getRawText());
        } else {
            return Optional.absent();
        }
    }
    // TODO(lukes): ideally this would be a static final StringData field rather than reboxing each
    // time, but we don't (yet) have a good mechanism for that.
    SanitizedContentKind kind = renderUnit.getContentKind();
    Expression constant = constant(builder == null ? "" : builder.toString(), parentVariables);
    if (kind == null) {
        return Optional.<Expression>of(MethodRef.STRING_DATA_FOR_VALUE.invoke(constant));
    } else {
        return Optional.<Expression>of(MethodRef.ORDAIN_AS_SAFE.invoke(constant, constantSanitizedContentKindAsContentKind(kind)));
    }
}
Also used : StandaloneNode(com.google.template.soy.soytree.SoyNode.StandaloneNode) Statement.returnExpression(com.google.template.soy.jbcsrc.restricted.Statement.returnExpression) SoyExpression(com.google.template.soy.jbcsrc.restricted.SoyExpression) Expression(com.google.template.soy.jbcsrc.restricted.Expression) SanitizedContentKind(com.google.template.soy.base.internal.SanitizedContentKind) RawTextNode(com.google.template.soy.soytree.RawTextNode)

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