Search in sources :

Example 16 with CompiledTemplates

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

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

the class ProtoSupportTest method testPassingManipulatedFields.

// tests for a bug in handling of ternaries
@Test
public void testPassingManipulatedFields() {
    String file = JOINER.join("{namespace ns}", "", "{template .caller}", "  {@param pair : example.KvPair}", "  {let $closeUrl : $pair.value /}", "  {call .callee}{param str : $closeUrl ? $closeUrl : '' /}{/call}", "{/template}", "", "{template .callee}", "  {@param? str : string}", "  {if $str}", "    {$str}", "  {/if}", "{/template}");
    CompiledTemplates templates = BytecodeCompiler.compile(SoyFileSetParserBuilder.forFileContents(file).typeRegistry(types).parse().registry(), false, ErrorReporter.exploding()).get();
    render(templates, "ns.caller", (SoyRecord) SoyValueConverter.INSTANCE.convert(ImmutableMap.of("pair", KvPair.newBuilder().setAnotherValue(2))));
}
Also used : ByteString(com.google.protobuf.ByteString) CompiledTemplates(com.google.template.soy.jbcsrc.shared.CompiledTemplates) Test(org.junit.Test)

Example 18 with CompiledTemplates

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

Example 19 with CompiledTemplates

use of com.google.template.soy.jbcsrc.shared.CompiledTemplates 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");
}
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 20 with CompiledTemplates

use of com.google.template.soy.jbcsrc.shared.CompiledTemplates 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");
}
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)

Aggregations

CompiledTemplates (com.google.template.soy.jbcsrc.shared.CompiledTemplates)33 Test (org.junit.Test)30 CompiledTemplate (com.google.template.soy.jbcsrc.shared.CompiledTemplate)22 RenderContext (com.google.template.soy.jbcsrc.shared.RenderContext)20 BufferingAppendable (com.google.template.soy.data.LoggingAdvisingAppendable.BufferingAppendable)14 RenderResult (com.google.template.soy.jbcsrc.api.RenderResult)6 SoyFileSetNode (com.google.template.soy.soytree.SoyFileSetNode)5 TemplateRegistry (com.google.template.soy.soytree.TemplateRegistry)5 SoyRecord (com.google.template.soy.data.SoyRecord)3 SoyDict (com.google.template.soy.data.SoyDict)2 IOException (java.io.IOException)2 Stopwatch (com.google.common.base.Stopwatch)1 SettableFuture (com.google.common.util.concurrent.SettableFuture)1 ByteString (com.google.protobuf.ByteString)1 SoyFileSetParserBuilder (com.google.template.soy.SoyFileSetParserBuilder)1 EscapeHtmlDirective (com.google.template.soy.coredirectives.EscapeHtmlDirective)1 BasicParamStore (com.google.template.soy.data.internal.BasicParamStore)1 ParamStore (com.google.template.soy.data.internal.ParamStore)1 ErrorReporter (com.google.template.soy.error.ErrorReporter)1 ClassData (com.google.template.soy.jbcsrc.internal.ClassData)1