Search in sources :

Example 16 with BufferingAppendable

use of com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable 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)

Example 17 with BufferingAppendable

use of com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable in project closure-templates by google.

the class LazyClosureCompilerTest method testLetContentNode_detaching.

@Test
public void testLetContentNode_detaching() throws IOException {
    SettableFuture<String> bar = SettableFuture.create();
    CompiledTemplates templates = compileTemplateBody("{@param bar : string }", "{let $foo kind=\"text\"}", "  hello {$bar}", "{/let}", "{$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("hello ");
    // 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("hello ");
    bar.set("bar");
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    assertThat(output.toString()).isEqualTo("hello 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)

Example 18 with BufferingAppendable

use of com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable in project closure-templates by google.

the class StreamingPrintDirectivesTest method testStreamingEscapeHtml.

@Test
public void testStreamingEscapeHtml() throws IOException {
    CompiledTemplates templates = compileFile("{namespace ns}", "", "{template .tag}", "  {let $tag kind=\"html\"}", "    <div {call .attrs /}></div>", "  {/let}", "  {$tag}", "{/template}", "", "{template .attrs kind=\"attributes\"}", "  class=\"foo\"", "{/template}");
    RenderContext context = getDefaultContext(templates);
    BufferingAppendable output = BufferingAppendable.buffering();
    templates.getTemplateFactory("ns.tag").create(EMPTY_DICT, EMPTY_DICT).render(output, context);
    assertThat(output.getAndClearBuffer()).isEqualTo("<div class=\"foo\"></div>");
}
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) Test(org.junit.Test)

Example 19 with BufferingAppendable

use of com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable in project closure-templates by google.

the class StreamingPrintDirectivesTest method testStreamingDisablesRuntimeTypeChecks.

/**
 * This test demonstrates a change in behavior when streaming print directives are in use, they
 * can elide some runtime type checking. This is because the compiler explicitly puts in {@code
 * checkcast} instructions whenever calling {@link SoyValueProvider#resolve} (see every caller of
 * {@link ExpressionDetacher#resolveSoyValueProvider(Expression)}). However, when we are able to
 * stream a soy value provider we can't insert a {@code checkcast} instruction because we never
 * actually calculate the full value.
 *
 * <p>We could change SoyValueProvider.renderAndResolve to accept a TypePredicate and we could
 * sometimes enforce it, but for the time being this isn't happening.
 */
@Test
public void testStreamingDisablesRuntimeTypeChecks() throws IOException {
    CompiledTemplates templates = compileFile("{namespace ns}", "", "{template .streamable}", "  {@param i : int}", "  {$i |streaming}", "{/template}", "", "{template .nonstreamable}", "  {@param i : int}", "  {$i |nonstreaming}", "{/template}");
    RenderContext context = getDefaultContext(templates);
    SoyDict badParam = SoyValueConverterUtility.newDict("i", "notAnInt");
    BufferingAppendable output = BufferingAppendable.buffering();
    templates.getTemplateFactory("ns.streamable").create(badParam, EMPTY_DICT).render(output, context);
    assertThat(output.getAndClearBuffer()).isEqualTo("(stream: notAnInt)");
    try {
        templates.getTemplateFactory("ns.nonstreamable").create(badParam, EMPTY_DICT).render(output, context);
        fail("Expected ClassCastException");
    } catch (ClassCastException cce) {
        assertThat(cce).hasMessageThat().isEqualTo("com.google.template.soy.data.restricted.StringData cannot be cast to " + "com.google.template.soy.data.restricted.IntegerData");
    }
}
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) Test(org.junit.Test)

Example 20 with BufferingAppendable

use of com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable 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)

Aggregations

BufferingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable)27 Test (org.junit.Test)23 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)14 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)14 RenderContext (com.google.template.soy.jbcsrc.shared.RenderContext)13 RenderResult (com.google.template.soy.jbcsrc.api.RenderResult)6 LoggingAdvisingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable)3 SoyRecord (com.google.template.soy.data.SoyRecord)3 SoyDict (com.google.template.soy.data.SoyDict)2 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 TemplateMetadata (com.google.template.soy.jbcsrc.shared.TemplateMetadata)1 IOException (java.io.IOException)1