Search in sources :

Example 31 with TemplateNode

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);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Test(org.junit.Test)

Example 32 with TemplateNode

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);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) ForNonemptyNode(com.google.template.soy.soytree.ForNonemptyNode) Test(org.junit.Test)

Example 33 with TemplateNode

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);
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Test(org.junit.Test)

Example 34 with TemplateNode

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");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) FunctionNode(com.google.template.soy.exprtree.FunctionNode) StringNode(com.google.template.soy.exprtree.StringNode) PrintNode(com.google.template.soy.soytree.PrintNode) Test(org.junit.Test)

Example 35 with TemplateNode

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");
}
Also used : TemplateNode(com.google.template.soy.soytree.TemplateNode) TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) RawTextNode(com.google.template.soy.soytree.RawTextNode) ForNonemptyNode(com.google.template.soy.soytree.ForNonemptyNode) Test(org.junit.Test)

Aggregations

TemplateNode (com.google.template.soy.soytree.TemplateNode)89 Test (org.junit.Test)59 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)35 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)19 ErrorReporter (com.google.template.soy.error.ErrorReporter)11 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)10 TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)9 RawTextNode (com.google.template.soy.soytree.RawTextNode)9 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)8 TemplateParam (com.google.template.soy.soytree.defn.TemplateParam)7 FunctionNode (com.google.template.soy.exprtree.FunctionNode)6 PrintNode (com.google.template.soy.soytree.PrintNode)5 TemplateDelegateNode (com.google.template.soy.soytree.TemplateDelegateNode)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 SourceLocation (com.google.template.soy.base.SourceLocation)4 StringNode (com.google.template.soy.exprtree.StringNode)4 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)4 ImmutableList (com.google.common.collect.ImmutableList)3 VarRefNode (com.google.template.soy.exprtree.VarRefNode)3 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)3