Search in sources :

Example 1 with RenderContext

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

the class DetachStateTest method testDetach_singleRawTextNode.

@Test
public void testDetach_singleRawTextNode() throws IOException {
    CompiledTemplates templates = TemplateTester.compileTemplateBody("hello world");
    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("hello world");
    output = new TestAppendable();
    output.softLimitReached = true;
    // detached!!!
    assertThat(template.render(output, context)).isEqualTo(RenderResult.limited());
    assertThat(output.toString()).isEqualTo("hello world");
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    // nothing was added
    assertThat(output.toString()).isEqualTo("hello world");
}
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 2 with RenderContext

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

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

the class DetachStateTest method testDetach_css.

@Test
public void testDetach_css() throws IOException {
    CompiledTemplates templates = TemplateTester.compileTemplateBody("{let $foo: 'foo'/}", "{css($foo, 'bar')}", "{css('baz')}");
    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-barbaz");
    output = new TestAppendable();
    output.softLimitReached = true;
    // css() does not detach
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    assertThat(output.toString()).isEqualTo("foo-barbaz");
}
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 4 with RenderContext

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

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

the class DetachStateTest method testDetach_saveRestore.

// ensure that when we call back in, locals are restored
@Test
public void testDetach_saveRestore() throws IOException {
    CompiledTemplates templates = TemplateTester.compileTemplateBody("{for $i in range(10)}", "  {$i}", "{/for}");
    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("0123456789");
    output = new TestAppendable();
    output.softLimitReached = true;
    for (int i = 0; i < 10; i++) {
        assertThat(template.render(output, context)).isEqualTo(RenderResult.limited());
        assertThat(output.toString()).isEqualTo(String.valueOf(i));
        output.delegate.setLength(0);
    }
    assertThat(template.render(output, context)).isEqualTo(RenderResult.done());
    // last render was empty
    assertThat(output.toString()).isEmpty();
}
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)

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