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);
}
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);
}
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);
}
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);
}
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);
}
Aggregations