Search in sources :

Example 16 with RenderContext

use of com.google.template.soy.jbcsrc.shared.RenderContext in project closure-templates by google.

the class StreamingPrintDirectivesTest method testStreamingCloseable.

@Test
public void testStreamingCloseable() throws IOException {
    // Test to make sure the .close() is consistently called
    // the |streamingCloseable directive buffers all data until close is called so if close isn't
    // called it should print nothing.
    CompiledTemplates templates = compileFile("{namespace ns}", "", "{template .basic}", "  {@param p : ?}", "  {$p|streamingCloseable:' closed!'}", "{/template}", "", "{template .nested kind=\"text\"}", "  {@param p : ?}", // escapeHtml is not closeable and it is streaming
    "  {$p|escapeHtml|streamingCloseable:'(close)'}", "{/template}", "", "{template .nestedDeeper kind=\"text\"}", "  {@param p : ?}", "  {$p|escapeHtml", "     |streamingCloseable:'(c1)'", "     |streamingCloseable:'(c2)'", "     |escapeHtml", "     |streamingCloseable:'(c3)'}", "{/template}", "");
    RenderContext context = getDefaultContext(templates);
    assertThat(renderToString("ns.basic", ImmutableMap.of("p", "hello"), templates, context)).isEqualTo("hello closed!");
    assertThat(renderToString("ns.nested", ImmutableMap.of("p", "hello"), templates, context)).isEqualTo("hello(close)");
    assertThat(renderToString("ns.nestedDeeper", ImmutableMap.of("p", "hello"), templates, context)).isEqualTo("hello(c1)(c2)(c3)");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) Test(org.junit.Test)

Example 17 with RenderContext

use of com.google.template.soy.jbcsrc.shared.RenderContext in project closure-templates by google.

the class StreamingPrintDirectivesTest method testStreaming.

@Test
public void testStreaming() throws IOException {
    BufferingAppendable output = BufferingAppendable.buffering();
    CompiledTemplates templates = compileFile("{namespace ns}", "", "{template .foo}", "  {@param future1 : ?}", "  {@param future2 : ?}", "  foo_prefix{sp}", "  {call .streamable}", "    {param p kind=\"html\"}", "      param_prefix{sp}{$future1}{sp}param_suffix", "    {/param}", "  {/call}", "", "  {sp}interlude{sp}", "", "  {call .unstreamable}", "    {param p kind=\"html\"}", "      param_prefix{sp}{$future2}{sp}param_suffix", "    {/param}", "  {/call}", "  {sp}foo_suffix", "{/template}", "", "{template .streamable}", "  {@param p : ?}", "  streamable_prefix{sp}", "  {$p |streaming}{sp}", "  streamable_suffix", "{/template}", "", "{template .unstreamable}", "  {@param p : ?}", "  unstreamable_prefix{sp}", "  {$p |nonstreaming}{sp}", "  unstreamable_suffix", "{/template}", "");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
    RenderContext context = getDefaultContext(templates);
    SettableFuture<String> future1 = SettableFuture.create();
    SettableFuture<String> future2 = SettableFuture.create();
    CompiledTemplate create = factory.create(SoyValueConverterUtility.newDict("future1", future1, "future2", future2), SoyValueConverter.EMPTY_DICT);
    RenderResult result = create.render(output, context);
    // rendering paused because it found our future
    assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
    assertThat(result.future()).isSameAs(future1);
    // but we actually rendered the first half of the param even though it went through a print
    // directive. all the content in parens went through our directive
    assertThat(output.getAndClearBuffer()).isEqualTo("foo_prefix streamable_prefix (stream: param_prefix )");
    future1.set("future1");
    result = create.render(output, context);
    assertThat(result.type()).isEqualTo(RenderResult.Type.DETACH);
    assertThat(result.future()).isSameAs(future2);
    // here we made it into .unstreamable, but printed no part of the parameter due to the non
    // streamable print directive
    assertThat(output.getAndClearBuffer()).isEqualTo("(stream: future1)(stream:  param_suffix) streamable_suffix interlude " + "unstreamable_prefix ");
    future2.set("future2");
    result = create.render(output, context);
    assertThat(result.isDone()).isTrue();
    // now we render the full future2 parameter all at once and the
    assertThat(output.getAndClearBuffer()).isEqualTo("param_prefix future2 param_suffix unstreamable_suffix foo_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 18 with RenderContext

use of com.google.template.soy.jbcsrc.shared.RenderContext in project closure-templates by google.

the class StreamingPrintDirectivesTest method testStreamingPrintOrdering.

// There was a bug that caused us to apply print directives in the wrong order when there were
// multiple streaming print directives.
@Test
public void testStreamingPrintOrdering() throws IOException {
    CompiledTemplates templates = compileFile("{namespace ns}", "", "{template .foo}", "  {@param s : ?}", "  {$s |streaming:'first' |streaming:'second'}", "{/template}", "");
    RenderContext context = getDefaultContext(templates);
    BufferingAppendable output = BufferingAppendable.buffering();
    CompiledTemplate template = templates.getTemplateFactory("ns.foo").create(SoyValueConverterUtility.newDict("s", "hello"), EMPTY_DICT);
    template.render(output, context);
    assertThat(output.getAndClearBuffer()).isEqualTo("(second: (first: hello))");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 19 with RenderContext

use of com.google.template.soy.jbcsrc.shared.RenderContext in project closure-templates by google.

the class StreamingPrintDirectivesTest method testStreamingCall.

@Test
public void testStreamingCall() throws IOException {
    // As of right now only a few directives support streaming, but this includes |escapeHtml and
    // |escapeJsString, so we should be able to transitively stream through all of that.
    CompiledTemplates templates = compileFile("{namespace ns}", "", "{template .foo}", "  {call .bar data=\"all\"/}", "{/template}", "", "{template .bar}", "  <script>var x=\"{call .baz data=\"all\" /}\";</script>", "{/template}", "", "{template .baz kind=\"text\"}", "  {@param future : ?}", "  \"{$future}\" ", "{/template}", "");
    RenderContext context = getDefaultContext(templates);
    BufferingAppendable output = BufferingAppendable.buffering();
    SettableFuture<String> future = SettableFuture.create();
    CompiledTemplate template = templates.getTemplateFactory("ns.foo").create(SoyValueConverterUtility.newDict("future", future), EMPTY_DICT);
    template.render(output, context);
    assertThat(output.getAndClearBuffer()).isEqualTo("<script>var x=\"\\x22");
    future.set("hello");
    template.render(output, context);
    assertThat(output.getAndClearBuffer()).isEqualTo("hello\\x22\";</script>");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 20 with RenderContext

use of com.google.template.soy.jbcsrc.shared.RenderContext in project closure-templates by google.

the class BytecodeCompilerTest method testParamValidation.

@Test
public void testParamValidation() throws Exception {
    CompiledTemplates templates = TemplateTester.compileTemplateBody("{@param foo : int}", "{$foo ?: -1}");
    CompiledTemplate.Factory singleParam = templates.getTemplateFactory("ns.foo");
    RenderContext context = getDefaultContext(templates);
    BufferingAppendable builder = LoggingAdvisingAppendable.buffering();
    SoyDict params = SoyValueConverterUtility.newDict("foo", IntegerData.forValue(1));
    singleParam.create(params, EMPTY_DICT).render(builder, context);
    assertThat(builder.getAndClearBuffer()).isEqualTo("1");
    singleParam.create(EMPTY_DICT, EMPTY_DICT).render(builder, context);
    assertThat(builder.getAndClearBuffer()).isEqualTo("-1");
    templates = TemplateTester.compileTemplateBody("{@inject foo : int}", "{$foo}");
    CompiledTemplate.Factory singleIj = templates.getTemplateFactory("ns.foo");
    context = getDefaultContext(templates);
    params = SoyValueConverterUtility.newDict("foo", IntegerData.forValue(1));
    singleIj.create(SoyValueConverter.EMPTY_DICT, params).render(builder, context);
    assertThat(builder.getAndClearBuffer()).isEqualTo("1");
    params = SoyValueConverterUtility.newDict();
    singleIj.create(SoyValueConverter.EMPTY_DICT, params).render(builder, context);
    assertThat(builder.getAndClearBuffer()).isEqualTo("null");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) SoyDict(com.google.template.soy.data.SoyDict) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Aggregations

CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)20 RenderContext (com.google.template.soy.jbcsrc.shared.RenderContext)20 Test (org.junit.Test)19 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)16 BufferingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable)13 RenderResult (com.google.template.soy.jbcsrc.api.RenderResult)6 SoyDict (com.google.template.soy.data.SoyDict)2 SoyRecord (com.google.template.soy.data.SoyRecord)2 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 SoyFileSetParserBuilder (com.google.template.soy.SoyFileSetParserBuilder)1 TemplateMetadata (com.google.template.soy.jbcsrc.shared.TemplateMetadata)1 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)1 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)1