use of com.google.template.soy.jbcsrc.api.RenderResult in project closure-templates by google.
the class VeLoggingTest method renderTemplate.
private void renderTemplate(Map<String, ?> params, OutputAppendable output, String... templateBodyLines) throws IOException {
SoyFileSetNode soyTree = SoyFileSetParserBuilder.forFileContents("{namespace ns}\n" + "{template .foo}\n" + Joiner.on("\n").join(templateBodyLines) + "\n{/template}").typeRegistry(new SoyTypeRegistry.Builder().addDescriptors(ImmutableList.of(com.google.template.soy.testing.Foo.getDescriptor())).build()).setLoggingConfig(config).addSoyFunction(new DepthFunction()).runAutoescaper(true).parse().fileSet();
TemplateRegistry templateRegistry = new TemplateRegistry(soyTree, ErrorReporter.exploding());
CompiledTemplates templates = BytecodeCompiler.compile(templateRegistry, false, ErrorReporter.exploding()).get();
RenderContext ctx = TemplateTester.getDefaultContext(templates).toBuilder().hasLogger(true).build();
RenderResult result = templates.getTemplateFactory("ns.foo").create(TemplateTester.asRecord(params), EMPTY_DICT).render(output, ctx);
assertThat(result).isEqualTo(RenderResult.done());
}
use of com.google.template.soy.jbcsrc.api.RenderResult in project closure-templates by google.
the class DetachStateTest method testDetachOnEachIteration.
@Test
public void testDetachOnEachIteration() throws IOException {
CompiledTemplates templates = TemplateTester.compileTemplateBody("{@param list : list<string>}", "prefix{\\n}", "{for $item in $list}", " loop-prefix{\\n}", " {$item}{\\n}", " loop-suffix{\\n}", "{/for}", "suffix");
CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
RenderContext context = getDefaultContext(templates);
List<SettableFuture<String>> futures = ImmutableList.of(SettableFuture.<String>create(), SettableFuture.<String>create(), SettableFuture.<String>create());
CompiledTemplate template = factory.create(asRecord(ImmutableMap.of("list", futures)), EMPTY_DICT);
BufferingAppendable output = LoggingAdvisingAppendable.buffering();
RenderResult result = template.render(output, context);
assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
assertThat(result.future()).isEqualTo(futures.get(0));
assertThat(output.getAndClearBuffer()).isEqualTo("prefix\nloop-prefix\n");
futures.get(0).set("first");
result = template.render(output, context);
assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
assertThat(result.future()).isEqualTo(futures.get(1));
assertThat(output.getAndClearBuffer()).isEqualTo("first\nloop-suffix\nloop-prefix\n");
futures.get(1).set("second");
result = template.render(output, context);
assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
assertThat(result.future()).isEqualTo(futures.get(2));
assertThat(output.getAndClearBuffer()).isEqualTo("second\nloop-suffix\nloop-prefix\n");
futures.get(2).set("third");
result = template.render(output, context);
assertThat(result).isEqualTo(RenderResult.done());
assertThat(output.toString()).isEqualTo("third\nloop-suffix\nsuffix");
}
use of com.google.template.soy.jbcsrc.api.RenderResult in project closure-templates by google.
the class StreamingPrintDirectivesTest method renderToString.
private static String renderToString(String name, ImmutableMap<String, Object> params, CompiledTemplates templates, RenderContext context) throws IOException {
BufferingAppendable output = BufferingAppendable.buffering();
RenderResult result = templates.getTemplateFactory(name).create(SoyValueConverter.INSTANCE.newDictFromMap(params), EMPTY_DICT).render(output, context);
assertThat(result.isDone()).isTrue();
return output.getAndClearBuffer();
}
use of com.google.template.soy.jbcsrc.api.RenderResult in project closure-templates by google.
the class DetachStateTest method testDetachOnUnResolvedProvider.
@Test
public void testDetachOnUnResolvedProvider() throws IOException {
SettableFuture<String> future = SettableFuture.create();
CompiledTemplates templates = TemplateTester.compileTemplateBody("{@param foo : string}", "prefix{sp}{$foo}{sp}suffix");
CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
RenderContext context = getDefaultContext(templates);
CompiledTemplate template = factory.create(asRecord(ImmutableMap.of("foo", future)), EMPTY_DICT);
BufferingAppendable output = LoggingAdvisingAppendable.buffering();
RenderResult result = template.render(output, context);
assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
assertThat(result.future()).isEqualTo(future);
assertThat(output.toString()).isEqualTo("prefix ");
// No progress is made, our caller is an idiot and didn't wait for the future
result = template.render(output, context);
assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
assertThat(result.future()).isEqualTo(future);
assertThat(output.toString()).isEqualTo("prefix ");
future.set("future");
result = template.render(output, context);
assertThat(result).isEqualTo(RenderResult.done());
assertThat(output.toString()).isEqualTo("prefix future suffix");
}
use of com.google.template.soy.jbcsrc.api.RenderResult in project closure-templates by google.
the class LazyClosureCompilerTest method testDetachOnFutureLazily.
@Test
public void testDetachOnFutureLazily() throws IOException {
SettableFuture<String> bar = SettableFuture.create();
CompiledTemplates templates = compileTemplateBody("{@param bar : string }", "{let $foo : $bar + $bar /}", "before use", "{$foo}");
CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
RenderContext context = getDefaultContext(templates);
CompiledTemplate template = factory.create(asRecord(ImmutableMap.of("bar", bar)), EMPTY_DICT);
BufferingAppendable output = LoggingAdvisingAppendable.buffering();
RenderResult result = template.render(output, context);
assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
// we found bar!
assertThat(result.future()).isSameAs(bar);
assertThat(output.toString()).isEqualTo("before use");
// make sure no progress is made
result = template.render(output, context);
assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
assertThat(result.future()).isSameAs(bar);
assertThat(output.toString()).isEqualTo("before use");
bar.set(" bar");
assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
assertThat(output.toString()).isEqualTo("before use bar bar");
}
Aggregations