use of com.google.template.soy.soytree.TemplateRegistry in project closure-templates by google.
the class RenderVisitorTest method testRenderFuture.
@Test
public void testRenderFuture() throws Exception {
final StringBuilder progress = new StringBuilder();
Flushable flushable = new Flushable() {
@Override
public void flush() {
progress.append("flush;");
}
};
String soyFileContent = "{namespace ns}\n" + "\n" + "/** @param boo @param foo @param goo */\n" + "{template .callerTemplate autoescape=\"deprecated-noncontextual\"}\n" + " {call .calleeTemplate data=\"all\" /}\n" + " {call .calleeTemplate data=\"$foo\" /}\n" + " {call .calleeTemplate data=\"all\"}\n" + " {param boo: $foo.boo /}\n" + " {/call}\n" + " {call .calleeTemplate data=\"all\"}\n" + " {param boo: 'moo' /}\n" + " {/call}\n" + " {call .calleeTemplate data=\"$foo\"}\n" + " {param boo}moo{/param}\n" + " {/call}\n" + " {call .calleeTemplate}\n" + " {param boo}zoo{/param}\n" + " {param goo: $foo.goo /}\n" + " {/call}\n" + "{/template}\n" + "\n" + "/**\n" + " * @param boo\n" + " * @param goo\n" + " */\n" + "{template .calleeTemplate autoescape=\"deprecated-noncontextual\"}\n" + " {$boo}{$ij.future}\n" + " {for $n in $goo} {$n}{/for}{\\n}\n" + "{/template}\n";
TemplateRegistry templateRegistry = SoyFileSetParserBuilder.forFileContents(soyFileContent).errorReporter(FAIL).parse().registry();
SoyDict foo = SoyValueConverterUtility.newDict("boo", new TestFuture("foo", progress), "goo", SoyValueConverterUtility.newList(3, 2, 1));
SoyDict data = SoyValueConverterUtility.newDict("boo", new TestFuture("boo", progress), "foo", foo, "goo", SoyValueConverterUtility.newList(1, 2, 3));
SoyRecord testIj = SoyValueConverterUtility.newDict("future", new TestFuture("ij", progress));
StringBuilder outputSb = new StringBuilder();
CountingFlushableAppendable output = new CountingFlushableAppendable(outputSb, flushable);
RenderVisitor rv = new RenderVisitor(new EvalVisitorFactoryImpl(), output, templateRegistry, data, testIj, Predicates.<String>alwaysFalse(), null, xidRenamingMap, cssRenamingMap, false);
rv.exec(templateRegistry.getBasicTemplate("ns.callerTemplate"));
String expectedOutput = "booij 1 2 3\n" + "fooij 3 2 1\n" + "fooij 1 2 3\n" + "mooij 1 2 3\n" + "mooij 3 2 1\n" + "zooij 3 2 1\n";
assertThat(outputSb.toString()).isEqualTo(expectedOutput);
assertThat(progress.toString()).isEqualTo("booflush;ijflush;foo");
}
use of com.google.template.soy.soytree.TemplateRegistry in project closure-templates by google.
the class AddHtmlCommentsForDebugPassTest method runPass.
/**
* Parses the given input as a Soy file content, runs the AddHtmlCommentsForDebug pass and returns
* a map that contains pairs of template names and rewritten template body.
*/
private static ImmutableMap<String, String> runPass(String input, String fileName) {
String soyFile = "{namespace ns}" + input;
IncrementingIdGenerator nodeIdGen = new IncrementingIdGenerator();
ErrorReporter boom = ErrorReporter.exploding();
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forSuppliers(SoyFileSupplier.Factory.create(soyFile, SoyFileKind.SRC, fileName)).errorReporter(boom).parse().fileSet();
TemplateRegistry registry = new TemplateRegistry(soyTree, boom);
// We need to run HtmlRewritePass to produce HTML nodes. Otherwise we will not add any comments.
new HtmlRewritePass(boom).run(soyTree.getChild(0), nodeIdGen);
new AddHtmlCommentsForDebugPass().run(soyTree, registry);
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
for (TemplateNode node : soyTree.getChild(0).getChildren()) {
StringBuilder sb = new StringBuilder();
node.appendSourceStringForChildren(sb);
String templateName = (node instanceof TemplateDelegateNode) ? ((TemplateDelegateNode) node).getDelTemplateName() : node.getTemplateName();
builder.put(templateName, sb.toString());
}
return builder.build();
}
use of com.google.template.soy.soytree.TemplateRegistry in project closure-templates by google.
the class FindIjParamsVisitorTest method testExecForAllTemplates.
@Test
public void testExecForAllTemplates() {
// 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";
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(fileContent).parse().fileSet();
TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, FAIL);
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, IjParamsInfo> templateToIjParamsInfoMap = new FindIjParamsVisitor(templateRegistry).execOnAllTemplates(soyTree);
assertThat(templateToIjParamsInfoMap).hasSize(4);
assertThat(templateToIjParamsInfoMap.get(ddd).ijParamToCalleesMultimap).hasSize(3);
assertThat(templateToIjParamsInfoMap.get(ccc).ijParamToCalleesMultimap).hasSize(3);
assertThat(templateToIjParamsInfoMap.get(bbb).ijParamToCalleesMultimap).hasSize(5);
assertThat(templateToIjParamsInfoMap.get(bbb).ijParamToCalleesMultimap.keySet()).hasSize(4);
assertThat(templateToIjParamsInfoMap.get(aaa).ijParamToCalleesMultimap).hasSize(10);
assertThat(templateToIjParamsInfoMap.get(aaa).ijParamToCalleesMultimap.keySet()).hasSize(6);
}
use of com.google.template.soy.soytree.TemplateRegistry in project closure-templates by google.
the class FindIjParamsVisitorTest method testSimple.
@Test
public void testSimple() {
// aaa -> {bbb, ccc}, bbb -> ddd.
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} {call .ddd /}\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";
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents(fileContent).parse().fileSet();
TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, FAIL);
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 5 with incorporateCalleeVisitInfo case 1 (aaa -> bbb).
FindIjParamsVisitor visitor = new FindIjParamsVisitor(templateRegistry);
visitor.exec(aaa);
assertThat(visitor.exec(ddd).ijParamToCalleesMultimap).hasSize(3);
assertThat(visitor.exec(ccc).ijParamToCalleesMultimap).hasSize(3);
assertThat(visitor.exec(bbb).ijParamToCalleesMultimap).hasSize(5);
assertThat(visitor.exec(bbb).ijParamToCalleesMultimap.keySet()).hasSize(4);
assertThat(visitor.exec(aaa).ijParamToCalleesMultimap).hasSize(10);
assertThat(visitor.exec(aaa).ijParamToCalleesMultimap.keySet()).hasSize(6);
// Test with exec(bbb) then exec(aaa).
// Exercises: processCalleeHelper case 1 (aaa -> bbb).
visitor = new FindIjParamsVisitor(templateRegistry);
visitor.exec(bbb);
assertThat(visitor.exec(ddd).ijParamToCalleesMultimap).hasSize(3);
assertThat(visitor.exec(bbb).ijParamToCalleesMultimap).hasSize(5);
assertThat(visitor.exec(bbb).ijParamToCalleesMultimap.keySet()).hasSize(4);
visitor.exec(aaa);
assertThat(visitor.exec(ddd).ijParamToCalleesMultimap).hasSize(3);
assertThat(visitor.exec(ccc).ijParamToCalleesMultimap).hasSize(3);
assertThat(visitor.exec(bbb).ijParamToCalleesMultimap).hasSize(5);
assertThat(visitor.exec(bbb).ijParamToCalleesMultimap.keySet()).hasSize(4);
assertThat(visitor.exec(aaa).ijParamToCalleesMultimap).hasSize(10);
assertThat(visitor.exec(aaa).ijParamToCalleesMultimap.keySet()).hasSize(6);
}
use of com.google.template.soy.soytree.TemplateRegistry in project closure-templates by google.
the class FindTransitiveDepTemplatesVisitorTest method testSmallerRecursiveCycleInLargerRecursiveCycle.
@Test
public void testSmallerRecursiveCycleInLargerRecursiveCycle() {
// aaa -> {bbb, ccc}, bbb -> aaa, ccc -> bbb.
String fileContent = "" + "{namespace ns}\n" + "\n" + "/***/\n" + "{template .aaa}\n" + " {$ij.foo} {$ij.boo} {call .bbb /} {call .ccc /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .bbb}\n" + " {$ij.goo} {$ij.boo} {call .aaa /}\n" + "{/template}\n" + "\n" + "/***/\n" + "{template .ccc}\n" + " {$ij.moo} {$ij.boo} {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 3 (ccc -> 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, bbb, aaa));
assertThat(memoizedInfoMap.get(bbb).depTemplateSet).isEqualTo(ImmutableSet.of(bbb, aaa, ccc));
assertThat(memoizedInfoMap.get(aaa).depTemplateSet).isEqualTo(ImmutableSet.of(aaa, bbb, ccc));
}
Aggregations