Search in sources :

Example 1 with RenderResult

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());
}
Also used : TemplateRegistry(com.google.template.soy.soytree.TemplateRegistry) RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) SoyFileSetParserBuilder(com.google.template.soy.SoyFileSetParserBuilder) SoyFileSetNode(com.google.template.soy.soytree.SoyFileSetNode) RenderResult(com.google.template.soy.jbcsrc.api.RenderResult) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates)

Example 2 with RenderResult

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");
}
Also used : SettableFuture(com.google.common.util.concurrent.SettableFuture) RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) RenderResult(com.google.template.soy.jbcsrc.api.RenderResult) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 3 with RenderResult

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();
}
Also used : BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) RenderResult(com.google.template.soy.jbcsrc.api.RenderResult)

Example 4 with RenderResult

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");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) RenderResult(com.google.template.soy.jbcsrc.api.RenderResult) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 5 with RenderResult

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");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) RenderResult(com.google.template.soy.jbcsrc.api.RenderResult) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Aggregations

RenderResult (com.google.template.soy.jbcsrc.api.RenderResult)9 BufferingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable)7 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)6 RenderContext (com.google.template.soy.jbcsrc.shared.RenderContext)6 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)5 Test (org.junit.Test)5 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 SoyFileSetParserBuilder (com.google.template.soy.SoyFileSetParserBuilder)1 LoggingAdvisingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable)1 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)1 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)1