Search in sources :

Example 11 with RenderContext

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

the class DetachStateTest method testDetach_xid.

@Test
public void testDetach_xid() throws IOException {
    CompiledTemplates templates = TemplateTester.compileTemplateBody("{xid('foo')}");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
    RenderContext context = getDefaultContext(templates);
    CompiledTemplate template = factory.create(EMPTY_DICT, EMPTY_DICT);
    // Basic stuff works
    TestAppendable output = new TestAppendable();
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    assertThat(output.toString()).isEqualTo("foo_");
    output = new TestAppendable();
    output.softLimitReached = true;
    // xid() does not detach
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    assertThat(output.toString()).isEqualTo("foo_");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 12 with RenderContext

use of com.google.template.soy.jbcsrc.shared.RenderContext 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 13 with RenderContext

use of com.google.template.soy.jbcsrc.shared.RenderContext 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 14 with RenderContext

use of com.google.template.soy.jbcsrc.shared.RenderContext 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 15 with RenderContext

use of com.google.template.soy.jbcsrc.shared.RenderContext 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)

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