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_");
}
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");
}
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");
}
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>");
}
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");
}
}
Aggregations