use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.
the class FindTransitiveDepTemplatesVisitorTest method testTwoPathsToSameRecursiveCycle.
@Test
public void testTwoPathsToSameRecursiveCycle() {
// aaa -> {bbb, ccc}, bbb -> ddd, ccc -> ddd, ddd -> bbb.
String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + " {$ij.boo} {$ij.foo} {call .bbb /} {call .ccc /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + " {$ij.boo} {$ij.goo} {call .ddd /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + " {$ij.boo} {$ij.moo} {call .ddd /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ddd}\n" + " {$ij.boo} {$ij.too} {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);
TemplateNode ddd = soyTree.getChild(0).getChild(3);
// Test with exec(aaa).
// Exercises: processCalleeHelper case 4 with incorporateCalleeVisitInfo case 4 (ccc -> ddd).
FindTransitiveDepTemplatesVisitor visitor = new FindTransitiveDepTemplatesVisitor(templateRegistry);
Map<TemplateNode, TransitiveDepTemplatesInfo> memoizedInfoMap = visitor.templateToFinishedInfoMap;
visitor.exec(aaa);
assertThat(memoizedInfoMap).hasSize(4);
assertThat(memoizedInfoMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb, ddd));
assertThat(memoizedInfoMap.get(ddd).depTemplateSet).isEqualTo(ImmutableSet.of(ddd, bbb));
assertThat(memoizedInfoMap.get(ccc).depTemplateSet).isEqualTo(ImmutableSet.of(ccc, ddd, bbb));
assertThat(memoizedInfoMap.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 testSimpleRecursion.
@Test
public void testSimpleRecursion() {
// Tests direct recursion (cycle of 1) and indirect recursion with a cycle of 2.
// aaa -> bbb, bbb -> {bbb, ccc}, ccc -> bbb.
String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + " {call .bbb /} {$ij.boo} {$ij.foo}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + " {$ij.boo} {$ij.goo} {call .bbb /} {call .ccc /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + " {$ij.boo} {call .bbb /} {$ij.moo}\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 2 (bbb -> bbb).
// Exercises: processCalleeHelper case 3 (ccc -> bbb).
// Exercises: processCalleeHelper case 5 with incorporateCalleeVisitInfo case 2 (bbb -> ccc).
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, bbb));
assertThat(memoizedInfoMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb, ccc));
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 PrerenderVisitorTest method prerender.
// -----------------------------------------------------------------------------------------------
// Helpers.
/**
* Renders the given input string (should be a template body) and returns the result.
*
* @param input The input string to prerender.
* @return The rendered result.
* @throws Exception If there's an error.
*/
private String prerender(String input) throws Exception {
ParseResult result = SoyFileSetParserBuilder.forTemplateContents(input).parse();
StringBuilder outputSb = new StringBuilder();
PrerenderVisitor prerenderVisitor = new PrerenderVisitor(new PreevalVisitorFactory(), outputSb, result.registry());
prerenderVisitor.exec(result.fileSet().getChild(0).getChild(0));
return outputSb.toString();
}
use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.
the class SimplifyVisitorTest method simplifySoyCode.
private List<StandaloneNode> simplifySoyCode(String soyCode) throws Exception {
ParseResult parse = SoyFileSetParserBuilder.forTemplateContents(soyCode).parse();
SimplifyVisitor simplifyVisitor = SimplifyVisitor.create();
simplifyVisitor.simplify(parse.fileSet(), parse.registry());
return parse.fileSet().getChild(0).getChild(0).getChildren();
}
use of com.google.template.soy.SoyFileSetParser.ParseResult in project closure-templates by google.
the class RenderVisitorTest method testRenderDelegateVariantCall.
@Test
public void testRenderDelegateVariantCall() throws Exception {
String soyFileContent1 = "" + "{namespace ns1}\n" + "\n" + "/** @param greekB */\n" + "{template .callerTemplate}\n" + " {delcall myApp.myDelegate variant=\"'alpha'\"}\n" + // variant is string
" {param boo: 'zzz' /}\n" + " {/delcall}\n" + " {delcall myApp.myDelegate variant=\"$greekB\"}\n" + // variant is expression
" {param boo: 'zzz' /}\n" + " {/delcall}\n" + " {delcall myApp.myDelegate variant=\"'gamma'\"}\n" + // variant "gamma" not implemented
" {param boo: 'zzz' /}\n" + " {/delcall}\n" + " {delcall myApp.myDelegate variant=\"test.GLOBAL\"}\n" + // variant is a global expression
" {param boo: 'zzz' /}\n" + " {/delcall}\n" + "{/template}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate}\n" + // variant "" default
" 000empty\n" + "{/deltemplate}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"'alpha'\"}\n" + // variant "alpha" default
" 000alpha\n" + "{/deltemplate}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"'beta'\"}\n" + // variant "beta" default
" 000beta\n" + "{/deltemplate}\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"test.GLOBAL\"}\n" + // variant using global
" 000global\n" + "{/deltemplate}\n";
String soyFileContent2 = "" + "{delpackage SecretFeature}\n" + "{namespace ns2}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate}\n" + // variant "" in SecretFeature
" 111empty\n" + "{/deltemplate}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"'alpha'\"}\n" + // "alpha" in SecretFeature
" 111alpha\n" + "{/deltemplate}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"'beta'\"}\n" + // "beta" in SecretFeature
" 111beta\n" + "{/deltemplate}\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"test.GLOBAL\"}\n" + // variant using global
" 111global\n" + "{/deltemplate}\n";
String soyFileContent3 = "" + "{delpackage AlternateSecretFeature}\n" + "{namespace ns3}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate}\n" + // variant "" in AlternateSecretFeature
" 222empty\n" + "{/deltemplate}\n" + "\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"'alpha'\"}\n" + // variant "alpha" in Alternate
" 222alpha\n" + "{/deltemplate}\n" + "/** @param boo */\n" + "{deltemplate myApp.myDelegate variant=\"test.GLOBAL\"}\n" + // variant using global
" 222global\n" + // Note: No variant "beta" in AlternateSecretFeature.
"{/deltemplate}\n";
SoyGeneralOptions options = new SoyGeneralOptions();
options.setCompileTimeGlobals(ImmutableMap.<String, Object>of("test.GLOBAL", 1));
ParseResult result = SoyFileSetParserBuilder.forFileContents(soyFileContent1, soyFileContent2, soyFileContent3).options(options).errorReporter(FAIL).parse();
Predicate<String> activeDelPackageNames = Predicates.alwaysFalse();
assertThat(renderTemplateInFile(result, "ns1.callerTemplate", TEST_DATA, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("000alpha000beta000empty000global");
activeDelPackageNames = Predicates.equalTo("SecretFeature");
assertThat(renderTemplateInFile(result, "ns1.callerTemplate", TEST_DATA, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("111alpha111beta111empty111global");
activeDelPackageNames = Predicates.equalTo("AlternateSecretFeature");
assertThat(renderTemplateInFile(result, "ns1.callerTemplate", TEST_DATA, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("222alpha000beta222empty222global");
activeDelPackageNames = Predicates.equalTo("NonexistentFeature");
assertThat(renderTemplateInFile(result, "ns1.callerTemplate", TEST_DATA, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("000alpha000beta000empty000global");
activeDelPackageNames = Predicates.in(ImmutableSet.of("NonexistentFeature", "AlternateSecretFeature"));
assertThat(renderTemplateInFile(result, "ns1.callerTemplate", TEST_DATA, TEST_IJ_DATA, activeDelPackageNames)).isEqualTo("222alpha000beta222empty222global");
try {
renderTemplateInFile(result, "ns1.callerTemplate", TEST_DATA, TEST_IJ_DATA, Predicates.in(ImmutableSet.of("SecretFeature", "AlternateSecretFeature")));
fail("expected RenderException");
} catch (RenderException e) {
assertThat(e).hasMessageThat().contains("For delegate template 'myApp.myDelegate:alpha', found two active implementations " + "with equal priority");
}
}
Aggregations