use of com.google.template.soy.soytree.LetValueNode 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.LetValueNode in project closure-templates by google.
the class ResolveNamesVisitorTest method testMultipleLocals.
@Test
public void testMultipleLocals() {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(constructTemplateSource("{let $la: 1 /}", "{let $lb: $la /}", "{let $lc: $lb /}")).parse().fileSet();
new ResolveNamesVisitor(ErrorReporter.exploding()).exec(soyTree);
TemplateNode n = soyTree.getChild(0).getChild(0);
// 3 because each new $la binding is a 'new variable'
assertThat(n.getMaxLocalVariableTableSize()).isEqualTo(3);
LetValueNode firstLet = (LetValueNode) n.getChild(0);
LetValueNode secondLet = (LetValueNode) n.getChild(1);
LetValueNode thirdLet = (LetValueNode) n.getChild(2);
assertThat(firstLet.getVar().localVariableIndex()).isEqualTo(0);
assertThat(secondLet.getVar().localVariableIndex()).isEqualTo(1);
assertThat(thirdLet.getVar().localVariableIndex()).isEqualTo(2);
assertThat(((VarRefNode) secondLet.getExpr().getRoot()).getDefnDecl()).isEqualTo(firstLet.getVar());
assertThat(((VarRefNode) thirdLet.getExpr().getRoot()).getDefnDecl()).isEqualTo(secondLet.getVar());
}
use of com.google.template.soy.soytree.LetValueNode in project closure-templates by google.
the class ResolveNamesVisitorTest method testLetContentSlotLifetime.
@Test
public void testLetContentSlotLifetime() {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(constructTemplateSource("{let $a kind=\"text\"}", // introduce an extra scope
" {if true}", " {let $b: 2 /}", " {$b}", " {/if}", "{/let}", "{$a}")).parse().fileSet();
new ResolveNamesVisitor(ErrorReporter.exploding()).exec(soyTree);
TemplateNode n = soyTree.getChild(0).getChild(0);
// 1 because each new $la binding overwrites the prior one
assertThat(n.getMaxLocalVariableTableSize()).isEqualTo(2);
LetContentNode aLetNode = (LetContentNode) n.getChild(0);
assertThat(aLetNode.getVar().localVariableIndex()).isEqualTo(1);
LetValueNode bLetNode = (LetValueNode) ((IfCondNode) ((IfNode) aLetNode.getChild(0)).getChild(0)).getChild(0);
assertThat(bLetNode.getVar().localVariableIndex()).isEqualTo(0);
}
Aggregations