Search in sources :

Example 21 with BufferingAppendable

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

the class StreamingPrintDirectivesTest method testStreamingPrintOrdering.

// There was a bug that caused us to apply print directives in the wrong order when there were
// multiple streaming print directives.
@Test
public void testStreamingPrintOrdering() throws IOException {
    CompiledTemplates templates = compileFile("{namespace ns}", "", "{template .foo}", "  {@param s : ?}", "  {$s |streaming:'first' |streaming:'second'}", "{/template}", "");
    RenderContext context = getDefaultContext(templates);
    BufferingAppendable output = BufferingAppendable.buffering();
    CompiledTemplate template = templates.getTemplateFactory("ns.foo").create(SoyValueConverterUtility.newDict("s", "hello"), EMPTY_DICT);
    template.render(output, context);
    assertThat(output.getAndClearBuffer()).isEqualTo("(second: (first: hello))");
}
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) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 22 with BufferingAppendable

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

the class StreamingPrintDirectivesTest method testStreamingCall.

@Test
public void testStreamingCall() throws IOException {
    // As of right now only a few directives support streaming, but this includes |escapeHtml and
    // |escapeJsString, so we should be able to transitively stream through all of that.
    CompiledTemplates templates = compileFile("{namespace ns}", "", "{template .foo}", "  {call .bar data=\"all\"/}", "{/template}", "", "{template .bar}", "  <script>var x=\"{call .baz data=\"all\" /}\";</script>", "{/template}", "", "{template .baz kind=\"text\"}", "  {@param future : ?}", "  \"{$future}\" ", "{/template}", "");
    RenderContext context = getDefaultContext(templates);
    BufferingAppendable output = BufferingAppendable.buffering();
    SettableFuture<String> future = SettableFuture.create();
    CompiledTemplate template = templates.getTemplateFactory("ns.foo").create(SoyValueConverterUtility.newDict("future", future), EMPTY_DICT);
    template.render(output, context);
    assertThat(output.getAndClearBuffer()).isEqualTo("<script>var x=\"\\x22");
    future.set("hello");
    template.render(output, context);
    assertThat(output.getAndClearBuffer()).isEqualTo("hello\\x22\";</script>");
}
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) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 23 with BufferingAppendable

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

the class BytecodeCompilerTest method testParamValidation.

@Test
public void testParamValidation() throws Exception {
    CompiledTemplates templates = TemplateTester.compileTemplateBody("{@param foo : int}", "{$foo ?: -1}");
    CompiledTemplate.Factory singleParam = templates.getTemplateFactory("ns.foo");
    RenderContext context = getDefaultContext(templates);
    BufferingAppendable builder = LoggingAdvisingAppendable.buffering();
    SoyDict params = SoyValueConverterUtility.newDict("foo", IntegerData.forValue(1));
    singleParam.create(params, EMPTY_DICT).render(builder, context);
    assertThat(builder.getAndClearBuffer()).isEqualTo("1");
    singleParam.create(EMPTY_DICT, EMPTY_DICT).render(builder, context);
    assertThat(builder.getAndClearBuffer()).isEqualTo("-1");
    templates = TemplateTester.compileTemplateBody("{@inject foo : int}", "{$foo}");
    CompiledTemplate.Factory singleIj = templates.getTemplateFactory("ns.foo");
    context = getDefaultContext(templates);
    params = SoyValueConverterUtility.newDict("foo", IntegerData.forValue(1));
    singleIj.create(SoyValueConverter.EMPTY_DICT, params).render(builder, context);
    assertThat(builder.getAndClearBuffer()).isEqualTo("1");
    params = SoyValueConverterUtility.newDict();
    singleIj.create(SoyValueConverter.EMPTY_DICT, params).render(builder, context);
    assertThat(builder.getAndClearBuffer()).isEqualTo("null");
}
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) CompiledTemplate(com.google.template.soy.jbcsrc.shared.CompiledTemplate) Test(org.junit.Test)

Example 24 with BufferingAppendable

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

the class BytecodeCompilerTest method renderWithContext.

private static String renderWithContext(CompiledTemplate.Factory factory, RenderContext context) throws IOException {
    BufferingAppendable builder = LoggingAdvisingAppendable.buffering();
    assertThat(factory.create(EMPTY_DICT, EMPTY_DICT).render(builder, context)).isEqualTo(RenderResult.done());
    String string = builder.toString();
    return string;
}
Also used : BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable)

Example 25 with BufferingAppendable

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

the class AbstractLoggingAdvisingAppendableTest method testLogonly_logonly_after_regular.

@Test
public void testLogonly_logonly_after_regular() throws IOException {
    // test against the buffering version since it is a simple concrete implementation.
    BufferingAppendable buffering = LoggingAdvisingAppendable.buffering();
    buffering.enterLoggableElement(NOT_LOGONLY);
    buffering.append("hello");
    buffering.exitLoggableElement();
    buffering.enterLoggableElement(LOGONLY);
    buffering.append("logonly");
    buffering.exitLoggableElement();
    assertThat(buffering.toString()).isEqualTo("hello");
}
Also used : BufferingAppendable(com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable) 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