Search in sources :

Example 26 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.

the class ResolveExpressionTypesVisitorTest method testMapKeys.

@Test
public void testMapKeys() {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(constructTemplateSource("{@param m: map<string, int>}", "{assertType('list<string>', mapKeys($m))}", "{assertType('list<null>', mapKeys(map()))}", "")).addSoyFunction(ASSERT_TYPE_FUNCTION).parse().fileSet();
    assertTypes(soyTree);
}
Also used : SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Test(org.junit.Test)

Example 27 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode in project closure-templates by google.

the class ResolveExpressionTypesVisitorTest method testDataFlowTypeNarrowing.

@Test
public void testDataFlowTypeNarrowing() {
    SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(constructTemplateSource("{@param pa: bool|null}", "{@param pb: bool}", "{if $pa != null}", // #0 must be non-null
    "  {assertType('bool', $pa)}", "{/if}", "{if $pa == null}", // #1 must be null
    "  {assertType('null', $pa)}", "{else}", // #2 must be non-null
    "  {assertType('bool', $pa)}", "{/if}", "{if $pa == null or $pb}", // #3 don't know
    "  {assertType('bool|null', $pa)}", "{else}", // #4 must be non-null
    "  {assertType('bool', $pa)}", "{/if}", "{if $pa == null and $pb}", // #5 must be null
    "  {assertType('null', $pa)}", "{else}", // #6 don't know
    "  {assertType('bool|null', $pa)}", "{/if}", // Reverse order
    "{if null != $pa}", // #7 must be non-null
    "  {assertType('bool', $pa)}", "{/if}", // Not operator
    "{if not ($pa == null)}", // #8 must be non-null
    "  {assertType('bool', $pa)}", "{/if}", // Implicit != null
    "{if $pa}", // #9 must be non-null
    "  {assertType('bool', $pa)}", "{/if}", // Implicit != null
    "{if $pa and $pb}", // #10 must be non-null
    "  {assertType('bool', $pa)}", "{/if}", // Chained conditions
    "{if $pa}", "{elseif $pb}", // #11 must be falsy
    "  {assertType('bool|null', $pa)}", "{else}", // #12 must be falsy
    "  {assertType('bool|null', $pa)}", "{/if}", // Nested if
    "{if $pa}", "  {if $pa}", // #13 must be non-null
    "    {assertType('bool', $pa)}", "  {/if}", "{/if}", // isNonnull function
    "{if isNonnull($pa)}", // #14 must be non-null
    "  {assertType('bool', $pa)}", "{else}", // #15 must be null
    "  {assertType('null', $pa)}", "{/if}", // isNull function
    "{if isNull($pa)}", // #16 must be null
    "  {assertType('null', $pa)}", "{else}", // #17 must be non-null
    "  {assertType('bool', $pa)}", "{/if}", "{if $pb or $pa == null}", // #18 don't know
    "  {assertType('bool|null', $pa)}", "{else}", // #19 must be non-null
    "  {assertType('bool', $pa)}", "{/if}", "{let $null: null /}", "{if $null == null or $null != null}", // #20  null type
    "  {assertType('null', $null)}", "{/if}", "{if $null}", // #21 null type (but this branch is dead)
    "  {assertType('null', $null)}", "{/if}", "")).addSoyFunction(ASSERT_TYPE_FUNCTION).parse().fileSet();
    assertTypes(soyTree);
}
Also used : SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) Test(org.junit.Test)

Example 28 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode 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 29 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode 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 30 with SoyFileSetNode

use of com.google.template.soy.soytree.SoyFileSetNode 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)

Aggregations

SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)106 Test (org.junit.Test)81 TemplateNode (com.google.template.soy.soytree.TemplateNode)35 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)29 ErrorReporter (com.google.template.soy.error.ErrorReporter)19 ParseResult (com.google.template.soy.SoyFileSetParser.ParseResult)13 TransitiveDepTemplatesInfo (com.google.template.soy.passes.FindTransitiveDepTemplatesVisitor.TransitiveDepTemplatesInfo)8 RawTextNode (com.google.template.soy.soytree.RawTextNode)8 MsgNode (com.google.template.soy.soytree.MsgNode)7 SoyNode (com.google.template.soy.soytree.SoyNode)6 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)5 SoyMsgBundle (com.google.template.soy.msgs.SoyMsgBundle)5 PrintNode (com.google.template.soy.soytree.PrintNode)5 MsgFallbackGroupNode (com.google.template.soy.soytree.MsgFallbackGroupNode)4 SoyFileNode (com.google.template.soy.soytree.SoyFileNode)4 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)3 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)3 SoyMsgBundleImpl (com.google.template.soy.msgs.restricted.SoyMsgBundleImpl)3 PluginResolver (com.google.template.soy.soyparse.PluginResolver)3 ImmutableList (com.google.common.collect.ImmutableList)2