use of com.google.template.soy.soytree.ForNonemptyNode 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());
}
use of com.google.template.soy.soytree.ForNonemptyNode in project closure-templates by google.
the class SoyNodeCompiler method calculateRangeArgs.
/**
* Interprets the given expressions as the arguments of a {@code range(...)} expression in a
* {@code foreach} loop.
*/
private CompiledForeachRangeArgs calculateRangeArgs(ForNode forNode, Scope scope) {
RangeArgs rangeArgs = RangeArgs.createFromNode(forNode).get();
ForNonemptyNode nonEmptyNode = (ForNonemptyNode) forNode.getChild(0);
ImmutableList.Builder<Statement> initStatements = ImmutableList.builder();
Expression startExpression = computeRangeValue(SyntheticVarName.foreachLoopRangeStart(nonEmptyNode), rangeArgs.start(), 0, scope, initStatements);
Expression stepExpression = computeRangeValue(SyntheticVarName.foreachLoopRangeStep(nonEmptyNode), rangeArgs.increment(), 1, scope, initStatements);
Expression endExpression = computeRangeValue(SyntheticVarName.foreachLoopRangeEnd(nonEmptyNode), Optional.of(rangeArgs.limit()), Integer.MAX_VALUE, scope, initStatements);
return new AutoValue_SoyNodeCompiler_CompiledForeachRangeArgs(startExpression, endExpression, stepExpression, initStatements.build());
}
Aggregations