use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.
the class RenderVisitorTest method testRenderDelegateCallWithoutDefault.
@Test
public void testRenderDelegateCallWithoutDefault() throws Exception {
String soyFileContent1a = "{namespace ns1}\n" + "\n" + "/***/\n" + "{template .callerTemplate}\n" + " {delcall myApp.myDelegate allowemptydefault=\"true\"}\n" + " {param boo: 'aaaaaah' /}\n" + " {/delcall}\n" + "{/template}\n";
String soyFileContent1b = "{namespace ns1}\n" + "\n" + "/***/\n" + "{template .callerTemplate}\n" + " {delcall myApp.myDelegate}\n" + " {param boo: 'aaaaaah' /}\n" + " {/delcall}\n" + "{/template}\n";
String soyFileContent2 = "{delpackage SecretFeature}\n" + "{namespace ns2}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate}\n" + // implementation in SecretFeature
" 111 {$boo}\n" + "{/deltemplate}\n";
SoyRecord data = SoyValueConverterUtility.newDict();
ParseResult parseResult;
// ------ Test with only file 1a in bundle. ------
parseResult = SoyFileSetParserBuilder.forFileContents(soyFileContent1a).parse();
Predicate<String> activeDelPackageNames = Predicates.alwaysFalse();
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEmpty();
activeDelPackageNames = Predicates.equalTo("SecretFeature");
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEmpty();
// ------ Test with both files 1a and 2 in bundle. ------
parseResult = SoyFileSetParserBuilder.forFileContents(soyFileContent1a, soyFileContent2).parse();
activeDelPackageNames = Predicates.alwaysFalse();
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEmpty();
activeDelPackageNames = Predicates.equalTo("SecretFeature");
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEqualTo("111 aaaaaah");
activeDelPackageNames = Predicates.equalTo("NonexistentFeature");
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEmpty();
activeDelPackageNames = Predicates.in(ImmutableSet.of("NonexistentFeature", "SecretFeature"));
assertThat(renderTemplateInFile(parseResult, "ns1.callerTemplate", data, null, activeDelPackageNames)).isEqualTo("111 aaaaaah");
// ------ Test with only file 1b in bundle. ------
activeDelPackageNames = Predicates.alwaysFalse();
try {
renderTemplateInFile(SoyFileSetParserBuilder.forFileContents(soyFileContent1b).parse(), "ns1.callerTemplate", data, null, activeDelPackageNames);
fail("expected RenderException");
} catch (RenderException e) {
assertThat(e).hasMessageThat().contains("Found no active impl for delegate call");
}
try {
renderTemplateInFile(SoyFileSetParserBuilder.forFileContents(soyFileContent1b, soyFileContent2).parse(), "ns1.callerTemplate", data, null, activeDelPackageNames);
fail("expected RenderException");
} catch (RenderException e) {
assertThat(e).hasMessageThat().contains("Found no active impl for delegate call");
}
activeDelPackageNames = Predicates.equalTo("SecretFeature");
assertThat(renderTemplateInFile(SoyFileSetParserBuilder.forFileContents(soyFileContent1b, soyFileContent2).parse(), "ns1.callerTemplate", data, null, activeDelPackageNames)).isEqualTo("111 aaaaaah");
}
use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.
the class FindTransitiveDepTemplatesVisitorTest method testTwoPathsToSameTemplate.
@Test
public void testTwoPathsToSameTemplate() {
// aaa -> {bbb, ccc}, ccc -> bbb.
String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + " {call .bbb /} {$ij.boo} {call .ccc /} {$ij.foo}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + " {$ij.boo} {$ij.goo}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + " {$ij.boo} {$ij.moo + $ij.woo} {call .bbb /}\n" + "{/template}\n";
ParseResult result = SoyFileSetParserBuilder.forFileContents(fileContent).errorReporter(FAIL).parse();
TemplateRegistry templateRegistry = result.registry();
SoyFileSetNode soyTree = result.fileSet();
TemplateNode aaa = soyTree.getChild(0).getChild(0);
TemplateNode bbb = soyTree.getChild(0).getChild(1);
TemplateNode ccc = soyTree.getChild(0).getChild(2);
// Test with exec(aaa).
// Exercises: processCalleeHelper case 4 with incorporateCalleeVisitInfo case 1 (ccc -> bbb).
FindTransitiveDepTemplatesVisitor visitor = new FindTransitiveDepTemplatesVisitor(templateRegistry);
Map<TemplateNode, TransitiveDepTemplatesInfo> memoizedInfoMap = visitor.templateToFinishedInfoMap;
visitor.exec(aaa);
assertThat(memoizedInfoMap).hasSize(3);
assertThat(memoizedInfoMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb));
assertThat(memoizedInfoMap.get(ccc).depTemplateSet).isEqualTo(ImmutableSet.of(ccc, bbb));
assertThat(memoizedInfoMap.get(aaa).depTemplateSet).isEqualTo(ImmutableSet.of(aaa, bbb, ccc));
}
use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.
the class FindTransitiveDepTemplatesVisitorTest method testExecOnAllTemplates.
@Test
public void testExecOnAllTemplates() {
// aaa -> {bbb, ccc}, bbb -> ddd.
String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + " {$ij.boo} {$ij.goo} {call .ddd /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + " {call .bbb /} {$ij.boo} {call .ccc /} {$ij.foo}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + " {$ij.boo} {$ij.moo + $ij.woo}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ddd}\n" + " {$ij.boo} {$ij.moo} {round($ij.zoo)}\n" + "{/template}\n";
ParseResult result = SoyFileSetParserBuilder.forFileContents(fileContent).errorReporter(FAIL).parse();
TemplateRegistry templateRegistry = result.registry();
SoyFileSetNode soyTree = result.fileSet();
TemplateNode bbb = soyTree.getChild(0).getChild(0);
TemplateNode aaa = soyTree.getChild(0).getChild(1);
TemplateNode ccc = soyTree.getChild(0).getChild(2);
TemplateNode ddd = soyTree.getChild(0).getChild(3);
ImmutableMap<TemplateNode, TransitiveDepTemplatesInfo> resultMap = new FindTransitiveDepTemplatesVisitor(templateRegistry).execOnAllTemplates(soyTree);
assertThat(resultMap).hasSize(4);
assertThat(resultMap.get(ddd).depTemplateSet).isEqualTo(ImmutableSet.of(ddd));
assertThat(resultMap.get(ccc).depTemplateSet).isEqualTo(ImmutableSet.of(ccc));
assertThat(resultMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb, ddd));
assertThat(resultMap.get(aaa).depTemplateSet).isEqualTo(ImmutableSet.of(aaa, bbb, ccc, ddd));
}
use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.
the class FindTransitiveDepTemplatesVisitorTest method testLargerRecursiveCycle.
@Test
public void testLargerRecursiveCycle() {
// Tests indirect recursion with a cycle of 3.
// aaa -> bbb, bbb -> ccc, ccc -> aaa.
String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + " {$ij.foo} {$ij.boo} {call .bbb /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + " {$ij.goo} {call .ccc /} {$ij.boo}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + " {call .aaa /} {$ij.moo} {$ij.boo}\n" + "{/template}\n";
ParseResult result = SoyFileSetParserBuilder.forFileContents(fileContent).errorReporter(FAIL).parse();
TemplateRegistry templateRegistry = result.registry();
SoyFileSetNode soyTree = result.fileSet();
TemplateNode aaa = soyTree.getChild(0).getChild(0);
TemplateNode bbb = soyTree.getChild(0).getChild(1);
TemplateNode ccc = soyTree.getChild(0).getChild(2);
// Test with exec(aaa).
// Exercises: processCalleeHelper case 3 (ccc-> aaa).
// Exercises: processCalleeHelper case 5 with incorporateCalleeVisitInfo case 3 (bbb -> ccc).
// Exercises: processCalleeHelper case 5 with incorporateCalleeVisitInfo case 2 (aaa -> bbb).
FindTransitiveDepTemplatesVisitor visitor = new FindTransitiveDepTemplatesVisitor(templateRegistry);
Map<TemplateNode, TransitiveDepTemplatesInfo> memoizedInfoMap = visitor.templateToFinishedInfoMap;
visitor.exec(aaa);
assertThat(memoizedInfoMap).hasSize(3);
assertThat(memoizedInfoMap.get(ccc).depTemplateSet).isEqualTo(ImmutableSet.of(ccc, aaa, bbb));
assertThat(memoizedInfoMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb, ccc, aaa));
assertThat(memoizedInfoMap.get(aaa).depTemplateSet).isEqualTo(ImmutableSet.of(aaa, bbb, ccc));
}
use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.
the class VeLogInstrumentationVisitorTest method runPass.
/**
* Parses the given input as a template content.
*/
private static SoyFileSetNode runPass(String input) {
String soyFile = Joiner.on('\n').join("{namespace ns}", "", "{template .t}", input, "{/template}");
ParseResult result = SoyFileSetParserBuilder.forFileContents(soyFile).desugarHtmlNodes(false).typeRegistry(new SoyTypeRegistry.Builder().addDescriptors(ImmutableList.of(com.google.template.soy.testing.Foo.getDescriptor())).build()).setLoggingConfig(LOGGING_CONFIG).addSoyFunction(new TestLoggingFunction()).errorReporter(ErrorReporter.exploding()).parse();
TemplateRegistry templateRegistry = result.registry();
SoyFileSetNode soyTree = result.fileSet();
new VeLogInstrumentationVisitor(templateRegistry).exec(soyTree);
return soyTree;
}
Aggregations