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);
}
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());
}
}
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());
}
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()));
}
}
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)));
}
}
Aggregations