Search in sources :

Example 1 with BufferingAppendable

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

the class DetachStateTest method testDetachOnCall.

@Test
public void testDetachOnCall() throws IOException {
    CompiledTemplates templates = TemplateTester.compileFile("{namespace ns}", "", "{template .caller}", "  {@param callerParam : string}", "  {call .callee}", "    {param calleeParam: $callerParam /}", "  {/call}", "{/template}", "", "{template .callee}", "  {@param calleeParam : string}", "  prefix {$calleeParam} suffix", "{/template}", "");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.caller");
    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, getDefaultContext(templates))).isEqualTo(RenderResult.continueAfter(param));
    assertThat(output.toString()).isEqualTo("prefix ");
    param.set("foo");
    assertThat(template.render(output, getDefaultContext(templates))).isEqualTo(RenderResult.done());
    assertThat(output.toString()).isEqualTo("prefix foo suffix");
}
Also used : 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 2 with BufferingAppendable

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

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

the class DetachStateTest method testDetachOnMultipleParamsInOneExpression.

// This test is for a bug where we were generating one detach logic block for a full expressions
// but it caused stack merge errors because the runtime stack wasn't consistent across all detach
// points.  See http://mail.ow2.org/wws/arc/asm/2015-04/msg00001.html
@Test
public void testDetachOnMultipleParamsInOneExpression() throws IOException {
    CompiledTemplates templates = TemplateTester.compileTemplateBody("{@param list : list<int>}", "{@param foo : int}", "{for $item in $list}", "  {$item + $foo}", "{/for}");
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns.foo");
    RenderContext context = getDefaultContext(templates);
    SoyRecord params = asRecord(ImmutableMap.of("list", ImmutableList.of(1, 2, 3, 4), "foo", 1));
    BufferingAppendable output = LoggingAdvisingAppendable.buffering();
    assertThat(factory.create(params, EMPTY_DICT).render(output, context)).isEqualTo(RenderResult.done());
    assertThat(output.toString()).isEqualTo("2345");
}
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 4 with BufferingAppendable

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

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

the class BytecodeCompilerTest method testDelCall_delVariant.

@Test
public void testDelCall_delVariant() throws IOException {
    String soyFileContent1 = Joiner.on("\n").join("{namespace ns1}", "", "/***/", "{template .callerTemplate}", "  {@param variant : string}", "  {delcall ns1.del variant=\"$variant\" allowemptydefault=\"true\"/}", "{/template}", "", "/** */", "{deltemplate ns1.del variant=\"'v1'\"}", "  v1", "{/deltemplate}", "", "/** */", "{deltemplate ns1.del variant=\"'v2'\"}", "  v2", "{/deltemplate}", "");
    CompiledTemplates templates = compileFiles(soyFileContent1);
    CompiledTemplate.Factory factory = templates.getTemplateFactory("ns1.callerTemplate");
    RenderContext context = getDefaultContext(templates);
    BufferingAppendable builder = LoggingAdvisingAppendable.buffering();
    assertThat(factory.create(TemplateTester.asRecord(ImmutableMap.of("variant", "v1")), EMPTY_DICT).render(builder, context)).isEqualTo(RenderResult.done());
    assertThat(builder.getAndClearBuffer()).isEqualTo("v1");
    assertThat(factory.create(TemplateTester.asRecord(ImmutableMap.of("variant", "v2")), EMPTY_DICT).render(builder, context)).isEqualTo(RenderResult.done());
    assertThat(builder.getAndClearBuffer()).isEqualTo("v2");
    assertThat(factory.create(TemplateTester.asRecord(ImmutableMap.of("variant", "unknown")), EMPTY_DICT).render(builder, context)).isEqualTo(RenderResult.done());
    assertThat(builder.toString()).isEmpty();
    TemplateMetadata templateMetadata = getTemplateMetadata(templates, "ns1.callerTemplate");
    assertThat(templateMetadata.callees()).isEmpty();
    assertThat(templateMetadata.delCallees()).asList().containsExactly("ns1.del");
}
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) TemplateMetadata(com.google.template.soy.jbcsrc.shared.TemplateMetadata) 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