Search in sources :

Example 11 with CompiledTemplate

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

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

the class DetachStateTest method testDetachOnParamTransclusion.

@Test
public void testDetachOnParamTransclusion() throws IOException {
    CompiledTemplates templates = TemplateTester.compileFile("{namespace ns}", "", "/** */", "{template .caller}", "  {@param callerParam : string}", "  {call .callee}", "    {param calleeParam kind=\"text\"}", "      prefix {$callerParam} suffix", "    {/param}", "  {/call}", "{/template}", "", "/** */", "{template .callee}", "  {@param calleeParam : string}", "  {$calleeParam}", "{/template}", "");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.caller");
    RenderContext context = getDefaultContext(templates);
    SettableFuture<String> param = SettableFuture.create();
    SoyRecord params = asRecord(ImmutableMap.of("callerParam", param));
    CompiledTemplate template = factory.create(params, EMPTY_DICT);
    BufferingAppendable output = LoggingAdvisingAppendable.buffering();
    assertThat(template.render(output, context)).isEqualTo(RenderResult.continueAfter(param));
    assertThat(output.toString()).isEqualTo("prefix ");
    param.set("foo");
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    assertThat(output.toString()).isEqualTo("prefix foo suffix");
}
Also used : RenderContext(com.google.template.soy.jbcsrc.shared.RenderContext) SoyRecord(com.google.template.soy.data.SoyRecord) 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 13 with CompiledTemplate

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

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

the class ProtoSupportTest method render.

private String render(CompiledTemplates templates, String name, SoyRecord params) {
    CompiledTemplate caller = templates.getTemplateFactory(name).create(params, EMPTY_DICT);
    BufferingAppendable sb = LoggingAdvisingAppendable.buffering();
    try {
        assertThat(caller.render(sb, getDefaultContext(templates))).isEqualTo(RenderResult.done());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return sb.toString();
}
Also used : BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) IOException(java.io.IOException) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate)

Example 15 with CompiledTemplate

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

the class LazyClosureCompilerTest method testLetValueNodeStructure.

@Test
public void testLetValueNodeStructure() {
    // make sure we don't break normal reflection apis
    CompiledTemplates templates = compileTemplateBody("{let $bar : 'a' /}", "{let $foo : $bar + 1 /}");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
    CompiledTemplate template = factory.create(EMPTY_DICT, EMPTY_DICT);
    assertThat(template.getClass().getDeclaredClasses()).hasLength(2);
    List<Class<?>> innerClasses = Lists.newArrayList(template.getClass().getDeclaredClasses());
    innerClasses.remove(factory.getClass());
    Class<?> let = Iterables.getOnlyElement(innerClasses);
    assertThat(let.getSimpleName()).isEqualTo("let_foo");
    assertThat(let.getDeclaringClass()).isEqualTo(template.getClass());
}
Also used : CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Aggregations

CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)20 Test (org.junit.Test)18 CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)16 RenderContext (com.google.template.soy.jbcsrc.shared.RenderContext)13 BufferingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable)11 RenderResult (com.google.template.soy.jbcsrc.api.RenderResult)5 SoyRecord (com.google.template.soy.data.SoyRecord)2 TemplateMetadata (com.google.template.soy.jbcsrc.shared.TemplateMetadata)2 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 SoyDict (com.google.template.soy.data.SoyDict)1 IOException (java.io.IOException)1