use of com.google.template.soy.soytree.TemplateNode in project closure-templates by google.
the class ResolveNamesVisitorTest method testParamNameLookupSuccess.
@Test
public void testParamNameLookupSuccess() {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(constructTemplateSource("{@param pa: bool}", "{$pa ? 1 : 0}")).parse().fileSet();
new ResolveNamesVisitor(ErrorReporter.exploding()).exec(soyTree);
TemplateNode n = soyTree.getChild(0).getChild(0);
assertThat(n.getMaxLocalVariableTableSize()).isEqualTo(1);
assertThat(n.getParams().get(0).localVariableIndex()).isEqualTo(0);
}
use of com.google.template.soy.soytree.TemplateNode in project closure-templates by google.
the class ResolveNamesVisitorTest method testMultipleLocalsAndScopesNumbering.
@Test
public void testMultipleLocalsAndScopesNumbering() {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(constructTemplateSource("{@param pa: bool}", "{@param pb: bool}", "{let $la: 1 /}", "{for $item in ['a', 'b']}", " {$pa ? 1 : 0}{$pb ? 1 : 0}{$la + $item}", "{/for}", "{let $lb: 1 /}")).parse().fileSet();
new ResolveNamesVisitor(ErrorReporter.exploding()).exec(soyTree);
TemplateNode n = soyTree.getChild(0).getChild(0);
// 6 because we have 2 params, 1 let and a foreach loop var which needs 3 slots (variable,
// index, lastIndex) active within the foreach loop. the $lb can reuse a slot for the foreach
// loop variable
assertThat(n.getMaxLocalVariableTableSize()).isEqualTo(6);
assertThat(n.getParams().get(0).localVariableIndex()).isEqualTo(0);
assertThat(n.getParams().get(1).localVariableIndex()).isEqualTo(1);
assertThat(((LetValueNode) n.getChild(0)).getVar().localVariableIndex()).isEqualTo(2);
ForNonemptyNode forNonemptyNode = (ForNonemptyNode) ((ForNode) n.getChild(1)).getChild(0);
assertThat(forNonemptyNode.getVar().localVariableIndex()).isEqualTo(3);
assertThat(forNonemptyNode.getVar().currentLoopIndexIndex()).isEqualTo(4);
assertThat(forNonemptyNode.getVar().isLastIteratorIndex()).isEqualTo(5);
// The loop variables are out of scope so we can reuse the 3rd slot
assertThat(((LetValueNode) n.getChild(2)).getVar().localVariableIndex()).isEqualTo(3);
}
use of com.google.template.soy.soytree.TemplateNode in project closure-templates by google.
the class ResolveNamesVisitorTest method testInjectedParamNameLookupSuccess.
@Test
public void testInjectedParamNameLookupSuccess() {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(constructTemplateSource("{@inject pa: bool}", "{$pa ? 1 : 0}")).parse().fileSet();
new ResolveNamesVisitor(ErrorReporter.exploding()).exec(soyTree);
TemplateNode n = soyTree.getChild(0).getChild(0);
assertThat(n.getMaxLocalVariableTableSize()).isEqualTo(1);
assertThat(n.getInjectedParams().get(0).localVariableIndex()).isEqualTo(0);
}
use of com.google.template.soy.soytree.TemplateNode 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");
}
use of com.google.template.soy.soytree.TemplateNode in project closure-templates by google.
the class SimplifyVisitorTest method testCombineConsecutiveRawTextNodes.
@Test
public void testCombineConsecutiveRawTextNodes() throws Exception {
String soyCode = "{@param boo : ?}\n" + "blah{$boo}blah" + "{for $i in range(5)}" + " blah{$boo}blah" + "{/for}";
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forTemplateContents(soyCode).parse().fileSet();
TemplateNode template = soyTree.getChild(0).getChild(0);
ForNonemptyNode forNode = (ForNonemptyNode) ((ForNode) template.getChild(3)).getChild(0);
forNode.addChild(new RawTextNode(0, "bleh", SourceLocation.UNKNOWN));
forNode.addChild(new RawTextNode(0, "bluh", SourceLocation.UNKNOWN));
template.addChild(0, new RawTextNode(0, "bleh", SourceLocation.UNKNOWN));
template.addChild(0, new RawTextNode(0, "bluh", SourceLocation.UNKNOWN));
assertThat(template.numChildren()).isEqualTo(6);
assertThat(forNode.numChildren()).isEqualTo(5);
SimplifyVisitor simplifyVisitor = SimplifyVisitor.create();
simplifyVisitor.simplify(soyTree, new TemplateRegistry(soyTree, ErrorReporter.exploding()));
assertThat(template.numChildren()).isEqualTo(4);
assertThat(forNode.numChildren()).isEqualTo(3);
assertThat(((RawTextNode) template.getChild(0)).getRawText()).isEqualTo("bluhblehblah");
assertThat(((RawTextNode) forNode.getChild(2)).getRawText()).isEqualTo("blahblehbluh");
}
Aggregations